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
@@ -255,90 +255,63 @@ class EntryManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// * Credits
|
// * Credits
|
||||||
if (!oldEntry.cast_fetched || oldEntry.credits.length == 0) {
|
if (!oldEntry.cast_fetched || oldEntry.credits.length === 0) {
|
||||||
const creditData = await fetchEntryCredits(oldEntry.entryId.tmdb_id, oldEntry.is_tvseries);
|
const creditData = await fetchEntryCredits(oldEntry.entryId.tmdb_id, oldEntry.is_tvseries);
|
||||||
if (!creditData) {
|
if (!creditData) {
|
||||||
throw new InternalServerError(`Couldn't fetch entry crew data`);
|
throw new InternalServerError(`Couldn't fetch entry crew data`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ? loop creditData.crew and creditData.cast and create a people for each if missing
|
const allCredits = [...creditData.cast.map(c => ({ ...c, cast_type: 'cast' })),
|
||||||
// ? then create a cast/crew for each people if missing
|
...creditData.crew.map(c => ({ ...c, cast_type: 'crew' }))];
|
||||||
for (const cast of creditData.cast) {
|
|
||||||
// ? check if the cast already exists
|
const tmdbIds = [...new Set(allCredits.map(c => c.id))];
|
||||||
let existingPeople = await People.findOne({
|
const existingPeopleList = await People.findAll({ where: { tmdb_id: tmdbIds } });
|
||||||
where: { tmdb_id: cast.id }
|
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()
|
||||||
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
// ? if not, create a new people
|
const createdPeople = await People.bulkCreate(newPeopleData);
|
||||||
if (!existingPeople) {
|
for (const person of createdPeople) {
|
||||||
existingPeople = await People.create({
|
existingPeopleMap[person.tmdb_id] = person;
|
||||||
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()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// ? check if the cast already exists for this entry
|
|
||||||
let existingCast = oldEntry.credits.find(c => c.people_id === existingPeople.id);
|
|
||||||
|
|
||||||
// ? if not, create a new cast
|
|
||||||
if (!existingCast) {
|
|
||||||
await Credit.create({
|
|
||||||
entry_id: oldEntry.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) {
|
const existingPeopleIds = new Set(oldEntry.credits.map(c => c.people_id));
|
||||||
// ? check if the crew already exists
|
|
||||||
let existingPeople = await People.findOne({
|
|
||||||
where: { tmdb_id: crew.id }
|
|
||||||
});
|
|
||||||
|
|
||||||
// ? if not, create a new people
|
const newCredits = allCredits
|
||||||
if (!existingPeople) {
|
.map(c => {
|
||||||
existingPeople = await People.create({
|
const person = existingPeopleMap[c.id];
|
||||||
tmdb_id: crew.id,
|
if (existingPeopleIds.has(person.id)) return null;
|
||||||
name: crew.name,
|
return {
|
||||||
gender: crew.gender || null,
|
|
||||||
popularity: crew.popularity,
|
|
||||||
known_for_department: crew.known_for_department || null,
|
|
||||||
profile_path: crew.profile_path,
|
|
||||||
adult: crew.adult || false,
|
|
||||||
last_fetch: new Date()
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
// ? check if the crew already exists for this entry
|
|
||||||
let existingCast = oldEntry.credits.find(c => c.people_id === existingPeople.id);
|
|
||||||
|
|
||||||
// ? if not, create a new cast
|
|
||||||
if (!existingCast) {
|
|
||||||
await Credit.create({
|
|
||||||
entry_id: oldEntry.id,
|
entry_id: oldEntry.id,
|
||||||
people_id: existingPeople.id,
|
people_id: person.id,
|
||||||
tmdb_credit_id: crew.credit_id,
|
tmdb_credit_id: c.credit_id,
|
||||||
character: null,
|
character: c.cast_type === 'cast' ? c.character : null,
|
||||||
order: null,
|
order: c.cast_type === 'cast' ? c.order : null,
|
||||||
profile_path: existingPeople.profile_path || null,
|
profile_path: person.profile_path,
|
||||||
adult: existingPeople.adult || false,
|
adult: person.adult,
|
||||||
cast_type: 'crew',
|
cast_type: c.cast_type,
|
||||||
department: crew.department || null,
|
department: c.cast_type === 'crew' ? c.department || null : null,
|
||||||
job: crew.job || null
|
job: c.cast_type === 'crew' ? c.job || null : null
|
||||||
});
|
};
|
||||||
}
|
})
|
||||||
|
.filter(Boolean);
|
||||||
|
|
||||||
|
if (newCredits.length) {
|
||||||
|
await Credit.bulkCreate(newCredits);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -248,75 +248,56 @@ class EntryService {
|
|||||||
throw new InternalServerError(`Couldn't fetch entry credits data`);
|
throw new InternalServerError(`Couldn't fetch entry credits data`);
|
||||||
}
|
}
|
||||||
|
|
||||||
// ? loop creditData.crew and creditData.cast and create a people for each if missing
|
const allCredits = [
|
||||||
// ? then create a cast/crew for each people if missing
|
...creditData.cast.map(c => ({ ...c, cast_type: 'cast' })),
|
||||||
for (const cast of creditData.cast) {
|
...creditData.crew.map(c => ({ ...c, cast_type: 'crew' })),
|
||||||
// ? check if the cast already exists
|
];
|
||||||
let existingPeople = await People.findOne({
|
|
||||||
where: { tmdb_id: cast.id }
|
|
||||||
});
|
|
||||||
|
|
||||||
// ? if not, create a new people
|
const tmdbIds = [...new Set(allCredits.map(c => c.id))];
|
||||||
if (!existingPeople) {
|
|
||||||
existingPeople = await People.create({
|
const existingPeopleList = await People.findAll({
|
||||||
tmdb_id: cast.id,
|
where: { tmdb_id: tmdbIds },
|
||||||
name: cast.name,
|
});
|
||||||
gender: cast.gender || null,
|
const existingPeopleMap = Object.fromEntries(existingPeopleList.map(p => [p.tmdb_id, p]));
|
||||||
popularity: cast.popularity,
|
|
||||||
known_for_department: cast.known_for_department || null,
|
const newPeopleData = tmdbIds
|
||||||
profile_path: cast.profile_path,
|
.filter(id => !existingPeopleMap[id])
|
||||||
adult: cast.adult || false,
|
.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()
|
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) {
|
const newCredits = allCredits.map(c => {
|
||||||
// ? check if the crew already exists
|
const person = existingPeopleMap[c.id];
|
||||||
let existingPeople = await People.findOne({
|
return {
|
||||||
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,
|
entry_id: savedEntry.id,
|
||||||
people_id: existingPeople.id,
|
people_id: person.id,
|
||||||
tmdb_credit_id: crew.credit_id,
|
tmdb_credit_id: c.credit_id,
|
||||||
character: null,
|
character: c.cast_type === 'cast' ? c.character : null,
|
||||||
order: null,
|
order: c.cast_type === 'cast' ? c.order : null,
|
||||||
profile_path: existingPeople.profile_path || null,
|
profile_path: person.profile_path || null,
|
||||||
adult: existingPeople.adult || false,
|
adult: person.adult || false,
|
||||||
cast_type: 'crew',
|
cast_type: c.cast_type,
|
||||||
department: crew.department || null,
|
department: c.cast_type === 'crew' ? c.department || null : null,
|
||||||
job: crew.job || null
|
job: c.cast_type === 'crew' ? c.job || null : null,
|
||||||
});
|
};
|
||||||
}
|
});
|
||||||
|
|
||||||
|
await Credit.bulkCreate(newCredits);
|
||||||
|
|
||||||
savedEntry.credit_fetched = true;
|
savedEntry.credit_fetched = true;
|
||||||
await savedEntry.save();
|
await savedEntry.save();
|
||||||
|
|||||||
Reference in new issue
Block a user