真机现场(本地环境,主系统 supplier_integration_webhook_deliveries):
event=preparations.allocated supplier=SUP37831443 order_code=ORD_WEB_20261017_BR002_0001
status=failed http_status=500 attempt=3
error={"failed":1,"results":[{"status":"failed","message":"台账指向的本系统订单已不存在"}]}
⇒ 不是主系统没推、也不是绑定/验签问题:推送到了、我方回 500、对方重试 3 次后放弃。
根因:日级占坑(main_system_daily_order_imports)与整单台账(main_system_order_imports)
还指着**已被清空/删掉的**本系统订单(本次是本地清库导致)⇒ 导入走"行级追加",
一查订单没了就 failed(500),且旧实现没有自愈 ⇒ 这单永远进不来。
修复(importDayBucket,webhook 与定时拉取共用):
· claimDayOrder 之后判断"占坑指向的订单是否还活着"(claimedOrderStillExists;查询异常按活着处理);
· 不活着 ⇒ resetStaleDayOrderPointers():
① 删该(分公司×主系统分公司×配送日)的日级占坑;
② 整单台账里指向不存在订单的 our_order_code 置 NULL(注意该表没有 updated_at 列);
③ 行级台账里指向不存在订单行的 our_order_item_id/our_order_code 清掉
(否则 needsAppendToOrder 会以为"已经导过"而不追加);
· 随后本轮重新占坑并建单;清理失败只记日志、不阻断(下一轮再试)。
验证(本地真环境):
· 修复前:导入失败 500,错误即上面那条;修复后同一次导入 created=1,
生成 ORD_API_20261017_BR002_0001(状态 20,两行苹果30/瘦肉15 都带 main_order_code),
台账与占坑指针同步更新;再导一次 skipped=1(幂等)。
· 新增单测 mainSystemStaleClaimHeal(7 条)钉住三条指针清理、含"不写 updated_at"这条踩过的坑;
mainSystem 子集 30 文件 / 471 用例全过。
· 设计门禁:8 通过 / 1 已知红 check-ai-cost / verify-facts 因本地缺 tenant_br003 红(均基线)。
79 lines
4.1 KiB
TypeScript
79 lines
4.1 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
||
import path from 'node:path';
|
||
import { describe, expect, it } from 'vitest';
|
||
|
||
/**
|
||
* 「主系统派了单、我方永远收不到」——**陈旧占坑/台账指针自愈**(2026-10-17 真机事故)。
|
||
*
|
||
* 现场(本地环境,主系统投递记录 `supplier_integration_webhook_deliveries`):
|
||
* event=preparations.allocated / status=failed / http_status=500 / attempt=3
|
||
* error={"ok":false,"created":0,"failed":1,"results":[{"mainOrderCode":"ORD_WEB_20261017_BR002_0001",
|
||
* "ourOrderCode":null,"status":"failed","message":"台账指向的本系统订单已不存在"}]}
|
||
*
|
||
* 根因:`public.main_system_daily_order_imports`(日级占坑)与
|
||
* `public.main_system_order_imports`(整单台账)里还指着**已经被清空/删掉的**本系统订单。
|
||
* 导入于是走"按行级追加",一查订单没了就返回 failed(500)⇒ 主系统重试 2~3 次后放弃 ⇒ 这张单永远进不来。
|
||
*
|
||
* 修复:占坑指向的订单不存在时,先清陈旧指针(占坑删掉、整单台账置 NULL、行级台账清对照),
|
||
* 本轮重新占坑并建单。两条通道(webhook / 轮询拉取)共用 `importDayBucket`,因此一处修好两条都通。
|
||
*/
|
||
const service = readFileSync(
|
||
path.join(__dirname, '../src/services/mainSystemOrderImport.ts'),
|
||
'utf8'
|
||
);
|
||
|
||
describe('占坑自愈:三条指针都要清', () => {
|
||
const idx = service.indexOf('async function resetStaleDayOrderPointers');
|
||
const block = idx >= 0 ? service.slice(idx, idx + 2200) : '';
|
||
|
||
it('函数存在,且删除陈旧的日级占坑', () => {
|
||
expect(idx).toBeGreaterThan(-1);
|
||
expect(block).toMatch(/DELETE FROM public\.main_system_daily_order_imports/);
|
||
});
|
||
|
||
it('整单台账:把指向"已不存在订单"的 our_order_code 置 NULL(且**不写** updated_at —— 该表没这列)', () => {
|
||
expect(block).toMatch(/UPDATE public\.main_system_order_imports msi/);
|
||
expect(block).toMatch(/NOT EXISTS \(SELECT 1 FROM orders o WHERE o\.order_code = msi\.our_order_code\)/);
|
||
expect(block).not.toMatch(/main_system_order_imports[\s\S]{0,200}updated_at/);
|
||
});
|
||
|
||
it('行级台账:指向"已不存在订单行"的 our_order_item_id 也要清(否则 needsAppendToOrder 以为导过了)', () => {
|
||
expect(block).toMatch(/UPDATE public\.main_system_order_import_items it/);
|
||
expect(block).toMatch(/our_order_item_id = NULL, our_order_code = NULL/);
|
||
expect(block).toMatch(/NOT EXISTS \(SELECT 1 FROM order_items oi WHERE oi\.id = it\.our_order_item_id\)/);
|
||
});
|
||
|
||
it('清理失败只记日志,不阻断本轮导入', () => {
|
||
expect(block).toMatch(/catch/);
|
||
expect(block).toMatch(/陈旧台账指针清理失败/);
|
||
});
|
||
});
|
||
|
||
describe('自愈时机与顺序', () => {
|
||
it('在 claim 之后、追加/建单之前判断"占坑指向的订单是否还活着"', () => {
|
||
const claimIdx = service.indexOf('const claim = await claimDayOrder(');
|
||
const checkIdx = service.indexOf('await claimedOrderStillExists(branchCode, effectiveClaim.ourOrderCode)');
|
||
const createIdx = service.indexOf('const outcome = await createDayOrder(', claimIdx);
|
||
expect(claimIdx).toBeGreaterThan(-1);
|
||
expect(checkIdx).toBeGreaterThan(claimIdx);
|
||
// 自愈后的重新占坑在建单之前
|
||
expect(service.indexOf('effectiveClaim = await claimDayOrder(')).toBeLessThan(createIdx);
|
||
});
|
||
|
||
it('查订单是否活着失败时按"活着"处理(数据库抖动不许清台账)', () => {
|
||
const idx = service.indexOf('async function claimedOrderStillExists');
|
||
expect(idx).toBeGreaterThan(-1);
|
||
const block = service.slice(idx, idx + 900);
|
||
expect(block).toMatch(/catch \{[\s\S]*return true;/);
|
||
});
|
||
|
||
it('两条通道共用 importDayBucket(webhook 与定时拉取都走自愈)', () => {
|
||
// webhook 通道:importMainSystemGroups → importDayBucket
|
||
const groupsIdx = service.indexOf('async function importMainSystemGroups');
|
||
expect(groupsIdx).toBeGreaterThan(-1);
|
||
expect(service.slice(groupsIdx, groupsIdx + 6000)).toMatch(/importDayBucket\(/);
|
||
// 拉取通道:importDayBucket 自身
|
||
expect(service).toMatch(/async function importDayBucket\(/);
|
||
});
|
||
});
|