fix(api): prevent duplicate movie requests

fixes #705
pull/712/head^2
sct 4 years ago
parent e97228899a
commit 421f4c17f0

@ -114,20 +114,40 @@ requestRoutes.post(
status4k: req.body.is4k ? MediaStatus.PENDING : MediaStatus.UNKNOWN, status4k: req.body.is4k ? MediaStatus.PENDING : MediaStatus.UNKNOWN,
mediaType: req.body.mediaType, mediaType: req.body.mediaType,
}); });
await mediaRepository.save(media);
} else { } else {
if (media.status === MediaStatus.UNKNOWN && !req.body.is4k) { if (media.status === MediaStatus.UNKNOWN && !req.body.is4k) {
media.status = MediaStatus.PENDING; media.status = MediaStatus.PENDING;
await mediaRepository.save(media);
} }
if (media.status4k === MediaStatus.UNKNOWN && req.body.is4k) { if (media.status4k === MediaStatus.UNKNOWN && req.body.is4k) {
media.status4k = MediaStatus.PENDING; media.status4k = MediaStatus.PENDING;
await mediaRepository.save(media);
} }
} }
if (req.body.mediaType === 'movie') { if (req.body.mediaType === 'movie') {
const existing = await requestRepository.findOne({
where: {
media: {
tmdbId: tmdbMedia.id,
},
requestedBy: req.user,
is4k: req.body.is4k,
},
});
if (existing) {
logger.warn('Duplicate request for media blocked', {
tmdbId: tmdbMedia.id,
mediaType: req.body.mediaType,
});
return next({
status: 409,
message: 'Request for this media already exists.',
});
}
await mediaRepository.save(media);
const request = new MediaRequest({ const request = new MediaRequest({
type: MediaType.MOVIE, type: MediaType.MOVIE,
media, media,
@ -185,6 +205,8 @@ requestRoutes.post(
}); });
} }
await mediaRepository.save(media);
const request = new MediaRequest({ const request = new MediaRequest({
type: MediaType.TV, type: MediaType.TV,
media: { media: {

Loading…
Cancel
Save