// P17 HR-Hub workflows (AK-P17b) — sick-leave domain service. // // An employee files a sick-leave report themselves, or HR/office files it on // their behalf (`reportedByUserId`). The AU certificate ("AU-Schein") is // sensitive (AD-8): we envelope-encrypt the certificate File REFERENCE through // the hrFieldCrypto seam and never return it without an explicit, // permission-checked + audited reveal (mirrors hrService.revealEmergencyContact). // NO React/UI imports. Every tenant access runs inside withTenantRLS(). import { CatalogError, foundationErrors } from "@hivegrid/foundation"; import { createAuditEvent } from "@hivegrid/db/audit-events.server"; import { withTenantRLS } from "~/utils/db/.server/tenantRls.server"; import { getHrFieldCrypto, type HrFieldCrypto } from "./hrFieldCrypto.server"; import { HrServiceError } from "./hrService.server"; const HR_AUDIT_MODULE = "hr"; export type HrSickLeaveStatus = "reported" | "closed" | "cancelled"; export type SickLeaveRow = { id: string; tenantId: string; employeeUserId: string; reportedByUserId: string; startDate: Date; expectedReturnDate: Date | null; returnDate: Date | null; certificateRefCiphertext: string | null; status: HrSickLeaveStatus; note: string | null; calendarEventId: string | null; createdAt: Date; updatedAt: Date; }; export type ReportSickLeaveInput = { tenantId: string; employeeUserId: string; /** The acting user — equals employeeUserId for self-reports, an HR/office user * for on-behalf reports. */ reportedByUserId: string; startDate: Date; expectedReturnDate?: Date | null; note?: string | null; isDemoData?: boolean; }; /** File a sick-leave report (self or on behalf of an employee). */ export async function reportSickLeave(input: ReportSickLeaveInput): Promise { if (input.expectedReturnDate && input.expectedReturnDate < input.startDate) { throw new HrServiceError("hr.error.sickRangeInvalid"); } const created = (await withTenantRLS(input.tenantId, (tx) => tx.employeeSickLeaveReport.create({ data: { tenantId: input.tenantId, employeeUserId: input.employeeUserId, reportedByUserId: input.reportedByUserId, startDate: input.startDate, expectedReturnDate: input.expectedReturnDate ?? null, note: input.note?.trim().slice(0, 2000) || null, status: "reported", isDemoData: input.isDemoData ?? false, }, }), )) as SickLeaveRow; await createAuditEvent({ tenantId: input.tenantId, actorId: input.reportedByUserId, actorType: "user", action: input.reportedByUserId === input.employeeUserId ? "HR_SICK_REPORT_SELF" : "HR_SICK_REPORT_OFFICE", targetType: "EmployeeSickLeaveReport", targetId: created.id, sourceModule: HR_AUDIT_MODULE, outcome: "success", metadata: { employeeUserId: input.employeeUserId }, }); return created; } async function getReportOrThrow(tenantId: string, reportId: string): Promise { const row = await withTenantRLS(tenantId, (tx) => tx.employeeSickLeaveReport.findFirst({ where: { id: reportId, tenantId } })); if (!row) { throw new CatalogError(foundationErrors.notFound, { reportId }); } return row as SickLeaveRow; } /** * Attach (or replace) the AU certificate, storing the File reference * ENVELOPE-ENCRYPTED. The plaintext fileId never lands in the column. */ export async function attachCertificate( input: { tenantId: string; reportId: string; actorUserId: string; certificateFileId: string }, crypto: HrFieldCrypto = getHrFieldCrypto(), ): Promise { const row = await getReportOrThrow(input.tenantId, input.reportId); const ciphertext = await crypto.encrypt(input.tenantId, input.certificateFileId); await withTenantRLS(input.tenantId, (tx) => tx.employeeSickLeaveReport.update({ where: { id: row.id }, data: { certificateRefCiphertext: ciphertext } }), ); await createAuditEvent({ tenantId: input.tenantId, actorId: input.actorUserId, actorType: "user", action: "HR_SICK_CERTIFICATE_ATTACH", targetType: "EmployeeSickLeaveReport", targetId: row.id, sourceModule: HR_AUDIT_MODULE, dataSensitivity: "pii", regulatoryScope: ["gdpr"], outcome: "success", metadata: { employeeUserId: row.employeeUserId }, }); } /** * Reveal (decrypt) the AU certificate File id ON DEMAND. Writes a pii/gdpr * audit event BEFORE returning. The caller must already have established that * `revealerUserId` is the employee themselves or holds hr.read_all (route). */ export async function revealCertificateFileId( input: { tenantId: string; reportId: string; revealerUserId: string }, crypto: HrFieldCrypto = getHrFieldCrypto(), ): Promise { const row = await getReportOrThrow(input.tenantId, input.reportId); if (!row.certificateRefCiphertext) { return null; } await createAuditEvent({ tenantId: input.tenantId, actorId: input.revealerUserId, actorType: "user", action: "HR_SICK_CERTIFICATE_REVEAL", targetType: "EmployeeSickLeaveReport", targetId: row.id, sourceModule: HR_AUDIT_MODULE, dataSensitivity: "pii", regulatoryScope: ["gdpr"], outcome: "success", metadata: { employeeUserId: row.employeeUserId }, }); return crypto.decrypt(input.tenantId, row.certificateRefCiphertext); } /** Mark a sick-leave report closed with an actual return date. */ export async function closeSickLeave(input: { tenantId: string; reportId: string; actorUserId: string; returnDate: Date }): Promise { const row = await getReportOrThrow(input.tenantId, input.reportId); if (row.status === "cancelled") { throw new HrServiceError("hr.error.sickNotUpdatable"); } if (input.returnDate < row.startDate) { throw new HrServiceError("hr.error.sickRangeInvalid"); } await withTenantRLS(input.tenantId, (tx) => tx.employeeSickLeaveReport.update({ where: { id: row.id }, data: { status: "closed", returnDate: input.returnDate } }), ); await createAuditEvent({ tenantId: input.tenantId, actorId: input.actorUserId, actorType: "user", action: "HR_SICK_CLOSE", targetType: "EmployeeSickLeaveReport", targetId: row.id, sourceModule: HR_AUDIT_MODULE, outcome: "success", }); } /** Cancel an erroneous sick-leave report (removes it from the absence feed). */ export async function cancelSickLeave(input: { tenantId: string; reportId: string; actorUserId: string }): Promise { const row = await getReportOrThrow(input.tenantId, input.reportId); await withTenantRLS(input.tenantId, (tx) => tx.employeeSickLeaveReport.update({ where: { id: row.id }, data: { status: "cancelled" } }), ); await createAuditEvent({ tenantId: input.tenantId, actorId: input.actorUserId, actorType: "user", action: "HR_SICK_CANCEL", targetType: "EmployeeSickLeaveReport", targetId: row.id, sourceModule: HR_AUDIT_MODULE, outcome: "success", }); } /** List the caller's own sick-leave reports, newest first. */ export async function listOwnSickLeave(tenantId: string, employeeUserId: string): Promise { const rows = await withTenantRLS(tenantId, (tx) => tx.employeeSickLeaveReport.findMany({ where: { tenantId, employeeUserId }, orderBy: { startDate: "desc" } }), ); return rows as SickLeaveRow[]; } /** HR/office view of all sick-leave reports (optionally only currently open). */ export async function listSickLeaveForOffice(tenantId: string, opts?: { onlyOpen?: boolean }): Promise { const rows = await withTenantRLS(tenantId, (tx) => tx.employeeSickLeaveReport.findMany({ where: { tenantId, ...(opts?.onlyOpen ? { status: "reported" } : {}) }, orderBy: { startDate: "desc" }, }), ); return rows as SickLeaveRow[]; }