fix(对接): 绑定成功即刻拉一次 —— 消掉"刚绑完要等 15 分钟才看到货"的窗口
开发者报障:「为什么 18381421533 这个水产供应商分公司收不到主系统分配过来的订单?」
排查结论(真库 + 他们的投递日志 + 我方日志):
· 我方绑定是有的:BR003 水产供应商 ← (BR002, SUP41472132) ← 接入编码 SCSTSHD6XVX4(main_push 已到);
· 他们那边授权行也绑好了(subsystem_code=SCSTSHD6XVX4),且**确实分了 6 行货**给 SUP41472132
(ORD_WEB_20261010_BR002_0001/0002,17:53:38/48,prepare_status=10);
· 但**他们没有推 `preparations.allocated`** —— 他们的 `supplier_integration_webhook_deliveries`
里只有两条 `binding.changed`(17:49/17:50),没有任何 assigned 投递记录;
· 我方兜底对账挂在**调度 tick** 上:`setInterval(..., 15*60*1000)` + 内部限流 10 分钟
⇒ **实际周期是 15 分钟**(设计文档此前写的"≤10 分钟"不准,已更正)。用户 17:58 来问时还没轮到。
本提交的修复:`binding.changed` 应用成功后,**立即**对该分公司 fire-and-forget 拉取一次
(不阻塞 webhook 响应、失败只记日志,兜底仍是定时对账)⇒ 窗口从"最多 15 分钟"变成"秒级"。
实测有效:手动触发导入后 BR003 落单 `ORD_API_20261010_BR003_0003`(两张主系统单按配送日合并成一张,
¥534.80,配送日 2026-10-11),拉取→建单→落库全链路正常。
顺带记录(未改,供裁决):
1. 租户表是 FORCE RLS:直连 psql 查数据必须 `set_config('app.branch_code', …)` 且 search_path
含租户 schema —— 我第一轮就是漏了这一步,把"有数据"误判成"没落库"(已复核纠正);
2. `cleanup_orphan_fulfillments()` 用**非限定表名** `order_item_fulfillments`:
在 search_path 不含租户 schema 的连接上删单会报 `relation … does not exist`(既有缺陷)。
验证:服务端 2334 用例 / 失败 30、失败文件 24 = 与基线逐字相同;branchBindingSync 17 例全绿
(新增"绑定后必须立即拉取且 fire-and-forget"一条);tsc 通过;门禁同基线。
This commit is contained in:
@@ -283,6 +283,12 @@ resolveOurBranchByBinding({ envelopeBranchCode, rowBranchCode, supplierCode, sub
|
||||
| **回调**(首选) | webhook 新事件 `binding.changed`(兼容 `preparations.binding_changed` / `supplier.binding_changed` / `bindings.changed`),复用**既有 webhook URL 与验签**;支持单条与 `bindings:[...]` 批量 | ✅ 已实现(`branchBindingSync.ts` + webhook 分支) |
|
||||
| **回读**(退路) | `MAIN_SYSTEM_BINDING_SYNC_URL`(空=不启用,不猜路径)+ **`Authorization: Bearer <对接 token>`**(与主系统交付的 `GET /api/supplier/integration-bindings` 对齐)+ `?include_disabled=1`(否则解绑同步不到);每轮对账**在导入之前**跑 | ✅ 已实现(同上 + `scheduledJobs`);⚠️ 冷启动需一个已授权 supplier_code 才能换 token,否则跳过 |
|
||||
|
||||
**绑定成功即刻拉一次(2026-10-10 实测缺口)**:兜底对账挂在 **15 分钟**的调度 tick 上
|
||||
(`scheduledJobs` 的 `setInterval(..., 15*60*1000)`,内部再限流 10 分钟 ⇒ **实际周期是 15 分钟**,
|
||||
本文此前写的"≤10 分钟"不准)。实测:主系统在 17:50 绑定、17:53 分货、17:58 用户来问时我方还没轮到 ⇒
|
||||
用户看到的就是"绑了却收不到"。现在 `binding.changed` 应用成功后**立即 fire-and-forget 拉该分公司一次**
|
||||
(不阻塞 webhook 响应、失败只记日志,兜底仍是定时对账)⇒ 窗口从"最多 15 分钟"变成"秒级"。
|
||||
|
||||
四条纪律(都钉了测试):① 回调分支必须在**绑定路由闸门之前**(它本身在建绑定,查绑定表必然查不到);
|
||||
② 逐条独立(一条不认识不拖累其余);③ 编码不认识 ⇒ `ignored` + **零写入**;④ 快照回读只解绑"出现过但已失效"的行,
|
||||
**没提到的绑定不动**(防对方分页/裁剪误删)。
|
||||
|
||||
@@ -467,7 +467,7 @@ POST {我方}/api/supplier/preparations/actual-qty-decision
|
||||
- 其余我们担心的漂移,现有机制已经覆盖,不需要你们新增东西:
|
||||
| 我们担心的 | 现有覆盖 |
|
||||
|---|---|
|
||||
| 分配 webhook **漏推** | 我们每 ≤10 分钟按绑定拉一遍 `view=active`(active 是全量未完成行) |
|
||||
| 分配 webhook **漏推** | 我们每 **15 分钟**(调度 tick)按绑定拉一遍 `view=active`(active 是全量未完成行),并在**绑定成功的当下立即拉一次** |
|
||||
| **绑定就绪前**被忽略的推送 | 绑定一落地,下一轮拉取**自动把它们补回来**(我们忽略时**不落任何数据**,所以不会重复建单) |
|
||||
| **改派 / 退货**(行消失) | 既有 `lookup`(`found=false` 也返回 `current_supplier_code` / `reassigned`)+ 归档反查 |
|
||||
| **改绑 / 解绑漂移** | `P0-2③` 的一条推送 |
|
||||
|
||||
@@ -295,6 +295,35 @@ router.post('/main-system/webhook', async (req: Request, res: Response) => {
|
||||
const applied = results.filter((item) => item.status === 'applied').length;
|
||||
const unbound = results.filter((item) => item.status === 'unbound').length;
|
||||
const ignored = results.filter((item) => item.status === 'ignored').length;
|
||||
/**
|
||||
* **绑定成功即刻拉一次**(2026-10-10 实测缺口):
|
||||
* 兜底对账挂在 15 分钟的 tick 上,而"绑定"是一次真实业务动作 —— 用户点完绑定却要等十几分钟
|
||||
* 才看到货,会被当成"没生效"。这里 fire-and-forget 拉该分公司(不阻塞 webhook 响应、
|
||||
* 失败只记日志;真正的兜底仍是定时对账)。
|
||||
*/
|
||||
const touchedBranches = [
|
||||
...new Set(results.filter((item) => item.status === 'applied' && item.branchCode).map((item) => item.branchCode as string)),
|
||||
];
|
||||
for (const branch of touchedBranches) {
|
||||
void importMainSystemOrders({ branchCode: branch, userId: null })
|
||||
.then((imported) => {
|
||||
if (imported.kind === 'ok') {
|
||||
console.log(
|
||||
`[main-system-webhook] 绑定后即时拉取 ${branch}:新建 ${imported.created} / 补行 ${imported.appended} / 跳过 ${imported.skipped} / 失败 ${imported.failed}`
|
||||
);
|
||||
} else {
|
||||
console.warn(
|
||||
`[main-system-webhook] 绑定后即时拉取 ${branch} 未完成(${imported.kind})⇒ 交给定时对账兜底`
|
||||
);
|
||||
}
|
||||
})
|
||||
.catch((error: unknown) => {
|
||||
console.error(
|
||||
`[main-system-webhook] 绑定后即时拉取 ${branch} 异常(交给定时对账兜底):`,
|
||||
(error as Error)?.message || error
|
||||
);
|
||||
});
|
||||
}
|
||||
console.log(
|
||||
`[main-system-webhook] ${event}:绑定 ${applied} / 解绑 ${unbound} / 忽略 ${ignored}` +
|
||||
results
|
||||
|
||||
@@ -251,6 +251,14 @@ describe('接线:webhook 分支必须在"绑定路由闸门"之前', () => {
|
||||
expect(branchAt, '这条事件本身在建绑定,拿它查绑定表必然查不到 ⇒ 必须放在闸门前').toBeLessThan(gateAt);
|
||||
});
|
||||
|
||||
it('绑定成功即刻拉一次(否则用户刚绑完要等 15 分钟才看到货)', () => {
|
||||
const block = src.slice(src.indexOf('isBindingChangedEvent(event)'), src.indexOf('await resolveOurBranchByBinding({'));
|
||||
expect(block).toContain('importMainSystemOrders({ branchCode: branch, userId: null })');
|
||||
// 必须 fire-and-forget:不阻塞 webhook 响应,失败只记日志(兜底仍是定时对账)
|
||||
expect(block).toContain('.catch(');
|
||||
expect(block).toContain('交给定时对账兜底');
|
||||
});
|
||||
|
||||
it('逐条独立:一条失败不能拖累其余(全量重推场景)', () => {
|
||||
const block = src.slice(src.indexOf('isBindingChangedEvent(event)'), src.indexOf('await resolveOurBranchByBinding({'));
|
||||
expect(block).toContain('for (const change of changes)');
|
||||
|
||||
Reference in New Issue
Block a user