Fix credits linking for TV aggregate format in create and refresh
Handle TV aggregate_credits (roles/jobs arrays) in entry creation, matching the clean logic already in refresh. Also fix director detection to search jobs[] array for TV series in both paths.
This commit is contained in:
1 parent
b117b6bb98
commit
ea91c6011a
2 files changed
+66
-18
No files matched your search
@@ -293,6 +293,7 @@ class EntryService {
|
||||
|
||||
const tmdbIds = [...new Set(allCredits.map((c) => c.id))];
|
||||
|
||||
// — FETCH EXISTING PEOPLE —
|
||||
const existingPeopleList = await People.findAll({
|
||||
where: { tmdb_id: tmdbIds },
|
||||
});
|
||||
@@ -300,6 +301,7 @@ class EntryService {
|
||||
existingPeopleList.map((p) => [p.tmdb_id, p]),
|
||||
);
|
||||
|
||||
// — CREATE NEW PEOPLE —
|
||||
const newPeopleData = tmdbIds
|
||||
.filter((id) => !existingPeopleMap[id])
|
||||
.map((id) => {
|
||||
@@ -316,36 +318,80 @@ class EntryService {
|
||||
};
|
||||
});
|
||||
|
||||
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 newCredits = allCredits.map((c) => {
|
||||
// — BUILD NEW CREDITS —
|
||||
const newCredits = [];
|
||||
|
||||
for (const c of allCredits) {
|
||||
const person = existingPeopleMap[c.id];
|
||||
return {
|
||||
if (!person) continue;
|
||||
|
||||
// Detect TV aggregate format (roles/jobs arrays) vs movie flat format.
|
||||
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;
|
||||
|
||||
newCredits.push({
|
||||
entry_id: savedEntry.id,
|
||||
people_id: person.id,
|
||||
tmdb_credit_id: c.credit_id,
|
||||
character: c.cast_type === "cast" ? c.character : null,
|
||||
tmdb_credit_id: tmdbCreditId,
|
||||
character,
|
||||
order: c.cast_type === "cast" ? c.order : null,
|
||||
profile_path: person.profile_path || null,
|
||||
adult: person.adult || false,
|
||||
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: c.cast_type === "crew" ? c.job || null : null,
|
||||
};
|
||||
});
|
||||
job,
|
||||
});
|
||||
}
|
||||
|
||||
await Credit.bulkCreate(newCredits);
|
||||
console.log(
|
||||
`[CREDITS] Inserted ${newCredits.length} credits for entry ${savedEntry.id}`,
|
||||
);
|
||||
|
||||
savedEntry.credit_fetched = true;
|
||||
|
||||
// Extract director name from credits
|
||||
const directorData = allCredits.find(
|
||||
(c) => c.cast_type === "crew" && c.job === "Director",
|
||||
);
|
||||
savedEntry.director = directorData?.name || null;
|
||||
// Extract director name from processed credits (handles both movie & TV aggregate format)
|
||||
const directorCredit = allCredits.find((c) => {
|
||||
if (c.cast_type !== "crew") return false;
|
||||
const jobs = Array.isArray(c.jobs) ? c.jobs.map((j) => j.job) : [c.job];
|
||||
return jobs.includes("Director");
|
||||
});
|
||||
savedEntry.director = directorCredit?.name || null;
|
||||
|
||||
await savedEntry.save();
|
||||
|
||||
|
||||
Reference in new issue
Block a user