From ab450c350eeaf99bbc2fe4171380d5571f8bf756 Mon Sep 17 00:00:00 2001 From: yeuimu <2197651308@qq.com> Date: Fri, 28 Aug 2026 12:52:22 +0800 Subject: [PATCH] fix(api): tolerant member attach in auto-group under concurrent cleanup --- .../product-families/product-families.service.ts | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/apps/api/src/product-families/product-families.service.ts b/apps/api/src/product-families/product-families.service.ts index 0beeafa..7408f05 100644 --- a/apps/api/src/product-families/product-families.service.ts +++ b/apps/api/src/product-families/product-families.service.ts @@ -196,9 +196,11 @@ export class ProductFamiliesService { primaryOriginGoodId: members[0].id, }, }); + // 宽容挂载:并行环境下成员可能在候选查询后消失(如测试清理),跳过即可 await this.attachMembers( family.id, members.map((m) => m.id), + { strict: false }, ); await this.recompute.recomputeFamily(family.id); applied += 1; @@ -431,7 +433,11 @@ export class ProductFamiliesService { 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; const existings = await this.prisma.originGood.findMany({ where: { id: { in: originGoodIds } }, @@ -439,14 +445,16 @@ export class ProductFamiliesService { }); const existingIds = new Set(existings.map((e) => e.id.toString())); const missing = originGoodIds.filter((v) => !existingIds.has(v.toString())); - if (missing.length) { + if (missing.length && opts.strict !== false) { throw new BadRequestException({ message: 'origin goods not found', ids: missing.map(String), }); } + const attachable = originGoodIds.filter((v) => existingIds.has(v.toString())); + if (!attachable.length) return; await this.prisma.originGood.updateMany({ - where: { id: { in: originGoodIds } }, + where: { id: { in: attachable } }, data: { familyId }, }); }