Files
SupplierSystem/server/tests/mainSystemSupplierCodeCallSites.test.ts
T
Ubuntu 3798082811
CI / server typecheck + tests (push) Canceled after 0s
CI / web typecheck + tests + miniprogram syntax (push) Canceled after 0s
CI / eslint (观察项,C7 骨架) (push) Canceled after 0s
CI / npm audit (critical 硬拦) (push) Canceled after 0s
fix(对接收尾): 9 处只读路径仍在用「分公司编码」当 supplierCode ⇒ 403 静默失效
真库日志抓到(每轮在刷):
  [main-system-actual-facts] BR003 … 刷新失败:换取对接 Token 失败(forbidden: 未授权访问该供应商:
    该(分公司 × 供应商)尚未绑定子系统接入编码…)

根因:接入编码落地时我改掉了全部**写**路径,**读**路径漏了 9 处,仍把 branchCode 传进
主系统接口的第一参数 `supplierCode`(按供货关系鉴权)⇒ 解耦后必然 403;而它们全是
"失败只记日志继续跑"的兜底/跟随路径 ⇒ **静默失效**:
  · 核货事实刷新 mainSystemActualFacts:135
  · 退货/改派跟随 lookup 与归档反查 mainSystemReturnFollow:357/:388
  · 缺货跟随剔除 mainSystemOrderImport:1764、报缺货前状态 mainSystemStockout:429
  · 履约事件对账兜底 mainSystemFulfillmentEvents:363
  · Q6 漏推自愈 mainSystemPriceQtyRequest:1416、mainSystemActualQtyInbox:917/:1117

修法:新增 server/src/services/mainSystemBranchApi.ts(与写路径的 resolveSupplierCodeForPrep 同口径)
  · listSupplierCodesForBranch:该分公司全部生效绑定的 supplier_code
  · groupItemsBySupplierCode:按**行级台账**分组;台账缺失时只有唯一绑定才回退,多绑定则**跳过并告警**(不猜)
  · lookup/归档/申请列表:逐供应商调用后**合并**;部分失败保留成功结果(兜底要能推进多少算多少),全失败才报错
并把这 9 处换成包装器(含两处测试 mock 转接)。

回归守卫(防复发):
  · mainSystemSupplierCodeCallSites.test.ts:全仓扫描,禁止再把 branch/branchCode/params.branchCode
    传进 9 个"首参是 supplierCode"的对接函数(连写路径一起钉),并断言 8 条兜底路径都在用新包装器
  · mainSystemBranchApi.test.ts 11 例:只认台账 / 唯一绑定才回退 / 多绑定跳过 / 合并与部分失败保留 / 未绑定返回

实测(真库 + 真主系统):
  · refreshOrderActualFacts(BR003, …_0001) ⇒ refreshed:true,6 行事实全取回(**403 消失**)
  · 调度真入口跑一轮 ⇒ 日志「BR003:在飞单 1 张,刷新到核货事实 1 张」(403 绝迹)
  · 顺带验证合并键:BR003 现有两张单(主系统 BR002 的 6 行 / 主系统 BR007 的 3 行)⇒ 不同主系统分公司各自成单 ✅
  · 服务端 2350 用例 / 失败文件 25 = 基线 24 + 另一条工作线遗留的 zeroCostClearsPlanPrice(非本次引入)

设计记录:docs/design/20261027-分公司接入编码与主系统供应商绑定.md §12.5.1
2026-10-10 18:56:31 +08:00

81 lines
4.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { describe, expect, it } from 'vitest';
import { readFileSync, readdirSync, statSync } from 'node:fs';
import path from 'node:path';
/**
* 回归守卫(2026-10-10 实测事故):**不许再把「我方分公司编码」传进 `supplierCode` 参数**。
*
* 背景:接入编码落地后,`branch_code` 与主系统的 `supplier_code` **不再相等**。
* 但主系统那些只读接口的第一参数仍是 `supplier_code`(按供货关系鉴权),
* 旧约定下到处都是 `xxx(branchCode)` 的写法 —— 解耦后**必然 403**,而它们全是
* 「兜底/跟随」路径(失败只记日志继续跑)⇒ **静默失效**:
* · 退货/改派跟随的 lookup 与归档反查(`mainSystemReturnFollow`)
* · 缺货跟随剔除(`mainSystemOrderImport`)、报缺货前的行状态(`mainSystemStockout`)
* · 履约事件对账兜底(`mainSystemFulfillmentEvents`)、核货事实刷新(`mainSystemActualFacts`)
* · Q6 漏推自愈(`mainSystemPriceQtyRequest` / `mainSystemActualQtyInbox`)
* 修法:统一走 `mainSystemBranchApi`(按分公司→多个绑定的包装器)或
* `branchAccessCode.resolveSupplierCodeForPrep`(行级台账取 supplier_code)。
*
* ⚠️ 诊断线索(真库日志原文):
* `换取对接 Token 失败(forbidden: 未授权访问该供应商:该(分公司 × 供应商)尚未绑定子系统接入编码…)`
*/
const SRC = path.join(__dirname, '../src');
function walk(dir: string, out: string[] = []): string[] {
for (const entry of readdirSync(dir)) {
const full = path.join(dir, entry);
if (statSync(full).isDirectory()) walk(full, out);
else if (entry.endsWith('.ts') && !entry.endsWith('.test.ts')) out.push(full);
}
return out;
}
/** 第一参数是 `supplierCode` 的对接函数(含写路径,一起钉住) */
const SUPPLIER_CODE_FNS = [
'lookupMainSystemPreparationStates',
'fetchMainSystemArchivedPreparations',
'fetchMainSystemPreparations',
'listMainSystemRequests',
'writebackMainSystemPreparationStatus',
'submitMainSystemPriceQtyRequest',
'postMainSystemActualQtyDecision',
'getMainSystemToken',
'fetchMainSystemPriceAmendRows',
];
describe('对接调用:第一参数不得是分公司编码', () => {
const files = walk(SRC).filter(
(f) => !f.includes(`${path.sep}mainSystem${path.sep}api${path.sep}`) // HTTP 层就是收 supplierCode 的
);
it('没有调用点把 branch / branchCode / params.branchCode 传进 supplierCode 参数', () => {
const violations: string[] = [];
for (const file of files) {
const text = readFileSync(file, 'utf8');
for (const fn of SUPPLIER_CODE_FNS) {
const re = new RegExp(`${fn}\\(\\s*(branch\\b|branchCode\\b|params\\.branchCode\\b)`, 'g');
const hits = text.match(re);
if (hits) violations.push(`${path.relative(SRC, file)} → ${fn}(${hits.length} 处)`);
}
}
expect(violations, `这些调用会把分公司编码当供应商编码 ⇒ 必然 403:\n${violations.join('\n')}`).toEqual([]);
});
it('按分公司的包装器存在且被这些兜底路径使用', () => {
const expectations: Array<[string, string]> = [
['services/mainSystemActualFacts.ts', 'lookupPreparationStatesForBranch'],
['services/mainSystemStockout.ts', 'lookupPreparationStatesForBranch'],
['services/mainSystemFulfillmentEvents.ts', 'lookupPreparationStatesForBranch'],
['services/mainSystemReturnFollow.ts', 'lookupPreparationStatesForBranch'],
['services/mainSystemReturnFollow.ts', 'fetchArchivedPreparationsForBranch'],
['services/mainSystemOrderImport.ts', 'lookupPreparationStatesForBranch'],
['services/mainSystemPriceQtyRequest.ts', 'listRequestsForBranch'],
['services/mainSystemActualQtyInbox.ts', 'listRequestsForBranch'],
];
for (const [file, fn] of expectations) {
const text = readFileSync(path.join(SRC, file), 'utf8');
expect(text, `${file} 应使用 ${fn}`).toContain(fn);
}
});
});