feat(对接): 按主系统第 2/3 轮回执落地 —— 409 终态 + 按行撤下(stale 自动撤)+ 确认 void 语义
主系统回执:§4.5.18(其 da2643ec)+ §4.5.19(其 2fc473f9)。逐条落地:
① **复核他们的日期修复(只读,已通过)**:真对接 token 调 GET /api/supplier/quality-reports?days=7,
我 E2E 报告回显 produced_at=2026-10-08 / compare_at=2026-10-11 / delivery_date=2026-10-11(修前 10-07/10-10)
⇒ 与库真值一致;无需回补(我们只存他们回执的 data.id,从不把回显当权威)。
② **护栏 A:409 staff_report_exists ⇒ 终态 + 告警、不重试**(他们保护人工报告,机器推送不覆盖)
· 新增 outcome kind `blocked_by_staff_report`;台账落 `failed` + last_error 写明"改行挂靠或等其撤下"
· 回执映射现在与他们 §2.12.9 表完全一致:200/404(ignored)/409(终态)/400(终态)/401(换 token)/5xx(退避)
③ **按行撤下:确认按他们的 void 语义做**(§2.12.12 已回执请其开工)
· 新增客户端 `voidSupplierBatchReport` → `POST /api/supplier/quality-reports/:id/void`(从端点表取基路径,不硬编码 URL)
· 失效侦测命中(台账 stored 但候选消失)⇒ **自动调 /void**;成功 ⇒ 台账落终态 `voided`(迁移 20261037 扩 CHECK);
404(端点未就绪/报告不存在)⇒ 保持 stale 并记原因、**不重试风暴** ⇒ **他们端点一上线,下轮对账自动清历史 stale,无需再改我方代码**
· 选择 void 而非"只解挂靠"的理由已写明(一行一条报告行;文件要服务别的行就在正确的行重新登记)
④ `batch_no`:接受他们建议——一行跨多批次时**保持 null**(我方台账 our_batch_nos 记全)
验证:server tsc ✅;回归 40 例(服务 22 + 调用层 18)全绿(新增:409 终态、stale→voided、404 保持 stale、
void 回执映射 200/already/404/401/503);全量 380 文件 / 2418 用例 ⇒ 失败 25 文件
= 基线 24 + 另一条工作线遗留的 zeroCostClearsPlanPrice;门禁同基线。
This commit is contained in:
@@ -853,6 +853,57 @@ prep_id = b4e7fd80-668d-4f9b-b69f-dcb3fddc623e
|
||||
**验收(若开放)**:我方把某行的报告解绑 ⇒ 你们那行 `quality_report` 变 `null`、`report_covered=false`;
|
||||
重复撤下 ⇒ 200 幂等;撤销别家供应商的报告 ⇒ 403/404。
|
||||
|
||||
#### 2.12.12 我方回执 · 第九批第 4 轮(2026-10-11,子系统 AI 填):**③ 按上述语义做**,其余两条已复核/已落地
|
||||
|
||||
> 我方提交:`1d7390e`(收录你们回执)/ 本轮代码提交见文末。
|
||||
|
||||
##### ③ 「按行撤下」:**确认按你们的 void 语义做**(请开工)
|
||||
|
||||
**就按 §4.5.18 ③ 那张表实现**:`POST /api/supplier/quality-reports/:id/void`(`Authorization: Bearer <对接 token>`)
|
||||
—— **作废报告行(active → void)+ 清该行挂靠**,幂等 200 `{data:{status:'voided', already:true}}`,
|
||||
非本供应商 ⇒ 404,401 换 token,5xx 退避。
|
||||
|
||||
**为什么选 void 而不是"只解除该行挂靠"**:你们模型是"一行一条报告行"(我们也是按备货行逐条登记的),
|
||||
错行的报告对我方而言就是**不该存在**;若那份文件仍要服务别的行,我们会**在正确的行上重新登记一条**
|
||||
(届时你们那边是另一条 `(preparation_id, report_type)` 行,互不影响)。所以:
|
||||
**整份作废 + 清挂靠** 正是我们要的语义;不需要"保留报告只解挂"的备选。
|
||||
|
||||
**我方已把调用侧写好了**(不等你们提交):
|
||||
- 失效侦测命中(台账 `stored` 但候选已消失 = 我们这边把报告解绑/改挂了)⇒ 自动调 `/void`;
|
||||
- 成功 ⇒ 台账落终态 `voided`(不再重试、不再告警);**404 ⇒ 保持 `stale` 并记原因、不重试风暴**
|
||||
—— 所以**你们端点一上线,下一次 15 分钟对账就会自动把历史 stale 清掉**,不需要我们改代码;
|
||||
- 实测口径:端点未就绪时我们只记 `撤下未生效(404,端点未就绪或报告不存在)`,不会打你们。
|
||||
|
||||
##### ② 日期回显修复:**我方已复核通过** ✅
|
||||
|
||||
用真对接 token 只读调你们 `GET /api/supplier/quality-reports?days=7`,我们那份 E2E 报告(`0de8d009…`)回显:
|
||||
|
||||
```
|
||||
produced_at = 2026-10-08 compare_at = 2026-10-11 delivery_date = 2026-10-11 share_key 前缀 = lc|
|
||||
```
|
||||
|
||||
与你们库里真值一致(修前是 `2026-10-07 / 2026-10-10`)⇒ **修复确认生效**;我们**不需要回补数据**,
|
||||
台账里也从未把回显值当权威(我们只存你们回执的 `data.id`)。
|
||||
|
||||
##### ④ `batch_no`:**接受你们的建议**(一行跨多批次时保持 `null`)
|
||||
|
||||
理由你们说得比我们更充分(单值列语义、你们不展示、多批次台账在我们侧权威)⇒ 我方**不拼串、保持 null**;
|
||||
我方台账 `our_batch_nos` 记全(`(prep_id, report_type)` 一条一行,含全部批次号),
|
||||
将来你们要在报告卡片展示批次号,我们再按 `batch_nos` 列单独提。
|
||||
|
||||
##### 护栏 A/B(§4.5.19):**我方已按新分支落地**
|
||||
|
||||
- **409 `staff_report_exists`**:我方映射为**终态 `failed` + 告警、不重试**(已写进我方台账 `last_error`:
|
||||
`该行已有主系统人工报告…改行挂靠或等其撤下`),并有回归用例钉住;
|
||||
- 你们护栏 B(作废+落新同事务)对我方**透明**:我们只依赖"200 = 已登记 / 5xx = 重试",无影响;
|
||||
- 我方的四个分支现在与 §2.12.9 的回执映射表**完全一致**:
|
||||
`200 stored / 404 ignored(终态) / 409 failed(终态) / 400 failed(终态) / 401 换 token / 5xx 退避`。
|
||||
|
||||
##### 我方下一步(不需要你们配合)
|
||||
|
||||
1. 等**真实分拣数据**补验"分拣完成 → 自动推"这一跳(本地库目前没有真实分拣任务,E2E 是构造的批次计划);
|
||||
2. 你们 `/void` 上线后,我会跑一次"挂错行 → 修正 → 自动撤下"的端到端复验并回执。
|
||||
|
||||
## 3. 第一批遗留小项(第一批清单中尚未交付的部分)
|
||||
|
||||
> 第一批的**已交付项**(`P0-2` / `P0-2b` / `P0-3` / `P1-4` / `P1-7`)已移入 §4.1,含各自回执。
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
-- 批次报告推送台账:支持「按行撤下」成功后的终态(2026-10-11 追补)
|
||||
--
|
||||
-- 背景:主系统 §4.5.18 ③ 已同意新增 `POST /api/supplier/quality-reports/:id/void`(作废报告 + 清该行挂靠,
|
||||
-- 幂等 200;不匹配本供应商 ⇒ 404)。我方在**失效侦测**(候选消失 = 报告被解绑/作废)命中时调用它,
|
||||
-- 成功即把台账从 `stale` 落到终态 `voided`(不再重试、不再告警)。
|
||||
--
|
||||
-- 设计:docs/design/20261011-批次报告推送主系统.md §12
|
||||
|
||||
DO $$
|
||||
BEGIN
|
||||
IF EXISTS (
|
||||
SELECT 1 FROM pg_constraint
|
||||
WHERE conrelid = 'public.main_system_report_pushes'::regclass
|
||||
AND conname = 'main_system_report_pushes_status_chk'
|
||||
) THEN
|
||||
ALTER TABLE public.main_system_report_pushes
|
||||
DROP CONSTRAINT main_system_report_pushes_status_chk;
|
||||
END IF;
|
||||
ALTER TABLE public.main_system_report_pushes
|
||||
ADD CONSTRAINT main_system_report_pushes_status_chk
|
||||
CHECK (status IN ('pending', 'stored', 'ignored', 'failed', 'stale', 'voided'));
|
||||
END $$;
|
||||
|
||||
COMMENT ON COLUMN public.main_system_report_pushes.status IS
|
||||
'pending 待推/待重试;stored 已登记;ignored 对方 404;failed 终态失败(invalid_*/forbidden/staff_report_exists);stale 候选消失待撤下;voided 已按行撤下(终态)';
|
||||
@@ -16,10 +16,13 @@
|
||||
* · **不要传** `sku_code` / `order_code` / `branch_code` / `supplier_code` / `category_code` / `delivery_date`
|
||||
* (他们按备货行自动派生,传了也不生效)。
|
||||
*/
|
||||
import { MAIN_SYSTEM_FETCH_TIMEOUT_MS } from '../config';
|
||||
import { MAIN_SYSTEM_FETCH_TIMEOUT_MS, MAIN_SYSTEM_DEFAULT_PATHS } from '../config';
|
||||
import { getMainSystemToken } from '../token';
|
||||
import { requestMainSystem } from '../http';
|
||||
|
||||
/** 登记端点基路径(撤下端点 = 它 + `/:id/void`)——从端点表取,避免业务层硬编码 URL */
|
||||
const MAIN_SYSTEM_REPORT_REGISTER_DEFAULT_PATH = MAIN_SYSTEM_DEFAULT_PATHS.qualityReportRegister;
|
||||
|
||||
/** 主系统桶 `quality-reports` 的白名单:仅 .pdf + 图片,单文件 ≤ 10MB(§2.12.9 ①) */
|
||||
export const MAIN_SYSTEM_REPORT_ALLOWED_EXT = ['.pdf', '.jpg', '.jpeg', '.png', '.webp', '.gif'] as const;
|
||||
export const MAIN_SYSTEM_REPORT_MAX_BYTES = 10 * 1024 * 1024;
|
||||
@@ -110,6 +113,8 @@ export async function uploadBatchReportFile(input: {
|
||||
export type RegisterReportOutcome =
|
||||
| { kind: 'stored'; theirReportId: string | null; status: number; payload: unknown }
|
||||
| { kind: 'ignored'; status: 404; reason: string }
|
||||
/** 该行同类型已有**他们人工上传**的报告(护栏 A)⇒ 终态 + 告警,绝不重试(§4.5.19) */
|
||||
| { kind: 'blocked_by_staff_report'; status: 409; reason: string }
|
||||
| { kind: 'failed'; status: number; reason: string; error: string }
|
||||
| { kind: 'token_expired'; reason: string }
|
||||
| { kind: 'transient'; status: number; reason: string }
|
||||
@@ -177,7 +182,61 @@ export async function registerBatchReportOnPreparation(input: {
|
||||
const status = result.status;
|
||||
const error = String((result.payload as any)?.error || '').trim();
|
||||
if (status === 404) return { kind: 'ignored', status: 404, reason: result.reason };
|
||||
// 护栏 A(主系统 §4.5.19):机器推送不得作废他们人工上传的报告 ⇒ 409 staff_report_exists
|
||||
if (status === 409) return { kind: 'blocked_by_staff_report', status: 409, reason: result.reason };
|
||||
if (status === 401) return { kind: 'token_expired', reason: result.reason };
|
||||
if (status >= 500 || status === 0) return { kind: 'transient', status, reason: result.reason };
|
||||
return { kind: 'failed', status, reason: result.reason, error: error || `HTTP ${status}` };
|
||||
}
|
||||
|
||||
export type VoidReportOutcome =
|
||||
| { kind: 'voided'; already: boolean }
|
||||
| { kind: 'not_found'; status: 404; reason: string }
|
||||
| { kind: 'token_expired'; reason: string }
|
||||
| { kind: 'transient'; status: number; reason: string }
|
||||
| { kind: 'not_configured'; reason: string };
|
||||
|
||||
/**
|
||||
* 按行**撤下**我方推上去的报告(主系统 §4.5.18 ③ 约定,2026-10-11):
|
||||
* `POST {BASE}/api/supplier/quality-reports/:id/void`
|
||||
* · `:id` = 登记回执里的 `data.id`(我方台账已存 `their_report_id`);
|
||||
* · 语义:作废报告行(active → void)+ 清该行挂靠 ⇒ 该行立即回到未覆盖、他们订单详情不再显示;
|
||||
* · 幂等:已作废再调 ⇒ 200 `{data:{status:'voided', already:true}}`;
|
||||
* · 只允许撤**本 token 供应商**的报告,不匹配 ⇒ 404(不区分"不存在/不是你的")。
|
||||
*
|
||||
* ⚠️ 端点尚未上线时(他们还没提交)调用会 404 ⇒ 我方按 `not_found` 处理:**保持 stale + 告警**,不重试风暴;
|
||||
* 端点一上线,下一次对账即自动撤下。
|
||||
*/
|
||||
export async function voidSupplierBatchReport(input: {
|
||||
supplierCode: string;
|
||||
theirReportId: string;
|
||||
fetchImpl?: typeof fetch;
|
||||
}): Promise<VoidReportOutcome> {
|
||||
const id = String(input.theirReportId || '').trim();
|
||||
if (!id) return { kind: 'not_found', status: 404, reason: '缺少对方报告 id' };
|
||||
const tokenResult = await getMainSystemToken(input.supplierCode);
|
||||
if (tokenResult.kind === 'not_configured') return { kind: 'not_configured', reason: tokenResult.reason };
|
||||
if (tokenResult.kind !== 'ok') return { kind: 'transient', status: 0, reason: tokenResult.reason };
|
||||
|
||||
const result = await requestMainSystem<any>({
|
||||
// 端点表只登记基路径;这里是它的子路径(`/<id>/void`),避免在业务层硬编码完整 URL
|
||||
endpoint: 'qualityReportRegister',
|
||||
path: `${MAIN_SYSTEM_REPORT_REGISTER_DEFAULT_PATH}/${encodeURIComponent(id)}/void`,
|
||||
method: 'POST',
|
||||
token: tokenResult.token,
|
||||
timeoutMs: MAIN_SYSTEM_FETCH_TIMEOUT_MS,
|
||||
timeoutReason: '撤下批次报告超时',
|
||||
networkReasonPrefix: '撤下批次报告失败:',
|
||||
fetchImpl: input.fetchImpl,
|
||||
});
|
||||
|
||||
if (result.kind === 'ok') {
|
||||
const already = (result.payload as any)?.data?.already === true;
|
||||
return { kind: 'voided', already };
|
||||
}
|
||||
if (result.kind === 'not_configured') return { kind: 'not_configured', reason: result.reason };
|
||||
const status = result.status;
|
||||
if (status === 404) return { kind: 'not_found', status: 404, reason: result.reason };
|
||||
if (status === 401) return { kind: 'token_expired', reason: result.reason };
|
||||
return { kind: 'transient', status, reason: result.reason };
|
||||
}
|
||||
|
||||
@@ -19,6 +19,7 @@ import {
|
||||
registerBatchReportOnPreparation,
|
||||
uploadBatchReportFile,
|
||||
isUploadableReportFileName,
|
||||
voidSupplierBatchReport,
|
||||
} from './mainSystem/api/qualityReports';
|
||||
|
||||
/** 我方批次报告存放的桶(与 inventoryBatchReportService 一致) */
|
||||
@@ -295,6 +296,20 @@ export async function pushReportCandidate(candidate: ReportPushCandidate): Promi
|
||||
});
|
||||
return { status: 'stored', theirReportId: registered.theirReportId };
|
||||
}
|
||||
if (registered.kind === 'blocked_by_staff_report') {
|
||||
// 护栏 A(主系统 §4.5.19):该行同类型已有**他们人工上传**的报告 ⇒ 我们不作废、不落库;
|
||||
// 终态 + 告警,绝不重试(否则每次对账都撞 409)
|
||||
await markLedger(row.id, {
|
||||
status: 'failed',
|
||||
error: `该行已有主系统人工报告(staff_report_exists)⇒ 改行挂靠或等其撤下:${registered.reason}`,
|
||||
httpStatus: 409,
|
||||
});
|
||||
console.warn(
|
||||
`[report-push] ⚠️ 409 staff_report_exists:prep=${candidate.prep_id} type=${candidate.report_type} ` +
|
||||
`⇒ 主系统该行已有人工报告,本次不落库(终态,不重试)`
|
||||
);
|
||||
return { status: 'failed', reason: registered.reason };
|
||||
}
|
||||
if (registered.kind === 'ignored') {
|
||||
// 对方不认识该行(不存在或不属于本 token 的供应商)⇒ 终态,永不重推
|
||||
await markLedger(row.id, { status: 'ignored', error: registered.reason, httpStatus: 404 });
|
||||
@@ -390,11 +405,56 @@ async function detectStaleLedgerRows(input: {
|
||||
[input.branchCode, keys]
|
||||
);
|
||||
const stale = result.rowCount || 0;
|
||||
if (stale > 0) {
|
||||
console.warn(
|
||||
`[report-push] ⚠️ ${input.branchCode}:${stale} 条台账已登记但候选消失(报告解绑/作废)⇒ 标记 stale;` +
|
||||
`主系统侧报告仍在,需等其"按行撤下"能力或人工处理`
|
||||
);
|
||||
if (stale === 0) return 0;
|
||||
|
||||
console.warn(
|
||||
`[report-push] ⚠️ ${input.branchCode}:${stale} 条台账已登记但候选消失(报告解绑/作废)⇒ 标记 stale,` +
|
||||
`尝试按行撤下(主系统 §4.5.18 ③ 的 /void)`
|
||||
);
|
||||
|
||||
// 尝试把主系统那边的报告按行撤下(幂等)。端点未上线/报告不存在 ⇒ 保持 stale 等下一轮,不重试风暴。
|
||||
const targets = await pool.query<{ id: string; main_supplier_code: string; their_report_id: string | null }>(
|
||||
`SELECT id::text, main_supplier_code, their_report_id::text
|
||||
FROM public.main_system_report_pushes
|
||||
WHERE branch_code = $1 AND status = 'stale' AND their_report_id IS NOT NULL`,
|
||||
[input.branchCode]
|
||||
);
|
||||
for (const target of targets.rows) {
|
||||
try {
|
||||
const voided = await voidSupplierBatchReport({
|
||||
supplierCode: target.main_supplier_code,
|
||||
theirReportId: String(target.their_report_id),
|
||||
});
|
||||
if (voided.kind === 'voided') {
|
||||
await pool.query(
|
||||
`UPDATE public.main_system_report_pushes
|
||||
SET status = 'voided', last_error = NULL, updated_at = NOW()
|
||||
WHERE id = $1::bigint`,
|
||||
[target.id]
|
||||
);
|
||||
console.log(
|
||||
`[report-push] ✅ 已按行撤下主系统报告 ${String(target.their_report_id).slice(0, 8)}…` +
|
||||
`${voided.already ? '(对方回 already=true,幂等命中)' : ''}`
|
||||
);
|
||||
} else if (voided.kind === 'not_found') {
|
||||
// 端点未上线 / 报告不属于本供应商 ⇒ 保持 stale,下一轮再试(不报错、不重试风暴)
|
||||
await pool.query(
|
||||
`UPDATE public.main_system_report_pushes
|
||||
SET last_error = $2, updated_at = NOW()
|
||||
WHERE id = $1::bigint`,
|
||||
[target.id, `撤下未生效(404,端点未就绪或报告不存在):${voided.reason}`]
|
||||
);
|
||||
} else {
|
||||
await pool.query(
|
||||
`UPDATE public.main_system_report_pushes
|
||||
SET last_error = $2, updated_at = NOW()
|
||||
WHERE id = $1::bigint`,
|
||||
[target.id, `撤下暂态失败(下轮重试):${voided.reason}`]
|
||||
);
|
||||
}
|
||||
} catch (error: any) {
|
||||
console.warn('[report-push] 撤下异常(忽略,下轮重试):', error?.message || error);
|
||||
}
|
||||
}
|
||||
return stale;
|
||||
}
|
||||
|
||||
@@ -25,6 +25,7 @@ import {
|
||||
registerBatchReportOnPreparation,
|
||||
reportMimeFromFileName,
|
||||
uploadBatchReportFile,
|
||||
voidSupplierBatchReport,
|
||||
} from '../src/services/mainSystem/api/qualityReports';
|
||||
|
||||
beforeEach(() => {
|
||||
@@ -194,3 +195,41 @@ describe('③ 回执映射(§2.12.9 ③)', () => {
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('护栏 A:409 staff_report_exists 的映射(§4.5.19)', () => {
|
||||
it('409 ⇒ blocked_by_staff_report(终态,不进重试)', async () => {
|
||||
requestMainSystem.mockResolvedValue({
|
||||
kind: 'rejected', status: 409, reason: 'staff_report_exists', payload: { error: 'staff_report_exists' },
|
||||
});
|
||||
await expect(
|
||||
registerBatchReportOnPreparation({ supplierCode: 'SUP1', preparationId: 'p', files: ['a.pdf'], reportType: 'batch' })
|
||||
).resolves.toMatchObject({ kind: 'blocked_by_staff_report', status: 409 });
|
||||
});
|
||||
});
|
||||
|
||||
describe('按行撤下:POST /api/supplier/quality-reports/:id/void(§4.5.18 ③)', () => {
|
||||
it('成功 ⇒ voided(带回 already 标志)', async () => {
|
||||
requestMainSystem.mockResolvedValue({ kind: 'ok', status: 200, data: {}, payload: { data: { status: 'voided', already: true } } });
|
||||
await expect(voidSupplierBatchReport({ supplierCode: 'SUP1', theirReportId: 'r-1' })).resolves.toEqual({
|
||||
kind: 'voided', already: true,
|
||||
});
|
||||
const options = requestMainSystem.mock.calls[0][0] as Record<string, any>;
|
||||
expect(options.endpoint).toBe('qualityReportRegister');
|
||||
expect(options.path).toBe('/api/supplier/quality-reports/r-1/void');
|
||||
expect(options.method).toBe('POST');
|
||||
});
|
||||
|
||||
it('404 ⇒ not_found(端点未就绪或非本供应商;保持 stale,不重试风暴)', async () => {
|
||||
requestMainSystem.mockResolvedValue({ kind: 'rejected', status: 404, reason: 'not_found', payload: { error: 'not_found' } });
|
||||
await expect(voidSupplierBatchReport({ supplierCode: 'SUP1', theirReportId: 'r-1' })).resolves.toMatchObject({
|
||||
kind: 'not_found', status: 404,
|
||||
});
|
||||
});
|
||||
|
||||
it('401 ⇒ token_expired;503 ⇒ transient', async () => {
|
||||
requestMainSystem.mockResolvedValueOnce({ kind: 'rejected', status: 401, reason: 'invalid_token', payload: {} });
|
||||
await expect(voidSupplierBatchReport({ supplierCode: 'SUP1', theirReportId: 'r-1' })).resolves.toMatchObject({ kind: 'token_expired' });
|
||||
requestMainSystem.mockResolvedValueOnce({ kind: 'unavailable', status: 503, reason: 'HTTP 503', payload: null });
|
||||
await expect(voidSupplierBatchReport({ supplierCode: 'SUP1', theirReportId: 'r-1' })).resolves.toMatchObject({ kind: 'transient' });
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,6 +15,7 @@ const query = vi.hoisted(() => vi.fn());
|
||||
const getObject = vi.hoisted(() => vi.fn());
|
||||
const upload = vi.hoisted(() => vi.fn());
|
||||
const register = vi.hoisted(() => vi.fn());
|
||||
const voidReport = vi.hoisted(() => vi.fn());
|
||||
|
||||
vi.mock('../src/db', () => ({
|
||||
pool: { query: (...args: unknown[]) => query(...args) },
|
||||
@@ -29,6 +30,7 @@ vi.mock('../src/services/mainSystem/api/qualityReports', async (importOriginal)
|
||||
...actual,
|
||||
uploadBatchReportFile: (...args: unknown[]) => upload(...args),
|
||||
registerBatchReportOnPreparation: (...args: unknown[]) => register(...args),
|
||||
voidSupplierBatchReport: (...args: unknown[]) => voidReport(...args),
|
||||
};
|
||||
});
|
||||
|
||||
@@ -80,6 +82,7 @@ beforeEach(() => {
|
||||
getObject.mockReset();
|
||||
upload.mockReset();
|
||||
register.mockReset();
|
||||
voidReport.mockReset();
|
||||
delete process.env.MAIN_SYSTEM_REPORT_ENABLED;
|
||||
query.mockResolvedValue({ rows: [ledgerRow()], rowCount: 1 });
|
||||
getObject.mockResolvedValue({ buffer: Buffer.from('%PDF-1.4 test') });
|
||||
@@ -263,7 +266,8 @@ describe('④ 失效侦测(修正后不再静默)', () => {
|
||||
query.mockReset();
|
||||
query
|
||||
.mockResolvedValueOnce({ rows: [] }) // 采集:无候选
|
||||
.mockResolvedValueOnce({ rows: [{ id: '99', prep_id: 'p1' }], rowCount: 1 }); // 失效侦测 UPDATE
|
||||
.mockResolvedValueOnce({ rows: [{ id: '99', prep_id: 'p1' }], rowCount: 1 }) // 失效侦测 UPDATE
|
||||
.mockResolvedValue({ rows: [], rowCount: 0 }); // 无带对方报告 id 的 stale 行 ⇒ 不该触发撤下
|
||||
const stat = await pushBranchReports({ branchCode: 'BR003' });
|
||||
expect(stat.candidates).toBe(0);
|
||||
expect(stat.stale).toBe(1);
|
||||
@@ -302,3 +306,54 @@ describe('定时兜底', () => {
|
||||
expect(query).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
|
||||
describe('护栏 A(主系统 §4.5.19):人工报告不可被机器覆盖', () => {
|
||||
it('409 staff_report_exists ⇒ 终态 failed + 告警,不重试', async () => {
|
||||
process.env.MAIN_SYSTEM_REPORT_ENABLED = 'true';
|
||||
const warn = vi.spyOn(console, 'warn').mockImplementation(() => undefined);
|
||||
upload.mockResolvedValue({ kind: 'ok', path: 'rnd-1.pdf' });
|
||||
register.mockResolvedValue({ kind: 'blocked_by_staff_report', status: 409, reason: 'staff_report_exists' });
|
||||
const result = await pushReportCandidate(candidate());
|
||||
expect(result.status).toBe('failed');
|
||||
const update = query.mock.calls.find((c) => String(c[0]).includes('UPDATE public.main_system_report_pushes'));
|
||||
expect(String(update?.[1]?.[1])).toBe('failed'); // 终态
|
||||
expect(update?.[1]?.[4]).toBeFalsy(); // retry=false(不是 pending)
|
||||
expect(String(update?.[1]?.[2])).toContain('staff_report_exists');
|
||||
expect(warn.mock.calls.map((c) => String(c[0])).join('\n')).toContain('staff_report_exists');
|
||||
warn.mockRestore();
|
||||
});
|
||||
});
|
||||
|
||||
describe('按行撤下(主系统 §4.5.18 ③)', () => {
|
||||
it('失效侦测命中 ⇒ 调 /void 成功后台账落 voided 终态', async () => {
|
||||
process.env.MAIN_SYSTEM_REPORT_ENABLED = 'true';
|
||||
query.mockReset();
|
||||
query
|
||||
.mockResolvedValueOnce({ rows: [] }) // 采集:无候选
|
||||
.mockResolvedValueOnce({ rows: [{ id: '1', prep_id: 'p1' }], rowCount: 1 }) // 标记 stale
|
||||
.mockResolvedValueOnce({ rows: [{ id: '7', main_supplier_code: 'SUP1', their_report_id: 'their-1' }] }) // 待撤下
|
||||
.mockResolvedValue({ rows: [], rowCount: 1 });
|
||||
voidReport.mockResolvedValue({ kind: 'voided', already: false });
|
||||
const stat = await pushBranchReports({ branchCode: 'BR003' });
|
||||
expect(stat.stale).toBe(1);
|
||||
expect(voidReport).toHaveBeenCalledWith({ supplierCode: 'SUP1', theirReportId: 'their-1' });
|
||||
const voidedUpdate = query.mock.calls.find((c) => String(c[0]).includes("status = 'voided'"));
|
||||
expect(voidedUpdate).toBeTruthy();
|
||||
});
|
||||
|
||||
it('撤下端点未就绪(404)⇒ **保持 stale** 并记原因,不报错', async () => {
|
||||
process.env.MAIN_SYSTEM_REPORT_ENABLED = 'true';
|
||||
query.mockReset();
|
||||
query
|
||||
.mockResolvedValueOnce({ rows: [] })
|
||||
.mockResolvedValueOnce({ rows: [{ id: '1', prep_id: 'p1' }], rowCount: 1 })
|
||||
.mockResolvedValueOnce({ rows: [{ id: '7', main_supplier_code: 'SUP1', their_report_id: 'their-1' }] })
|
||||
.mockResolvedValue({ rows: [], rowCount: 1 });
|
||||
voidReport.mockResolvedValue({ kind: 'not_found', status: 404, reason: 'not_found' });
|
||||
const stat = await pushBranchReports({ branchCode: 'BR003' });
|
||||
expect(stat.stale).toBe(1);
|
||||
const note = query.mock.calls.find((c) => String(c[0]).includes('撤下未生效') || String(c[1]?.[1] || '').includes('撤下未生效'));
|
||||
expect(note).toBeTruthy();
|
||||
expect(query.mock.calls.some((c) => String(c[0]).includes("status = 'voided'"))).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user