fix(approval): isolate runs by corp, handle execution error, bundle hose-aipro alias, and bump to v0.7.1
This commit is contained in:
1 parent
77bd125ef3
commit
9fa4cfe38f
15 files changed
+156
-62
No files matched your search
@@ -2,6 +2,7 @@ import { Database } from "bun:sqlite";
|
||||
import { afterEach, beforeEach, describe, expect, test } from "bun:test";
|
||||
import { closeApprovalDatabase, getApprovalDatabase } from "./db.js";
|
||||
import { ApprovalRepository } from "./repository.js";
|
||||
import { ApprovalService } from "./service.js";
|
||||
import { ApprovalRuleValidationError } from "./types.js";
|
||||
|
||||
describe("Approval Domain - SQLite 持久化与规约仓储", () => {
|
||||
@@ -248,5 +249,51 @@ describe("Approval Domain - SQLite 持久化与规约仓储", () => {
|
||||
expect(logs[0]!.action).toBe("decide");
|
||||
expect(logs[0]!.status).toBe("BLOCKED");
|
||||
});
|
||||
});
|
||||
|
||||
test("审批运行按当前企业隔离相同单号", () => {
|
||||
const corpARun = repo.createOrResetApprovalRun({
|
||||
documentSn: "SAME-SN",
|
||||
corpId: "corp-a",
|
||||
decision: "PENDING",
|
||||
});
|
||||
repo.injectFindings(corpARun.id, [
|
||||
{ clauseText: "企业 A 要点", strictness: "硬性", checkStatus: "PENDING" },
|
||||
]);
|
||||
|
||||
const corpBRun = repo.createOrResetApprovalRun({
|
||||
documentSn: "SAME-SN",
|
||||
corpId: "corp-b",
|
||||
decision: "PENDING",
|
||||
});
|
||||
expect(corpBRun.id).not.toBe(corpARun.id);
|
||||
expect(repo.getAuditChecklist("SAME-SN", "corp-a").findings[0]?.clauseText).toBe("企业 A 要点");
|
||||
expect(repo.getAuditChecklist("SAME-SN", "corp-b").findings).toEqual([]);
|
||||
});
|
||||
|
||||
test("下游审批执行失败时将运行标记为 ERROR", async () => {
|
||||
const run = repo.createOrResetApprovalRun({
|
||||
documentSn: "EXEC-FAIL",
|
||||
corpId: "corp-a",
|
||||
decision: "PENDING",
|
||||
});
|
||||
repo.injectFindings(run.id, [
|
||||
{ clauseText: "已核验", strictness: "硬性", checkStatus: "PASS" },
|
||||
]);
|
||||
|
||||
const service = new ApprovalService(repo);
|
||||
const client = {
|
||||
post: async () => {
|
||||
throw new Error("downstream unavailable");
|
||||
},
|
||||
} as any;
|
||||
|
||||
await expect(
|
||||
service.decideAudit(client, "corp-a", "EXEC-FAIL", {
|
||||
apply: true,
|
||||
requestedAction: "approve",
|
||||
}),
|
||||
).rejects.toThrow("downstream unavailable");
|
||||
|
||||
expect(repo.getAuditChecklist("EXEC-FAIL", "corp-a").run?.decision).toBe("ERROR");
|
||||
});
|
||||
});
|
||||
@@ -1,5 +1,5 @@
|
||||
import { Database } from "bun:sqlite";
|
||||
import { existsSync, mkdirSync } from "node:fs";
|
||||
import { chmodSync, existsSync, mkdirSync } from "node:fs";
|
||||
import { homedir } from "node:os";
|
||||
import { dirname, join } from "node:path";
|
||||
|
||||
@@ -40,6 +40,11 @@ export function getApprovalDatabase(customPath?: string): Database {
|
||||
}
|
||||
|
||||
const db = new Database(targetPath);
|
||||
if (targetPath !== ":memory:") {
|
||||
// 规则、单据快照、审核证据和审计日志都可能包含敏感信息。
|
||||
// SQLite 默认文件权限受 umask 影响,不能依赖默认值保护凭证与业务数据。
|
||||
chmodSync(targetPath, 0o600);
|
||||
}
|
||||
currentDbPath = targetPath;
|
||||
activeDb = db;
|
||||
|
||||
|
||||
@@ -288,7 +288,7 @@ export class ApprovalRepository {
|
||||
// ===================== 审批运行与要点清单 (Runs & Findings) =====================
|
||||
|
||||
public createOrResetApprovalRun(input: CreateApprovalRunInput): ApprovalRunRecord {
|
||||
const existing = this.getLatestRunBySn(input.documentSn);
|
||||
const existing = this.getLatestRunBySn(input.documentSn, input.corpId ?? undefined);
|
||||
const now = Date.now();
|
||||
if (existing) {
|
||||
// 清理旧要点,重置运行记录
|
||||
@@ -339,10 +339,15 @@ export class ApprovalRepository {
|
||||
return this.getRunById(id)!;
|
||||
}
|
||||
|
||||
public getLatestRunBySn(documentSn: string): ApprovalRunRecord | null {
|
||||
const row = this.db
|
||||
.query("SELECT * FROM approval_runs WHERE document_sn = ? ORDER BY created_at DESC LIMIT 1")
|
||||
.get(documentSn.trim()) as any;
|
||||
public getLatestRunBySn(documentSn: string, corpId?: string): ApprovalRunRecord | null {
|
||||
const params: string[] = [documentSn.trim()];
|
||||
let sql = "SELECT * FROM approval_runs WHERE document_sn = ?";
|
||||
if (corpId !== undefined) {
|
||||
sql += " AND corp_id = ?";
|
||||
params.push(corpId);
|
||||
}
|
||||
sql += " ORDER BY created_at DESC LIMIT 1";
|
||||
const row = this.db.query(sql).get(...params) as any;
|
||||
return row ? this.mapRunRow(row) : null;
|
||||
}
|
||||
|
||||
@@ -464,11 +469,11 @@ export class ApprovalRepository {
|
||||
return this.mapFindingRow(updated);
|
||||
}
|
||||
|
||||
public getAuditChecklist(documentSn: string): {
|
||||
public getAuditChecklist(documentSn: string, corpId?: string): {
|
||||
run: ApprovalRunRecord | null;
|
||||
findings: ApprovalFindingRecord[];
|
||||
} {
|
||||
const run = this.getLatestRunBySn(documentSn);
|
||||
const run = this.getLatestRunBySn(documentSn, corpId);
|
||||
if (!run) {
|
||||
return { run: null, findings: [] };
|
||||
}
|
||||
@@ -632,4 +637,3 @@ export interface CreateAiProLogInput {
|
||||
summary?: string | null;
|
||||
}
|
||||
|
||||
|
||||
@@ -146,8 +146,8 @@ export class ApprovalService {
|
||||
/**
|
||||
* 查看单据当前审核清单状态
|
||||
*/
|
||||
public getAuditChecklist(documentSn: string) {
|
||||
return this.repo.getAuditChecklist(documentSn);
|
||||
public getAuditChecklist(documentSn: string, corpId?: string) {
|
||||
return this.repo.getAuditChecklist(documentSn, corpId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -161,9 +161,10 @@ export class ApprovalService {
|
||||
status: FindingCheckStatus;
|
||||
evidence?: string;
|
||||
reasoning?: string;
|
||||
}>
|
||||
}>,
|
||||
corpId?: string,
|
||||
): ApprovalFindingRecord[] {
|
||||
const { run, findings } = this.repo.getAuditChecklist(documentSn);
|
||||
const { run, findings } = this.repo.getAuditChecklist(documentSn, corpId);
|
||||
if (!run || findings.length === 0) {
|
||||
throw new ApprovalGatewayBlockedError(
|
||||
`[审批链路阻断] 单据 [${documentSn}] 尚未注入任何审核要点清单(要点列表为空)。\n` +
|
||||
@@ -203,7 +204,7 @@ export class ApprovalService {
|
||||
documentSn: string,
|
||||
options: DecideAuditOptions = {}
|
||||
): Promise<DecideAuditResult> {
|
||||
const { run, findings } = this.repo.getAuditChecklist(documentSn);
|
||||
const { run, findings } = this.repo.getAuditChecklist(documentSn, corpId);
|
||||
|
||||
// ── 核心门禁 1:如果审核要点列表为空,审批链路物理不通! ────────────────────
|
||||
if (!run || findings.length === 0) {
|
||||
@@ -293,24 +294,34 @@ export class ApprovalService {
|
||||
let appliedAction = options.requestedAction;
|
||||
let actionResult: unknown = undefined;
|
||||
|
||||
// 5. 如果开启了 --apply,由网关在内部安全受控地调用下游接口
|
||||
// 5. 如果开启了 --apply,由网关在内部安全受控地调用下游接口。
|
||||
// 远端失败时把本地运行标记为 ERROR,避免数据库显示“已决策”但实际未执行。
|
||||
if (options.apply) {
|
||||
if (!appliedAction) {
|
||||
appliedAction = decision === "REJECT" ? "reject" : decision === "APPROVE" || decision === "PASS_WITH_NOTE" ? "approve" : "comment";
|
||||
}
|
||||
|
||||
if (appliedAction === "reject") {
|
||||
actionResult = await rejectBill(client, corpId, documentSn, {
|
||||
comment: decisionSummary,
|
||||
});
|
||||
} else if (appliedAction === "approve") {
|
||||
actionResult = await approveBill(client, corpId, documentSn, {
|
||||
comment: decisionSummary,
|
||||
});
|
||||
} else {
|
||||
actionResult = await commentBill(client, corpId, documentSn, {
|
||||
comment: decisionSummary,
|
||||
try {
|
||||
if (appliedAction === "reject") {
|
||||
actionResult = await rejectBill(client, corpId, documentSn, {
|
||||
comment: decisionSummary,
|
||||
});
|
||||
} else if (appliedAction === "approve") {
|
||||
actionResult = await approveBill(client, corpId, documentSn, {
|
||||
comment: decisionSummary,
|
||||
});
|
||||
} else {
|
||||
actionResult = await commentBill(client, corpId, documentSn, {
|
||||
comment: decisionSummary,
|
||||
});
|
||||
}
|
||||
} catch (error) {
|
||||
const message = error instanceof Error ? error.message : String(error);
|
||||
this.repo.updateApprovalRun(run.id, {
|
||||
decision: "ERROR",
|
||||
decisionSummary: `${decisionSummary}\n[下游执行失败] ${message}`,
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user