Files
SupplierSystem/server/tests/orderBatchReportFiles.test.ts
T
Ubuntu 4b102552df
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
feat(订单详情): 「报告」列并显示**批次报告** —— 修「我这边没看到报告」
现象(开发者):抄手皮那几批明明都绑定了已确认的批次报告,订单详情那一行却是空的;
主系统那侧也没有 —— 主系统侧是第九批推送的事,我方侧是**这一列只吃质检报告**
(`quality_reports` 按 sku+supplier 命中),压根不看**批次报告**(`inventory_batch_reports`)。

改动:
- 新增服务 `orderBatchReportFilesService.listOrderBatchReportFiles`:
  批次判据与推送侧 `collectReportPushCandidates` **逐字同口径**
  (① `picking_items.outbound_batches` ∪ ② `inventory_logs` 的 `picking_out`/`delivery_loading_out`),
  只取 `status = 30` 已人工确认的报告,URL 复用 `buildSignedStoragePath('quality-reports', …)`。
- 新增只读路由 `GET /api/workflow/orders/:orderCode/batch-reports`
  (权限与既有 `quality-report-files` 一致;路由只鉴权 + 委派,不写 SQL)。
- 前端 `OrderManagementPage`:与质检报告**并行**拉取、失败降级 `[]`(不卡详情),
  按**订单行 id** 归并后**合并**进既有 `quality_report_files` 列并按 `url` 去重(不是替换)。
  未新增任何界面文案 / 组件。

真库实测(BR003 `ORD_API_20261012_BR003_0001`,fixture 用完即删):
现状 0 条 → 挂一条 `delivery_loading_out` 后该行出现报告(抄手皮 / STK20261012NJLG01 /
红河质安检验检测有限公司 / 签名 URL)→ 删除 fixture 后回到 0 条(零残留)。
⚠️ 该单本身仍为空是**数据事实**:库存抵扣 / 免入库两条路不出库(不写 `inventory_logs`)
⇒ 两侧都没有批次事实;改动涉及冻结项,另行裁决(见设计文档 §5)。

测试:新增 6 条服务/路由守卫 + 4 条前端守卫;两处判据有源码级守卫钉住口径不许漂。
门禁 `run-all.sh --verify`:8 通过 / `check-ai-cost` 仍红(基线)/ `verify-facts` 未执行(本机连不上库)。
套件:server 358 通过 / 25 失败文件,相对基线新增失败**仅** `zeroCostClearsPlanPrice`
(另一工作流在途的 allowZeroCost 改动,与本次无关);root 737 通过 / 2 失败(同一在途改动)。
2026-10-12 10:26:01 +08:00

161 lines
6.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 { beforeEach, describe, expect, it, vi } from 'vitest';
import { readFileSync } from 'fs';
import path from 'path';
/**
* 订单详情「报告」列显示**批次报告**(2026-10-12)。
*
* 设计:`docs/design/20261012-订单详情显示批次报告.md`
*
* 用户报告「我这边没有看到报告,主系统那边也没有」——主系统侧已由第九批推送修好,
* 我方侧此前只显示**质检报告**(`quality_reports`),不显示**批次报告**
* (`inventory_batch_reports`)⇒ 抄手皮那几批明明绑了报告,订单行仍是空的。
*
* 这里钉住三件做反就要返工的事:
* ① **口径不许漂**:批次判据必须与推送侧 `collectReportPushCandidates` 同一套两来源
* (`picking_items.outbound_batches` ∪ `inventory_logs` 的 `picking_out`/`delivery_loading_out`),
* 否则会出现"我方看得到、主系统收不到"(或反之);
* ② **只显示已人工确认的报告**(`status = 30`),草稿/待确认不许出现在订单详情;
* ③ 路由只做鉴权 + 委派(与既有 `quality-report-files` 同权限集合),不写 SQL。
*/
const query = vi.hoisted(() => vi.fn());
vi.mock('../src/db', () => ({
pool: { query: (...args: unknown[]) => query(...args) },
}));
import { listOrderBatchReportFiles } from '../src/services/orderBatchReportFilesService';
const serviceSrc = readFileSync(
path.join(__dirname, '../src/services/orderBatchReportFilesService.ts'),
'utf8'
);
const pushSrc = readFileSync(
path.join(__dirname, '../src/services/mainSystemReportPushService.ts'),
'utf8'
);
const routeSrc = readFileSync(
path.join(__dirname, '../src/modules/workflow/routes/orderRoutes.ts'),
'utf8'
);
/** 取出 GET /orders/:orderCode/batch-reports 这一个 handler 的源码片段 */
function batchReportRouteBlock(): string {
const start = routeSrc.indexOf("router.get('/orders/:orderCode/batch-reports'");
expect(start, '路由必须存在').toBeGreaterThan(-1);
const end = routeSrc.indexOf('router.get(', start + 10);
return routeSrc.slice(start, end > start ? end : start + 2000);
}
describe('订单详情批次报告(我方侧可见性)', () => {
beforeEach(() => {
query.mockReset();
});
it('① 口径守卫:批次判据与推送侧同为「分拣选定 ∪ 实际出库流水」两来源', () => {
for (const src of [serviceSrc, pushSrc]) {
expect(src).toContain('outbound_batches');
expect(src).toContain("'picking_out'");
expect(src).toContain("'delivery_loading_out'");
expect(src).toContain('related_order_code');
}
// 实际出库流水必须两者都认(只看 picking_out 会漏发车兜底出库的单)
expect(serviceSrc).toMatch(/operation_type\s+IN\s*\(\s*'picking_out'\s*,\s*'delivery_loading_out'\s*\)/i);
expect(pushSrc).toMatch(/operation_type\s+IN\s*\(\s*'picking_out'\s*,\s*'delivery_loading_out'\s*\)/i);
});
it('② 只显示已人工确认的批次报告(status = 30)', () => {
expect(serviceSrc).toMatch(/r\.status\s*=\s*30/);
expect(serviceSrc).toContain('inventory_batch_report_matches');
expect(serviceSrc).toContain('inventory_batch_reports');
});
it('③ 路由只做鉴权 + 委派,且与既有质检报告列同权限集合', () => {
const block = batchReportRouteBlock();
expect(block).toContain('authMiddleware');
for (const perm of ['order_manage', 'order_entry', 'order_status_update', 'allocation_manage']) {
expect(block).toContain(`'${perm}'`);
}
expect(block).toContain('listOrderBatchReportFiles');
expect(block, '路由里不许写 SQL').not.toMatch(/SELECT\s+/i);
});
it('④ 空单号直接返回空,不打扰数据库', async () => {
await expect(listOrderBatchReportFiles(' ')).resolves.toEqual({ items: [] });
expect(query).not.toHaveBeenCalled();
});
it('⑤ 组装:按行 × 批次给出报告与签名文件(pdf/image 分类)', async () => {
query.mockResolvedValue({
rowCount: 2,
rows: [
{
our_order_item_id: '887',
sku_code: 'SKU62634064428',
item_name: '抄手皮',
batch_no: 'STK20261011HHFCUN',
report_id: '42',
report_no: 'HZ-2026-1011',
produced_at: '2026-10-08',
testing_org: '红河质安检验检测有限公司',
product_names: ['抄手皮'],
file_paths: ['抄手皮/2026/报告.pdf', '抄手皮/2026/照片.jpg', '', null],
},
],
});
const { items } = await listOrderBatchReportFiles('ORD_X');
expect(query).toHaveBeenCalledTimes(1);
expect(query.mock.calls[0][1]).toEqual(['ORD_X']);
expect(items).toHaveLength(1);
expect(items[0]).toMatchObject({
our_order_item_id: '887',
sku_code: 'SKU62634064428',
item_name: '抄手皮',
batch_no: 'STK20261011HHFCUN',
report_id: '42',
report_no: 'HZ-2026-1011',
produced_at: '2026-10-08',
testing_org: '红河质安检验检测有限公司',
product_names: ['抄手皮'],
});
// 空文件名被滤掉,剩下两个;URL 走 quality-reports 同一套签名桶
expect(items[0].files.map((f) => f.kind)).toEqual(['pdf', 'image']);
expect(items[0].files[0].url).toContain('/api/storage/quality-reports/public/');
expect(items[0].files[0].url).toContain('sig=');
});
it('⑥ 字段可空(无报告号/无检测机构/无产品名)不炸', async () => {
query.mockResolvedValue({
rowCount: 1,
rows: [
{
our_order_item_id: '1',
sku_code: null,
item_name: null,
batch_no: 'STK1',
report_id: '9',
report_no: null,
produced_at: null,
testing_org: null,
product_names: null,
file_paths: null,
},
],
});
const { items } = await listOrderBatchReportFiles('ORD_Y');
expect(items[0]).toMatchObject({
sku_code: null,
item_name: null,
report_no: null,
produced_at: null,
testing_org: null,
product_names: [],
files: [],
});
});
});