@@ -183,71 +183,75 @@ class MediaItemRepository extends repository<MediaItemBase>({
183183 id : mediaItem . id ,
184184 } ) ;
185185
186- await Promise . all (
187- result . seasons . map ( async ( season ) => {
188- let updated = false ;
186+ if ( result . seasons ) {
187+ await Promise . all (
188+ result . seasons . map ( async ( season ) => {
189+ let updated = false ;
189190
190- season . numberOfEpisodes =
191- season . numberOfEpisodes || season . episodes ?. length || 0 ;
192- season . tvShowId = mediaItem . id ;
191+ season . numberOfEpisodes =
192+ season . numberOfEpisodes || season . episodes ?. length || 0 ;
193+ season . tvShowId = mediaItem . id ;
193194
194- if ( ! season . externalPosterUrl ) {
195- season . posterId = null ;
196- }
197-
198- if ( season . externalPosterUrl && ! season . posterId ) {
199- season . posterId = getImageId ( ) ;
200- }
201-
202- const newSeason = omitUndefinedValues (
203- tvSeasonRepository . stripValue ( tvSeasonRepository . serialize ( season ) )
204- ) ;
205-
206- if ( season . id ) {
207- const res = await trx ( 'season' )
208- . update ( newSeason )
209- . where ( { id : season . id } ) ;
210-
211- updated = res === 1 ;
212- }
195+ if ( ! season . externalPosterUrl ) {
196+ season . posterId = null ;
197+ }
213198
214- if ( ! updated ) {
215- season . id = (
216- await trx ( 'season' ) . insert ( newSeason ) . returning ( 'id' )
217- ) . at ( 0 ) . id ;
218- }
199+ if ( season . externalPosterUrl && ! season . posterId ) {
200+ season . posterId = getImageId ( ) ;
201+ }
219202
220- if ( season . episodes ) {
221- for ( const episode of season . episodes ) {
222- let updated = false ;
203+ const newSeason = omitUndefinedValues (
204+ tvSeasonRepository . stripValue (
205+ tvSeasonRepository . serialize ( season )
206+ )
207+ ) ;
223208
224- episode . seasonAndEpisodeNumber =
225- episode . seasonNumber * 1000 + episode . episodeNumber ;
226- episode . seasonId = season . id ;
227- episode . tvShowId = mediaItem . id ;
209+ if ( season . id ) {
210+ const res = await trx ( 'season' )
211+ . update ( newSeason )
212+ . where ( { id : season . id } ) ;
228213
229- const newEpisode = omitUndefinedValues (
230- tvEpisodeRepository . stripValue (
231- tvEpisodeRepository . serialize ( episode )
232- )
233- ) ;
214+ updated = res === 1 ;
215+ }
234216
235- if ( episode . id ) {
236- const res = await trx < TvEpisode > ( 'episode' )
237- . update ( newEpisode )
238- . where ( { id : episode . id } ) ;
217+ if ( ! updated ) {
218+ season . id = (
219+ await trx ( 'season' ) . insert ( newSeason ) . returning ( 'id' )
220+ ) . at ( 0 ) . id ;
221+ }
239222
240- updated = res === 1 ;
241- }
242- if ( ! updated ) {
243- episode . id = (
244- await trx ( 'episode' ) . insert ( newEpisode ) . returning ( 'id' )
245- ) . at ( 0 ) . id ;
223+ if ( season . episodes ) {
224+ for ( const episode of season . episodes ) {
225+ let updated = false ;
226+
227+ episode . seasonAndEpisodeNumber =
228+ episode . seasonNumber * 1000 + episode . episodeNumber ;
229+ episode . seasonId = season . id ;
230+ episode . tvShowId = mediaItem . id ;
231+
232+ const newEpisode = omitUndefinedValues (
233+ tvEpisodeRepository . stripValue (
234+ tvEpisodeRepository . serialize ( episode )
235+ )
236+ ) ;
237+
238+ if ( episode . id ) {
239+ const res = await trx < TvEpisode > ( 'episode' )
240+ . update ( newEpisode )
241+ . where ( { id : episode . id } ) ;
242+
243+ updated = res === 1 ;
244+ }
245+ if ( ! updated ) {
246+ episode . id = (
247+ await trx ( 'episode' ) . insert ( newEpisode ) . returning ( 'id' )
248+ ) . at ( 0 ) . id ;
249+ }
246250 }
247251 }
248- }
249- } )
250- ) ;
252+ } )
253+ ) ;
254+ }
251255
252256 return result ;
253257 } ) ;
@@ -681,9 +685,7 @@ class MediaItemRepository extends repository<MediaItemBase>({
681685 . insert ( {
682686 ...this . serialize ( omitUndefinedValues ( this . stripValue ( item ) ) ) ,
683687 posterId : item . externalPosterUrl ? getImageId ( ) : null ,
684- backdropId : item . externalBackdropUrl
685- ? getImageId ( )
686- : null ,
688+ backdropId : item . externalBackdropUrl ? getImageId ( ) : null ,
687689 lastTimeUpdated : Date . now ( ) ,
688690 } )
689691 . returning ( '*' ) ;
0 commit comments