开发者报障(客户列表截图):「主系统订单(BR003)」这个**无关客户**是谁塞进来的?
「水产供应商这个分公司不能植入无关的客户呀,是不是什么脚本或者兜底触发了?」
根因(**不是脚本,是兜底**):`importDayBucket` 每轮开头的"客户自愈"调用**只传了 branchCode**
⇒ `ensureImportCustomer` 走**兜底占位客户**分支(`MAIN_<我方分公司>`,名字「主系统订单(…)」)
⇒ **每导入一轮就创建/保住**这个占位客户。建单与追加两条路径早已改成带当天的主系统分公司
(「自贡」,见 §2.6 客户口径),只有这处自愈漏了。
修法(代码,不是补数据脚本):
1. 自愈调用改为 `ensureImportCustomer(branchCode, pickDayMainBranch(bucket…))` —— 与建单/追加同口径,
只有**判不出**主系统分公司时才落占位客户;
2. **防呆**:调用方没给分公司名时**保持库里现有名字**(优先级:调用方给的名字 > 库里名字 > 兜底名)。
⚠️ 这条是我本次自己踩出来的:`reattributeMainSystemOrdersToBranchCustomers({branchCode})`
不传 `branchNames` 时会用兜底名把已经叫「自贡」的客户**改名成**「主系统分公司 BR002」,
而且毫无提示 ⇒ 加防呆后"空跑"不会再破坏名字;
3. 存量修复走**既有服务**(先按台账里记的真实分公司名传 `branchNames`):订单归到「自贡」,
**0 引用的占位客户置 `status=40` 停用**(沿用"不删只停用"约定)。
验证(真库):BR003 客户 = `MAIN_BR002 自贡(10)` + `MAIN_BR003 主系统订单(BR003)(40 停用)`;
订单挂在「自贡」;随后不带名字空跑一次复核,名字**未被覆盖**。
回归守卫:`server/tests/mainSystemImportGuards.test.ts` +2 条(自愈必须带分公司;不许用兜底名覆盖)。
设计记录:docs/design/20261010-主系统订单按配送日合并与缺货流转.md §2.6.1。
⚠️ 基线说明(**非本次改动引入**,来自变基进来的另一条工作线 `1d9adce`「库存入库/Web」):
· `server/tests/zeroCostClearsPlanPrice.test.ts` 未填=0 那条:断言 `StockInboundPanel.tsx` 里的旧表达式,该文件被其重写;
· `src/pages/system/SystemConfig.allowZeroCost.test.ts` 2 条:断言该面板含 `useAllowZeroCost`,同样已不存在。
三处红都在他们那条线里,需要他们同步测试(我没有替他们改)。
96 lines
5.0 KiB
TypeScript
96 lines
5.0 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
||
import { describe, expect, it } from 'vitest';
|
||
|
||
/**
|
||
* 导入守卫(2026-10-11 真实事故后补):
|
||
* 事故现象:某分公司(SUP35143228)换库后**对接 Token 换不到**(主系统回
|
||
* `forbidden: 未授权访问该供应商`),拉单返回 `unavailable`,导入把它当"本轮 0 行"静默跳过
|
||
* ⇒ 该分公司永远收不到单、却全程无告警(`failed: 0`、无认领记录、无台账行)。
|
||
* 用户表现为「登录后看不到任何订单」。
|
||
*
|
||
* 本文件钉两条:① 拉不到必须计入 failed + 带分公司编码的告警;② 归属断言(不得张冠李戴)。
|
||
*/
|
||
const src = readFileSync(new URL('../src/services/mainSystemOrderImport.ts', import.meta.url), 'utf8');
|
||
const fanout = src.slice(src.indexOf('export async function importMainSystemOrdersForAllSupplierBranches'));
|
||
|
||
describe('拉取失败不得静默跳过', () => {
|
||
it('unavailable ⇒ 计入 failed 并打带分公司编码的错误日志', () => {
|
||
/**
|
||
* 2026-10-27 口径变更:拉取从"用 branchCode 当 supplier_code 拉一次"改成
|
||
* `pullPreparationsForBranch()`(按该分公司**全部生效绑定**逐个拉,见设计文档 §4 第 4 行),
|
||
* 因此断言的对象由 `preloadedActive` 换成 `preloadedPull` —— **不变量不变**:
|
||
* 拉不到必须计入 failed + 带分公司编码告警,且必须在导入之前拦下。
|
||
*/
|
||
expect(fanout).toContain("preloadedPull.kind === 'unavailable'");
|
||
expect(fanout).toContain('拉取失败,本分公司本轮未导入');
|
||
expect(fanout).toContain('failed: 1');
|
||
// 必须在调用 importMainSystemOrders **之前**拦下(否则会被当成 0 行继续走)
|
||
expect(fanout.indexOf("kind === 'unavailable'")).toBeLessThan(
|
||
fanout.indexOf('const outcome = await importMainSystemOrders(')
|
||
);
|
||
});
|
||
|
||
it('未绑定(not_bound)不算失败:那是"这家不接主系统"的正常状态', () => {
|
||
expect(fanout).toContain("not_bound");
|
||
expect(fanout).toContain('未被绑定 = 这家不接主系统,是正常状态');
|
||
});
|
||
});
|
||
|
||
describe('归属断言:绝不能张冠李戴', () => {
|
||
it('本轮写入的台账行 main_supplier_code 必须落在本分公司的**生效绑定**里', () => {
|
||
/**
|
||
* 2026-10-27 口径变更:改前断言"等于本分公司编码"(那时 `branch_code == supplier_code`);
|
||
* 接入编码落地后两者不再相等 ⇒ 断言改为"属于本分公司某条生效绑定上的 supplier_code"。
|
||
* **不变量不变**:仍然拦"这行其实属于别家",且失败计入 failed。
|
||
*/
|
||
expect(fanout).toContain('归属断言');
|
||
expect(fanout).toContain("NOT (COALESCE(main_supplier_code, '') = ANY($2::text[]))");
|
||
expect(fanout).toContain('不属于本分公司的任何绑定');
|
||
// 断言失败要计入 failed(宁可报警,不可静默错建)
|
||
expect(fanout).toContain('failed: bad');
|
||
});
|
||
|
||
it('断言自身异常不阻断导入(只告警)', () => {
|
||
expect(fanout).toContain('归属断言执行失败(不阻断导入)');
|
||
});
|
||
});
|
||
|
||
describe('导入客户:不许顺手建出「主系统订单(我方分公司)」占位客户(2026-10-10 报障)', () => {
|
||
it('每轮的自愈必须带**当天的主系统分公司**(否则每轮都创建/保住占位客户)', () => {
|
||
const start = src.indexOf('async function importDayBucket');
|
||
const fn = src.slice(start, start + 3000);
|
||
expect(fn, '自愈调用必须传当天的主系统分公司').toContain(
|
||
'ensureImportCustomer(branchCode, dayMainBranch)'
|
||
);
|
||
expect(fn, '不许再有"只传 branchCode"的自愈调用(那就是占位客户的来源)').not.toContain(
|
||
'ensureImportCustomer(branchCode).catch'
|
||
);
|
||
expect(fn).toContain('pickDayMainBranch(bucket.groups');
|
||
});
|
||
|
||
it('调用方没给分公司名时,**保持库里的名字**,不得用兜底名覆盖(防呆)', () => {
|
||
const start = src.indexOf('async function ensureImportCustomer');
|
||
const fn = src.slice(start, start + 3200);
|
||
expect(fn).toContain('nameForExisting');
|
||
// 优先级:调用方给的名字 > 库里现有名字 > 兜底名
|
||
expect(fn).toMatch(/mainBranchName \|\| String\(current\?\.company_name \|\| ''\)\.trim\(\) \|\| fallbackBranchName/);
|
||
});
|
||
});
|
||
|
||
describe('客户归属兜底:不得挂在禁用客户上(2026-10-11 报障)', () => {
|
||
it('行里缺 branch_code 时,从主系统单号解析分公司(orderCode 兜底)', () => {
|
||
const start = src.indexOf('function pickDayMainBranch');
|
||
const fn = src.slice(start, start + 2600);
|
||
expect(fn).toContain('orderCode');
|
||
expect(fn).toContain('if (!counter.size) {');
|
||
expect(fn).toContain('match(/_(BR[0-9A-Z]+)_/i)');
|
||
});
|
||
|
||
it('占位客户被停用(status=40)后,一旦用回它必须自愈启用', () => {
|
||
const start = src.indexOf('async function ensureImportCustomer');
|
||
const fn = src.slice(start, start + 4200);
|
||
expect(fn).toContain('Number(current?.status) === 40');
|
||
expect(fn).toContain('禁用客户');
|
||
});
|
||
});
|