function resolveEffectiveDate(rawValue) { return rawValue ? new Date(`${rawValue}T00:00:00.000Z`) : new Date(); } function buildDefaultChecklist(scene, employeeId) { const common = { employee_id: employeeId }; if (scene === "onboarding") { return [ { ...common, scene, task_type: "account_setup", title: "完成账号与权限初始化", status: "pending" }, { ...common, scene, task_type: "asset_issue", title: "确认办公资产发放", status: "pending" }, { ...common, scene, task_type: "policy_ack", title: "确认制度与入职须知", status: "pending" }, ]; } if (scene === "transfer") { return [{ ...common, scene, task_type: "handover_note", title: "确认调岗交接说明", status: "pending" }]; } return [ { ...common, scene, task_type: "asset_return", title: "确认资产归还/转交", status: "pending" }, { ...common, scene, task_type: "access_revoke", title: "确认账号和权限回收", status: "pending" }, { ...common, scene, task_type: "expense_clearance", title: "确认费用与审批待办已清理", status: "pending" }, { ...common, scene, task_type: "handover_note", title: "确认离职交接说明", status: "pending" }, ]; } async function applyEmployeeTransfer(tx, options) { await tx.employee.update({ where: { id: options.employeeId }, data: { ...(options.departmentId !== undefined ? { department_id: options.departmentId } : {}), ...(options.directManagerId !== undefined ? { direct_manager_id: options.directManagerId } : {}), ...(options.title !== undefined ? { title: options.title } : {}), updated_by: options.actorId, }, }); await tx.employeeLifecycleEvent.create({ data: { employee_id: options.employeeId, event_type: "transfer", effective_date: resolveEffectiveDate(options.effectiveDate), payload_json: { departmentId: options.departmentId ?? null, directManagerId: options.directManagerId ?? null, title: options.title ?? null, notes: options.notes ?? null, }, created_by: options.actorId, }, }); await tx.employeeChecklistTask.createMany({ data: buildDefaultChecklist("transfer", options.employeeId), }); } module.exports = { applyEmployeeTransfer, buildDefaultChecklist, resolveEffectiveDate, };