From 2a2e8318772dc851403515f51f721c4df60c735d Mon Sep 17 00:00:00 2001 From: 4gray Date: Wed, 27 Nov 2024 18:51:26 +0100 Subject: [PATCH] refactor(playlists): improve error handling and filtering logic Enhance the `getPortalFavorites` and `getPortalLiveStreamFavorites` methods to include null checks for the fetched items. This prevents potential runtime errors when accessing properties of undefined. Additionally, improve code readability by restructuring the filter logic a more explicit format. Add a console log for the parsed playlist in the `addPortalFavorite` method for debugging purposes. --- src/app/services/playlists.service.ts | 35 +++++++++++++++++++-------- 1 file changed, 25 insertions(+), 10 deletions(-) diff --git a/src/app/services/playlists.service.ts b/src/app/services/playlists.service.ts index 7c2d687c4..227c641e6 100644 --- a/src/app/services/playlists.service.ts +++ b/src/app/services/playlists.service.ts @@ -143,20 +143,34 @@ export class PlaylistsService { getPortalFavorites(portalId: string) { return this.dbService - .getByID<{ favorites: Partial[] }>( - DbStores.Playlists, - portalId - ) - .pipe(map((item) => item.favorites.filter(itm => itm && itm.stream_type && itm.stream_type !== 'live') ?? [])); + .getByID<{ + favorites: Partial[]; + }>(DbStores.Playlists, portalId) + .pipe( + map((item) => { + if (!item || !item.favorites) return []; + return item.favorites.filter( + (itm) => + itm && itm.stream_type && itm.stream_type !== 'live' + ); + }) + ); } getPortalLiveStreamFavorites(portalId: string) { return this.dbService - .getByID<{ favorites: Partial[] }>( - DbStores.Playlists, - portalId - ) - .pipe(map((item) => item.favorites.filter(itm => itm && itm.stream_type && itm.stream_type === 'live') ?? [])); + .getByID<{ + favorites: Partial[]; + }>(DbStores.Playlists, portalId) + .pipe( + map((item) => { + if (!item || !item.favorites) return []; + return item.favorites.filter( + (itm) => + itm && itm.stream_type && itm.stream_type === 'live' + ); + }) + ); } addPortalFavorite(portalId: string, item: any) { @@ -217,6 +231,7 @@ export class PlaylistsService { ) { try { const parsedPlaylist = parse(playlist); + console.log(parsedPlaylist); return createPlaylistObject( title, parsedPlaylist,