Add type-level duplicate credit cleanup to maintenance
Pass 1 handles exact role-level dupes. Pass 2 detects same person/entry/type with different roles and keeps the credit with a meaningful role, deleting the empty one.
This commit is contained in:
1 parent
ea91c6011a
commit
c09f70054c
1 file changed
+107
-15
@@ -252,14 +252,18 @@ class MaintenanceService {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Find and optionally remove duplicate credits.
|
* 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
|
* @param {boolean} [dry=false] - if true only count, if false also delete
|
||||||
*/
|
*/
|
||||||
async cleanDuplicateCredits(dry = false) {
|
async cleanDuplicateCredits(dry = false) {
|
||||||
const { Sequelize } = require("sequelize");
|
const { Sequelize } = require("sequelize");
|
||||||
|
|
||||||
const duplicates = await Credit.findAll({
|
// ── Pass 1: Exact role-level duplicates ──
|
||||||
|
const duplicateGroups = await Credit.findAll({
|
||||||
attributes: [
|
attributes: [
|
||||||
"entry_id",
|
"entry_id",
|
||||||
"people_id",
|
"people_id",
|
||||||
@@ -274,11 +278,11 @@ class MaintenanceService {
|
|||||||
raw: true,
|
raw: true,
|
||||||
});
|
});
|
||||||
|
|
||||||
let duplicateGroups = duplicates.length;
|
let pass1Groups = duplicateGroups.length;
|
||||||
let deletedCount = 0;
|
let deletedCount = 0;
|
||||||
|
|
||||||
if (!dry) {
|
if (!dry) {
|
||||||
for (const row of duplicates) {
|
for (const row of duplicateGroups) {
|
||||||
const idList = row.ids
|
const idList = row.ids
|
||||||
.split(",")
|
.split(",")
|
||||||
.map(Number)
|
.map(Number)
|
||||||
@@ -290,24 +294,112 @@ class MaintenanceService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const totalDupes = duplicates.reduce(
|
const pass1Dupes = duplicateGroups.reduce(
|
||||||
(sum, r) => sum + parseInt(r.cnt) - 1,
|
(sum, r) => sum + parseInt(r.cnt) - 1,
|
||||||
0,
|
0,
|
||||||
);
|
);
|
||||||
|
|
||||||
if (dry)
|
// ── Pass 2: Type-level duplicates (same person/entry/type, different roles) ──
|
||||||
logToFile(
|
const typeDupes = await Credit.findAll({
|
||||||
`Dry duplicate credits check: ${duplicateGroups} groups, ${totalDupes} duplicates`,
|
attributes: [
|
||||||
);
|
"entry_id",
|
||||||
else
|
"people_id",
|
||||||
logToFile(
|
"cast_type",
|
||||||
`Cleaned duplicate credits: ${deletedCount} removed from ${duplicateGroups} groups`,
|
[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: Pass1=${pass1Groups} groups (${pass1Dupes} dupes), Pass2=${pass2Groups} groups (${pass2Dupes} dupes) → ${totalGroups} groups, ${totalDupes} duplicates`,
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
logToFile(
|
||||||
|
`Cleaned duplicate credits: Pass1=${pass1Groups} groups (${deletedCount} deleted), Pass2=${pass2Groups} groups (${pass2Deleted} deleted) → ${totalDeleted} total removed`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
groups: duplicateGroups,
|
groups: totalGroups,
|
||||||
duplicates: totalDupes,
|
duplicates: totalDupes,
|
||||||
deleted: deletedCount,
|
deleted: totalDeleted,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user