R42: 完成考勤增强首轮交付

这个提交包含在:
2026-03-28 22:13:18 +08:00
父节点 2706717e6b
当前提交 e8d16c47f2
共修改 13 个文件,包含 2386 行新增和 365 行删除
+149 -2
查看文件
@@ -3,10 +3,17 @@ const { Readable } = require("node:stream");
const ExcelJS = require("exceljs");
const { z } = require("zod");
const {
HISTORY_STATUS_VALUES,
buildPresenceValidationError,
buildTeamPresenceHistory,
resolvePresenceHistoryRange,
} = require("../services/presenceHistory");
const { AppError } = require("../utils/AppError");
const { buildAuditLogData } = require("../utils/audit");
const EXPORT_FORMAT_SCHEMA = z.enum(["csv", "xlsx"]);
const DATE_ONLY_SCHEMA = z.string().regex(/^\d{4}-\d{2}-\d{2}$/);
const REIMBURSEMENT_STATUS_SCHEMA = z.enum([
"draft",
@@ -43,8 +50,8 @@ const AUDIT_EXPORT_QUERY_SCHEMA = z.object({
const DASHBOARD_EXPORT_QUERY_SCHEMA = z.object({
format: EXPORT_FORMAT_SCHEMA.optional(),
dateFrom: z.string().regex(/^\d{4}-\d{2}-\d{2}$/).optional(),
dateTo: z.string().regex(/^\d{4}-\d{2}-\d{2}$/).optional(),
dateFrom: DATE_ONLY_SCHEMA.optional(),
dateTo: DATE_ONLY_SCHEMA.optional(),
departmentId: z.string().uuid().optional(),
limit: z.coerce.number().int().min(1).max(20000).optional(),
});
@@ -57,6 +64,16 @@ const EMPLOYEE_EXPORT_QUERY_SCHEMA = z.object({
limit: z.coerce.number().int().min(1).max(20000).optional(),
});
const PRESENCE_HISTORY_EXPORT_QUERY_SCHEMA = z.object({
format: EXPORT_FORMAT_SCHEMA.optional(),
dateFrom: DATE_ONLY_SCHEMA.optional(),
dateTo: DATE_ONLY_SCHEMA.optional(),
departmentId: z.string().uuid().optional(),
employeeId: z.string().uuid().optional(),
status: z.enum(HISTORY_STATUS_VALUES).optional(),
});
const PRESENCE_HISTORY_EXPORT_LIMIT = 5000;
function csvCell(value) {
if (value == null) return "";
@@ -801,6 +818,87 @@ async function buildEmployeesXlsx(prisma, where, limit) {
return workbook.xlsx.writeBuffer();
}
function presenceStatusLabel(status) {
if (status === "office") return "在岗";
if (status === "remote") return "居家";
if (status === "out") return "外出";
if (status === "travel") return "出差";
if (status === "leave") return "请假";
return "未更新";
}
function presenceTimeRangeLabel(timeRange) {
if (timeRange === "morning") return "上午";
if (timeRange === "afternoon") return "下午";
if (timeRange === "full_day") return "全天";
return "-";
}
function presenceSourceLabel(source) {
if (source === "manual") return "手工";
if (source === "leave_sync") return "审批同步";
if (source === "admin_override") return "管理员修正";
return "-";
}
function mapPresenceHistoryExportRow(item) {
return [
item.date,
item.employee.employeeNo,
item.employee.name,
item.employee.department?.name ?? "",
item.employee.title ?? "",
presenceStatusLabel(item.presence?.status ?? null),
presenceTimeRangeLabel(item.presence?.timeRange),
presenceSourceLabel(item.presence?.source),
item.presence?.note ?? "-",
];
}
function buildPresenceHistoryCsv(items) {
const rows = [
"\ufeff",
csvRow(["日期", "工号", "姓名", "部门", "岗位", "状态", "时间段", "来源", "备注"]),
];
for (const item of items) {
rows.push(csvRow(mapPresenceHistoryExportRow(item)));
}
return rows.join("");
}
async function buildPresenceHistoryXlsx(items) {
const workbook = new ExcelJS.Workbook();
const sheet = workbook.addWorksheet("presence_history");
sheet.columns = [
{ header: "日期", key: "date", width: 14 },
{ header: "工号", key: "employeeNo", width: 14 },
{ header: "姓名", key: "name", width: 14 },
{ header: "部门", key: "departmentName", width: 18 },
{ header: "岗位", key: "title", width: 18 },
{ header: "状态", key: "status", width: 12 },
{ header: "时间段", key: "timeRange", width: 12 },
{ header: "来源", key: "source", width: 14 },
{ header: "备注", key: "note", width: 32 },
];
for (const item of items) {
sheet.addRow({
date: item.date,
employeeNo: item.employee.employeeNo,
name: item.employee.name,
departmentName: item.employee.department?.name ?? "",
title: item.employee.title ?? "",
status: presenceStatusLabel(item.presence?.status ?? null),
timeRange: presenceTimeRangeLabel(item.presence?.timeRange),
source: presenceSourceLabel(item.presence?.source),
note: item.presence?.note ?? "-",
});
}
return workbook.xlsx.writeBuffer();
}
async function exportsRoutes(fastify) {
fastify.get(
"/exports/employees",
@@ -843,6 +941,55 @@ async function exportsRoutes(fastify) {
},
);
fastify.get(
"/exports/presence-history",
{
preHandler: fastify.requirePermission("system.users"),
},
async (request, reply) => {
const query = parseQuery(PRESENCE_HISTORY_EXPORT_QUERY_SCHEMA, request.query);
const format = resolveExportFormat(query);
const range = resolvePresenceHistoryRange(query, fastify.config.appTimezone);
const history = await buildTeamPresenceHistory(fastify.prisma, {
dateStrings: range.dateStrings,
departmentId: query.departmentId,
employeeId: query.employeeId,
status: query.status,
});
if (history.items.length > PRESENCE_HISTORY_EXPORT_LIMIT) {
throw buildPresenceValidationError({ export: "too_large" }, 409);
}
await fastify.prisma.auditLog.create({
data: buildAuditLogData(request, {
actorId: request.employee.id,
action: "EXPORT_PRESENCE_HISTORY",
targetType: "export",
extra: {
format,
dateFrom: range.dateFrom,
dateTo: range.dateTo,
departmentId: query.departmentId ?? null,
employeeId: query.employeeId ?? null,
status: query.status ?? null,
matchedTotal: history.items.length,
},
}),
});
const filename = exportFilename("presence-history", format);
setExportHeaders(reply, format, filename);
if (format === "xlsx") {
const buffer = await buildPresenceHistoryXlsx(history.items);
return reply.send(Buffer.from(buffer));
}
return reply.send(buildPresenceHistoryCsv(history.items));
},
);
fastify.get(
"/exports/reimbursements",
{