- main_supplier_bindings 新增 main_branch_name(迁移 20261021) - 绑定推送/回读接受 main_branch_name / branch_name,建客户直接用真名(缺省仍兜底) - 重新绑定把停用(40)客户恢复启用(10) - 测试钉住:ensureImportCustomer 先查后插 + ON CONFLICT DO NOTHING(已有客户不重复建)
103 lines
5.9 KiB
TypeScript
103 lines
5.9 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');
|
|
});
|
|
|
|
it('已有客户时不再新建:订单导入按 customer_code 命中即更新,INSERT 走 ON CONFLICT DO NOTHING', () => {
|
|
const start = importSrc.indexOf('async function ensureImportCustomer');
|
|
const fn = importSrc.slice(start, start + 3200);
|
|
expect(fn).toContain('SELECT status, source, company_name FROM customers WHERE customer_code = $1');
|
|
expect(fn).toContain('INSERT INTO customers (customer_code, company_name, branch_code, status, source)');
|
|
expect(fn).toContain('ON CONFLICT (customer_code) DO NOTHING');
|
|
// 先查后插:已有客户只更新、不新建
|
|
expect(fn.indexOf('SELECT status, source, company_name')).toBeLessThan(fn.indexOf('INSERT INTO customers'));
|
|
});
|
|
|
|
it('绑定即建客户:重新绑定时把停用(40)恢复为启用(10)', () => {
|
|
const start = importSrc.indexOf('export async function ensureMainBranchCustomerForBinding');
|
|
const fn = importSrc.slice(start, start + 1200);
|
|
expect(fn).toContain('UPDATE customers SET status = 10 WHERE customer_code = $1 AND status = 40');
|
|
expect(fn).toContain('ensureImportCustomer(params.branchCode');
|
|
});
|
|
});
|