Backend: Optimized entry credits creation on create and refrech
This commit is contained in:
1 parent
f1cd3f9167
commit
1224d48c5c
2 files changed
+90
-136
No files matched your search
@@ -248,75 +248,56 @@ class EntryService {
|
||||
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 }
|
||||
});
|
||||
const allCredits = [
|
||||
...creditData.cast.map(c => ({ ...c, cast_type: 'cast' })),
|
||||
...creditData.crew.map(c => ({ ...c, cast_type: 'crew' })),
|
||||
];
|
||||
|
||||
// ? 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,
|
||||
const tmdbIds = [...new Set(allCredits.map(c => c.id))];
|
||||
|
||||
const existingPeopleList = await People.findAll({
|
||||
where: { tmdb_id: tmdbIds },
|
||||
});
|
||||
const existingPeopleMap = Object.fromEntries(existingPeopleList.map(p => [p.tmdb_id, p]));
|
||||
|
||||
const newPeopleData = tmdbIds
|
||||
.filter(id => !existingPeopleMap[id])
|
||||
.map(id => {
|
||||
const credit = allCredits.find(c => c.id === id);
|
||||
return {
|
||||
tmdb_id: credit.id,
|
||||
name: credit.name,
|
||||
gender: credit.gender || null,
|
||||
popularity: credit.popularity,
|
||||
known_for_department: credit.known_for_department || null,
|
||||
profile_path: credit.profile_path,
|
||||
adult: credit.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'
|
||||
};
|
||||
});
|
||||
|
||||
const createdPeople = await People.bulkCreate(newPeopleData);
|
||||
for (const person of createdPeople) {
|
||||
existingPeopleMap[person.tmdb_id] = person;
|
||||
}
|
||||
|
||||
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({
|
||||
const newCredits = allCredits.map(c => {
|
||||
const person = existingPeopleMap[c.id];
|
||||
return {
|
||||
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
|
||||
});
|
||||
}
|
||||
people_id: person.id,
|
||||
tmdb_credit_id: c.credit_id,
|
||||
character: c.cast_type === 'cast' ? c.character : null,
|
||||
order: c.cast_type === 'cast' ? c.order : null,
|
||||
profile_path: person.profile_path || null,
|
||||
adult: person.adult || false,
|
||||
cast_type: c.cast_type,
|
||||
department: c.cast_type === 'crew' ? c.department || null : null,
|
||||
job: c.cast_type === 'crew' ? c.job || null : null,
|
||||
};
|
||||
});
|
||||
|
||||
await Credit.bulkCreate(newCredits);
|
||||
|
||||
savedEntry.credit_fetched = true;
|
||||
await savedEntry.save();
|
||||
|
||||
Reference in new issue
Block a user