4372018c09
Refund rollback, winery T+3, multi withdraw, mini-user store detail, dev plan batch edit and WeCom dispatch, support ticket edit/attachments/batch status, package imageUrl. Co-authored-by: Cursor <cursoragent@cursor.com>
55 lines
1.7 KiB
TypeScript
55 lines
1.7 KiB
TypeScript
import type { StorePackageItemDto } from '@dukang/shared-types';
|
|
import { STORE_PACKAGE_MAX_COUNT } from '@dukang/shared-types';
|
|
|
|
export type PackageFormItem = StorePackageItemDto;
|
|
|
|
export function emptyPackage(index = 0): PackageFormItem {
|
|
return {
|
|
name: '',
|
|
price: '',
|
|
dishes: '',
|
|
usableTime: '',
|
|
otherNotes: '',
|
|
imageUrl: '',
|
|
sortOrder: index,
|
|
};
|
|
}
|
|
|
|
export function normalizePackageFormItems(raw: PackageFormItem[]): PackageFormItem[] {
|
|
return raw
|
|
.map((item, index) => ({
|
|
name: item.name.trim(),
|
|
price: item.price.trim(),
|
|
dishes: item.dishes.trim(),
|
|
usableTime: item.usableTime?.trim() || '',
|
|
otherNotes: item.otherNotes?.trim() || '',
|
|
imageUrl: item.imageUrl?.trim() || '',
|
|
sortOrder: index,
|
|
}))
|
|
.filter(
|
|
(item) =>
|
|
item.name || item.price || item.dishes || item.usableTime || item.otherNotes || item.imageUrl,
|
|
);
|
|
}
|
|
|
|
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} 条套餐菜品不能为空`;
|
|
}
|
|
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);
|
|
}
|