来源仓库:jxc/ai-fresh-delivery 来源提交:f0b1a180e2deb7e4673b9d1c8542619f7bc0f73d 快照树: 09f679334ff38f0ff25e907961c94a8223bcaedd(与来源 HEAD 工作树逐字节一致,3801 个文件) 按约定本仓库不带任何历史提交:来源仓库的历史中含真实凭据 (生产 SSH 私钥 deploy/id_rsa_tencent、server/.env、.env.tencent/.env.production/.env.gitea、 deploy/小程序AppSecret.txt,其中 MP_APPSECRET / S3 AccessKey 经哈希比对与线上同值), 整包推送会把这些凭据永久带入本仓库,故只取「当前版本」的单提交快照。 供应商系统后续开发自本提交开始。
102 lines
3.9 KiB
TypeScript
102 lines
3.9 KiB
TypeScript
import type { PoolClient } from 'pg';
|
||
import { pool } from '../db';
|
||
import type { QuerySpec } from './queryExecutor';
|
||
|
||
const CHINA_TZ = 'Asia/Shanghai';
|
||
|
||
export function formatChinaDate(date = new Date()): string {
|
||
return new Intl.DateTimeFormat('en-CA', { timeZone: CHINA_TZ }).format(date);
|
||
}
|
||
|
||
export function normalizeDateOnly(value: unknown): string | null {
|
||
if (value === null || value === undefined || value === '') return null;
|
||
if (value instanceof Date) {
|
||
if (Number.isNaN(value.getTime())) return null;
|
||
return formatChinaDate(value);
|
||
}
|
||
const trimmed = String(value).trim();
|
||
// 纯日期:按日历日原样使用
|
||
if (/^\d{4}-\d{2}-\d{2}$/.test(trimmed)) return trimmed;
|
||
// ISO/时间戳:按中国时区日历日,禁止截 UTC 前缀(否则会少一天)
|
||
const date = new Date(trimmed);
|
||
if (Number.isNaN(date.getTime())) return null;
|
||
return formatChinaDate(date);
|
||
}
|
||
|
||
export function getOrderDateFromCreatedAt(createdAt?: Date | string | null): string {
|
||
if (!createdAt) return formatChinaDate();
|
||
const date = createdAt instanceof Date ? createdAt : new Date(createdAt);
|
||
if (Number.isNaN(date.getTime())) return formatChinaDate();
|
||
return formatChinaDate(date);
|
||
}
|
||
|
||
export function assertDeliveryDateOnOrAfterOrderDate(deliveryDate: string, orderDate: string): void {
|
||
if (deliveryDate.localeCompare(orderDate) < 0) {
|
||
throw new Error(`配送日期不能早于下单日期(${orderDate})`);
|
||
}
|
||
}
|
||
|
||
async function resolveOrderDateForUpdate(
|
||
spec: QuerySpec,
|
||
client?: PoolClient
|
||
): Promise<string | null> {
|
||
const orderCodeFilter = (spec.filters || []).find(
|
||
(filter) => filter.column === 'order_code' && filter.op === 'eq' && filter.value
|
||
);
|
||
if (!orderCodeFilter?.value) return null;
|
||
|
||
// 须与写操作同一连接/search_path(tenant_*);勿在无租户上下文时查 public.orders
|
||
const executor = client ?? pool;
|
||
const result = await executor.query(`SELECT created_at FROM orders WHERE order_code = $1 LIMIT 1`, [
|
||
orderCodeFilter.value,
|
||
]);
|
||
if (!result.rows[0]?.created_at) return null;
|
||
return getOrderDateFromCreatedAt(result.rows[0].created_at);
|
||
}
|
||
|
||
export function getDefaultDeliveryDate(orderDate?: string | null): string {
|
||
const base = normalizeDateOnly(orderDate) || formatChinaDate();
|
||
const anchor = new Date(`${base}T12:00:00+08:00`);
|
||
anchor.setUTCDate(anchor.getUTCDate() + 1);
|
||
return formatChinaDate(anchor);
|
||
}
|
||
|
||
export async function validateOrdersWrite(spec: QuerySpec, client?: PoolClient): Promise<void> {
|
||
if (spec.table !== 'orders') return;
|
||
|
||
const forbidStatus = (record: Record<string, unknown> | null | undefined) => {
|
||
if (!record || typeof record !== 'object') return;
|
||
if (Object.prototype.hasOwnProperty.call(record, 'order_status')) {
|
||
throw Object.assign(new Error('禁止通过通用数据接口修改 order_status,请走订单工作流'), {
|
||
status: 403,
|
||
code: 'forbidden_order_status',
|
||
});
|
||
}
|
||
};
|
||
|
||
if (spec.action === 'insert') {
|
||
const rows = Array.isArray(spec.insertData) ? spec.insertData : [spec.insertData];
|
||
const orderDate = formatChinaDate();
|
||
for (const row of rows) {
|
||
if (!row || typeof row !== 'object') continue;
|
||
const record = row as Record<string, unknown>;
|
||
forbidStatus(record);
|
||
let deliveryDate = normalizeDateOnly(record.delivery_date);
|
||
if (!deliveryDate) {
|
||
deliveryDate = getDefaultDeliveryDate(orderDate);
|
||
record.delivery_date = deliveryDate;
|
||
}
|
||
assertDeliveryDateOnOrAfterOrderDate(deliveryDate, orderDate);
|
||
}
|
||
return;
|
||
}
|
||
|
||
if (spec.action === 'update' && spec.updateData && typeof spec.updateData === 'object') {
|
||
forbidStatus(spec.updateData as Record<string, unknown>);
|
||
const deliveryDate = normalizeDateOnly((spec.updateData as Record<string, unknown>).delivery_date);
|
||
if (!deliveryDate) return;
|
||
const orderDate = (await resolveOrderDateForUpdate(spec, client)) || formatChinaDate();
|
||
assertDeliveryDateOnOrAfterOrderDate(deliveryDate, orderDate);
|
||
}
|
||
}
|