Files
SupplierSystem/server/scripts/smoke-main-system-report-push.ts
Ubuntu fdbef63516 feat(对接): 批次报告推送到主系统(第九批)—— 真环境冒烟已通,三重门禁 + 台账去重
主系统已回执「可以开工」(其 §2.12.9 最终契约 / §4.5.17 逐条回执,接收侧提交 c24f4881)。
按契约实现我方推送侧,**未新开端点**:文件走他们 `/api/storage/quality-reports/upload`(multipart),
登记走 `/api/supplier/quality-reports`(按备货行逐条)。

**三重门禁(开发者要求"没绑定就别传、不能造成混乱")**:
  ① 分公司 ↔ 主系统有 active 绑定(未绑定 ⇒ 连 token 都取不到 ⇒ 物理上发不出去)
  ② 该行是主系统下发的(`main_system_order_import_items` 有 prep_id 对照)
  ③ 该批次已人工确认关联到报告
  未满足 ⇒ **零出站**(采集 SQL 里就用 JOIN 卡死,且开关关闭时连候选都不查)

**幂等(B4=我方台账去重)**:新表 `public.main_system_report_pushes`,唯一键 `(prep_id, report_type)` +
**完整文件集指纹**:指纹未变 ⇒ 不重推;指纹变了(事后补充)⇒ 按**完整文件集**重推一次
(对方 latest wins:同 (prep, type) 再登记会作废旧报告,所以不能只推新增文件)。

**契约硬语义(单测钉死)**:绝不传 `defer_coverage`(暂存态:既不覆盖也不展示);
绝不传 sku/order/branch/supplier/category/delivery(对方按行派生);`report_type` 按行要求的类型
(`qualityReportType`,本轮给拉取映射补上;取不到且该行要求报告时**不猜、跳过**);
回执映射 200 stored / 404 ignored(终态) / 400 failed(终态) / 401 换 token / 5xx 退避重试。

**触发点三处**:① 分拣完成(pickingService 成功路径,旁路 fire-and-forget)
② 报告确认关联(inventoryRoutes confirm 路由)③ scheduler tick 定时兜底(默认关 ⇒ 零出站)。

**真环境冒烟(对照主系统 3001,已跑通)**:
  · 上传 ⇒ `{"kind":"ok","path":"4019fbdf…png"}`(用他们回执里的 path 登记)
  · 登记 ⇒ **200** `data.id=0de8d009-d28e-4124-a333-5bef819b6dac`
    `order_code/BR002/sku_code/supplier_code` 全由他们从备货行派生;我们传的
    `batch_no=STK202610107YRQEO`、`product_name=香菇、平菇`、`testing_org=XX检测中心` 都正确落库;
    `share_key` **不以 `st|` 开头**(= 正常覆盖态,非 defer)
  · 我方台账回执:`order_code=ORD_WEB_20261010_BR002_0001`、`prep_id=b4e7fd80-668d-4f9b-b69f-dcb3fddc623e`
  · ⚠️ **发现对方一处回显缺陷(已坐实,待他们修)**:`POST /api/supplier/quality-reports` 回执里
    `produced_at` 比传入/库里少一天(我们传 2026-10-08、他们库 `tenant_br002.quality_reports.produced_at`
    实测 = 2026-10-08,但回执 JSON = 2026-10-07);`compare_at` 同样(库 2026-10-11 / 回执 2026-10-10)
    ⇒ 是他们 date 列的**序列化时区**问题(值没存错,只在回显),与他们清单 §2.12.9 ⑦-1
    "与你们传的一致"这条判定相冲突,需他们确认修

验证:server tsc ✅;新增 27 例(服务 14 + 调用层 13)全绿;全量 380 文件 / 2405 用例 ⇒ 失败 25 文件
= 基线 24 + 另一条工作线遗留的 zeroCostClearsPlanPrice;门禁同基线。
设计:docs/design/20261011-批次报告推送主系统.md;冒烟脚本:server/scripts/smoke-main-system-report-push.ts
2026-10-11 15:15:47 +08:00

85 lines
4.5 KiB
TypeScript
Raw Permalink 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.
/**
* 冒烟:批次报告推送(第九批「批次报告同步」)—— 真环境端到端,**只读 + 一次真实登记**。
*
* 用法:
* npx tsx --env-file=.env scripts/smoke-main-system-report-push.ts [文件路径]
* (默认用 docs/design/assets 里的 png;也可给一个 pdf 路径)
*
* 做四件事:
* ① 从**我方台账 + 绑定**里挑一条「已绑定 + 主系统下发(有 prep_id)」的行(这三重门禁的前两重);
* ② 从主系统实时取该行要求的报告类型(契约 §2.12.9 ④-3:按行传;取不到且该行要求报告时**不猜**);
* ③ 走 ①上传(multipart)→ ②登记(按备货行)两步调用;
* ④ 打印回执与 `order_code`(用于对方核 §2.12.9 ⑦-3 内部订单详情「报告」列)。
*
* ⚠️ 这是**联调脚本**,不是生产链路:真链路由 mainSystemReportPushService + 定时兜底驱动。
*/
import fs from 'node:fs';
import pg from 'pg';
import { fetchMainSystemPreparations } from './src/services/mainSystem/api/preparations';
import { uploadBatchReportFile, registerBatchReportOnPreparation } from './src/services/mainSystem/api/qualityReports';
(async () => {
// ① 找一条「已绑定 + 主系统下发」的行
const c = new pg.Client({ connectionString: process.env.DATABASE_URL });
await c.connect();
const rows = await c.query(`SELECT l.branch_code, l.main_supplier_code, l.prep_id::text, l.main_order_code, l.item_name,
l.payload->>'requiresQualityReport' AS requires_report,
l.payload->>'qualityReportType' AS payload_type
FROM public.main_system_order_import_items l
JOIN public.main_supplier_bindings b
ON b.branch_code = l.branch_code AND b.main_supplier_code = l.main_supplier_code AND b.status='active'
WHERE l.prep_id IS NOT NULL ORDER BY l.branch_code, l.main_order_code LIMIT 10`);
await c.end();
console.log('候选行(台账):');
for (const r of rows.rows) {
console.log(` ${r.branch_code} | ${r.main_supplier_code} | prep=${r.prep_id.slice(0, 8)}… | ${r.main_order_code} | ${r.item_name} | requires=${r.requires_report} | type=${r.payload_type ?? '(未知)'}`);
}
const picked = rows.rows[0];
if (!picked) { console.log('没有可用的行'); process.exit(0); }
// ② 从主系统实时取该行的"要求报告类型"(契约 ④-3:按行传)
const outcome: any = await fetchMainSystemPreparations(picked.main_supplier_code, 'active');
if (outcome.kind !== 'ok') { console.log('拉取备货行失败:', outcome.kind, outcome.reason); process.exit(1); }
let live: any = null;
for (const order of outcome.orders || []) {
for (const item of order.items || []) {
if (String(item.id) === picked.prep_id) live = item;
}
}
console.log('\n主系统实时行:', live ? JSON.stringify({
id: String(live.id).slice(0, 8) + '…',
requires: live.requiresQualityReport,
type: live.qualityReportType ?? null,
order: live.orderCode,
}) : '(该行已不在 active 视图)');
const reportType = live?.qualityReportType || (live?.requiresQualityReport ? '' : 'batch');
if (!reportType) { console.log('该行要求报告但类型未知 ⇒ 按契约不猜,终止'); process.exit(0); }
// ③ 上传真实文件(用仓库里现成的 png)
const filePath = '../docs/design/assets/mp-ui-20260806/01-登录页.png';
const buffer = fs.readFileSync(filePath);
const uploaded = await uploadBatchReportFile({
supplierCode: picked.main_supplier_code,
fileName: 'batch-report-smoke.png',
buffer,
mime: 'image/png',
});
console.log('\n① 上传:', JSON.stringify(uploaded));
if (uploaded.kind !== 'ok') process.exit(1);
// ④ 登记(带四个来源字段;不带 defer_coverage)
const registered = await registerBatchReportOnPreparation({
supplierCode: picked.main_supplier_code,
preparationId: picked.prep_id,
files: [uploaded.path],
reportType,
batchNo: 'STK202610107YRQEO',
producedAt: '2026-10-08',
testingOrg: 'XX检测中心',
productNames: ['香菇', '平菇'],
});
console.log('② 登记:', JSON.stringify(registered));
console.log('\n>>> 联调回执用:order_code =', picked.main_order_code, '| prep_id =', picked.prep_id, '| report_type =', reportType);
process.exit(0);
})().catch((e) => { console.error('冒烟失败:', e.message); process.exit(1); });