|
1 | 1 | import type { SonarrSeries } from '@server/api/servarr/sonarr';
|
2 | 2 | import SonarrAPI from '@server/api/servarr/sonarr';
|
3 | 3 | import type { TmdbTvDetails } from '@server/api/themoviedb/interfaces';
|
| 4 | +import { MediaType } from '@server/constants/media'; |
4 | 5 | import { getRepository } from '@server/datasource';
|
5 | 6 | import Media from '@server/entity/Media';
|
| 7 | +import { MediaRequest } from '@server/entity/MediaRequest'; |
| 8 | +import SeasonRequest from '@server/entity/SeasonRequest'; |
6 | 9 | import type {
|
7 | 10 | ProcessableSeason,
|
8 | 11 | RunnableScanner,
|
@@ -85,6 +88,7 @@ class SonarrScanner
|
85 | 88 | private async processSonarrSeries(sonarrSeries: SonarrSeries) {
|
86 | 89 | try {
|
87 | 90 | const mediaRepository = getRepository(Media);
|
| 91 | + const settings = getSettings(); |
88 | 92 | const server4k = this.enable4kShow && this.currentServer.is4k;
|
89 | 93 | const processableSeasons: ProcessableSeason[] = [];
|
90 | 94 | let tvShow: TmdbTvDetails;
|
@@ -112,13 +116,42 @@ class SonarrScanner
|
112 | 116 | for (const season of filteredSeasons) {
|
113 | 117 | const totalAvailableEpisodes = season.statistics?.episodeFileCount ?? 0;
|
114 | 118 |
|
| 119 | + if (settings.main.removeUnmonitoredFromRequestsEnabled && season.monitored === false && totalAvailableEpisodes === 0) { |
| 120 | + // Remove unmonitored seasons from Requests |
| 121 | + const requestRepository = getRepository(MediaRequest); |
| 122 | + const seasonRequestRepository = getRepository(SeasonRequest); |
| 123 | + |
| 124 | + const existingRequests = await requestRepository |
| 125 | + .createQueryBuilder('request') |
| 126 | + .innerJoinAndSelect('request.media', 'media') |
| 127 | + .innerJoinAndSelect('request.seasons', 'seasons') |
| 128 | + .where('media.tmdbId = :tmdbId', { tmdbId: tmdbId }) |
| 129 | + .andWhere('media.mediaType = :mediaType', { |
| 130 | + mediaType: MediaType.TV |
| 131 | + }) |
| 132 | + .andWhere('seasons.seasonNumber = :seasonNumber', { seasonNumber: season.seasonNumber }) |
| 133 | + .getMany(); |
| 134 | + |
| 135 | + if (existingRequests && existingRequests.length > 0) { |
| 136 | + existingRequests.forEach((existingRequest) => { |
| 137 | + existingRequest.seasons.forEach(async (requestedSeason) => { |
| 138 | + if (requestedSeason.seasonNumber === season.seasonNumber) { |
| 139 | + this.log(`Removing request for Season ${season.seasonNumber} of ${sonarrSeries.title} as it is unmonitored`); |
| 140 | + await seasonRequestRepository.remove(requestedSeason); |
| 141 | + } |
| 142 | + }); |
| 143 | + }) |
| 144 | + } |
| 145 | + } |
| 146 | + |
115 | 147 | processableSeasons.push({
|
116 | 148 | seasonNumber: season.seasonNumber,
|
117 | 149 | episodes: !server4k ? totalAvailableEpisodes : 0,
|
118 | 150 | episodes4k: server4k ? totalAvailableEpisodes : 0,
|
119 | 151 | totalEpisodes: season.statistics?.totalEpisodeCount ?? 0,
|
120 | 152 | processing: season.monitored && totalAvailableEpisodes === 0,
|
121 | 153 | is4kOverride: server4k,
|
| 154 | + monitored: season.monitored, |
122 | 155 | });
|
123 | 156 | }
|
124 | 157 |
|
|
0 commit comments