diff --git a/docs/design/20261027-分公司接入编码与主系统供应商绑定.md b/docs/design/20261027-分公司接入编码与主系统供应商绑定.md index f832f61..b225e23 100644 --- a/docs/design/20261027-分公司接入编码与主系统供应商绑定.md +++ b/docs/design/20261027-分公司接入编码与主系统供应商绑定.md @@ -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` + **零写入**;④ 快照回读只解绑"出现过但已失效"的行, **没提到的绑定不动**(防对方分页/裁剪误删)。 diff --git a/docs/给主系统AI的对接改造清单.md b/docs/给主系统AI的对接改造清单.md index 1401cb9..9638578 100644 --- a/docs/给主系统AI的对接改造清单.md +++ b/docs/给主系统AI的对接改造清单.md @@ -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③` 的一条推送 | diff --git a/server/src/routes/mainSystemIntegration.ts b/server/src/routes/mainSystemIntegration.ts index e076e8e..89e2f9a 100644 --- a/server/src/routes/mainSystemIntegration.ts +++ b/server/src/routes/mainSystemIntegration.ts @@ -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 diff --git a/server/tests/branchBindingSync.test.ts b/server/tests/branchBindingSync.test.ts index e01f653..c7693a6 100644 --- a/server/tests/branchBindingSync.test.ts +++ b/server/tests/branchBindingSync.test.ts @@ -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)');