Skip to content

Your favorite shift management application!

12.1 KB
Raw Blame
import { NextRequest, NextResponse } from "next/server";
import { db } from "@/lib/db";
import { calendars, shifts } from "@/lib/db/schema";
import { inArray } from "drizzle-orm";
import { jsPDF } from "jspdf";
import { getSessionUser } from "@/lib/auth/sessions";
import { hasCapability } from "@/lib/auth/permissions";
import { rateLimit } from "@/lib/rate-limiter";
import { formatDateToLocal } from "@/lib/date-utils";
import { formatTimeRange } from "@/lib/shift-display";
import { getCalendarCustomFields, withShiftCustomFields } from "@/lib/shift-custom-fields";
import { formatValueForDisplay } from "@/lib/custom-fields";

export async function POST(request: NextRequest) {
  try {
    const user = await getSessionUser(request.headers);

    // Rate limiting: 10 PDF exports per 10 minutes
    const rateLimitResponse = rateLimit(request, user?.id, "export-pdf");
    if (rateLimitResponse) return rateLimitResponse;

    const { searchParams } = new URL(request.url);
    const month = searchParams.get("month"); // Format: YYYY-MM
    const year = searchParams.get("year"); // Format: YYYY
    const locale = searchParams.get("locale") || "en"; // Default to English

    const { calendarIds } = await request.json();

    if (!Array.isArray(calendarIds) || calendarIds.length === 0) {
      return NextResponse.json(
        { error: "No calendar IDs provided" },
        { status: 400 }
      );
    }

    // Get all requested calendars
    const requestedCalendars = await db.query.calendars.findMany({
      where: inArray(calendars.id, calendarIds),
    });

    if (requestedCalendars.length === 0) {
      return NextResponse.json(
        { error: "No calendars found" },
        { status: 404 }
      );
    }

    // Filter calendars by permission - only include calendars user can view
    const accessFlags = await Promise.all(
      requestedCalendars.map((calendar) =>
        hasCapability(user?.id, calendar.id, "viewShifts")
      )
    );
    const accessibleCalendars = requestedCalendars.filter(
      (_, i) => accessFlags[i]
    );

    if (accessibleCalendars.length === 0) {
      return NextResponse.json(
        { error: "Insufficient permissions for selected calendars" },
        { status: 403 }
      );
    }

    // Get all shifts for accessible calendars
    let allShifts = await db.query.shifts.findMany({
      where: inArray(
        shifts.calendarId,
        accessibleCalendars.map((c) => c.id)
      ),
      orderBy: (shifts, { asc }) => [asc(shifts.date)],
      with: { segments: true },
    });

    // A calendar that has since turned split shifts off must fall back to
    // showing only its shifts' primary ranges, without deleting the stored segments.
    const splitShiftsEnabledIds = new Set(
      accessibleCalendars.filter((c) => c.splitShiftsEnabled).map((c) => c.id)
    );
    allShifts = allShifts.map((shift) =>
      splitShiftsEnabledIds.has(shift.calendarId) ? shift : { ...shift, segments: [] }
    );

    // Filter by month or year if provided
    if (month) {
      const [filterYear, filterMonth] = month.split("-").map(Number);
      allShifts = allShifts.filter((shift) => {
        const shiftDate = shift.date as Date;
        return (
          shiftDate.getFullYear() === filterYear &&
          shiftDate.getMonth() + 1 === filterMonth
        );
      });
    } else if (year) {
      const filterYear = parseInt(year);
      allShifts = allShifts.filter((shift) => {
        const shiftDate = shift.date as Date;
        return shiftDate.getFullYear() === filterYear;
      });
    }

    // Create calendar lookup map
    const calendarMap = new Map(
      accessibleCalendars.map((c) => [c.id, { name: c.name, color: c.color }])
    );

    // Field keys are per calendar, so definitions and values must be resolved calendar by calendar.
    const definitionsByCalendar = new Map(
      await Promise.all(
        accessibleCalendars.map(
          async (c) => [c.id, await getCalendarCustomFields(c.id)] as const
        )
      )
    );
    const customFieldsByShiftId = new Map<string, Record<string, string | number | boolean | null>>();
    for (const c of accessibleCalendars) {
      const definitions = definitionsByCalendar.get(c.id) ?? [];
      const calendarShifts = allShifts.filter((s) => s.calendarId === c.id);
      const withFields = await withShiftCustomFields(calendarShifts, definitions);
      for (const s of withFields) {
        customFieldsByShiftId.set(s.id, s.customFields);
      }
    }

    // Determine if multi-calendar export
    const isMultiCalendar = accessibleCalendars.length > 1;

    // Sort all shifts by date (already sorted from DB, but ensure it)
    allShifts.sort((a, b) => {
      const dateA = (a.date as Date).getTime();
      const dateB = (b.date as Date).getTime();
      return dateA - dateB;
    });

    // Create PDF
    const doc = new jsPDF();
    const pageWidth = doc.internal.pageSize.getWidth();
    const pageHeight = doc.internal.pageSize.getHeight();
    const margin = 15;
    let yPosition = margin;

    // Helper function to add new page if needed
    const checkPageBreak = (neededSpace: number) => {
      if (yPosition + neededSpace > pageHeight - margin) {
        doc.addPage();
        yPosition = margin;
        return true;
      }
      return false;
    };

    // Helper function to parse hex color
    const hexToRgb = (hex: string) => {
      const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
      return result
        ? {
            r: parseInt(result[1], 16),
            g: parseInt(result[2], 16),
            b: parseInt(result[3], 16),
          }
        : { r: 0, g: 0, b: 0 };
    };

    // Title
    doc.setFontSize(20);
    doc.setFont("helvetica", "bold");
    doc.text(
      isMultiCalendar
        ? "BetterShift Multi-Calendar Export"
        : accessibleCalendars[0].name,
      pageWidth / 2,
      yPosition,
      { align: "center" }
    );
    yPosition += 10;

    // Calendar names (only for multi-calendar)
    if (isMultiCalendar) {
      doc.setFontSize(10);
      doc.setFont("helvetica", "normal");
      const calendarNames = accessibleCalendars.map((c) => c.name).join(", ");
      const wrappedNames = doc.splitTextToSize(
        calendarNames,
        pageWidth - margin * 2
      );
      for (const line of wrappedNames) {
        doc.text(line, pageWidth / 2, yPosition, { align: "center" });
        yPosition += 5;
      }
    }

    // Date range
    if (month || year) {
      const dateRangeText = month ? `${month}` : `${year}`;
      doc.text(dateRangeText, pageWidth / 2, yPosition, { align: "center" });
      yPosition += 5;
    }

    // Horizontal line
    doc.setDrawColor(200, 200, 200);
    doc.line(margin, yPosition, pageWidth - margin, yPosition);
    yPosition += 8;

    if (allShifts.length === 0) {
      // Empty state
      yPosition += 20;
    } else {
      // Group shifts by month
      const shiftsByMonth = new Map<string, typeof allShifts>();
      for (const shift of allShifts) {
        const shiftDate = shift.date as Date;
        const monthKey = `${shiftDate.getFullYear()}-${String(
          shiftDate.getMonth() + 1
        ).padStart(2, "0")}`;
        if (!shiftsByMonth.has(monthKey)) {
          shiftsByMonth.set(monthKey, []);
        }
        shiftsByMonth.get(monthKey)!.push(shift);
      }

      // Sort months chronologically
      const sortedMonths = Array.from(shiftsByMonth.keys()).sort();

      for (const monthKey of sortedMonths) {
        const monthShifts = shiftsByMonth.get(monthKey)!;
        const [monthYear, monthNum] = monthKey.split("-");
        const monthDate = new Date(parseInt(monthYear), parseInt(monthNum) - 1);
        const monthName = monthDate.toLocaleDateString(locale, {
          month: "long",
          year: "numeric",
        });

        checkPageBreak(20);

        // Month header
        doc.setFontSize(14);
        doc.setFont("helvetica", "bold");
        doc.text(monthName, margin, yPosition);
        yPosition += 7;

        // Shifts for this month
        doc.setFontSize(10);
        doc.setFont("helvetica", "normal");

        for (const shift of monthShifts) {
          checkPageBreak(15);

          const shiftDate = shift.date as Date;
          const dateStr = shiftDate.toLocaleDateString(locale, {
            weekday: "short",
            day: "2-digit",
            month: "2-digit",
          });

          // Calendar color indicator (small rectangle)
          const calendarInfo = calendarMap.get(shift.calendarId);
          const rgb = hexToRgb(shift.color);
          doc.setFillColor(rgb.r, rgb.g, rgb.b);
          doc.rect(margin, yPosition - 2, 3, 3, "F");

          // Date
          doc.setFont("helvetica", "bold");
          doc.text(dateStr, margin + 6, yPosition);

          // Time — a split shift's joined range (e.g. "06:00 – 10:00, 15:00 – 21:00")
          // can be wider than the fixed column, so later columns shift right to
          // clear it instead of overlapping; short strings keep the original offsets.
          const columnGap = 3;
          doc.setFont("helvetica", "normal");
          const timeStr = shift.isAllDay ? "—" : formatTimeRange(shift);
          doc.text(timeStr, margin + 35, yPosition);
          const timeTextEnd = margin + 35 + doc.getTextWidth(timeStr);

          // Calendar name (only for multi-calendar exports)
          let titleX: number;
          if (isMultiCalendar) {
            doc.setFont("helvetica", "italic");
            doc.setFontSize(9);
            const calendarLabel = `[${calendarInfo?.name || "Unknown"}]`;
            const calendarX = Math.max(margin + 70, timeTextEnd + columnGap);
            doc.text(calendarLabel, calendarX, yPosition);
            titleX = Math.max(
              margin + 105,
              calendarX + doc.getTextWidth(calendarLabel) + columnGap
            );
          } else {
            titleX = Math.max(margin + 70, timeTextEnd + columnGap);
          }

          // Shift title
          doc.setFont("helvetica", "bold");
          doc.setFontSize(10);
          doc.text(shift.title, titleX, yPosition);

          yPosition += 5;

          // Notes and custom field values, wrapped and paged together
          const definitions = definitionsByCalendar.get(shift.calendarId) ?? [];
          const customFields = customFieldsByShiftId.get(shift.id) ?? {};
          const fieldLines = definitions
            .map((d) => {
              const raw = customFields[d.key];
              if (raw === null || raw === undefined || raw === "") return null;
              return `${d.label}: ${formatValueForDisplay(d, String(raw), locale)}`;
            })
            .filter((line): line is string => line !== null);
          const noteText = [shift.notes, ...fieldLines].filter(Boolean).join("\n");

          if (noteText) {
            doc.setFont("helvetica", "italic");
            doc.setFontSize(9);
            const notesLines = doc.splitTextToSize(
              noteText,
              pageWidth - margin * 2 - 10
            );
            for (const line of notesLines) {
              checkPageBreak(5);
              doc.text(line, margin + 10, yPosition);
              yPosition += 4;
            }
            doc.setFontSize(10);
            yPosition += 2;
          } else {
            yPosition += 3;
          }
        }

        yPosition += 5; // Extra space between months
      }
    }

    // Generate PDF
    const pdfBuffer = Buffer.from(doc.output("arraybuffer"));

    // Create filename from calendar names (truncated)
    const calendarNamesParts = accessibleCalendars
      .map((c) =>
        c.name
          .replace(/[^a-z0-9]/gi, "_")
          .toLowerCase()
          .substring(0, 20)
      )
      .slice(0, 3); // Max 3 calendar names

    const filename = `${calendarNamesParts.join("_")}_${
      formatDateToLocal(new Date())
    }.pdf`;

    return new NextResponse(pdfBuffer, {
      headers: {
        "Content-Type": "application/pdf",
        "Content-Disposition": `attachment; filename="${filename}"`,
      },
    });
  } catch (error) {
    console.error("Error exporting calendars as PDF:", error);
    return NextResponse.json(
      { error: "Failed to export calendars" },
      { status: 500 }
    );
  }
}