feat(backend): fix credit duplicates, support multi-role TV credits, fix collection upload

- Fix credit dedup key to people_id + cast_type (scoped to entry_id)
- Detect aggregate_credits format via Array.isArray(c.roles) instead of is_tvseries flag
- Join multiple roles/jobs with "; " instead of taking only the first
- Add episode_count field to Credit model from total_episode_count
- Increase character and job columns to VARCHAR(500)
- Deselect existing collection posters/backdrops before inserting new upload
- Updated docs-vault
- Updated some agents files
This commit is contained in:
Mohammed Abad committed 2026-04-23 16:51:18 +01:00
1 parent 260282a0e2
commit ce01dc34fa
14 files changed
+293 -45

No files matched your search

+162 -28
View File
@@ -157,7 +157,7 @@ class EntryManager {
// * For TV-Series (Seasons & Episodes)
if (oldEntry.is_tvseries) {
// ? Only refrech if the series's status is not 'Ended'
// ? and atleast 48 hours has passed
// ? and atleast 48 hours has passed
// * Update or insert seasons for the entry
for (const season of entryData.seasons) {
@@ -257,19 +257,30 @@ class EntryManager {
}
// * Credits
if (!oldEntry.cast_fetched || oldEntry.credits.length === 0) {
// if (!oldEntry.cast_fetched || oldEntry.credits.length === 0) {
if (true) {
const creditData = await fetchEntryCredits(oldEntry.entryId.tmdb_id, oldEntry.is_tvseries);
if (!creditData) {
throw new InternalServerError(`Couldn't fetch entry crew data`);
}
const allCredits = [...creditData.cast.map(c => ({ ...c, cast_type: 'cast' })),
...creditData.crew.map(c => ({ ...c, cast_type: 'crew' }))];
const allCredits = [
...creditData.cast.map(c => ({ ...c, cast_type: "cast" })),
...creditData.crew.map(c => ({ ...c, cast_type: "crew" }))
];
const tmdbIds = [...new Set(allCredits.map(c => c.id))];
// -------------------------
// FETCH EXISTING PEOPLE
// -------------------------
const existingPeopleList = await People.findAll({ where: { tmdb_id: tmdbIds } });
const existingPeopleMap = Object.fromEntries(existingPeopleList.map(p => [p.tmdb_id, p]));
// -------------------------
// CREATE NEW PEOPLE
// -------------------------
const newPeopleData = tmdbIds
.filter(id => !existingPeopleMap[id])
.map(id => {
@@ -286,34 +297,157 @@ class EntryManager {
};
});
if (newPeopleData.length) {
console.log(`[PEOPLE] Adding ${newPeopleData.length} new people...`);
}
const createdPeople = await People.bulkCreate(newPeopleData);
for (const person of createdPeople) {
console.log(`[PEOPLE] Added: ${person.name} (tmdb_id=${person.tmdb_id})`);
existingPeopleMap[person.tmdb_id] = person;
}
const existingPeopleIds = new Set(oldEntry.credits.map(c => c.people_id));
// -------------------------
// UPDATE EXISTING PEOPLE POPULARITY
// -------------------------
const peopleToUpdate = [];
const newCredits = allCredits
.map(c => {
const person = existingPeopleMap[c.id];
if (existingPeopleIds.has(person.id)) return null;
return {
entry_id: oldEntry.id,
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,
adult: person.adult,
cast_type: c.cast_type,
department: c.cast_type === 'crew' ? c.department || null : null,
job: c.cast_type === 'crew' ? c.job || null : null
};
})
.filter(Boolean);
for (const credit of allCredits) {
const existingPerson = existingPeopleMap[credit.id];
if (!existingPerson) continue;
if (newCredits.length) {
await Credit.bulkCreate(newCredits);
// only update if changed
if (existingPerson.popularity !== credit.popularity) {
existingPerson.popularity = credit.popularity;
existingPerson.last_fetch = new Date();
// TODO: update person photo and add a new field to track the last photo update
peopleToUpdate.push(existingPerson);
}
}
if (peopleToUpdate.length) {
await People.bulkCreate(
peopleToUpdate.map(p => ({
id: p.id,
tmdb_id: p.tmdb_id,
popularity: p.popularity,
last_fetch: p.last_fetch
})),
{
updateOnDuplicate: ["popularity", "last_fetch"]
}
);
console.log(`[PEOPLE] Updated popularity for ${peopleToUpdate.length} people`);
}
// -------------------------
// FETCH EXISTING CREDITS FOR ENTRY
// -------------------------
const existingCreditsList = await Credit.findAll({
where: { entry_id: oldEntry.id }
});
const existingCreditsMap = new Map();
for (const credit of existingCreditsList) {
// unique key: people_id + cast_type (scoped to entry_id already)
existingCreditsMap.set(`${credit.people_id}_${credit.cast_type}`, credit);
}
const creditsToInsert = [];
const creditsToUpdate = [];
for (const c of allCredits) {
const person = existingPeopleMap[c.id];
if (!person) continue;
const key = `${person.id}_${c.cast_type}`;
const existingCredit = existingCreditsMap.get(key);
// For TV series (aggregate_credits), character/job are nested inside roles[]/jobs[].
// Detect TV format by presence of roles/jobs arrays rather than relying on is_tvseries.
const character = c.cast_type === "cast"
? (Array.isArray(c.roles)
? (c.roles.map(r => r.character).filter(Boolean).join("; ") || null)
: (c.character || null))
: null;
const job = c.cast_type === "crew"
? (Array.isArray(c.jobs)
? (c.jobs.map(j => j.job).filter(Boolean).join("; ") || null)
: (c.job || null))
: null;
const tmdbCreditId = Array.isArray(c.roles)
? c.roles?.[0]?.credit_id
: Array.isArray(c.jobs)
? c.jobs?.[0]?.credit_id
: c.credit_id;
const newCreditData = {
entry_id: oldEntry.id,
people_id: person.id,
tmdb_credit_id: tmdbCreditId,
character,
order: c.cast_type === "cast" ? c.order : null,
episode_count: c.total_episode_count ?? null,
profile_path: person.profile_path,
adult: person.adult,
cast_type: c.cast_type,
department: c.cast_type === "crew" ? c.department || null : null,
job
};
if (!existingCredit) {
creditsToInsert.push(newCreditData);
console.log(
`[CREDIT] INSERT ${c.cast_type.toUpperCase()} -> ${person.name} (people_id=${person.id})`
);
} else {
// check if update needed
let changed = false;
if (existingCredit.order !== newCreditData.order) changed = true;
if (existingCredit.character !== newCreditData.character) changed = true;
if (existingCredit.department !== newCreditData.department) changed = true;
if (existingCredit.job !== newCreditData.job) changed = true;
if (existingCredit.episode_count !== newCreditData.episode_count) changed = true;
if (changed) {
creditsToUpdate.push({
id: existingCredit.id,
...newCreditData
});
console.log(
`[CREDIT] UPDATE ${c.cast_type.toUpperCase()} -> ${person.name} (people_id=${person.id})`
);
}
}
}
// -------------------------
// INSERT NEW CREDITS
// -------------------------
if (creditsToInsert.length) {
await Credit.bulkCreate(creditsToInsert);
console.log(`[CREDITS] Inserted ${creditsToInsert.length} new credits for entry ${oldEntry.id}`);
}
// -------------------------
// UPDATE EXISTING CREDITS
// -------------------------
if (creditsToUpdate.length) {
await Credit.bulkCreate(creditsToUpdate, {
updateOnDuplicate: ["order", "character", "episode_count", "department", "job", "profile_path", "adult"]
});
console.log(`[CREDITS] Updated ${creditsToUpdate.length} credits for entry ${oldEntry.id}`);
}
if (!creditsToInsert.length && !creditsToUpdate.length) {
console.log(`[CREDITS] No changes for entry ${oldEntry.id}`);
}
}
@@ -370,13 +504,13 @@ class EntryManager {
/**
* Retrieves an entry by its ID and refreshes it if needed.
*
*
* An entry is refreshed if:
* - It has never been fetched before (`last_fetch` is null or undefined), or
* - The last fetch was more than 24 hours ago.
*
*
* If a refresh is required, it logs the operation and calls `refrechEntryById`.
*
*
* @async
* @function getUpdatedEntry
* @param {number} id - The ID of the entry to retrieve.