1+ import type { SonarrSeason } from '@server/api/servarr/sonarr' ;
12import TheMovieDb from '@server/api/themoviedb' ;
23import { MediaStatus , MediaType } from '@server/constants/media' ;
34import { getRepository } from '@server/datasource' ;
45import Media from '@server/entity/Media' ;
6+ import { MediaRequest } from '@server/entity/MediaRequest' ;
57import Season from '@server/entity/Season' ;
8+ import SeasonRequest from '@server/entity/SeasonRequest' ;
69import { getSettings } from '@server/lib/settings' ;
710import logger from '@server/logger' ;
811import AsyncLock from '@server/utils/asyncLock' ;
@@ -212,7 +215,7 @@ class BaseScanner<T> {
212215
213216 /**
214217 * processShow takes a TMDB ID and an array of ProcessableSeasons, which
215- * should include the total episodes a sesaon has + the total available
218+ * should include the total episodes a season has + the total available
216219 * episodes that each season currently has. Unlike processMovie, this method
217220 * does not take an `is4k` option. We handle both the 4k _and_ non 4k status
218221 * in one method.
@@ -279,13 +282,14 @@ class BaseScanner<T> {
279282 // force it to stay available (to avoid competing scanners)
280283 existingSeason . status =
281284 ( season . totalEpisodes === season . episodes && season . episodes > 0 ) ||
282- existingSeason . status === MediaStatus . AVAILABLE
285+ ( existingSeason . status === MediaStatus . AVAILABLE &&
286+ season . episodes > 0 )
283287 ? MediaStatus . AVAILABLE
284288 : season . episodes > 0
285289 ? MediaStatus . PARTIALLY_AVAILABLE
286290 : ! season . is4kOverride && season . processing
287291 ? MediaStatus . PROCESSING
288- : settings . main . removeUnmonitoredFromRequestsEnabled &&
292+ : settings . main . removeUnmonitoredEnabled &&
289293 ! season . monitored &&
290294 season . episodes == 0
291295 ? MediaStatus . UNKNOWN
@@ -296,13 +300,14 @@ class BaseScanner<T> {
296300 ( this . enable4kShow &&
297301 season . episodes4k === season . totalEpisodes &&
298302 season . episodes4k > 0 ) ||
299- existingSeason . status4k === MediaStatus . AVAILABLE
303+ ( existingSeason . status4k === MediaStatus . AVAILABLE &&
304+ season . episodes > 0 )
300305 ? MediaStatus . AVAILABLE
301306 : this . enable4kShow && season . episodes4k > 0
302307 ? MediaStatus . PARTIALLY_AVAILABLE
303308 : season . is4kOverride && season . processing
304309 ? MediaStatus . PROCESSING
305- : settings . main . removeUnmonitoredFromRequestsEnabled &&
310+ : settings . main . removeUnmonitoredEnabled &&
306311 ! season . monitored &&
307312 season . episodes4k == 0
308313 ? MediaStatus . UNKNOWN
@@ -643,6 +648,41 @@ class BaseScanner<T> {
643648 }
644649 } ) ;
645650 }
651+
652+ protected async processUnmonitoredSeason (
653+ tmdbId : number ,
654+ season : SonarrSeason
655+ ) : Promise < void > {
656+ // Remove unmonitored seasons from Requests
657+ const requestRepository = getRepository ( MediaRequest ) ;
658+ const seasonRequestRepository = getRepository ( SeasonRequest ) ;
659+
660+ const existingRequests = await requestRepository
661+ . createQueryBuilder ( 'request' )
662+ . innerJoinAndSelect ( 'request.media' , 'media' )
663+ . innerJoinAndSelect ( 'request.seasons' , 'seasons' )
664+ . where ( 'media.tmdbId = :tmdbId' , { tmdbId : tmdbId } )
665+ . andWhere ( 'media.mediaType = :mediaType' , {
666+ mediaType : MediaType . TV ,
667+ } )
668+ . andWhere ( 'seasons.seasonNumber = :seasonNumber' , {
669+ seasonNumber : season . seasonNumber ,
670+ } )
671+ . getMany ( ) ;
672+
673+ if ( existingRequests && existingRequests . length > 0 ) {
674+ existingRequests . forEach ( ( existingRequest ) => {
675+ existingRequest . seasons . forEach ( async ( requestedSeason ) => {
676+ if ( requestedSeason . seasonNumber === season . seasonNumber ) {
677+ this . log (
678+ `Removing request for Season ${ season . seasonNumber } of tmdbId ${ tmdbId } as it is unmonitored`
679+ ) ;
680+ await seasonRequestRepository . remove ( requestedSeason ) ;
681+ }
682+ } ) ;
683+ } ) ;
684+ }
685+ }
646686}
647687
648688export default BaseScanner ;
0 commit comments