213 行
6.3 KiB
JavaScript
213 行
6.3 KiB
JavaScript
const Fastify = require("fastify");
|
|
|
|
const { AppError } = require("../../../src/utils/AppError");
|
|
const invoicePoolRoutes = require("../../../src/routes/invoicePool");
|
|
|
|
const INVOICE_ID = "11111111-1111-4111-8111-111111111111";
|
|
const OWNER_ID = "22222222-2222-4222-8222-222222222222";
|
|
|
|
function buildInvoice(overrides = {}) {
|
|
return {
|
|
id: INVOICE_ID,
|
|
status: "ocr_failed",
|
|
archived_reason: null,
|
|
archived_at: null,
|
|
owner_employee_id: OWNER_ID,
|
|
uploaded_by_employee_id: OWNER_ID,
|
|
original_name: "invoice.png",
|
|
mime_type: "image/png",
|
|
file_size: 1024,
|
|
file_sha256: "sha-1",
|
|
remarks: null,
|
|
ocr_type: "vat",
|
|
ocr_provider: "mock",
|
|
ocr_attempts: 1,
|
|
ocr_last_requested_at: new Date("2026-04-28T11:00:00.000Z"),
|
|
ocr_last_error: "missing_required_fields:invoiceCode,sellerName",
|
|
ocr_completed_at: new Date("2026-04-28T11:00:05.000Z"),
|
|
invoice_code: null,
|
|
invoice_number: "251F7425",
|
|
invoice_date: new Date("2026-02-08T00:00:00.000Z"),
|
|
seller_name: null,
|
|
buyer_name: "Mock Buyer",
|
|
amount: "107.69",
|
|
tax_amount: "400.71",
|
|
total_amount: "282.53",
|
|
linked_reimbursement_id: null,
|
|
linked_item_id: null,
|
|
travel_order_id: null,
|
|
is_duplicate_override: false,
|
|
override_ref_form_number: null,
|
|
override_reason: null,
|
|
overridden_by: null,
|
|
created_at: new Date("2026-04-28T11:00:00.000Z"),
|
|
updated_at: new Date("2026-04-28T11:00:05.000Z"),
|
|
verification_records: [],
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function buildQueryRawHandler(state) {
|
|
return vi.fn(async (strings) => {
|
|
const sql = Array.isArray(strings) ? strings.join("?") : String(strings);
|
|
if (sql.includes('FROM "invoice_pool"')) {
|
|
return state.invoice ? [{ id: state.invoice.id }] : [];
|
|
}
|
|
return [];
|
|
});
|
|
}
|
|
|
|
function buildPrisma(invoiceOverrides = {}) {
|
|
const state = {
|
|
invoice: buildInvoice(invoiceOverrides),
|
|
};
|
|
|
|
const prisma = {
|
|
invoicePool: {
|
|
findUnique: vi.fn(async () => state.invoice),
|
|
update: vi.fn(async ({ data }) => {
|
|
state.invoice = {
|
|
...state.invoice,
|
|
...data,
|
|
updated_at: new Date("2026-04-28T11:05:00.000Z"),
|
|
};
|
|
return state.invoice;
|
|
}),
|
|
},
|
|
auditLog: {
|
|
create: vi.fn().mockResolvedValue({ id: "audit-1" }),
|
|
},
|
|
$queryRaw: buildQueryRawHandler(state),
|
|
$transaction: vi.fn().mockImplementation(async (callback) => callback(prisma)),
|
|
};
|
|
|
|
return { prisma, state };
|
|
}
|
|
|
|
async function buildApp(invoiceOverrides = {}) {
|
|
const { prisma, state } = buildPrisma(invoiceOverrides);
|
|
const app = Fastify();
|
|
|
|
app.decorate("prisma", prisma);
|
|
app.decorate("config", {
|
|
storageDir: "/tmp",
|
|
appOrigin: "http://localhost:5173",
|
|
maxUploadSizeBytes: 10 * 1024 * 1024,
|
|
invoiceArchiveUnusedDays: 30,
|
|
ocrProvider: "mock",
|
|
});
|
|
app.decorate("authenticate", async (request) => {
|
|
if (!request.headers.authorization) {
|
|
throw new AppError({ code: "AUTH_001", message: "未登录", statusCode: 401 });
|
|
}
|
|
request.employee = {
|
|
id: OWNER_ID,
|
|
permissionCodes: ["invoice.upload"],
|
|
roleCodes: ["employee"],
|
|
};
|
|
});
|
|
app.decorate(
|
|
"hasPermission",
|
|
(employee, permissionCode) =>
|
|
(employee?.permissionCodes ?? []).includes("*") ||
|
|
(employee?.permissionCodes ?? []).includes(permissionCode),
|
|
);
|
|
app.decorate("requirePermission", (permissionCode) => async (request) => {
|
|
await app.authenticate(request);
|
|
if (!app.hasPermission(request.employee, permissionCode)) {
|
|
throw new AppError({ code: "PERM_001", message: "无权限", statusCode: 403 });
|
|
}
|
|
});
|
|
app.setErrorHandler((error, request, reply) => {
|
|
if (error instanceof AppError) {
|
|
return reply.status(error.statusCode).send({
|
|
requestId: request.id,
|
|
error: {
|
|
code: error.code,
|
|
message: error.message,
|
|
details: error.details ?? {},
|
|
},
|
|
});
|
|
}
|
|
|
|
return reply.status(500).send({
|
|
requestId: request.id,
|
|
error: { code: "SYS_001", message: "服务器内部错误", details: {} },
|
|
});
|
|
});
|
|
|
|
await app.register(invoicePoolRoutes, { prefix: "/api" });
|
|
return { app, prisma, state };
|
|
}
|
|
|
|
describe("PATCH /api/invoice-pool/:id", () => {
|
|
it("accepts valid invoiceDate and allows marking OCR-failed VAT invoice as available", async () => {
|
|
const { app, state } = await buildApp();
|
|
|
|
try {
|
|
const response = await app.inject({
|
|
method: "PATCH",
|
|
url: `/api/invoice-pool/${INVOICE_ID}`,
|
|
headers: { authorization: "Bearer test" },
|
|
payload: {
|
|
status: "available",
|
|
invoiceCode: "044003204111",
|
|
invoiceNumber: "251F7425",
|
|
invoiceDate: "2026-02-08",
|
|
sellerName: "北京悦海堂餐饮管理有限公司",
|
|
buyerName: "Mock Buyer",
|
|
amount: "107.69",
|
|
taxAmount: "400.71",
|
|
totalAmount: "282.53",
|
|
},
|
|
});
|
|
|
|
expect(response.statusCode).toBe(200);
|
|
expect(response.json().data).toMatchObject({
|
|
id: INVOICE_ID,
|
|
status: "available",
|
|
invoiceCode: "044003204111",
|
|
invoiceDate: "2026-02-08",
|
|
sellerName: "北京悦海堂餐饮管理有限公司",
|
|
});
|
|
expect(state.invoice.status).toBe("available");
|
|
expect(state.invoice.invoice_date.toISOString().slice(0, 10)).toBe("2026-02-08");
|
|
expect(state.invoice.ocr_last_error).toBeNull();
|
|
} finally {
|
|
await app.close();
|
|
}
|
|
});
|
|
|
|
it("returns missing OCR required fields when marking available without required data", async () => {
|
|
const { app } = await buildApp();
|
|
|
|
try {
|
|
const response = await app.inject({
|
|
method: "PATCH",
|
|
url: `/api/invoice-pool/${INVOICE_ID}`,
|
|
headers: { authorization: "Bearer test" },
|
|
payload: {
|
|
status: "available",
|
|
invoiceNumber: "251F7425",
|
|
invoiceDate: "2026-02-08",
|
|
buyerName: "Mock Buyer",
|
|
amount: "107.69",
|
|
taxAmount: "400.71",
|
|
totalAmount: "282.53",
|
|
},
|
|
});
|
|
|
|
expect(response.statusCode).toBe(409);
|
|
expect(response.json().error).toMatchObject({
|
|
code: "VALID_001",
|
|
details: {
|
|
ocr: "missing_required_fields",
|
|
missingFields: ["invoiceCode", "sellerName"],
|
|
},
|
|
});
|
|
} finally {
|
|
await app.close();
|
|
}
|
|
});
|
|
});
|