R40: 完成档案编辑扩展与员工导出

这个提交包含在:
2026-03-28 17:42:09 +08:00
父节点 1cac1447b1
当前提交 2eff8e01a2
共修改 10 个文件,包含 654 行新增和 23 行删除
+7 -1
查看文件
@@ -14,6 +14,7 @@ const PROFILE_PATCH_SCHEMA = z
departmentId: UUID_SCHEMA.optional(),
directManagerId: UUID_SCHEMA.nullable().optional(),
title: z.string().trim().min(1).max(100).optional(),
hireDate: DATE_SCHEMA.nullable().optional(),
probationEndDate: DATE_SCHEMA.nullable().optional(),
lastWorkingDate: DATE_SCHEMA.nullable().optional(),
workLocation: z.string().trim().max(100).nullable().optional(),
@@ -370,7 +371,11 @@ async function employeesRoutes(fastify) {
const body = parseBody(PROFILE_PATCH_SCHEMA, request.body);
const managerMode = canManageLifecycle(fastify, request);
const selfEditableKeys = ["workLocation", "emergencyContactName", "emergencyContactPhone", "notes"];
if (!managerMode && request.employee.id === employeeId) {
if (!managerMode) {
if (request.employee.id !== employeeId) {
throw new AppError({ code: "PERM_001", message: "无权限", statusCode: 403, details: { permission: "employee.profile.write" } });
}
const invalidKeys = Object.keys(body).filter((key) => !selfEditableKeys.includes(key));
if (invalidKeys.length) {
throw buildValidationError({ body: `self_profile_fields_forbidden:${invalidKeys.join(",")}` }, 403);
@@ -384,6 +389,7 @@ async function employeesRoutes(fastify) {
...(body.departmentId !== undefined ? { department_id: body.departmentId } : {}),
...(body.directManagerId !== undefined ? { direct_manager_id: body.directManagerId } : {}),
...(body.title !== undefined ? { title: body.title } : {}),
...(body.hireDate !== undefined ? { hire_date: body.hireDate ? new Date(`${body.hireDate}T00:00:00.000Z`) : null } : {}),
...(body.probationEndDate !== undefined ? { probation_end_date: body.probationEndDate ? new Date(`${body.probationEndDate}T00:00:00.000Z`) : null } : {}),
...(body.lastWorkingDate !== undefined ? { last_working_date: body.lastWorkingDate ? new Date(`${body.lastWorkingDate}T00:00:00.000Z`) : null } : {}),
...(body.workLocation !== undefined ? { work_location: body.workLocation ?? null } : {}),
+179
查看文件
@@ -49,6 +49,14 @@ const DASHBOARD_EXPORT_QUERY_SCHEMA = z.object({
limit: z.coerce.number().int().min(1).max(20000).optional(),
});
const EMPLOYEE_EXPORT_QUERY_SCHEMA = z.object({
format: EXPORT_FORMAT_SCHEMA.optional(),
status: z.enum(["active", "archived"]).optional(),
departmentId: z.string().uuid().optional(),
q: z.string().max(100).optional(),
limit: z.coerce.number().int().min(1).max(20000).optional(),
});
function csvCell(value) {
if (value == null) return "";
@@ -168,6 +176,25 @@ function buildDashboardWhere(request, query) {
};
}
function buildEmployeeExportWhere(query) {
const q = query.q?.trim() ?? "";
return {
...(query.status ? { status: query.status } : {}),
...(query.departmentId ? { department_id: query.departmentId } : {}),
...(q
? {
OR: [
{ employee_no: { contains: q, mode: "insensitive" } },
{ name: { contains: q, mode: "insensitive" } },
{ phone: { contains: q, mode: "insensitive" } },
{ email: { contains: q, mode: "insensitive" } },
],
}
: {}),
};
}
async function* streamDashboardReimbursementsCsv(prisma, where, limit) {
yield "\ufeff";
yield csvRow([
@@ -663,7 +690,159 @@ async function buildAuditLogsXlsx(prisma, where, limit) {
return workbook.xlsx.writeBuffer();
}
async function* streamEmployeesCsv(prisma, where, limit) {
yield "\ufeff";
yield csvRow(["工号", "姓名", "部门", "职位", "手机", "邮箱", "账号状态", "入职日期"]);
const batchSize = 500;
let remaining = limit;
let cursorCreatedAt = null;
let cursorId = null;
while (remaining > 0) {
const take = Math.min(batchSize, remaining);
const rows = await prisma.employee.findMany({
where: {
...where,
...(cursorCreatedAt
? {
OR: [
{ created_at: { lt: cursorCreatedAt } },
{ created_at: cursorCreatedAt, id: { lt: cursorId } },
],
}
: {}),
},
orderBy: [{ created_at: "desc" }, { id: "desc" }],
take,
select: {
id: true,
employee_no: true,
name: true,
phone: true,
email: true,
status: true,
title: true,
hire_date: true,
created_at: true,
department: { select: { name: true } },
},
});
if (!rows.length) {
break;
}
for (const item of rows) {
yield csvRow([
item.employee_no,
item.name,
item.department?.name ?? "",
item.title ?? "",
item.phone ?? "",
item.email ?? "",
item.status,
item.hire_date?.toISOString?.()?.slice(0, 10) ?? "",
]);
remaining -= 1;
if (remaining <= 0) break;
}
const last = rows[rows.length - 1];
cursorCreatedAt = last.created_at;
cursorId = last.id;
}
}
async function buildEmployeesXlsx(prisma, where, limit) {
const workbook = new ExcelJS.Workbook();
const sheet = workbook.addWorksheet("employees");
sheet.columns = [
{ header: "工号", key: "employeeNo", width: 16 },
{ header: "姓名", key: "name", width: 14 },
{ header: "部门", key: "departmentName", width: 16 },
{ header: "职位", key: "title", width: 16 },
{ header: "手机", key: "phone", width: 16 },
{ header: "邮箱", key: "email", width: 24 },
{ header: "账号状态", key: "status", width: 12 },
{ header: "入职日期", key: "hireDate", width: 14 },
];
const rows = await prisma.employee.findMany({
where,
orderBy: [{ created_at: "desc" }, { id: "desc" }],
take: limit,
select: {
employee_no: true,
name: true,
phone: true,
email: true,
status: true,
title: true,
hire_date: true,
department: { select: { name: true } },
},
});
for (const item of rows) {
sheet.addRow({
employeeNo: item.employee_no,
name: item.name,
departmentName: item.department?.name ?? "",
title: item.title ?? "",
phone: item.phone ?? "",
email: item.email ?? "",
status: item.status,
hireDate: item.hire_date?.toISOString?.()?.slice(0, 10) ?? "",
});
}
return workbook.xlsx.writeBuffer();
}
async function exportsRoutes(fastify) {
fastify.get(
"/exports/employees",
{
preHandler: fastify.requirePermission("system.users"),
},
async (request, reply) => {
const query = parseQuery(EMPLOYEE_EXPORT_QUERY_SCHEMA, request.query);
const where = buildEmployeeExportWhere(query);
const format = resolveExportFormat(query);
const limit = query.limit ?? 5000;
const total = await fastify.prisma.employee.count({ where });
await fastify.prisma.auditLog.create({
data: buildAuditLogData(request, {
actorId: request.employee.id,
action: "EXPORT_EMPLOYEES",
targetType: "export",
extra: {
format,
status: query.status ?? null,
departmentId: query.departmentId ?? null,
q: query.q?.trim() || null,
limit,
matchedTotal: total,
},
}),
});
const filename = exportFilename("employees", format);
setExportHeaders(reply, format, filename);
if (format === "xlsx") {
const buffer = await buildEmployeesXlsx(fastify.prisma, where, limit);
return reply.send(Buffer.from(buffer));
}
const stream = Readable.from(streamEmployeesCsv(fastify.prisma, where, limit));
return reply.send(stream);
},
);
fastify.get(
"/exports/reimbursements",
{