fix(product-family): tolerant auto-group member attach
This commit is contained in:
@@ -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 },
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user