76375eff83
Allow HQ to replace/delete store photos; support multi-image packages with a 20-image cap. Co-authored-by: Cursor <cursoragent@cursor.com>
72 lines
2.1 KiB
TypeScript
72 lines
2.1 KiB
TypeScript
import type { StorePackageItemDto } from '@dukang/shared-types';
|
|
import {
|
|
STORE_PACKAGE_IMAGE_MAX_COUNT,
|
|
STORE_PACKAGE_MAX_COUNT,
|
|
normalizeStorePackageImageUrls,
|
|
} from '@dukang/shared-types';
|
|
|
|
export type PackageFormItem = StorePackageItemDto;
|
|
|
|
export function emptyPackage(index = 0): PackageFormItem {
|
|
return {
|
|
name: '',
|
|
price: '',
|
|
dishes: '',
|
|
usableTime: '',
|
|
otherNotes: '',
|
|
imageUrl: '',
|
|
imageUrls: [],
|
|
sortOrder: index,
|
|
};
|
|
}
|
|
|
|
export function normalizePackageFormItems(raw: PackageFormItem[]): PackageFormItem[] {
|
|
return raw
|
|
.map((item, index) => {
|
|
const imageUrls = normalizeStorePackageImageUrls(item);
|
|
return {
|
|
name: item.name.trim(),
|
|
price: item.price.trim(),
|
|
dishes: item.dishes.trim(),
|
|
usableTime: item.usableTime?.trim() || '',
|
|
otherNotes: item.otherNotes?.trim() || '',
|
|
imageUrl: imageUrls[0] ?? '',
|
|
imageUrls,
|
|
sortOrder: index,
|
|
};
|
|
})
|
|
.filter(
|
|
(item) =>
|
|
item.name ||
|
|
item.price ||
|
|
item.dishes ||
|
|
item.usableTime ||
|
|
item.otherNotes ||
|
|
item.imageUrls.length > 0,
|
|
);
|
|
}
|
|
|
|
export function validatePackageFormItems(items: PackageFormItem[]): string | null {
|
|
const filled = normalizePackageFormItems(items);
|
|
if (filled.length > STORE_PACKAGE_MAX_COUNT) {
|
|
return `套餐最多 ${STORE_PACKAGE_MAX_COUNT} 条`;
|
|
}
|
|
for (let i = 0; i < filled.length; i++) {
|
|
const item = filled[i];
|
|
if (!item.name) return `第 ${i + 1} 条套餐名称不能为空`;
|
|
const price = Number(item.price);
|
|
if (!Number.isFinite(price) || price < 0) return `第 ${i + 1} 条套餐价格须为非负数字`;
|
|
if (!item.dishes) return `第 ${i + 1} 条套餐菜品不能为空`;
|
|
if ((item.imageUrls?.length ?? 0) > STORE_PACKAGE_IMAGE_MAX_COUNT) {
|
|
return `第 ${i + 1} 条套餐图片最多 ${STORE_PACKAGE_IMAGE_MAX_COUNT} 张`;
|
|
}
|
|
}
|
|
return null;
|
|
}
|
|
|
|
export function formatPackagePrice(price: string | number) {
|
|
const n = typeof price === 'number' ? price : Number(price);
|
|
if (!Number.isFinite(n)) return String(price);
|
|
return n % 1 === 0 ? String(n) : n.toFixed(2);
|
|
}
|