实现报销费用政策与票据明细门禁(08-29-reimbursement-expense-policy-invoice-guard)

这个提交包含在:
2026-08-29 23:47:22 +08:00
父节点 9e2aea5f70
当前提交 6e999d5bf9
共修改 34 个文件,包含 1458 行新增和 92 行删除
@@ -97,13 +97,22 @@ async function buildApp(options = {}) {
}),
},
invoicePool: {
count: vi.fn(async () => 1),
count: vi.fn(async ({ where } = {}) => (
where?.linked_item_id === null
? (options.unlinkedBoundInvoiceCount ?? 0)
: (options.boundInvoiceCount ?? 1)
)),
},
reimbursementItem: {
findFirst: vi.fn(async () => (options.travelItem ? { id: "travel-item" } : null)),
findMany: vi.fn(async () => options.reimbursementItems ?? [
findMany: vi.fn(async () => (options.reimbursementItems ?? [
{ id: "item-1", amount: reimbursement.total_amount },
]),
]).map((item) => ({
asset_prior_approval_basis: null,
expense_type: { code: "office_supplies", category: "office", requires_invoice: true },
invoice_pool_items: [{ id: "invoice-1", total_amount: item.amount }],
...item,
}))),
aggregate: vi.fn(async () => ({
_sum: { amount: options.authoritativeTotal ?? reimbursement.total_amount },
})),
@@ -169,6 +178,19 @@ async function buildApp(options = {}) {
auditLog: {
create: vi.fn(async () => ({ id: "audit-1" })),
},
featureFlag: {
findUnique: vi.fn(async () => options.invoiceGuardEnabled ? {
id: "88888888-8888-4888-8888-888888888888",
flag_key: "reimbursement_invoice_guard_v1",
override_mode: "force_on",
rollout_type: "global",
department_ids: [],
note: "test",
version: 1,
created_at: new Date(),
updated_at: new Date(),
} : null),
},
};
const prisma = {
@@ -194,6 +216,7 @@ async function buildApp(options = {}) {
budgetControlMode: "warn",
imNotifierEnabled: false,
emailEnabled: false,
reimbursementInvoiceGuardEnabled: options.invoiceGuardEnabled ?? false,
});
app.decorate("hasPermission", (employee, permissionCode) => {
const permissionCodes = employee?.permissionCodes ?? [];
@@ -306,6 +329,142 @@ describe("reimbursements routes expense standard integration", () => {
await app.close();
});
it("requires prior approval basis for assets items", async () => {
const { app, tx } = await buildApp({
reimbursementItems: [{
id: "asset-item",
amount: "2200.00",
expense_type: { code: "assets_office_electronics", category: "assets", requires_invoice: true },
}],
});
const response = await app.inject({ method: "POST", url: `/api/reimbursements/${REIMBURSEMENT_ID}/submit` });
expect(response.statusCode).toBe(400);
expect(response.json().error.details).toEqual({
items: "asset_prior_approval_basis_required",
itemIds: ["asset-item"],
});
expect(tx.workflowInstance.create).not.toHaveBeenCalled();
await app.close();
});
it("skips invoice amount checks for invoice-free allowance items when the guard is on", async () => {
resolveEntityPrecheckMock.mockResolvedValue(buildPrecheck());
const { app } = await buildApp({
invoiceGuardEnabled: true,
boundInvoiceCount: 0,
reimbursementItems: [{
id: "allowance-item",
amount: "180.00",
expense_type: { code: "travel_meal", category: "travel", requires_invoice: false },
invoice_pool_items: [],
}],
});
const response = await app.inject({ method: "POST", url: `/api/reimbursements/${REIMBURSEMENT_ID}/submit` });
expect(response.statusCode).toBe(200);
await app.close();
});
it("allows an invoice-free allowance-only reimbursement when the guard is off", async () => {
resolveEntityPrecheckMock.mockResolvedValue(buildPrecheck());
const { app } = await buildApp({
invoiceGuardEnabled: false,
boundInvoiceCount: 0,
reimbursementItems: [{
id: "allowance-item",
amount: "180.00",
expense_type: { code: "travel_meal", category: "travel", requires_invoice: false },
invoice_pool_items: [],
}],
});
const response = await app.inject({ method: "POST", url: `/api/reimbursements/${REIMBURSEMENT_ID}/submit` });
expect(response.statusCode).toBe(200);
await app.close();
});
it("keeps the legacy whole-form invoice requirement for mixed or required items when the guard is off", async () => {
const { app, tx } = await buildApp({
invoiceGuardEnabled: false,
boundInvoiceCount: 0,
reimbursementItems: [
{
id: "allowance-item",
amount: "180.00",
expense_type: { code: "travel_meal", category: "travel", requires_invoice: false },
invoice_pool_items: [],
},
{
id: "required-item",
amount: "100.00",
expense_type: { code: "office_supplies", category: "office", requires_invoice: true },
invoice_pool_items: [],
},
],
});
const response = await app.inject({ method: "POST", url: `/api/reimbursements/${REIMBURSEMENT_ID}/submit` });
expect(response.statusCode).toBe(400);
expect(response.json().error.details).toEqual({ invoices: "bound_invoice_required" });
expect(tx.workflowInstance.create).not.toHaveBeenCalled();
await app.close();
});
it("blocks required-invoice items without linked invoices when the guard is on", async () => {
const { app, tx } = await buildApp({
invoiceGuardEnabled: true,
reimbursementItems: [{ id: "missing-invoice", amount: "180.00", invoice_pool_items: [] }],
});
const response = await app.inject({ method: "POST", url: `/api/reimbursements/${REIMBURSEMENT_ID}/submit` });
expect(response.statusCode).toBe(400);
expect(response.json().error.details).toEqual(expect.objectContaining({
items: "invoice_item_guard_failed",
missingInvoiceItemIds: ["missing-invoice"],
}));
expect(tx.workflowInstance.create).not.toHaveBeenCalled();
await app.close();
});
it("blocks bound invoices that have no explicit item link when the guard is on", async () => {
const { app, tx } = await buildApp({
invoiceGuardEnabled: true,
unlinkedBoundInvoiceCount: 1,
});
const response = await app.inject({ method: "POST", url: `/api/reimbursements/${REIMBURSEMENT_ID}/submit` });
expect(response.statusCode).toBe(400);
expect(response.json().error.details).toEqual(expect.objectContaining({
items: "invoice_item_guard_failed",
unlinkedInvoiceCount: 1,
}));
expect(tx.workflowInstance.create).not.toHaveBeenCalled();
await app.close();
});
it("blocks declared amounts above the linked invoice total when the guard is on", async () => {
const { app, tx } = await buildApp({
invoiceGuardEnabled: true,
reimbursementItems: [{
id: "amount-over",
amount: "180.00",
invoice_pool_items: [{ id: "invoice-low", total_amount: "120.00" }],
}],
});
const response = await app.inject({ method: "POST", url: `/api/reimbursements/${REIMBURSEMENT_ID}/submit` });
expect(response.statusCode).toBe(400);
expect(response.json().error.details.amountExceededItems).toEqual([{
itemId: "amount-over",
declaredAmount: "180.00",
invoiceTotal: "120.00",
}]);
expect(tx.workflowInstance.create).not.toHaveBeenCalled();
await app.close();
});
it("recomputes a stale total before selecting the approval rule", async () => {
resolveEntityPrecheckMock.mockResolvedValue(buildPrecheck());
const { app, tx } = await buildApp({ authoritativeTotal: "320.00" });