开发者报障:「主系统分配来的订单,子系统这边客户名还是『主系统订单(SUP38887897)』, 不应该是主系统分公司的名字吗?」真库实测**所有**分公司的新单都挂在兜底占位客户上。 根因:一天一张单的两条建单路径口径不一致 —— · createDayOrder(新建单,每天第一张单走这条):`ensureImportCustomer(branchCode)` **没带分公司** ⇒ 永远拿兜底占位客户「主系统订单(<我方分公司>)」; · appendNewRowsToDayOrder(当天追加行):`pickDayMainBranch(...)` ⇒ `MAIN_<BRxxx>` / 分公司名 ✓ 2026-10-11 那次验收用的是**存量改挂脚本**改过的老单(脚本会把 customer_code 改成 MAIN_BR002), 所以"看起来生效了",新建单路径从未被覆盖;测试也只钉了追加路径。 修法:createDayOrder 与追加路径同口径 —— `pickDayMainBranch(allRows)` 后 `ensureImportCustomer(branchCode, dayMainBranch)`(行的 branch_code 优先,缺失退回主系统单号 `_BRxxx_` 段)。 测试补钉子:**两条建单路径都必须带分公司**(此前只钉了追加路径)。 真库(幂等脚本 reattribute-main-system-branch-customers,已执行): · SUP38887897 今天的单 ORD_API_20261010_SUP38887897_0001 ⇒ MAIN_BR002「自贡」✓ · SUP35143228 / SUP37831443 全部单 ⇒ 自贡;SUP92185841 的 2 张 10-09 单 ⇒ 自贡 · SUP92185841 另 2 张 10-08 老单**不动**(单号无 _BRxxx_ 段且台账无分公司 ⇒ 按设计"绝不瞎猜") · 占位客户不再被引用者已停用(status=40),仍被引用者保持可用 文档:设计文档新增 §2.6.1(时间线/根因/修法/真库验证),§2.6 打"已被取代"标记。 测试:mainSystemBranchCustomer / mainSystemImportGuards / mainSystemDayMergeAndStockout 28 绿; 服务端全量失败集与基线一致(24 文件 / 30 用例,零新增)。
86 lines
4.8 KiB
TypeScript
86 lines
4.8 KiB
TypeScript
import { readFileSync } from 'node:fs';
|
|
import { describe, expect, it } from 'vitest';
|
|
|
|
/**
|
|
* 客户口径(开发者 2026-10-11 定稿):**主系统的分公司名字就是客户名字**。
|
|
* · 一个主系统下单分公司 = 我方一个客户;客户编码 `MAIN_<分公司编码>`;客户名 = 分公司名;
|
|
* · 分公司名在主系统为空 ⇒ 「主系统分公司 <编码>」兜底(开发者批准);
|
|
* · 名字**没被占用就不加编号**(占用才 `-2`/`-3`);
|
|
* · 拿不到分公司编码(webhook 剥字段)⇒ 退回原占位客户,行为与本改动前一致;
|
|
* · 存量订单按台账/主系统单号里的 `_BRxxx_` 段改挂(开发者决策 ③「需要」)。
|
|
*/
|
|
const importSrc = readFileSync(
|
|
new URL('../src/services/mainSystemOrderImport.ts', import.meta.url),
|
|
'utf8'
|
|
);
|
|
const ledgerSrc = readFileSync(new URL('../src/services/mainSystemPrepLedger.ts', import.meta.url), 'utf8');
|
|
const script = readFileSync(
|
|
new URL('../scripts/reattribute-main-system-branch-customers.ts', import.meta.url),
|
|
'utf8'
|
|
);
|
|
|
|
describe('客户 = 主系统分公司,客户名 = 分公司名', () => {
|
|
it('客户编码用 MAIN_<分公司编码>,名字用分公司名,空名走兜底', () => {
|
|
expect(importSrc).toContain('`${IMPORT_CUSTOMER_PREFIX}${mainBranchCode}`');
|
|
expect(importSrc).toContain('function fallbackBranchName(mainBranchCode: string)');
|
|
expect(importSrc).toContain('主系统分公司 ${mainBranchCode}');
|
|
expect(importSrc).toMatch(/mainBranchName \|\| fallbackBranchName\(mainBranchCode\)/);
|
|
});
|
|
|
|
it('名字只在被别的客户占用时才加编号', () => {
|
|
const start = importSrc.indexOf('async function dedupeCustomerName');
|
|
const fn = importSrc.slice(start, start + 1200);
|
|
expect(fn).toContain('WHERE company_name = $1 AND customer_code <> $2');
|
|
expect(fn).toContain('if (!taken.rowCount) return baseName;');
|
|
expect(fn).toContain('${baseName}-${i}');
|
|
});
|
|
|
|
it('拿不到分公司编码时退回占位客户(webhook 剥字段的历史兼容)', () => {
|
|
expect(importSrc).toContain('fallbackPlaceholderName');
|
|
expect(importSrc).toMatch(/hasMainBranch\s*\?\s*`\$\{IMPORT_CUSTOMER_PREFIX\}\$\{mainBranchCode\}`/);
|
|
});
|
|
|
|
it('归属取当日行里出现最多的分公司(真混合时告警,不静默丢)', () => {
|
|
const start = importSrc.indexOf('function pickDayMainBranch');
|
|
const fn = importSrc.slice(start, start + 1400);
|
|
expect(fn).toContain('counter');
|
|
expect(fn).toContain('多个下单分公司');
|
|
expect(importSrc).toContain('pickDayMainBranch(pending.flatMap((entry) => entry.rows))');
|
|
});
|
|
|
|
it('**新建单**路径也必须用当日分公司(2026-10-10 报障:此前只有"追加"路径带了)', () => {
|
|
// 事故:每天第一张单走 `createDayOrder`,那里调的是 `ensureImportCustomer(branchCode)`(无分公司)
|
|
// ⇒ 单子永远挂在占位客户 `MAIN_<我方分公司>`(名字「主系统订单(…)」),
|
|
// 用户看到的是"主系统订单(SUP38887897)"而不是主系统分公司名「自贡」。
|
|
const start = importSrc.indexOf('async function createDayOrder');
|
|
expect(start, '未找到 createDayOrder').toBeGreaterThan(-1);
|
|
const body = importSrc.slice(start, start + 4000);
|
|
expect(body).toContain('pickDayMainBranch(allRows)');
|
|
expect(body).toMatch(/ensureImportCustomer\(branchCode, dayMainBranch\)/);
|
|
// 两条建单路径(新建 / 追加)都必须带分公司
|
|
expect(importSrc.match(/ensureImportCustomer\(branchCode, dayMainBranch\)/g)?.length ?? 0).toBeGreaterThanOrEqual(2);
|
|
});
|
|
|
|
it('台账落 main_branch_code / main_branch_name(非空才覆盖)', () => {
|
|
expect(ledgerSrc).toContain('main_branch_code, main_branch_name');
|
|
expect(ledgerSrc).toContain(
|
|
'main_branch_code = COALESCE(EXCLUDED.main_branch_code, public.main_system_order_import_items.main_branch_code)'
|
|
);
|
|
expect(ledgerSrc).toContain('row.branchCode ? String(row.branchCode).trim().toUpperCase() : null');
|
|
});
|
|
});
|
|
|
|
describe('存量归属改挂(决策 ③)', () => {
|
|
it('导出可复用函数 + 一次性脚本,归属依据逐级回退且不瞎猜', () => {
|
|
expect(importSrc).toContain('export async function reattributeMainSystemOrdersToBranchCustomers');
|
|
expect(importSrc).toContain('BR[0-9A-Z]+');
|
|
const start = importSrc.indexOf('export async function reattributeMainSystemOrdersToBranchCustomers');
|
|
const fn = importSrc.slice(start, start + 3800);
|
|
expect(fn).toContain('if (!mainBranchCode) continue;');
|
|
expect(fn).toContain('customer_code IS DISTINCT FROM $2');
|
|
expect(fn).toContain('SET status = 40');
|
|
expect(fn).toContain('SELECT count(*)::text AS count FROM orders WHERE customer_code = $1');
|
|
expect(script).toContain('reattributeMainSystemOrdersToBranchCustomers');
|
|
});
|
|
});
|