fix(api): tolerant member attach in auto-group under concurrent cleanup

This commit is contained in:
yeuimu
2026-08-28 12:52:22 +08:00
parent 502b7eba6e
commit ab450c350e
@@ -196,9 +196,11 @@ export class ProductFamiliesService {
primaryOriginGoodId: members[0].id, primaryOriginGoodId: members[0].id,
}, },
}); });
// 宽容挂载:并行环境下成员可能在候选查询后消失(如测试清理),跳过即可
await this.attachMembers( await this.attachMembers(
family.id, family.id,
members.map((m) => m.id), members.map((m) => m.id),
{ strict: false },
); );
await this.recompute.recomputeFamily(family.id); await this.recompute.recomputeFamily(family.id);
applied += 1; applied += 1;
@@ -431,7 +433,11 @@ export class ProductFamiliesService {
return this.detail(id); return this.detail(id);
} }
private async attachMembers(familyId: bigint, originGoodIds: bigint[]) { private async attachMembers(
familyId: bigint,
originGoodIds: bigint[],
opts: { strict?: boolean } = {},
) {
if (!originGoodIds.length) return; if (!originGoodIds.length) return;
const existings = await this.prisma.originGood.findMany({ const existings = await this.prisma.originGood.findMany({
where: { id: { in: originGoodIds } }, where: { id: { in: originGoodIds } },
@@ -439,14 +445,16 @@ export class ProductFamiliesService {
}); });
const existingIds = new Set(existings.map((e) => e.id.toString())); const existingIds = new Set(existings.map((e) => e.id.toString()));
const missing = originGoodIds.filter((v) => !existingIds.has(v.toString())); const missing = originGoodIds.filter((v) => !existingIds.has(v.toString()));
if (missing.length) { if (missing.length && opts.strict !== false) {
throw new BadRequestException({ throw new BadRequestException({
message: 'origin goods not found', message: 'origin goods not found',
ids: missing.map(String), ids: missing.map(String),
}); });
} }
const attachable = originGoodIds.filter((v) => existingIds.has(v.toString()));
if (!attachable.length) return;
await this.prisma.originGood.updateMany({ await this.prisma.originGood.updateMany({
where: { id: { in: originGoodIds } }, where: { id: { in: attachable } },
data: { familyId }, data: { familyId },
}); });
} }