R38: 完成员工管理筛选与部门树试跑
这个提交包含在:
@@ -0,0 +1,129 @@
|
||||
const Fastify = require("fastify");
|
||||
|
||||
const usersRoutes = require("../../../src/routes/users");
|
||||
|
||||
function buildEmployeeRow(overrides = {}) {
|
||||
const now = new Date("2026-03-28T10:00:00.000Z");
|
||||
return {
|
||||
id: "11111111-1111-4111-8111-111111111111",
|
||||
employee_no: "E001",
|
||||
name: "Alice",
|
||||
phone: "13800000000",
|
||||
email: "[email protected]",
|
||||
status: "active",
|
||||
department_id: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa",
|
||||
title: "员工",
|
||||
created_at: now,
|
||||
updated_at: now,
|
||||
roles: [{ role_code: "employee" }],
|
||||
...overrides,
|
||||
};
|
||||
}
|
||||
|
||||
async function buildApp(routeOptions = {}) {
|
||||
const app = Fastify();
|
||||
const findMany = vi.fn().mockResolvedValue([buildEmployeeRow()]);
|
||||
const count = vi.fn().mockResolvedValue(1);
|
||||
|
||||
app.decorate("prisma", {
|
||||
employee: {
|
||||
findMany,
|
||||
count,
|
||||
},
|
||||
});
|
||||
app.decorate("requirePermission", () => async (request) => {
|
||||
request.employee = {
|
||||
id: "actor-1",
|
||||
department_id: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa",
|
||||
permissionCodes: ["system.users"],
|
||||
};
|
||||
});
|
||||
app.decorate("authenticate", async (request) => {
|
||||
request.employee = {
|
||||
id: "actor-1",
|
||||
department_id: "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa",
|
||||
permissionCodes: ["system.users"],
|
||||
};
|
||||
});
|
||||
app.decorate("hasPermission", () => true);
|
||||
app.decorate("config", {});
|
||||
|
||||
await app.register(usersRoutes, { prefix: "/api", ...routeOptions });
|
||||
return { app, findMany, count };
|
||||
}
|
||||
|
||||
describe("usersRoutes list query", () => {
|
||||
it("supports departmentId filter for /employees when enabled", async () => {
|
||||
const departmentId = "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb";
|
||||
const { app, findMany, count } = await buildApp({
|
||||
basePath: "/employees",
|
||||
allowDepartmentFilter: true,
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/employees?page=1&pageSize=20&departmentId=${departmentId}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: expect.objectContaining({
|
||||
department_id: departmentId,
|
||||
}),
|
||||
}),
|
||||
);
|
||||
expect(count).toHaveBeenCalledWith({
|
||||
where: expect.objectContaining({
|
||||
department_id: departmentId,
|
||||
}),
|
||||
});
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("combines q + status + departmentId for /employees", async () => {
|
||||
const departmentId = "cccccccc-cccc-4ccc-8ccc-cccccccccccc";
|
||||
const { app, findMany } = await buildApp({
|
||||
basePath: "/employees",
|
||||
allowDepartmentFilter: true,
|
||||
});
|
||||
|
||||
const response = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/employees?status=active&q=alice&departmentId=${departmentId}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(findMany).toHaveBeenCalledWith(
|
||||
expect.objectContaining({
|
||||
where: expect.objectContaining({
|
||||
status: "active",
|
||||
department_id: departmentId,
|
||||
OR: expect.arrayContaining([
|
||||
expect.objectContaining({ name: expect.any(Object) }),
|
||||
expect.objectContaining({ employee_no: expect.any(Object) }),
|
||||
]),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it("does not apply departmentId filter for /users", async () => {
|
||||
const departmentId = "dddddddd-dddd-4ddd-8ddd-dddddddddddd";
|
||||
const { app, findMany } = await buildApp();
|
||||
|
||||
const response = await app.inject({
|
||||
method: "GET",
|
||||
url: `/api/users?departmentId=${departmentId}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const where = findMany.mock.calls[0][0].where;
|
||||
expect(where.department_id).toBeUndefined();
|
||||
|
||||
await app.close();
|
||||
});
|
||||
});
|
||||
在新工单中引用
屏蔽一个用户