fix(store): persist package imageUrls on audit approve
Ensure multi-image packages are written on approval and always returned as arrays from store detail. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -34,7 +34,7 @@ export const STORE_PACKAGE_IMAGE_MAX_COUNT = 20;
|
||||
/** 门店环境照最多张数(总部/合伙人上传) */
|
||||
export const STORE_ENV_PHOTO_MAX_COUNT = 20;
|
||||
|
||||
/** 归一化套餐图片:兼容 imageUrl / imageUrls,去重后截断上限 */
|
||||
/** 归一化套餐图片:兼容 imageUrl / imageUrls(含 JSON 字符串),去重后截断上限 */
|
||||
export function normalizeStorePackageImageUrls(input: {
|
||||
imageUrl?: string | null;
|
||||
imageUrls?: unknown;
|
||||
@@ -47,8 +47,25 @@ export function normalizeStorePackageImageUrls(input: {
|
||||
seen.add(url);
|
||||
out.push(url);
|
||||
};
|
||||
if (Array.isArray(input.imageUrls)) {
|
||||
for (const item of input.imageUrls) push(item);
|
||||
|
||||
let list: unknown = input.imageUrls;
|
||||
if (typeof list === 'string') {
|
||||
const trimmed = list.trim();
|
||||
if (trimmed.startsWith('[')) {
|
||||
try {
|
||||
list = JSON.parse(trimmed);
|
||||
} catch {
|
||||
list = trimmed ? [trimmed] : [];
|
||||
}
|
||||
} else if (trimmed) {
|
||||
list = [trimmed];
|
||||
} else {
|
||||
list = [];
|
||||
}
|
||||
}
|
||||
|
||||
if (Array.isArray(list)) {
|
||||
for (const item of list) push(item);
|
||||
}
|
||||
if (out.length === 0) push(input.imageUrl);
|
||||
return out.slice(0, STORE_PACKAGE_IMAGE_MAX_COUNT);
|
||||
|
||||
@@ -69,7 +69,7 @@ export class StorePackageService {
|
||||
usableTime,
|
||||
otherNotes,
|
||||
imageUrl: imageUrls[0] ?? null,
|
||||
imageUrls: imageUrls.length ? imageUrls : null,
|
||||
imageUrls,
|
||||
sortOrder: Number.isFinite(sortOrder) ? sortOrder : index,
|
||||
};
|
||||
}
|
||||
@@ -97,7 +97,7 @@ export class StorePackageService {
|
||||
usableTime: row.usableTime,
|
||||
otherNotes: row.otherNotes,
|
||||
imageUrl: imageUrls[0] ?? null,
|
||||
imageUrls: imageUrls.length ? imageUrls : null,
|
||||
imageUrls,
|
||||
sortOrder: row.sortOrder,
|
||||
};
|
||||
}
|
||||
@@ -229,6 +229,19 @@ export class StorePackageService {
|
||||
return serializeBigInt({ live });
|
||||
}
|
||||
|
||||
private packageImageWrite(pkg: {
|
||||
imageUrl?: string | null;
|
||||
imageUrls?: string[] | null;
|
||||
}) {
|
||||
const imageUrls = normalizeStorePackageImageUrls(pkg);
|
||||
return {
|
||||
imageUrl: imageUrls[0] ?? null,
|
||||
imageUrls: imageUrls.length
|
||||
? (imageUrls as Prisma.InputJsonValue)
|
||||
: Prisma.JsonNull,
|
||||
};
|
||||
}
|
||||
|
||||
private async replaceLivePackages(storeId: bigint, packages: StorePackageItemDto[]) {
|
||||
await this.prisma.$transaction([
|
||||
this.prisma.storePackage.deleteMany({ where: { storeId } }),
|
||||
@@ -241,10 +254,7 @@ export class StorePackageService {
|
||||
dishes: pkg.dishes,
|
||||
usableTime: pkg.usableTime ?? null,
|
||||
otherNotes: pkg.otherNotes ?? null,
|
||||
imageUrl: pkg.imageUrl ?? null,
|
||||
imageUrls: pkg.imageUrls?.length
|
||||
? (pkg.imageUrls as Prisma.InputJsonValue)
|
||||
: Prisma.JsonNull,
|
||||
...this.packageImageWrite(pkg),
|
||||
sortOrder: pkg.sortOrder ?? index,
|
||||
},
|
||||
}),
|
||||
@@ -336,7 +346,7 @@ export class StorePackageService {
|
||||
});
|
||||
return serializeBigInt({ id: updated.id.toString(), status: updated.status });
|
||||
}
|
||||
const packages = req.packagesJson as unknown as StorePackageItemDto[];
|
||||
const packages = this.normalizePackages(req.packagesJson);
|
||||
await this.prisma.$transaction(async (tx) => {
|
||||
await tx.storePackage.deleteMany({ where: { storeId: req.storeId } });
|
||||
for (let i = 0; i < packages.length; i++) {
|
||||
@@ -349,6 +359,7 @@ export class StorePackageService {
|
||||
dishes: pkg.dishes,
|
||||
usableTime: pkg.usableTime ?? null,
|
||||
otherNotes: pkg.otherNotes ?? null,
|
||||
...this.packageImageWrite(pkg),
|
||||
sortOrder: pkg.sortOrder ?? i,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -247,7 +247,8 @@ export class StoreService {
|
||||
usableTime: p.usableTime,
|
||||
otherNotes: p.otherNotes,
|
||||
imageUrl: imageUrls[0] ?? null,
|
||||
imageUrls: imageUrls.length ? imageUrls : null,
|
||||
/** C 端始终返回数组,便于多图轮播 */
|
||||
imageUrls,
|
||||
sortOrder: p.sortOrder,
|
||||
};
|
||||
}),
|
||||
|
||||
Reference in New Issue
Block a user