fix(主系统计划): 对齐主系统 order_item_id 与 request_id 契约
主系统接口要求主系统订单行 UUID;从导入台账 payload.mainOrderItemId 读取并写入 outbox;request_id 归一化为主系统允许字符集。
This commit is contained in:
@@ -0,0 +1,11 @@
|
||||
-- 2026-11-02:主系统备货计划变更 outbox 增加主系统订单行 id。
|
||||
-- 主系统接口要求 order_item_id = 主系统订单明细 UUID,不能传供应商本地 order_items.id。
|
||||
ALTER TABLE public.main_system_plan_changes
|
||||
ADD COLUMN IF NOT EXISTS main_order_item_id UUID;
|
||||
|
||||
COMMENT ON COLUMN public.main_system_plan_changes.main_order_item_id IS
|
||||
'主系统订单明细 id(从 main_system_order_import_items.payload->>mainOrderItemId 取得);不是供应商本地 id。';
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_main_system_plan_changes_main_item
|
||||
ON public.main_system_plan_changes (main_order_item_id)
|
||||
WHERE main_order_item_id IS NOT NULL;
|
||||
@@ -37,6 +37,14 @@ function roundQty(value: unknown): number {
|
||||
return Math.round((Number(value) || 0) * 1000) / 1000;
|
||||
}
|
||||
|
||||
/** 主系统 client_request_id 只接受字母/数字/下划线/短横线,最长 64;本地 id 先归一化。 */
|
||||
function normalizeRequestId(raw: unknown): string {
|
||||
const cleaned = String(raw || '')
|
||||
.replace(/[^0-9A-Za-z_-]/g, '_')
|
||||
.slice(0, 64);
|
||||
return cleaned || randomUUID();
|
||||
}
|
||||
|
||||
function flagOn(): boolean {
|
||||
return mainSystemFeatureFlags().planChange === true;
|
||||
}
|
||||
@@ -52,7 +60,7 @@ async function loadChange(id: string) {
|
||||
function payloadOf(row: Record<string, any>) {
|
||||
return {
|
||||
prep_id: String(row.prep_id || ''),
|
||||
order_item_id: String(row.our_order_item_id || '') || null,
|
||||
order_item_id: String(row.main_order_item_id || '') || null,
|
||||
before_qty: Number(row.before_qty) || 0,
|
||||
before_unit: String(row.before_unit || ''),
|
||||
after_qty: Number(row.after_qty) || 0,
|
||||
@@ -202,7 +210,7 @@ export async function changeOrderItemPlan(input: {
|
||||
const afterQty = roundQty(input.afterQty);
|
||||
const actualReceivedQty = roundQty(input.actualReceivedQty);
|
||||
const afterUnit = String(input.afterUnit || '').trim();
|
||||
const requestId = String(input.requestId || '').trim() || randomUUID();
|
||||
const requestId = normalizeRequestId(input.requestId);
|
||||
if (!orderCode || !itemId) throw bizError('缺少订单或明细', 400);
|
||||
if (!(afterQty > 0) || !(actualReceivedQty > 0)) throw bizError('计划数量和实收数量必须大于 0', 400);
|
||||
if (!afterUnit) throw bizError('计划单位不能为空', 400);
|
||||
@@ -262,7 +270,8 @@ export async function changeOrderItemPlan(input: {
|
||||
}
|
||||
|
||||
const mapping = await client.query(
|
||||
`SELECT prep_id, main_order_code
|
||||
`SELECT prep_id, main_order_code,
|
||||
NULLIF(TRIM(payload->>'mainOrderItemId'), '') AS main_order_item_id
|
||||
FROM public.main_system_order_import_items
|
||||
WHERE our_order_item_id = $1::uuid AND branch_code = $2
|
||||
LIMIT 1`,
|
||||
@@ -273,6 +282,14 @@ export async function changeOrderItemPlan(input: {
|
||||
}
|
||||
const prepId = String(mapping.rows[0].prep_id || '').trim();
|
||||
if (!prepId) throw bizError('缺少主系统备货行 id', 400, 'missing_prep_id');
|
||||
const mainOrderItemId = String(mapping.rows[0].main_order_item_id || '').trim();
|
||||
if (!/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(mainOrderItemId)) {
|
||||
throw bizError(
|
||||
'缺少主系统订单行 id,无法同步主系统计划;请先重新拉取该主系统备货行',
|
||||
409,
|
||||
'missing_main_order_item_id'
|
||||
);
|
||||
}
|
||||
|
||||
const skuUnit = await client.query(
|
||||
`SELECT unit FROM skus WHERE sku_code = $1 AND branch_code = $2 LIMIT 1`,
|
||||
@@ -297,16 +314,17 @@ export async function changeOrderItemPlan(input: {
|
||||
|
||||
const inserted = await client.query(
|
||||
`INSERT INTO public.main_system_plan_changes (
|
||||
branch_code, our_order_code, our_order_item_id, prep_id, main_order_code,
|
||||
branch_code, our_order_code, our_order_item_id, main_order_item_id, prep_id, main_order_code,
|
||||
before_qty, before_unit, after_qty, after_unit, actual_received_qty,
|
||||
original_customer_qty, original_customer_unit, reason, source, status,
|
||||
request_id, created_by
|
||||
) VALUES ($1,$2,$3,$4::varchar,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,'pending',$15,$16)
|
||||
) VALUES ($1,$2,$3,$4::uuid,$5::varchar,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,'pending',$16,$17)
|
||||
RETURNING id`,
|
||||
[
|
||||
String(item.branch_code || ''),
|
||||
orderCode,
|
||||
itemId,
|
||||
mainOrderItemId,
|
||||
prepId,
|
||||
mapping.rows[0].main_order_code || null,
|
||||
beforeQty,
|
||||
@@ -401,7 +419,7 @@ export async function listPlanChanges(input: {
|
||||
}) {
|
||||
const limit = Math.max(1, Math.min(Number(input.limit) || 100, 200));
|
||||
const result = await pool.query(
|
||||
`SELECT id, branch_code, our_order_code, our_order_item_id, prep_id, main_order_code,
|
||||
`SELECT id, branch_code, our_order_code, our_order_item_id, main_order_item_id, prep_id, main_order_code,
|
||||
before_qty, before_unit, after_qty, after_unit, actual_received_qty,
|
||||
original_customer_qty, original_customer_unit, status, attempts,
|
||||
last_error, request_id, created_at, updated_at, synced_at
|
||||
|
||||
@@ -1826,7 +1826,7 @@ export async function confirmPickingComplete(input: {
|
||||
afterUnit: inventoryUnit,
|
||||
actualReceivedQty: Number(row.picked_weight),
|
||||
reason: '分拣称重后按实际重量',
|
||||
requestId: `pick:${pickCode}:${row.id}`,
|
||||
requestId: `pick_${pickCode}_${row.id}`,
|
||||
operatorId: input.reviewerId || null,
|
||||
});
|
||||
} catch (error) {
|
||||
|
||||
@@ -7,6 +7,7 @@ const read = (rel: string) => readFileSync(new URL(`../${rel}`, import.meta.url)
|
||||
const svc = read('src/services/mainSystemPlanChangeService.ts');
|
||||
const api = read('src/services/mainSystem/api/planChanges.ts');
|
||||
const migration = read('../migrations/20261103_main_system_plan_change.sql');
|
||||
const mainItemMigration = read('../migrations/20261104_main_system_plan_change_main_item.sql');
|
||||
const orderRoutes = read('src/modules/workflow/routes/orderRoutes.ts');
|
||||
const outbound = read('src/services/inventoryOutboundService.ts');
|
||||
const picking = read('src/services/pickingService.ts');
|
||||
@@ -27,6 +28,7 @@ describe('主系统备货计划变更(供应商侧)', () => {
|
||||
|
||||
it('先写 outbox,主系统接口关闭时只 pending', () => {
|
||||
expect(migration).toContain('main_system_plan_changes');
|
||||
expect(mainItemMigration).toContain('main_order_item_id');
|
||||
expect(migration).toContain('status IN');
|
||||
expect(svc).toContain('planChange');
|
||||
expect(svc).toContain('主系统同步未启用');
|
||||
@@ -35,6 +37,9 @@ describe('主系统备货计划变更(供应商侧)', () => {
|
||||
it('有主系统备货行映射才允许同步', () => {
|
||||
expect(svc).toContain('main_system_order_import_items');
|
||||
expect(svc).toContain('not_main_system_item');
|
||||
expect(svc).toContain('mainOrderItemId');
|
||||
expect(svc).toContain('main_order_item_id');
|
||||
expect(svc).toContain("payload->>'mainOrderItemId'");
|
||||
});
|
||||
|
||||
it('库存出库和分拣称重按 SKU 库存单位判断', () => {
|
||||
|
||||
Reference in New Issue
Block a user