汇总已落盘的借款本金与还款、兼容历史编号的周期化编号、付款责任与回单核验、报销进度和排序、PWA/采购事务、错误原因与日期校验及ERP退役。 统一共享预算池锁序和锁后判断,恢复差旅原预算选择,补原因码固定夹具及反向完整性检查。 纳入配套迁移、测试、受管QA、分层验收合同、规约及任务记录;原始证据留在ignored artifacts。 关联任务:09-17-company-loan-principal-repayment、09-17-erp-exclusive-retirement、09-18-reimbursement-periodic-numbering、09-08-reimbursement-payment-closeout、09-18-local-concurrency-product-fixes、09-19-budget-pool-concurrency、09-05-error-explainability-payment-readiness。 预算补审由Claude定向验证2文件928项通过,原4446/28及r6结果仍仅对应各自历史输入;本轮未重跑测试。 完整新候选门禁、R1真实入口接入与12项公网义务仍开放。生产编号未启用、差旅仍冻结;不推送、不部署,不宣称发布资格。
281 行
9.9 KiB
JavaScript
281 行
9.9 KiB
JavaScript
const Fastify = require("fastify");
|
|
|
|
const payrollRoutes = require("../../../src/routes/payroll");
|
|
const { AppError } = require("../../../src/utils/AppError");
|
|
|
|
const ADMIN_ID = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
|
const EMPLOYEE_ID = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb";
|
|
|
|
function buildEmployee(overrides = {}) {
|
|
return {
|
|
id: EMPLOYEE_ID,
|
|
employee_no: "E001",
|
|
name: "Alice",
|
|
title: "专员",
|
|
status: "active",
|
|
department: { id: "dept-1", name: "行政部" },
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function buildSalaryItem(overrides = {}) {
|
|
return {
|
|
id: "11111111-1111-4111-8111-111111111111",
|
|
name: "基本工资",
|
|
item_type: "earning",
|
|
sort_order: 10,
|
|
enabled: true,
|
|
created_at: new Date("2026-03-29T08:00:00.000Z"),
|
|
created_by: ADMIN_ID,
|
|
updated_at: new Date("2026-03-29T08:00:00.000Z"),
|
|
updated_by: ADMIN_ID,
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
function buildSalaryRow(overrides = {}) {
|
|
return {
|
|
id: "salary-1",
|
|
employee_id: EMPLOYEE_ID,
|
|
salary_item_id: "11111111-1111-4111-8111-111111111111",
|
|
monthly_amount: "10000.00",
|
|
effective_date: new Date("2026-01-01T00:00:00.000Z"),
|
|
created_at: new Date("2026-01-01T00:00:00.000Z"),
|
|
created_by: ADMIN_ID,
|
|
updated_at: new Date("2026-01-01T00:00:00.000Z"),
|
|
updated_by: ADMIN_ID,
|
|
salary_item: buildSalaryItem(),
|
|
...overrides,
|
|
};
|
|
}
|
|
|
|
async function buildApp({
|
|
actor = { id: ADMIN_ID, permissionCodes: ["payroll.manage"] },
|
|
} = {}) {
|
|
const app = Fastify();
|
|
const state = {
|
|
employee: buildEmployee(),
|
|
salaryItems: [
|
|
buildSalaryItem(),
|
|
buildSalaryItem({
|
|
id: "22222222-2222-4222-8222-222222222222",
|
|
name: "社保扣除",
|
|
item_type: "deduction",
|
|
sort_order: 20,
|
|
}),
|
|
],
|
|
salaryRows: [buildSalaryRow()],
|
|
};
|
|
|
|
function attachSalaryItem(row) {
|
|
return {
|
|
...row,
|
|
salary_item: state.salaryItems.find((item) => item.id === row.salary_item_id),
|
|
};
|
|
}
|
|
|
|
const employeeSalary = {
|
|
findMany: vi.fn(async ({ where }) =>
|
|
state.salaryRows
|
|
.filter((item) => item.employee_id === where.employee_id)
|
|
.map(attachSalaryItem),
|
|
),
|
|
deleteMany: vi.fn(async ({ where }) => {
|
|
const effectiveDate = where.effective_date.toISOString().slice(0, 10);
|
|
state.salaryRows = state.salaryRows.filter(
|
|
(item) => !(item.employee_id === where.employee_id && item.effective_date.toISOString().slice(0, 10) === effectiveDate),
|
|
);
|
|
return { count: 1 };
|
|
}),
|
|
createMany: vi.fn(async ({ data }) => {
|
|
state.salaryRows.push(
|
|
...data.map((item, index) => ({
|
|
id: `salary-${state.salaryRows.length + index + 1}`,
|
|
employee_id: item.employee_id,
|
|
salary_item_id: item.salary_item_id,
|
|
monthly_amount: item.monthly_amount,
|
|
effective_date: item.effective_date,
|
|
created_at: new Date("2026-03-29T08:00:00.000Z"),
|
|
created_by: item.created_by,
|
|
updated_at: new Date("2026-03-29T08:00:00.000Z"),
|
|
updated_by: item.updated_by,
|
|
})),
|
|
);
|
|
return { count: data.length };
|
|
}),
|
|
};
|
|
|
|
app.decorate("authenticate", async (request) => {
|
|
request.employee = actor;
|
|
});
|
|
app.decorate("requirePermission", (permissionCode) => async (request) => {
|
|
request.employee = actor;
|
|
const codes = actor.permissionCodes ?? [];
|
|
if (!codes.includes("*") && !codes.includes(permissionCode)) {
|
|
throw new AppError({ code: "PERM_001", message: "无权限", statusCode: 403 });
|
|
}
|
|
});
|
|
app.decorate("hasPermission", (employee, permissionCode) => {
|
|
const codes = employee.permissionCodes ?? [];
|
|
return codes.includes("*") || codes.includes(permissionCode);
|
|
});
|
|
app.decorate("prisma", {
|
|
employee: {
|
|
findUnique: vi.fn(async ({ where }) => (where.id === state.employee.id ? state.employee : null)),
|
|
},
|
|
salaryItem: {
|
|
findMany: vi.fn(async ({ where }) => state.salaryItems.filter((item) => where.id.in.includes(item.id))),
|
|
findUnique: vi.fn(async ({ where }) => state.salaryItems.find((item) => item.id === where.id) ?? null),
|
|
},
|
|
employeeSalary,
|
|
auditLog: {
|
|
create: vi.fn().mockResolvedValue({}),
|
|
},
|
|
$transaction: vi.fn(async (callback) => callback(app.prisma)),
|
|
});
|
|
app.setErrorHandler((error, request, reply) => {
|
|
reply.status(error.statusCode || 500).send({
|
|
requestId: request.id,
|
|
error: {
|
|
code: error.code || "SYS_001",
|
|
message: error.message,
|
|
details: error.details ?? null,
|
|
},
|
|
});
|
|
});
|
|
|
|
await app.register(payrollRoutes, { prefix: "/api" });
|
|
return { app, state, employeeSalary };
|
|
}
|
|
|
|
describe("payroll salary profile routes", () => {
|
|
it("replaces salary snapshots for the same effective date and preserves history", async () => {
|
|
const { app, state, employeeSalary } = await buildApp();
|
|
|
|
const putResponse = await app.inject({
|
|
method: "PUT",
|
|
url: `/api/payroll/employees/${EMPLOYEE_ID}/profile`,
|
|
payload: {
|
|
effectiveDate: "2026-04-01",
|
|
items: [
|
|
{
|
|
salaryItemId: state.salaryItems[0].id,
|
|
monthlyAmount: 12000,
|
|
},
|
|
{
|
|
salaryItemId: state.salaryItems[1].id,
|
|
monthlyAmount: 800,
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
expect(putResponse.statusCode).toBe(200);
|
|
expect(putResponse.json().data.snapshots[0]).toMatchObject({
|
|
effectiveDate: "2026-04-01",
|
|
items: [
|
|
{ salaryItemId: state.salaryItems[0].id, monthlyAmount: 12000 },
|
|
{ salaryItemId: state.salaryItems[1].id, monthlyAmount: 800 },
|
|
],
|
|
});
|
|
expect(putResponse.json().data.snapshots[1]).toMatchObject({
|
|
effectiveDate: "2026-01-01",
|
|
});
|
|
|
|
const replaceResponse = await app.inject({
|
|
method: "PUT",
|
|
url: `/api/payroll/employees/${EMPLOYEE_ID}/profile`,
|
|
payload: {
|
|
effectiveDate: "2026-04-01",
|
|
items: [
|
|
{
|
|
salaryItemId: state.salaryItems[0].id,
|
|
monthlyAmount: 12500,
|
|
},
|
|
],
|
|
},
|
|
});
|
|
|
|
expect(replaceResponse.statusCode).toBe(200);
|
|
expect(replaceResponse.json().data.snapshots[0]).toMatchObject({
|
|
effectiveDate: "2026-04-01",
|
|
items: [{ salaryItemId: state.salaryItems[0].id, monthlyAmount: 12500 }],
|
|
});
|
|
expect(replaceResponse.json().data.snapshots).toHaveLength(2);
|
|
|
|
const getResponse = await app.inject({
|
|
method: "GET",
|
|
url: `/api/payroll/employees/${EMPLOYEE_ID}/profile`,
|
|
});
|
|
|
|
expect(getResponse.statusCode).toBe(200);
|
|
expect(getResponse.json().data.snapshots).toHaveLength(2);
|
|
expect(employeeSalary.deleteMany).toHaveBeenCalledTimes(2);
|
|
expect(employeeSalary.createMany).toHaveBeenCalledTimes(2);
|
|
|
|
await app.close();
|
|
});
|
|
|
|
it("rejects non-admin users", async () => {
|
|
const { app } = await buildApp({
|
|
actor: {
|
|
id: EMPLOYEE_ID,
|
|
permissionCodes: ["employee"],
|
|
},
|
|
});
|
|
|
|
const response = await app.inject({
|
|
method: "GET",
|
|
url: `/api/payroll/employees/${EMPLOYEE_ID}/profile`,
|
|
});
|
|
|
|
expect(response.statusCode).toBe(403);
|
|
|
|
await app.close();
|
|
});
|
|
});
|
|
|
|
describe("salary profile calendar boundary", () => {
|
|
it.each(["2024-02-29", "2026-04-30"])("stores exact effectiveDate %s", async (effectiveDate) => {
|
|
const { app, state, employeeSalary } = await buildApp();
|
|
try {
|
|
const response = await app.inject({ method: "PUT", url: `/api/payroll/employees/${EMPLOYEE_ID}/profile`, payload: { effectiveDate, items: [{ salaryItemId: state.salaryItems[0].id, monthlyAmount: "100.00" }] } });
|
|
expect(response.statusCode).toBe(200);
|
|
expect(employeeSalary.createMany.mock.calls[0][0].data[0].effective_date.toISOString()).toBe(`${effectiveDate}T00:00:00.000Z`);
|
|
} finally { await app.close(); }
|
|
});
|
|
});
|
|
|
|
describe("salary profile reasons", () => {
|
|
it.each([
|
|
[{ effectiveDate: "2026-02-30", items: [] }, "payroll.profile_input_invalid"],
|
|
[{ effectiveDate: "2026-04-01", items: [{ salaryItemId: "11111111-1111-4111-8111-111111111111", monthlyAmount: "1.234" }] }, "payroll.amount_invalid"],
|
|
[{ effectiveDate: "2026-04-01", items: [{ salaryItemId: "11111111-1111-4111-8111-111111111111", monthlyAmount: -1 }] }, "payroll.amount_invalid"],
|
|
])("explains invalid profile %j", async (payload, reasonCode) => {
|
|
const { app } = await buildApp();
|
|
try {
|
|
const response = await app.inject({ method: "PUT", url: `/api/payroll/employees/${EMPLOYEE_ID}/profile`, payload });
|
|
expect(response.statusCode).toBe(400);
|
|
expect(response.json().error.details.reasonCode).toBe(reasonCode);
|
|
if (reasonCode === "payroll.amount_invalid") expect(response.json().error.details).toEqual({ items: { 0: { monthlyAmount: "invalid_money_amount" } }, reasonCode });
|
|
expect(app.prisma.employee.findUnique).not.toHaveBeenCalled();
|
|
expect(app.prisma.employeeSalary.deleteMany).not.toHaveBeenCalled();
|
|
expect(app.prisma.employeeSalary.createMany).not.toHaveBeenCalled();
|
|
expect(app.prisma.auditLog.create).not.toHaveBeenCalled();
|
|
expect(app.prisma.$transaction).not.toHaveBeenCalled();
|
|
} finally { await app.close(); }
|
|
});
|
|
it("explains unavailable salary items before replacing a snapshot", async () => {
|
|
const { app } = await buildApp();
|
|
app.prisma.salaryItem.findMany.mockResolvedValueOnce([]);
|
|
try {
|
|
const response = await app.inject({ method: "PUT", url: `/api/payroll/employees/${EMPLOYEE_ID}/profile`, payload: { effectiveDate: "2026-04-01", items: [{ salaryItemId: "11111111-1111-4111-8111-111111111111", monthlyAmount: 100 }] } });
|
|
expect(response.statusCode).toBe(400);
|
|
expect(response.json().error.details).toEqual({ items: "salary_item_not_found", reasonCode: "payroll.item_not_found" });
|
|
expect(app.prisma.$transaction).not.toHaveBeenCalled();
|
|
expect(app.prisma.employeeSalary.deleteMany).not.toHaveBeenCalled();
|
|
expect(app.prisma.auditLog.create).not.toHaveBeenCalled();
|
|
} finally { await app.close(); }
|
|
});
|
|
});
|