feat(F-117): completed feature

This commit is contained in:
chattie
2026-08-21 14:26:52 +02:00
parent 365059916d
commit 13436652cd
9 changed files with 171 additions and 28 deletions

View File

@@ -87,6 +87,7 @@ try {
console.log(` + brand ${brand.name} (slug=${brand.slug})`);
}
let renamedCount = 0;
for (const rename of renamePlan) {
const result = await pool.query(
`UPDATE categories_categories SET name = $1, updated_at = now()
@@ -94,11 +95,33 @@ try {
[rename.newName, rename.oldName],
);
if (result.rowCount > 0) {
renamedCount += result.rowCount;
console.log(` ~ rename ${rename.oldName}${rename.newName}`);
}
}
// Also handle case-only renames for legacy Spanish entries that are
// already in the catalog but with the wrong casing (FRUTAS Y VERDURAS,
// SNACKS, GRANOLA). The UPDATE above is case-sensitive so it cannot
// match these. We re-query the DB for the actual stored names.
const storedNames = await pool.query('SELECT id, name FROM categories_categories');
const storedLower = new Map(storedNames.rows.map((r) => [r.name.toLowerCase(), r.id]));
for (const rename of renamePlan) {
const oldLower = rename.oldName.toLowerCase();
if (oldLower !== rename.newName.toLowerCase()) continue;
const id = storedLower.get(oldLower);
if (!id) continue;
const r2 = await pool.query(
`UPDATE categories_categories SET name = $1, updated_at = now()
WHERE id = $2 AND name <> $1`,
[rename.newName, id],
);
if (r2.rowCount > 0) {
renamedCount += r2.rowCount;
console.log(` ~ rename → ${rename.newName} (case-only)`);
}
}
console.log(`\nDone. Inserted ${plan.categories.length} categories and ${plan.brands.length} brands. Renamed ${renamePlan.length} categories.`);
console.log(`\nDone. Inserted ${plan.categories.length} categories and ${plan.brands.length} brands. Renamed ${renamedCount} categories.`);
} catch (error) {
console.error('Error:', error.message);
process.exit(1);

View File

@@ -271,10 +271,10 @@ export function buildRenamePlan(
if (entry.kind !== 'category') continue;
const translated = translateCategoryName(entry.name);
const oldLower = entry.name.toLowerCase();
const newLower = translated.toLowerCase();
if (oldLower === newLower) continue;
// The legacy Spanish entries (FRUTAS Y VERDURAS, SNACKS, GRANOLA) are
// already on the catalog but with the wrong casing. We rename them.
if (!existingCategoryNames.has(oldLower)) continue;
if (existingCategoryNames.has(newLower)) continue;
if (entry.name === translated) continue;
renames.push({ ocId: entry.ocId, oldName: entry.name, newName: translated });
}
return renames;