diff --git a/backend/services/maintenanceService.js b/backend/services/maintenanceService.js index c400afe..69ec9ee 100644 --- a/backend/services/maintenanceService.js +++ b/backend/services/maintenanceService.js @@ -252,14 +252,18 @@ class MaintenanceService { /** * 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. + * + * Pass 1: Role-level — same (entry, person, type, role). + * Pass 2: Type-level — same (entry, person, type) but different roles. + * Keeps the credit WITH a role; if both have roles, keeps newest ID. + * * @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({ + // ── Pass 1: Exact role-level duplicates ── + const duplicateGroups = await Credit.findAll({ attributes: [ "entry_id", "people_id", @@ -274,11 +278,11 @@ class MaintenanceService { raw: true, }); - let duplicateGroups = duplicates.length; + let pass1Groups = duplicateGroups.length; let deletedCount = 0; if (!dry) { - for (const row of duplicates) { + for (const row of duplicateGroups) { const idList = row.ids .split(",") .map(Number) @@ -290,24 +294,112 @@ class MaintenanceService { } } - const totalDupes = duplicates.reduce( + const pass1Dupes = duplicateGroups.reduce( (sum, r) => sum + parseInt(r.cnt) - 1, 0, ); - if (dry) + // ── Pass 2: Type-level duplicates (same person/entry/type, different roles) ── + const typeDupes = await Credit.findAll({ + attributes: [ + "entry_id", + "people_id", + "cast_type", + [Sequelize.fn("COUNT", Sequelize.col("id")), "cnt"], + [Sequelize.fn("GROUP_CONCAT", Sequelize.col("id")), "ids"], + [ + Sequelize.fn( + "GROUP_CONCAT", + Sequelize.fn( + "COALESCE", + Sequelize.col("character"), + Sequelize.col("job"), + ), + ), + "roles", + ], + ], + group: ["entry_id", "people_id", "cast_type"], + having: Sequelize.literal("COUNT(id) > 1"), + raw: true, + }); + + let pass2Groups = typeDupes.length; + let pass2Deleted = 0; + + if (!dry) { + for (const row of typeDupes) { + const idList = row.ids.split(",").map(Number); + + // Fetch these credits to inspect their roles + const credits = await Credit.findAll({ + where: { id: idList }, + order: [["id", "DESC"]], + }); + + // Separate: credits with roles vs without + const withRole = credits.filter((c) => { + if (c.cast_type === "cast") return c.character && c.character.trim(); + return c.job && c.job.trim(); + }); + const withoutRole = credits.filter((c) => { + if (c.cast_type === "cast") + return !c.character || !c.character.trim(); + return !c.job || !c.job.trim(); + }); + + let keep; + const toDelete = []; + + if (withRole.length > 0 && withoutRole.length > 0) { + // Prefer the one WITH a role; keep the newest among those with roles. + // Delete all without-role entries. + keep = withRole[0]; // already sorted by id DESC + toDelete.push(...withoutRole.map((c) => c.id)); + // If there are multiple with-role, delete the older ones + if (withRole.length > 1) { + toDelete.push(...withRole.slice(1).map((c) => c.id)); + } + } else if (withRole.length > 1) { + // All have roles — keep newest, delete older + keep = withRole[0]; + toDelete.push(...withRole.slice(1).map((c) => c.id)); + } else { + // None have roles — keep newest, delete older + keep = withoutRole[0]; + toDelete.push(...withoutRole.slice(1).map((c) => c.id)); + } + + if (toDelete.length > 0) { + await Credit.destroy({ where: { id: toDelete } }); + pass2Deleted += toDelete.length; + } + } + } + + const pass2Dupes = typeDupes.reduce( + (sum, r) => sum + parseInt(r.cnt) - 1, + 0, + ); + + const totalGroups = pass1Groups + pass2Groups; + const totalDupes = pass1Dupes + pass2Dupes; + const totalDeleted = deletedCount + pass2Deleted; + + if (dry) { logToFile( - `Dry duplicate credits check: ${duplicateGroups} groups, ${totalDupes} duplicates`, + `Dry duplicate credits: Pass1=${pass1Groups} groups (${pass1Dupes} dupes), Pass2=${pass2Groups} groups (${pass2Dupes} dupes) → ${totalGroups} groups, ${totalDupes} duplicates`, ); - else + } else { logToFile( - `Cleaned duplicate credits: ${deletedCount} removed from ${duplicateGroups} groups`, + `Cleaned duplicate credits: Pass1=${pass1Groups} groups (${deletedCount} deleted), Pass2=${pass2Groups} groups (${pass2Deleted} deleted) → ${totalDeleted} total removed`, ); + } return { - groups: duplicateGroups, + groups: totalGroups, duplicates: totalDupes, - deleted: deletedCount, + deleted: totalDeleted, }; }