From 8d96c0ce53ec1f396f1553de4ceecd1b30efc1fd Mon Sep 17 00:00:00 2001 From: Brandon Cohen Date: Mon, 26 Jun 2023 13:49:06 -0400 Subject: [PATCH] refactor: cleaned up code and added more detail to logs --- server/lib/availabilitySync.ts | 33 ++++++++++++++++++++++++++++++--- server/routes/request.ts | 2 +- 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/server/lib/availabilitySync.ts b/server/lib/availabilitySync.ts index bab387fbf..cfe02565c 100644 --- a/server/lib/availabilitySync.ts +++ b/server/lib/availabilitySync.ts @@ -3,7 +3,7 @@ import PlexAPI from '@server/api/plexapi'; import RadarrAPI, { type RadarrMovie } from '@server/api/servarr/radarr'; import type { SonarrSeason, SonarrSeries } from '@server/api/servarr/sonarr'; import SonarrAPI from '@server/api/servarr/sonarr'; -import { MediaStatus } from '@server/constants/media'; +import { MediaRequestStatus, MediaStatus } from '@server/constants/media'; import { getRepository } from '@server/datasource'; import Media from '@server/entity/Media'; import MediaRequest from '@server/entity/MediaRequest'; @@ -203,8 +203,8 @@ class AvailabilitySync { await this.mediaUpdater(media, true); } } - if (!mediaExists || didDeleteSeasons) { - await mediaRepository.save(media); + if (!mediaExists) { + await mediaRepository.save({ media, ...media }); } } } catch (ex) { @@ -487,6 +487,14 @@ class AvailabilitySync { } } + if ((!radarr || !radarr.hasFile) && !existsInPlex) { + movieExists = false; + } + + if ((!radarr4k || !radarr4k.hasFile) && !existsInPlex4k) { + movieExists4k = false; + } + return existsInRadarr; } @@ -540,6 +548,25 @@ class AvailabilitySync { } } + if ( + (!sonarr || sonarr.statistics.episodeFileCount === 0) && + !existsInPlex + ) { + showExists = false; + } + + if ( + (!sonarr4k || sonarr4k.statistics.episodeFileCount === 0) && + !existsInPlex4k + ) { + showExists4k = false; + } + + // Here we check each season for availability + for (const season of media.seasons) { + await this.seasonExists(media, season, showExists, showExists4k); + } + // Here we check each season for availability // If the API returns an error other than a 404, // we will have to prevent the season check from happening diff --git a/server/routes/request.ts b/server/routes/request.ts index 18cb6458e..dd53588df 100644 --- a/server/routes/request.ts +++ b/server/routes/request.ts @@ -40,7 +40,6 @@ requestRoutes.get, RequestResultsResponse>( switch (req.query.filter) { case 'approved': case 'processing': - case 'available': statusFilter = [MediaRequestStatus.APPROVED]; break; case 'pending': @@ -56,6 +55,7 @@ requestRoutes.get, RequestResultsResponse>( statusFilter = [MediaRequestStatus.FAILED]; break; case 'completed': + case 'available': case 'deleted': statusFilter = [MediaRequestStatus.COMPLETED]; break;