R5-M1-M5 收口正式审批规则与移动反馈
这个提交包含在:
@@ -4,6 +4,7 @@ const workflowRoutes = require("../../../src/routes/workflow");
|
||||
const { AppError } = require("../../../src/utils/AppError");
|
||||
|
||||
const ACTOR_ID = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
||||
const OWNER_ID = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb";
|
||||
|
||||
function buildTask(taskId, overrides = {}) {
|
||||
return {
|
||||
@@ -112,7 +113,14 @@ async function buildApp(options = {}) {
|
||||
},
|
||||
reimbursementForm: {
|
||||
update: vi.fn(async () => ({})),
|
||||
findUnique: vi.fn(async () => null),
|
||||
findUnique: vi.fn(async () => ({
|
||||
id: "reimbursement-1",
|
||||
form_number: "BX-TEST",
|
||||
total_amount: "100.00",
|
||||
owner_id: options.reimbursementOwnerId ?? OWNER_ID,
|
||||
submitter_id: options.reimbursementSubmitterId ?? options.reimbursementOwnerId ?? OWNER_ID,
|
||||
owner: { id: options.reimbursementOwnerId ?? OWNER_ID, name: "申请人" },
|
||||
})),
|
||||
},
|
||||
leaveRequest: {
|
||||
findUnique: vi.fn(async () => null),
|
||||
@@ -180,6 +188,30 @@ async function buildApp(options = {}) {
|
||||
}
|
||||
|
||||
describe("workflow batch routes", () => {
|
||||
it("rejects self approval on a single task action", async () => {
|
||||
const { app, state } = await buildApp({
|
||||
reimbursementOwnerId: ACTOR_ID,
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/workflow/tasks/11111111-1111-4111-8111-111111111111/approve",
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(409);
|
||||
expect(response.json().error).toEqual(
|
||||
expect.objectContaining({
|
||||
code: "VALID_001",
|
||||
details: expect.objectContaining({
|
||||
workflow: "self_approval_not_allowed",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(state.tasks.get("11111111-1111-4111-8111-111111111111").status).toBe("pending");
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("approves reimbursement tasks and skips already processed ones", async () => {
|
||||
const { app, prisma, state } = await buildApp();
|
||||
|
||||
@@ -311,6 +343,40 @@ describe("workflow batch routes", () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("rejects self approval in batch actions without changing the task", async () => {
|
||||
const { app, state } = await buildApp({
|
||||
reimbursementOwnerId: ACTOR_ID,
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: "POST",
|
||||
url: "/api/workflow/batch-approve",
|
||||
payload: {
|
||||
taskIds: ["11111111-1111-4111-8111-111111111111"],
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.json().data).toEqual({
|
||||
results: [
|
||||
{
|
||||
taskId: "11111111-1111-4111-8111-111111111111",
|
||||
outcome: "error",
|
||||
reason: "self_approval_not_allowed",
|
||||
},
|
||||
],
|
||||
summary: {
|
||||
approved: 0,
|
||||
rejected: 0,
|
||||
skipped: 0,
|
||||
errored: 1,
|
||||
},
|
||||
});
|
||||
expect(state.tasks.get("11111111-1111-4111-8111-111111111111").status).toBe("pending");
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("rejects oversized batch payloads and missing permissions", async () => {
|
||||
const { app } = await buildApp();
|
||||
const oversizedPayload = {
|
||||
|
||||
@@ -3,6 +3,8 @@ const Fastify = require("fastify");
|
||||
const { AppError } = require("../../../src/utils/AppError");
|
||||
const workflowRoutes = require("../../../src/routes/workflow");
|
||||
|
||||
const APPLICANT_ID = "55555555-5555-4555-8555-555555555555";
|
||||
|
||||
function buildTaskRow() {
|
||||
return {
|
||||
id: "11111111-1111-4111-8111-111111111111",
|
||||
@@ -51,6 +53,39 @@ function buildTx({ entityType, targetRoleCode = "manager", transferCount = 0 })
|
||||
employee: {
|
||||
findUnique: vi.fn().mockResolvedValue(buildTarget(targetRoleCode)),
|
||||
},
|
||||
reimbursementForm: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "33333333-3333-4333-8333-333333333333",
|
||||
owner_id: APPLICANT_ID,
|
||||
submitter_id: APPLICANT_ID,
|
||||
owner: { id: APPLICANT_ID, name: "申请人" },
|
||||
}),
|
||||
},
|
||||
leaveRequest: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "33333333-3333-4333-8333-333333333333",
|
||||
owner_id: APPLICANT_ID,
|
||||
owner: { id: APPLICANT_ID, name: "申请人" },
|
||||
}),
|
||||
},
|
||||
purchaseRequest: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "33333333-3333-4333-8333-333333333333",
|
||||
request_no: "CG-TEST",
|
||||
applicant_id: APPLICANT_ID,
|
||||
submitter_id: APPLICANT_ID,
|
||||
applicant: { id: APPLICANT_ID, name: "申请人" },
|
||||
}),
|
||||
},
|
||||
travelRequest: {
|
||||
findUnique: vi.fn().mockResolvedValue({
|
||||
id: "33333333-3333-4333-8333-333333333333",
|
||||
request_no: "CL-TEST",
|
||||
applicant_employee_id: APPLICANT_ID,
|
||||
submitter_id: APPLICANT_ID,
|
||||
applicant: { id: APPLICANT_ID, name: "申请人" },
|
||||
}),
|
||||
},
|
||||
workflowTask: {
|
||||
update: vi.fn().mockResolvedValue({ id: "11111111-1111-4111-8111-111111111111" }),
|
||||
},
|
||||
@@ -132,7 +167,7 @@ describe("workflow transfer route", () => {
|
||||
prisma: buildPrisma(tx),
|
||||
employee: {
|
||||
id: "actor-1",
|
||||
roleCodes: [],
|
||||
roleCodes: ["manager"],
|
||||
permissionCodes: ["expense.transfer"],
|
||||
},
|
||||
});
|
||||
@@ -188,7 +223,7 @@ describe("workflow transfer route", () => {
|
||||
prisma: buildPrisma(tx),
|
||||
employee: {
|
||||
id: "actor-1",
|
||||
roleCodes: [],
|
||||
roleCodes: ["manager"],
|
||||
permissionCodes: ["expense.transfer"],
|
||||
},
|
||||
});
|
||||
@@ -217,13 +252,80 @@ describe("workflow transfer route", () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("does not treat admin-only target as a business approver", async () => {
|
||||
const tx = buildTx({ entityType: "reimbursement", targetRoleCode: "admin" });
|
||||
const app = await buildApp({
|
||||
prisma: buildPrisma(tx),
|
||||
employee: {
|
||||
id: "actor-1",
|
||||
roleCodes: ["manager"],
|
||||
permissionCodes: ["expense.transfer"],
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: "POST",
|
||||
url: `/api/workflow/tasks/${buildTaskRow().id}/transfer`,
|
||||
headers: { authorization: "Bearer test" },
|
||||
payload: {
|
||||
toApproverId: "44444444-4444-4444-8444-444444444444",
|
||||
reason: "当前事项需转交对应审批人继续处理,请及时跟进。",
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(409);
|
||||
expect(response.json().error.details).toEqual(
|
||||
expect.objectContaining({
|
||||
toApproverId: "target_not_approver",
|
||||
}),
|
||||
);
|
||||
expect(tx.workflowTask.update).not.toHaveBeenCalled();
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("rejects transfer to the applicant", async () => {
|
||||
const tx = buildTx({ entityType: "leave", targetRoleCode: "manager" });
|
||||
const app = await buildApp({
|
||||
prisma: buildPrisma(tx),
|
||||
employee: {
|
||||
id: "actor-1",
|
||||
roleCodes: ["manager"],
|
||||
permissionCodes: ["expense.transfer"],
|
||||
},
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: "POST",
|
||||
url: `/api/workflow/tasks/${buildTaskRow().id}/transfer`,
|
||||
headers: { authorization: "Bearer test" },
|
||||
payload: {
|
||||
toApproverId: APPLICANT_ID,
|
||||
reason: "当前事项需转交对应审批人继续处理,请及时跟进。",
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(409);
|
||||
expect(response.json().error).toEqual(
|
||||
expect.objectContaining({
|
||||
code: "VALID_001",
|
||||
details: expect.objectContaining({
|
||||
workflow: "self_approval_not_allowed",
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(tx.workflowTask.update).not.toHaveBeenCalled();
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("returns WORKFLOW_003 when transfer chain exceeds limit", async () => {
|
||||
const tx = buildTx({ entityType: "leave", transferCount: 3 });
|
||||
const app = await buildApp({
|
||||
prisma: buildPrisma(tx),
|
||||
employee: {
|
||||
id: "actor-1",
|
||||
roleCodes: [],
|
||||
roleCodes: ["manager"],
|
||||
permissionCodes: ["expense.transfer"],
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1,53 +1,132 @@
|
||||
|
||||
const workflowUtils = require("../../../src/utils/workflow");
|
||||
|
||||
describe("Workflow Utils", () => {
|
||||
describe("parseWorkflowSteps", () => {
|
||||
it("throws error for invalid steps array", () => {
|
||||
expect(() => workflowUtils.parseWorkflowSteps({})).toThrow(/参数校验失败/);
|
||||
expect(() => workflowUtils.parseWorkflowSteps({ steps: [] })).toThrow(/参数校验失败/);
|
||||
});
|
||||
const { resolveActionableStep } = workflowUtils;
|
||||
|
||||
it("parses valid steps array", () => {
|
||||
expect(workflowUtils.parseWorkflowSteps({ steps: [{ id: 1 }] })).toEqual([{ id: 1 }]);
|
||||
});
|
||||
const OWNER_ID = "11111111-1111-4111-8111-111111111111";
|
||||
|
||||
function buildSteps() {
|
||||
return [
|
||||
{
|
||||
id: "approval_1_manager",
|
||||
name: "主管审批",
|
||||
type: "approval",
|
||||
assignee_rule: { type: "department_manager" },
|
||||
},
|
||||
{
|
||||
id: "approval_2_finance",
|
||||
name: "财务复核",
|
||||
type: "approval",
|
||||
assignee_rule: { type: "role", value: "finance" },
|
||||
},
|
||||
];
|
||||
}
|
||||
|
||||
describe("workflow assignee resolution", () => {
|
||||
describe("parseWorkflowSteps", () => {
|
||||
it("throws error for invalid steps array", () => {
|
||||
expect(() => workflowUtils.parseWorkflowSteps({})).toThrow(/参数校验失败/);
|
||||
expect(() => workflowUtils.parseWorkflowSteps({ steps: [] })).toThrow(/参数校验失败/);
|
||||
});
|
||||
|
||||
describe("buildWorkflowStepsFromApprovalRule", () => {
|
||||
it("builds steps correctly for valid rule", () => {
|
||||
const result = workflowUtils.buildWorkflowStepsFromApprovalRule({
|
||||
level1_role: "manager",
|
||||
level2_role: "finance",
|
||||
});
|
||||
expect(result.length).toBe(2);
|
||||
expect(result[0].assignee_rule.type).toBe("department_manager");
|
||||
expect(result[1].assignee_rule.type).toBe("role");
|
||||
expect(result[1].assignee_rule.value).toBe("finance");
|
||||
});
|
||||
it("parses valid steps array", () => {
|
||||
expect(workflowUtils.parseWorkflowSteps({ steps: [{ id: 1 }] })).toEqual([{ id: 1 }]);
|
||||
});
|
||||
});
|
||||
|
||||
it("throws if rule is empty or missing roles", () => {
|
||||
expect(() => workflowUtils.buildWorkflowStepsFromApprovalRule({})).toThrow(/参数校验失败/);
|
||||
});
|
||||
describe("buildWorkflowStepsFromApprovalRule", () => {
|
||||
it("builds steps correctly for valid rule", () => {
|
||||
const result = workflowUtils.buildWorkflowStepsFromApprovalRule({
|
||||
level1_role: "manager",
|
||||
level2_role: "finance",
|
||||
});
|
||||
|
||||
expect(result.length).toBe(2);
|
||||
expect(result[0].assignee_rule.type).toBe("department_manager");
|
||||
expect(result[1].assignee_rule.type).toBe("role");
|
||||
expect(result[1].assignee_rule.value).toBe("finance");
|
||||
});
|
||||
|
||||
describe("resolveAssigneeFromRule", () => {
|
||||
it("resolves role type", async () => {
|
||||
const res = await workflowUtils.resolveAssigneeFromRule({ type: "role", value: "hr" });
|
||||
expect(res.assigneeRole).toBe("hr");
|
||||
expect(res.assigneeId).toBe(null);
|
||||
});
|
||||
it("throws if rule is empty or missing roles", () => {
|
||||
expect(() => workflowUtils.buildWorkflowStepsFromApprovalRule({})).toThrow(/参数校验失败/);
|
||||
});
|
||||
});
|
||||
|
||||
it("throws for invalid rule", async () => {
|
||||
await expect(workflowUtils.resolveAssigneeFromRule(null)).rejects.toThrow(/参数校验失败/);
|
||||
await expect(workflowUtils.resolveAssigneeFromRule({ type: "unknown" })).rejects.toThrow(/参数校验失败/);
|
||||
});
|
||||
describe("resolveAssigneeFromRule", () => {
|
||||
it("resolves role type", async () => {
|
||||
const result = await workflowUtils.resolveAssigneeFromRule({ type: "role", value: "hr" });
|
||||
|
||||
expect(result.assigneeRole).toBe("hr");
|
||||
expect(result.assigneeId).toBe(null);
|
||||
});
|
||||
|
||||
describe("toMoneyString", () => {
|
||||
it("converts values to string correctly", () => {
|
||||
expect(workflowUtils.toMoneyString(null)).toBe(null);
|
||||
expect(workflowUtils.toMoneyString("10.0")).toBe("10.0");
|
||||
expect(workflowUtils.toMoneyString(10)).toBe("10");
|
||||
});
|
||||
it("throws for invalid rule", async () => {
|
||||
await expect(workflowUtils.resolveAssigneeFromRule(null)).rejects.toThrow(/参数校验失败/);
|
||||
await expect(workflowUtils.resolveAssigneeFromRule({ type: "unknown" })).rejects.toThrow(/参数校验失败/);
|
||||
});
|
||||
});
|
||||
|
||||
describe("toMoneyString", () => {
|
||||
it("converts values to string correctly", () => {
|
||||
expect(workflowUtils.toMoneyString(null)).toBe(null);
|
||||
expect(workflowUtils.toMoneyString("10.0")).toBe("10.0");
|
||||
expect(workflowUtils.toMoneyString(10)).toBe("10");
|
||||
});
|
||||
});
|
||||
|
||||
it("blocks submit-time workflow resolution when the direct manager is missing", async () => {
|
||||
const tx = {
|
||||
employee: {
|
||||
findUnique: vi.fn(async () => ({
|
||||
id: OWNER_ID,
|
||||
direct_manager_id: null,
|
||||
})),
|
||||
},
|
||||
};
|
||||
|
||||
await expect(
|
||||
resolveActionableStep({
|
||||
tx,
|
||||
steps: buildSteps(),
|
||||
startIndex: 0,
|
||||
ownerId: OWNER_ID,
|
||||
blockOnUnresolvedAssignee: true,
|
||||
}),
|
||||
).rejects.toMatchObject({
|
||||
details: {
|
||||
workflow: "no_actionable_approver",
|
||||
reason: "no_manager_found",
|
||||
stepId: "approval_1_manager",
|
||||
},
|
||||
});
|
||||
});
|
||||
|
||||
it("keeps legacy skip behavior when strict submit resolution is not requested", async () => {
|
||||
const tx = {
|
||||
employee: {
|
||||
findUnique: vi.fn(async () => ({
|
||||
id: OWNER_ID,
|
||||
direct_manager_id: null,
|
||||
})),
|
||||
},
|
||||
};
|
||||
|
||||
const result = await resolveActionableStep({
|
||||
tx,
|
||||
steps: buildSteps(),
|
||||
startIndex: 0,
|
||||
ownerId: OWNER_ID,
|
||||
});
|
||||
|
||||
expect(result.actionable).toEqual(
|
||||
expect.objectContaining({
|
||||
step: expect.objectContaining({ id: "approval_2_finance" }),
|
||||
assigneeRole: "finance",
|
||||
}),
|
||||
);
|
||||
expect(result.skipped).toEqual([
|
||||
expect.objectContaining({
|
||||
reason: "no_manager_found",
|
||||
}),
|
||||
]);
|
||||
});
|
||||
});
|
||||
|
||||
在新工单中引用
屏蔽一个用户