import { sendMail } from "./mailer"; import { config } from "../config/env"; import { localDateCzStr, localDateTimeCzStr } from "../utils/date"; const LEAVE_TYPE_LABELS: Record = { vacation: "Dovolená", sick: "Nemocenská", unpaid: "Neplacené volno", }; function formatDate(dateStr: string): string { try { const d = new Date(dateStr); return localDateCzStr(d); } catch { return dateStr; } } function escapeHtml(str: string): string { return str .replace(/&/g, "&") .replace(//g, ">") .replace(/"/g, """); } interface LeaveRequestData { leave_type: string; date_from: string; date_to: string; total_days: number; total_hours: number; notes?: string | null; } export async function notifyNewLeaveRequest( request: LeaveRequestData, employeeName: string, ): Promise { const notifyEmail = config.email.leaveNotify; if (!notifyEmail) return; const leaveType = LEAVE_TYPE_LABELS[request.leave_type] || request.leave_type; const dateFrom = formatDate(request.date_from); const dateTo = formatDate(request.date_to); const notes = request.notes || ""; const subject = `Nová žádost o nepřítomnost - ${employeeName} (${leaveType})`; const appUrl = config.appUrl || ""; const approvalLink = appUrl ? `

Přejít ke schvalování

` : ""; const now = new Date(); const timestamp = localDateTimeCzStr(now); const html = `

Nová žádost o nepřítomnost

${ notes ? `` : "" }
Zaměstnanec: ${escapeHtml(employeeName)}
Typ: ${escapeHtml(leaveType)}
Období: ${dateFrom} – ${dateTo}
Pracovní dny: ${request.total_days} dní (${request.total_hours} hodin)
Poznámka: ${escapeHtml(notes)}
${approvalLink}

Tato zpráva byla automaticky vygenerována systémem.
Datum: ${timestamp}

`; const sent = await sendMail(notifyEmail, subject, html); if (!sent) { console.error( `LeaveNotification: Failed to send notification to ${notifyEmail}`, ); } }