Add duplicate credits maintenance and fix dedup logic

This commit is contained in:
X4Lo committed 2026-06-18 22:08:46 +01:00
1 parent 5a0328a17f
commit 3c089ee110
6 files changed
+188 -5

No files matched your search

+11 -5
View File
@@ -382,9 +382,13 @@ class EntryManager {
const existingCreditsMap = new Map();
for (const credit of existingCreditsList) {
// unique key: people_id + cast_type (scoped to entry_id already)
// unique key: people_id + cast_type + role (character or job)
const roleKey =
credit.cast_type === "cast"
? credit.character || ""
: credit.job || "";
existingCreditsMap.set(
`${credit.people_id}_${credit.cast_type}`,
`${credit.people_id}_${credit.cast_type}_${roleKey}`,
credit,
);
}
@@ -396,9 +400,6 @@ class EntryManager {
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 =
@@ -421,6 +422,11 @@ class EntryManager {
: c.job || null
: null;
// Role key must match map key: use the resolved character/job (after aggregate handling)
const roleKey = c.cast_type === "cast" ? character || "" : job || "";
const key = `${person.id}_${c.cast_type}_${roleKey}`;
const existingCredit = existingCreditsMap.get(key);
const tmdbCreditId = Array.isArray(c.roles)
? c.roles?.[0]?.credit_id
: Array.isArray(c.jobs)
+61
View File
@@ -249,6 +249,67 @@ class MaintenanceService {
return { count, fixed };
}
/**
* Find and optionally remove duplicate credits.
* A duplicate is the same person in the same entry with the same role.
* For cast: same character. For crew: same job.
* @param {boolean} [dry=false] - if true only count, if false also delete
*/
async cleanDuplicateCredits(dry = false) {
const { Sequelize } = require("sequelize");
const duplicates = await Credit.findAll({
attributes: [
"entry_id",
"people_id",
"cast_type",
"character",
"job",
[Sequelize.fn("COUNT", Sequelize.col("id")), "cnt"],
[Sequelize.fn("GROUP_CONCAT", Sequelize.col("id")), "ids"],
],
group: ["entry_id", "people_id", "cast_type", "character", "job"],
having: Sequelize.literal("COUNT(id) > 1"),
raw: true,
});
let duplicateGroups = duplicates.length;
let deletedCount = 0;
if (!dry) {
for (const row of duplicates) {
const idList = row.ids
.split(",")
.map(Number)
.sort((a, b) => a - b);
// Keep the newest (highest ID), delete the older ones
const toDelete = idList.slice(0, -1);
await Credit.destroy({ where: { id: toDelete } });
deletedCount += toDelete.length;
}
}
const totalDupes = duplicates.reduce(
(sum, r) => sum + parseInt(r.cnt) - 1,
0,
);
if (dry)
logToFile(
`Dry duplicate credits check: ${duplicateGroups} groups, ${totalDupes} duplicates`,
);
else
logToFile(
`Cleaned duplicate credits: ${deletedCount} removed from ${duplicateGroups} groups`,
);
return {
groups: duplicateGroups,
duplicates: totalDupes,
deleted: deletedCount,
};
}
}
module.exports = new MaintenanceService();