fix(public): media images are objects ({id,url,sortOrder}) - handle both shapes in mergeMedia

This commit is contained in:
yeuimu
2026-08-28 11:22:53 +08:00
parent d826c5bdd4
commit 270e516f94
2 changed files with 29 additions and 16 deletions
+8 -4
View File
@@ -435,7 +435,7 @@ describe('PublicService', () => {
sizeChart: { rows: [{ sizeName: 'S', measurements: [{ key: 'chest', cm: '94' }] }, { sizeName: 'M', measurements: [{ key: 'chest', cm: '100' }] }] },
packageSpecs: { rows: [{ sizeName: 'S' }] },
options: { sizes: [{ name: 'S' }, { name: 'M' }] },
media: { images: ['http://img/pri-a', 'http://img/pri-b'], primaryImageUrl: 'http://img/pri-a' },
media: { images: [{ id: 'i1', url: 'http://img/pri-a', sortOrder: 0 }, { id: 'i2', url: 'http://img/pri-b', sortOrder: 1 }], primaryImageUrl: 'http://img/pri-a' },
},
});
// Secondary: duplicate Black|S with a DIFFERENT price (must be dropped,
@@ -452,7 +452,7 @@ describe('PublicService', () => {
sizeChart: { rows: [{ sizeName: 'XXXL', measurements: [{ key: 'chest', cm: '120' }] }] },
packageSpecs: { rows: [{ sizeName: 'M' }, { sizeName: 'XXXL' }] },
options: { sizes: [{ name: 'XXXL' }] },
media: { images: ['http://img/pri-a', 'http://img/sec-x'], primaryImageUrl: 'http://img/pri-a' },
media: { images: [{ id: 'i1', url: 'http://img/pri-a', sortOrder: 0 }, { id: 'i9', url: 'http://img/sec-x', sortOrder: 0 }], primaryImageUrl: 'http://img/pri-a' },
},
});
const mergedGood = await prisma.good.create({
@@ -486,9 +486,13 @@ describe('PublicService', () => {
const optSizes = (detail.options as any).sizes.map((s: any) => s.name).sort();
expect(optSizes).toEqual(['M', 'S', 'XXXL']);
// Media gallery: primary images first, secondary-only URL appended,
// duplicate URL (pri-a) kept once.
// duplicate URL (pri-a) kept once. Entries keep their object shape.
const media = detail.media as any;
expect(media.images).toEqual(['http://img/pri-a', 'http://img/pri-b', 'http://img/sec-x']);
expect(media.images.map((i: any) => i.url)).toEqual([
'http://img/pri-a',
'http://img/pri-b',
'http://img/sec-x',
]);
expect(media.primaryImageUrl).toBe('http://img/pri-a');
} finally {
await prisma.goodOriginGood.deleteMany({ where: { goodId: mergedGood.id } });