fix: requests will now be updated to completed on scan

pull/3460/head
Brandon 2 years ago committed by OwsleyJr
parent 1cb31f43ea
commit 05ea56188b

@ -1,8 +1,14 @@
import TheMovieDb from '@server/api/themoviedb'; import TheMovieDb from '@server/api/themoviedb';
import { MediaStatus, MediaType } from '@server/constants/media'; import {
MediaRequestStatus,
MediaStatus,
MediaType,
} from '@server/constants/media';
import { getRepository } from '@server/datasource'; import { getRepository } from '@server/datasource';
import Media from '@server/entity/Media'; import Media from '@server/entity/Media';
import MediaRequest from '@server/entity/MediaRequest';
import Season from '@server/entity/Season'; import Season from '@server/entity/Season';
import SeasonRequest from '@server/entity/SeasonRequest';
import { getSettings } from '@server/lib/settings'; import { getSettings } from '@server/lib/settings';
import logger from '@server/logger'; import logger from '@server/logger';
import AsyncLock from '@server/utils/asyncLock'; import AsyncLock from '@server/utils/asyncLock';
@ -89,6 +95,53 @@ class BaseScanner<T> {
return existing; return existing;
} }
private async requestUpdater(mediaId: number, is4k: boolean) {
const requestRepository = getRepository(MediaRequest);
const request = await requestRepository.findOne({
relations: {
media: true,
},
where: { media: { id: mediaId }, is4k: is4k },
});
await requestRepository.update(
{ media: { id: request?.id } },
{ status: MediaRequestStatus.COMPLETED }
);
}
private async seasonRequestUpdater(
mediaId: number | undefined,
seasonNumber: number,
is4k: boolean
) {
const seasonRequestRepository = getRepository(SeasonRequest);
const seasonToBeCompleted = await seasonRequestRepository.findOne({
relations: {
request: {
media: true,
},
},
where: {
request: {
is4k: is4k,
media: {
id: mediaId,
},
},
seasonNumber: seasonNumber,
},
});
if (seasonToBeCompleted) {
await seasonRequestRepository.update(seasonToBeCompleted?.id, {
status: MediaRequestStatus.COMPLETED,
});
}
}
protected async processMovie( protected async processMovie(
tmdbId: number, tmdbId: number,
{ {
@ -162,7 +215,16 @@ class BaseScanner<T> {
} }
if (changedExisting) { if (changedExisting) {
if (existing['status'] === MediaStatus.AVAILABLE) {
this.requestUpdater(existing.id, false);
}
if (existing['status4k'] === MediaStatus.AVAILABLE) {
this.requestUpdater(existing.id, true);
}
await mediaRepository.save(existing); await mediaRepository.save(existing);
this.log( this.log(
`Media for ${title} exists. Changes were detected and the title will be updated.`, `Media for ${title} exists. Changes were detected and the title will be updated.`,
'info' 'info'
@ -203,6 +265,15 @@ class BaseScanner<T> {
newMedia.ratingKey4k = newMedia.ratingKey4k =
is4k && this.enable4kMovie ? ratingKey : undefined; is4k && this.enable4kMovie ? ratingKey : undefined;
} }
if (newMedia['status'] === MediaStatus.AVAILABLE) {
this.requestUpdater(newMedia.id, false);
}
if (newMedia['status4k'] === MediaStatus.AVAILABLE && existing) {
this.requestUpdater(newMedia.id, true);
}
await mediaRepository.save(newMedia); await mediaRepository.save(newMedia);
this.log(`Saved new media: ${title}`); this.log(`Saved new media: ${title}`);
} }
@ -297,6 +368,14 @@ class BaseScanner<T> {
: season.is4kOverride && season.processing : season.is4kOverride && season.processing
? MediaStatus.PROCESSING ? MediaStatus.PROCESSING
: existingSeason.status4k; : existingSeason.status4k;
if (existingSeason['status'] === MediaStatus.AVAILABLE) {
this.seasonRequestUpdater(media?.id, season.seasonNumber, false);
}
if (existingSeason['status4k'] === MediaStatus.AVAILABLE) {
this.seasonRequestUpdater(media?.id, season.seasonNumber, true);
}
} else { } else {
newSeasons.push( newSeasons.push(
new Season({ new Season({
@ -321,6 +400,18 @@ class BaseScanner<T> {
: MediaStatus.UNKNOWN, : MediaStatus.UNKNOWN,
}) })
); );
if (season.totalEpisodes === season.episodes && season.episodes > 0) {
this.seasonRequestUpdater(media?.id, season.seasonNumber, false);
}
if (
this.enable4kShow &&
season.totalEpisodes === season.episodes4k &&
season.episodes4k > 0
) {
this.seasonRequestUpdater(media?.id, season.seasonNumber, true);
}
} }
} }
@ -439,7 +530,16 @@ class BaseScanner<T> {
) )
? MediaStatus.PROCESSING ? MediaStatus.PROCESSING
: MediaStatus.UNKNOWN; : MediaStatus.UNKNOWN;
if (isAllStandardSeasons) {
this.requestUpdater(media.id, false);
}
if (isAll4kSeasons) {
this.requestUpdater(media.id, true);
}
await mediaRepository.save(media); await mediaRepository.save(media);
this.log(`Updating existing title: ${title}`); this.log(`Updating existing title: ${title}`);
} else { } else {
const newMedia = new Media({ const newMedia = new Media({
@ -499,7 +599,16 @@ class BaseScanner<T> {
? MediaStatus.PROCESSING ? MediaStatus.PROCESSING
: MediaStatus.UNKNOWN, : MediaStatus.UNKNOWN,
}); });
if (newMedia['status'] === MediaStatus.AVAILABLE) {
this.requestUpdater(newMedia.id, false);
}
if (newMedia['status4k'] === MediaStatus.AVAILABLE) {
this.requestUpdater(newMedia.id, true);
}
await mediaRepository.save(newMedia); await mediaRepository.save(newMedia);
this.log(`Saved ${title}`); this.log(`Saved ${title}`);
} }
}); });

@ -1,7 +1,13 @@
import TautulliAPI from '@server/api/tautulli'; import TautulliAPI from '@server/api/tautulli';
import { MediaStatus, MediaType } from '@server/constants/media'; import {
MediaRequestStatus,
MediaStatus,
MediaType,
} from '@server/constants/media';
import { getRepository } from '@server/datasource'; import { getRepository } from '@server/datasource';
import Media from '@server/entity/Media'; import Media from '@server/entity/Media';
import MediaRequest from '@server/entity/MediaRequest';
import SeasonRequest from '@server/entity/SeasonRequest';
import { User } from '@server/entity/User'; import { User } from '@server/entity/User';
import type { import type {
MediaResultsResponse, MediaResultsResponse,
@ -98,6 +104,8 @@ mediaRoutes.post<
isAuthenticated(Permission.MANAGE_REQUESTS), isAuthenticated(Permission.MANAGE_REQUESTS),
async (req, res, next) => { async (req, res, next) => {
const mediaRepository = getRepository(Media); const mediaRepository = getRepository(Media);
const requestRepository = getRepository(MediaRequest);
const seasonRequestRepository = getRepository(SeasonRequest);
const media = await mediaRepository.findOne({ const media = await mediaRepository.findOne({
where: { id: Number(req.params.id) }, where: { id: Number(req.params.id) },
@ -138,6 +146,26 @@ mediaRoutes.post<
media.status = MediaStatus.UNKNOWN; media.status = MediaStatus.UNKNOWN;
} }
if (req.params.status === 'available') {
const request = await requestRepository.findOne({
relations: {
media: true,
},
where: { media: { id: media.id }, is4k: is4k },
});
await requestRepository.update(
{ media: { id: request?.id } },
{ status: MediaRequestStatus.COMPLETED }
);
request?.seasons.forEach(async (season) => {
await seasonRequestRepository.update(season.id, {
status: MediaRequestStatus.COMPLETED,
});
});
}
await mediaRepository.save(media); await mediaRepository.save(media);
return res.status(200).json(media); return res.status(200).json(media);

Loading…
Cancel
Save