Backend: Implemented people fetching on entry create/refrech

This commit is contained in:
X4Lo committed 2025-07-11 23:28:37 +01:00
1 parent b5ea25631d
commit 381f921df7
2 files changed
+181 -13

No files matched your search

+87 -3
View File
@@ -14,9 +14,12 @@ const NotFoundError = require("../errors/NotFoundError");
const mediaOptimizerService = require("./mediaOptimizerService.js");
const mediaManager = require("./mediaManager.js");
const { fetchSeasonData } = require("./tmdbService.js");
const { fetchSeasonData, fetchEntryCredits } = require("./tmdbService.js");
const Network = require("../models/Network.js");
const networkService = require("./networkService.js");
const Cast = require("../models/Credit.js");
const People = require("../models/People.js");
const Credit = require("../models/Credit.js");
const _mediaManager = mediaManager;
const _mediaOptimizerService = mediaOptimizerService;
@@ -72,6 +75,12 @@ class EntryService {
{ model: Season, as: 'seasons' },
{ model: Media, as: 'medias' },
{ model: Network, as: 'networks' },
{
model: Cast, as: 'credits',
include: [
{ model: People, as: 'person' }
]
},
{
model: Collection, as: 'collections',
include: [
@@ -233,8 +242,84 @@ class EntryService {
}
// + end of: seasons & episodes for tv series
// ? cast
// ? Credits
const creditData = await fetchEntryCredits(data.id, is_tvseries);
if (!creditData) {
throw new InternalServerError(`Couldn't fetch entry credits data`);
}
// ? loop creditData.crew and creditData.cast and create a people for each if missing
// ? then create a cast/crew for each people if missing
for (const cast of creditData.cast) {
// ? check if the cast already exists
let existingPeople = await People.findOne({
where: { tmdb_id: cast.id }
});
// ? if not, create a new people
if (!existingPeople) {
existingPeople = await People.create({
tmdb_id: cast.id,
name: cast.name,
gender: cast.gender || null,
popularity: cast.popularity,
known_for_department: cast.known_for_department || null,
profile_path: cast.profile_path,
adult: cast.adult || false,
last_fetch: new Date()
});
}
// ? create a new cast
await Credit.create({
entry_id: savedEntry.id,
people_id: existingPeople.id,
tmdb_credit_id: cast.credit_id,
character: cast.character,
order: cast.order,
profile_path: existingPeople.profile_path || null,
adult: existingPeople.adult || false,
cast_type: 'cast'
});
}
for (const crew of creditData.crew) {
// ? check if the crew already exists
let existingPeople = await People.findOne({
where: { tmdb_id: crew.id }
});
// ? if not, create a new people
if (!existingPeople) {
existingPeople = await People.create({
tmdb_id: crew.id,
name: crew.name,
gender: crew.gender,
popularity: crew.popularity,
known_for_department: crew.known_for_department || null,
profile_path: crew.profile_path,
adult: crew.adult || false,
last_fetch: new Date()
});
}
// ? create a new cast
await Credit.create({
entry_id: savedEntry.id,
people_id: existingPeople.id,
tmdb_credit_id: crew.credit_id,
character: null,
order: null,
profile_path: existingPeople.profile_path || null,
adult: existingPeople.adult || false,
cast_type: 'crew',
department: crew.department || null,
job: crew.job || null
});
}
savedEntry.credit_fetched = true;
await savedEntry.save();
// ? media
const downloadedImages = await _mediaManager.getEntryMedia(data.id, is_tvseries);
@@ -252,7 +337,6 @@ class EntryService {
_mediaOptimizerService.doGeneratePreview('entries', image.type + 's', image.path);
}
// const currentGenresNames = await data.genres.map(genre => genre.name);
// savedEntry.setDataValue('entryId', ids);