feat(api): product families backfill script

This commit is contained in:
yeuimu
2026-08-28 12:36:39 +08:00
parent d20a933e34
commit d63f1a6f35
4 changed files with 159 additions and 76 deletions
@@ -132,7 +132,7 @@ export class ProductFamiliesService {
async autoGroup(apply: boolean) {
const candidates = await this.prisma.originGood.findMany({
where: { familyId: null, delisted: false },
select: { id: true, goodName: true, goodImage: true },
select: { id: true, goodName: true, goodImage: true, source: true },
orderBy: { id: 'asc' },
});
const groups = new Map<string, typeof candidates>();
@@ -160,10 +160,16 @@ export class ProductFamiliesService {
let applied = 0;
for (const members of groups.values()) {
const parsed = parseOriginName(members[0].goodName);
const fallbackCode =
members[0].source === 'CUSTOM' ? `CUSTOM-${members[0].id}` : null;
const family = await this.prisma.productFamily.create({
data: {
familyName: parsed.productName ?? parsed.country ?? originGroupKey(members[0].goodName),
familyCode: parsed.skuCode ? await this.ensureUniqueCode(parsed.skuCode) : null,
familyCode: parsed.skuCode
? await this.ensureUniqueCode(parsed.skuCode)
: fallbackCode
? await this.ensureUniqueCode(fallbackCode)
: null,
familyImage: members[0].goodImage ?? null,
primaryOriginGoodId: members[0].id,
},