Repository Public
1import type { Appointment, AppointmentType } from "./service";23const esc = (s: string) => s.replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/([,;])/g, "\\$1");4const stamp = (ms: number) => new Date(ms).toISOString().replace(/[-:]/g, "").replace(/\.\d{3}/, "");5const fold = (line: string) => line.replace(/(.{72})(?=.)/g, "$1\r\n ");67export const LOCATION_LABEL: Record<string, string> = { visio: "Visioconférence", telephone: "Téléphone", presentiel: "En personne" };89function vevent(a: Appointment, t: AppointmentType | { title: string; location_kind: string; location_detail: string }, opts: { withPerson: boolean; host: string }) {10 const lines = [11 "BEGIN:VEVENT",12 `UID:rdv-${a.id}@${opts.host}`,13 `DTSTAMP:${stamp(a.updated_at)}`,14 `DTSTART:${stamp(a.start_utc)}`,15 `DTEND:${stamp(a.end_utc)}`,16 `SUMMARY:${esc(opts.withPerson ? `${t.title} — ${a.name}` : `${t.title} — PHI Conseil`)}`,17 `LOCATION:${esc([LOCATION_LABEL[t.location_kind] ?? "", t.location_detail].filter(Boolean).join(" — "))}`,18 `STATUS:${a.status === "confirmed" || a.status === "completed" ? "CONFIRMED" : a.status === "cancelled" ? "CANCELLED" : "TENTATIVE"}`,19 ...(opts.withPerson ? [`DESCRIPTION:${esc([a.organization, a.email, a.phone, a.reason].filter(Boolean).join("\n"))}`] : []),20 "END:VEVENT",21 ];22 return lines.map(fold).join("\r\n");23}2425export function icsCalendar(events: { a: Appointment; t: { title: string; location_kind: string; location_detail: string } }[], opts: { withPerson: boolean; host: string; name: string }) {26 return [27 "BEGIN:VCALENDAR",28 "VERSION:2.0",29 "PRODID:-//PHI Conseil//Rendez-vous//FR",30 "CALSCALE:GREGORIAN",31 "METHOD:PUBLISH",32 fold(`X-WR-CALNAME:${esc(opts.name)}`),33 ...events.map((e) => vevent(e.a, e.t, opts)),34 "END:VCALENDAR",35 "",36 ].join("\r\n");37}3839/** Lien « Ajouter à Google Agenda » (aucune connexion OAuth requise). */40export function googleCalendarUrl(a: Appointment, t: { title: string; location_kind: string; location_detail: string }): string {41 const p = new URLSearchParams({42 action: "TEMPLATE",43 text: `${t.title} — PHI Conseil`,44 dates: `${stamp(a.start_utc)}/${stamp(a.end_utc)}`,45 location: [LOCATION_LABEL[t.location_kind] ?? "", t.location_detail].filter(Boolean).join(" — "),46 ctz: "America/Toronto",47 });48 return `https://calendar.google.com/calendar/render?${p.toString()}`;49}50