R111/A3-A6 强化发票查重和状态治理
这个提交包含在:
@@ -24,9 +24,28 @@ function buildQueryRawHandler(state) {
|
||||
});
|
||||
}
|
||||
|
||||
function buildPrisma({ invoice, travelOrder, reimbursement = null }) {
|
||||
function buildPrisma({
|
||||
invoice,
|
||||
travelOrder,
|
||||
reimbursement = null,
|
||||
reimbursementItems = [{ id: "item-1", amount: "500.00" }],
|
||||
linkedItem = { id: "item-1" },
|
||||
expenseType = { id: "expense-type-1" },
|
||||
}) {
|
||||
const state = {
|
||||
invoice: { ...invoice },
|
||||
invoice: {
|
||||
owner_employee_id: "actor-1",
|
||||
uploaded_by_employee_id: "actor-1",
|
||||
file_sha256: "sha256-1",
|
||||
invoice_code: null,
|
||||
invoice_number: null,
|
||||
invoice_date: new Date("2026-05-08T00:00:00.000Z"),
|
||||
seller_name: "测试商户",
|
||||
original_name: "invoice.pdf",
|
||||
archived_reason: null,
|
||||
is_duplicate_override: false,
|
||||
...invoice,
|
||||
},
|
||||
travelOrder: travelOrder ? { ...travelOrder } : null,
|
||||
reimbursement,
|
||||
};
|
||||
@@ -34,6 +53,7 @@ function buildPrisma({ invoice, travelOrder, reimbursement = null }) {
|
||||
const prisma = {
|
||||
invoicePool: {
|
||||
findUnique: vi.fn().mockImplementation(async () => state.invoice),
|
||||
findMany: vi.fn().mockResolvedValue([]),
|
||||
update: vi.fn().mockImplementation(async ({ data, select }) => {
|
||||
state.invoice = { ...state.invoice, ...data };
|
||||
if (!select) {
|
||||
@@ -46,7 +66,16 @@ function buildPrisma({ invoice, travelOrder, reimbursement = null }) {
|
||||
}),
|
||||
},
|
||||
reimbursementItem: {
|
||||
findFirst: vi.fn().mockResolvedValue({ id: "item-1" }),
|
||||
findFirst: vi.fn().mockResolvedValue(linkedItem),
|
||||
findMany: vi.fn().mockResolvedValue(reimbursementItems),
|
||||
create: vi.fn().mockResolvedValue({ id: "item-auto-1" }),
|
||||
aggregate: vi.fn().mockResolvedValue({ _sum: { amount: state.invoice.total_amount ?? "0.00" } }),
|
||||
},
|
||||
reimbursementForm: {
|
||||
update: vi.fn().mockResolvedValue({ id: reimbursement?.id ?? "reim-1" }),
|
||||
},
|
||||
expenseType: {
|
||||
findFirst: vi.fn().mockResolvedValue(expenseType),
|
||||
},
|
||||
auditLog: {
|
||||
create: vi.fn().mockResolvedValue({ id: "audit-1" }),
|
||||
@@ -104,6 +133,101 @@ async function buildApp({ prisma, permissionCodes = ["travel.payment.manage"] })
|
||||
}
|
||||
|
||||
describe("invoicePool travel-order bind routes", () => {
|
||||
it("adds invoice to reimbursement and auto-creates an item when no detail matches", async () => {
|
||||
const reimbursementId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
||||
const { prisma, state } = buildPrisma({
|
||||
invoice: {
|
||||
id: "11111111-1111-4111-8111-111111111111",
|
||||
status: "available",
|
||||
linked_reimbursement_id: null,
|
||||
linked_item_id: null,
|
||||
travel_order_id: null,
|
||||
total_amount: "1683.00",
|
||||
},
|
||||
travelOrder: null,
|
||||
reimbursement: {
|
||||
id: reimbursementId,
|
||||
owner_id: "actor-1",
|
||||
submitter_id: "actor-1",
|
||||
status: "draft",
|
||||
reimbursement_date: new Date("2026-05-08T00:00:00.000Z"),
|
||||
},
|
||||
reimbursementItems: [],
|
||||
});
|
||||
const app = await buildApp({ prisma, permissionCodes: ["invoice.upload"] });
|
||||
|
||||
const response = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/invoice-pool/11111111-1111-4111-8111-111111111111/bind",
|
||||
headers: { authorization: "Bearer test" },
|
||||
payload: { reimbursementId },
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.json().data.linkedReimbursementId).toBe(reimbursementId);
|
||||
expect(response.json().data.linkedItemId).toBe("item-auto-1");
|
||||
expect(state.invoice.linked_item_id).toBe("item-auto-1");
|
||||
expect(prisma.reimbursementItem.create).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
data: expect.objectContaining({
|
||||
reimbursement_id: reimbursementId,
|
||||
expense_type_id: "expense-type-1",
|
||||
amount: "1683.00",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(prisma.reimbursementForm.update).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: { id: reimbursementId },
|
||||
data: expect.objectContaining({
|
||||
total_amount: "1683.00",
|
||||
updated_by: "actor-1",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("rejects reimbursement binding when linked item does not belong to the form", async () => {
|
||||
const reimbursementId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
||||
const { prisma } = buildPrisma({
|
||||
invoice: {
|
||||
id: "11111111-1111-4111-8111-111111111111",
|
||||
status: "available",
|
||||
linked_reimbursement_id: null,
|
||||
linked_item_id: null,
|
||||
travel_order_id: null,
|
||||
total_amount: "1683.00",
|
||||
},
|
||||
travelOrder: null,
|
||||
reimbursement: {
|
||||
id: reimbursementId,
|
||||
owner_id: "actor-1",
|
||||
submitter_id: "actor-1",
|
||||
status: "draft",
|
||||
reimbursement_date: new Date("2026-05-08T00:00:00.000Z"),
|
||||
},
|
||||
linkedItem: null,
|
||||
});
|
||||
const app = await buildApp({ prisma, permissionCodes: ["invoice.upload"] });
|
||||
|
||||
const response = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/invoice-pool/11111111-1111-4111-8111-111111111111/bind",
|
||||
headers: { authorization: "Bearer test" },
|
||||
payload: {
|
||||
reimbursementId,
|
||||
linkedItemId: "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb",
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(400);
|
||||
expect(response.json().error.details).toEqual({ linkedItemId: "item_not_in_reimbursement" });
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("binds invoice to confirmed travel order", async () => {
|
||||
const { prisma, state } = buildPrisma({
|
||||
invoice: {
|
||||
|
||||
在新工单中引用
屏蔽一个用户