feat(public): resolve goods by secondary sds id and merge variants
This commit is contained in:
@@ -34,6 +34,16 @@ const PUBLIC_GOOD_INCLUDE = {
|
||||
variants: { orderBy: [{ sortOrder: 'asc' as const }, { id: 'asc' as const }] },
|
||||
},
|
||||
},
|
||||
mergedOriginGoods: {
|
||||
orderBy: { createdAt: 'asc' as const },
|
||||
include: {
|
||||
originGood: {
|
||||
include: {
|
||||
variants: { orderBy: [{ sortOrder: 'asc' as const }, { id: 'asc' as const }] },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
goodTags: { include: { tag: { include: { tagGroup: true } } } },
|
||||
} satisfies Prisma.GoodInclude;
|
||||
|
||||
@@ -198,7 +208,17 @@ export class PublicService {
|
||||
|
||||
async getGood(goodId: string): Promise<PublicGoodDetailDto> {
|
||||
const good = await this.prisma.good.findFirst({
|
||||
where: { originGood: { sdsGoodId: goodId, delisted: false } },
|
||||
where: {
|
||||
OR: [
|
||||
{ originGood: { sdsGoodId: goodId, delisted: false } },
|
||||
// Merged secondary sources also resolve to the same good.
|
||||
{
|
||||
mergedOriginGoods: {
|
||||
some: { originGood: { sdsGoodId: goodId, delisted: false } },
|
||||
},
|
||||
},
|
||||
],
|
||||
},
|
||||
include: PUBLIC_GOOD_INCLUDE,
|
||||
orderBy: [{ goodPriority: 'desc' }, { id: 'asc' }],
|
||||
});
|
||||
@@ -277,7 +297,7 @@ export class PublicService {
|
||||
* design-layer素材图 and the product-level blank garment photo are excluded
|
||||
* because they are not per-color gallery photos. */
|
||||
private groupImagesByColor(
|
||||
variants: PublicGoodRow['originGood']['variants'],
|
||||
variants: Array<PublicGoodRow['originGood']['variants'][number]>,
|
||||
): Array<{ colorId: string | null; colorName: string | null; colorHex: string | null; images: string[] }> {
|
||||
const groups = new Map<string, {
|
||||
colorId: string | null;
|
||||
@@ -335,6 +355,11 @@ export class PublicService {
|
||||
private toPublicGoodDetail(good: PublicGoodRow): PublicGoodDetailDto {
|
||||
const base = this.toPublicGood(good);
|
||||
const detail = good.originGood.detail;
|
||||
// Merge primary and secondary origin good variants (dedup identical URLs).
|
||||
const allVariants = [
|
||||
...good.originGood.variants,
|
||||
...good.mergedOriginGoods.flatMap((m) => m.originGood.variants),
|
||||
];
|
||||
return {
|
||||
...base,
|
||||
productCode: detail?.productCode ?? null,
|
||||
@@ -354,11 +379,11 @@ export class PublicService {
|
||||
pictureRequest: detail?.pictureRequest ?? null,
|
||||
},
|
||||
media: (detail?.media as Record<string, unknown> | null) ?? null,
|
||||
mediaByColor: this.groupImagesByColor(good.originGood.variants),
|
||||
mediaByColor: this.groupImagesByColor(allVariants),
|
||||
options: (detail?.options as Record<string, unknown> | null) ?? null,
|
||||
sizeChart: (detail?.sizeChart as Record<string, unknown> | null) ?? null,
|
||||
packageSpecs: (detail?.packageSpecs as Record<string, unknown> | null) ?? null,
|
||||
variants: good.originGood.variants.map((variant) => ({
|
||||
variants: allVariants.map((variant) => ({
|
||||
id: variant.sdsVariantId,
|
||||
sku: variant.sku,
|
||||
sizeId: variant.sizeId,
|
||||
|
||||
Reference in New Issue
Block a user