R111/A3-A6 强化发票查重和状态治理

这个提交包含在:
2026-05-08 10:05:27 +08:00
父节点 77fbfb5eb8
当前提交 0ab0740d1a
共修改 8 个文件,包含 888 行新增和 53 行删除
@@ -36,17 +36,29 @@ function buildMultipartRequest({ filename, contentType, content, fields = [] })
};
}
async function buildApp({ ocrProvider = "off" } = {}) {
async function buildApp({
ocrProvider = "off",
invoiceConflicts = [],
statusRows = [],
permissionCodes = ["invoice.upload"],
} = {}) {
const tempDir = await fs.mkdtemp(path.join(os.tmpdir(), "hyb-oa-r71-invoice-upload-"));
const invoicePoolCreate = vi.fn(async ({ data }) => ({
id: "11111111-1111-4111-8111-111111111111",
status: data.status,
file_sha256: data.file_sha256,
}));
const invoicePoolFindMany = vi.fn(async (args) => {
if (args?.where?.id?.in) {
return statusRows;
}
return invoiceConflicts;
});
const auditCreate = vi.fn().mockResolvedValue({ id: "audit-1" });
const prisma = {
invoicePool: {
findMany: invoicePoolFindMany,
create: invoicePoolCreate,
},
auditLog: {
@@ -71,7 +83,7 @@ async function buildApp({ ocrProvider = "off" } = {}) {
request.employee = {
id: "actor-1",
roleCodes: ["employee"],
permissionCodes: ["invoice.upload"],
permissionCodes,
};
});
app.decorate("hasPermission", (actor, permissionCode) => {
@@ -207,4 +219,179 @@ describe("invoicePool upload route", () => {
await fs.rm(tempDir, { recursive: true, force: true });
}
});
it("reuses same-owner existing invoice instead of creating a duplicate file record", async () => {
const existingInvoiceId = "22222222-2222-4222-8222-222222222222";
const { app, prisma, tempDir } = await buildApp({
invoiceConflicts: [
{
id: existingInvoiceId,
owner_employee_id: "actor-1",
uploaded_by_employee_id: "actor-1",
status: "available",
archived_reason: null,
linked_reimbursement_id: null,
linked_item_id: null,
travel_order_id: null,
invoice_code: null,
invoice_number: null,
file_sha256: "unused-in-test",
linked_reimbursement: null,
},
],
});
try {
const requestPayload = buildMultipartRequest({
filename: "same.pdf",
contentType: "application/pdf",
content: "%PDF-1.4 same invoice payload",
fields: [["ocr_type", "vat"]],
});
const response = await app.inject({
method: "POST",
url: "/api/invoice-pool/upload",
headers: {
authorization: "Bearer test",
...requestPayload.headers,
},
payload: requestPayload.payload,
});
expect(response.statusCode).toBe(200);
expect(response.json().data).toEqual(
expect.objectContaining({
invoicePoolId: existingInvoiceId,
status: "available",
uploadAction: "reuse_existing",
unavailableReason: "duplicate_available_in_company",
}),
);
expect(prisma.invoicePool.create).not.toHaveBeenCalled();
expect(prisma.auditLog.create).not.toHaveBeenCalled();
} finally {
await app.close();
await fs.rm(tempDir, { recursive: true, force: true });
}
});
it("blocks upload when the same company invoice has already been used", async () => {
const { app, prisma, tempDir } = await buildApp({
invoiceConflicts: [
{
id: "33333333-3333-4333-8333-333333333333",
owner_employee_id: "actor-1",
uploaded_by_employee_id: "actor-1",
status: "bound",
archived_reason: null,
linked_reimbursement_id: "44444444-4444-4444-8444-444444444444",
linked_item_id: "55555555-5555-4555-8555-555555555555",
travel_order_id: null,
invoice_code: null,
invoice_number: null,
file_sha256: "unused-in-test",
linked_reimbursement: {
form_number: "BX20260508001",
status: "reviewing",
owner_id: "actor-1",
submitter_id: "actor-1",
},
},
],
});
try {
const requestPayload = buildMultipartRequest({
filename: "used.pdf",
contentType: "application/pdf",
content: "%PDF-1.4 used invoice payload",
fields: [["ocr_type", "vat"]],
});
const response = await app.inject({
method: "POST",
url: "/api/invoice-pool/upload",
headers: {
authorization: "Bearer test",
...requestPayload.headers,
},
payload: requestPayload.payload,
});
expect(response.statusCode).toBe(200);
expect(response.json().data).toEqual(
expect.objectContaining({
invoicePoolId: null,
status: "bound",
uploadAction: "blocked",
unavailableReason: "used_by_company",
unavailableMessage: "该发票已用于报销,不能重复添加",
}),
);
expect(prisma.invoicePool.create).not.toHaveBeenCalled();
expect(prisma.auditLog.create).not.toHaveBeenCalled();
} finally {
await app.close();
await fs.rm(tempDir, { recursive: true, force: true });
}
});
it("returns readable invoice statuses for upload polling", async () => {
const invoiceId = "66666666-6666-4666-8666-666666666666";
const { app, tempDir } = await buildApp({
permissionCodes: ["invoice.view_own"],
statusRows: [
{
id: invoiceId,
status: "archived",
archived_reason: "used_complete",
owner_employee_id: "actor-1",
uploaded_by_employee_id: "actor-1",
linked_reimbursement_id: "77777777-7777-4777-8777-777777777777",
linked_item_id: null,
travel_order_id: null,
invoice_code: "044001900111",
invoice_number: "12345678",
invoice_date: new Date("2026-05-08T00:00:00.000Z"),
seller_name: "北京测试有限公司",
total_amount: "1683.00",
original_name: "invoice.pdf",
ocr_attempts: 1,
ocr_last_error: null,
ocr_completed_at: new Date("2026-05-08T01:00:00.000Z"),
updated_at: new Date("2026-05-08T01:01:00.000Z"),
linked_reimbursement: {
owner_id: "actor-1",
submitter_id: "actor-1",
},
},
],
});
try {
const response = await app.inject({
method: "GET",
url: `/api/invoice-pool/status?ids=${invoiceId}`,
headers: { authorization: "Bearer test" },
});
expect(response.statusCode).toBe(200);
expect(response.json().data).toEqual({
items: [
expect.objectContaining({
id: invoiceId,
status: "archived",
reason: "used_by_company",
message: "该发票已用于报销,不能重复添加",
totalAmount: "1683.00",
}),
],
missingIds: [],
});
} finally {
await app.close();
await fs.rm(tempDir, { recursive: true, force: true });
}
});
});