Your favorite shift management application!
fix(api): serve shift and note dates as calendar days (#229)
panteL6cca9a05f366
diff --git a/app/api/calendars/[id]/route.ts b/app/api/calendars/[id]/route.tsindex 438602d..e5aeac7 100644--- a/app/api/calendars/[id]/route.ts+++ b/app/api/calendars/[id]/route.ts@@ -1,5 +1,6 @@import { NextRequest, NextResponse } from "next/server";import { db } from "@/lib/db";+import { withCalendarDay } from "@/lib/date-utils";import {calendars,shifts,@@ -66,7 +67,7 @@ export async function GET(.where(eq(shifts.calendarId, id)).orderBy(shifts.date);- return NextResponse.json({ ...calendar, shifts: calendarShifts });+ return NextResponse.json({ ...calendar, shifts: calendarShifts.map(withCalendarDay) });} catch (error) {console.error("Failed to fetch calendar:", error);return NextResponse.json(diff --git a/app/api/notes/[id]/route.ts b/app/api/notes/[id]/route.tsindex 0ce1343..88ebf09 100644--- a/app/api/notes/[id]/route.ts+++ b/app/api/notes/[id]/route.ts@@ -1,5 +1,6 @@import { NextResponse } from "next/server";import { db } from "@/lib/db";+import { withCalendarDay } from "@/lib/date-utils";import { calendarNotes, calendars } from "@/lib/db/schema";import { eq } from "drizzle-orm";import { getSessionUser } from "@/lib/auth/sessions";@@ -52,7 +53,7 @@ export async function GET();}- return NextResponse.json(result[0]);+ return NextResponse.json(withCalendarDay(result[0]));} catch (error) {console.error("Failed to fetch calendar note:", error);return NextResponse.json(@@ -157,7 +158,7 @@ export async function PUT();}- return NextResponse.json(updated);+ return NextResponse.json(withCalendarDay(updated));} catch (error) {console.error("Failed to update calendar note:", error);return NextResponse.json(diff --git a/app/api/notes/route.ts b/app/api/notes/route.tsindex f12492d..6119310 100644--- a/app/api/notes/route.ts+++ b/app/api/notes/route.ts@@ -4,7 +4,7 @@ import { calendarNotes, calendars } from "@/lib/db/schema";import { eq, and, gte, lte } from "drizzle-orm";import { getSessionUser } from "@/lib/auth/sessions";import { hasCapability, hasOwnedCapability } from "@/lib/auth/permissions";-import { parseLocalDate } from "@/lib/date-utils";+import { parseLocalDate, withCalendarDay } from "@/lib/date-utils";// GET calendar notes for a calendar (with optional date filter)export async function GET(request: Request) {@@ -73,11 +73,11 @@ export async function GET(request: Request) {lte(calendarNotes.date, endOfDay)));- return NextResponse.json(result);+ return NextResponse.json(result.map(withCalendarDay));}const result = await query;- return NextResponse.json(result);+ return NextResponse.json(result.map(withCalendarDay));} catch (error) {console.error("Failed to fetch calendar notes:", error);return NextResponse.json(@@ -172,7 +172,7 @@ export async function POST(request: Request) {}).returning();- return NextResponse.json(calendarNote);+ return NextResponse.json(withCalendarDay(calendarNote));} catch (error) {console.error("Failed to create calendar note:", error);return NextResponse.json(diff --git a/app/api/shifts/[id]/route.ts b/app/api/shifts/[id]/route.tsindex ec33070..1ccbcdf 100644--- a/app/api/shifts/[id]/route.ts+++ b/app/api/shifts/[id]/route.ts@@ -4,7 +4,7 @@ import { calendars, shiftPresets, shifts } from "@/lib/db/schema";import { and, eq } from "drizzle-orm";import { getSessionUser } from "@/lib/auth/sessions";import { hasCapability, hasOwnedCapability } from "@/lib/auth/permissions";-import { parseLocalDate } from "@/lib/date-utils";+import { parseLocalDate, withCalendarDay } from "@/lib/date-utils";import { replaceShiftSegments, withShiftSegments } from "@/lib/shift-time-ranges";import { normalizeTimeRanges, toTimeRanges, validateTimeRanges, type TimeRange } from "@/lib/time-ranges";import {@@ -68,10 +68,10 @@ export async function GET(}if (!result[0].calendar?.splitShiftsEnabled) {- return NextResponse.json(result[0]);+ return NextResponse.json(withCalendarDay(result[0]));}const [withSegments] = await withShiftSegments([result[0]]);- return NextResponse.json(withSegments);+ return NextResponse.json(withCalendarDay(withSegments));} catch (error) {console.error("Failed to fetch shift:", error);return NextResponse.json(@@ -293,7 +293,7 @@ export async function PUT(const [withSegments] = await withShiftSegments([updatedShift]);const [withCustomFields] = await withShiftCustomFields([withSegments], customFieldDefinitions);- return NextResponse.json(withCustomFields);+ return NextResponse.json(withCalendarDay(withCustomFields));} catch (error) {console.error("Failed to update shift:", error);return NextResponse.json(diff --git a/app/api/shifts/route.ts b/app/api/shifts/route.tsindex cb7269d..691771b 100644--- a/app/api/shifts/route.ts+++ b/app/api/shifts/route.ts@@ -5,7 +5,7 @@ import { eq, and, gte, lte, or, isNull } from "drizzle-orm";import { getSessionUser } from "@/lib/auth/sessions";import { hasCapability, getCalendarAccess } from "@/lib/auth/permissions";import { addShiftSignup, withSignups } from "@/lib/shift-signups";-import { parseLocalDate } from "@/lib/date-utils";+import { parseLocalDate, withCalendarDay } from "@/lib/date-utils";import type { CalendarMember } from "@/lib/types";import { withShiftSegments, replaceShiftSegments, withPresetSegments } from "@/lib/shift-time-ranges";import { normalizeTimeRanges, toTimeRanges, validateTimeRanges, type TimeRange } from "@/lib/time-ranges";@@ -117,7 +117,9 @@ export async function GET(request: Request) {? await withShiftSegments(withSigs): withSigs;const definitions = await getCalendarCustomFields(calendarId);- return NextResponse.json(await withShiftCustomFields(withSegs, definitions));+ return NextResponse.json(+ (await withShiftCustomFields(withSegs, definitions)).map(withCalendarDay)+ );}const result = await query.where(@@ -132,7 +134,9 @@ export async function GET(request: Request) {? await withShiftSegments(withSigs): withSigs;const definitions = await getCalendarCustomFields(calendarId);- return NextResponse.json(await withShiftCustomFields(withSegs, definitions));+ return NextResponse.json(+ (await withShiftCustomFields(withSegs, definitions)).map(withCalendarDay)+ );} catch (error) {console.error("Failed to fetch shifts:", error);return NextResponse.json(@@ -388,7 +392,7 @@ export async function POST(request: Request) {const [withCustomFields] = await withShiftCustomFields([shift], customFieldDefinitions);return NextResponse.json({- ...withCustomFields,+ ...withCalendarDay(withCustomFields),calendar,signups,segments: segmentsToPersist,diff --git a/lib/date-utils.ts b/lib/date-utils.tsindex 0069ce8..2384f55 100644--- a/lib/date-utils.ts+++ b/lib/date-utils.ts@@ -73,6 +73,16 @@ export function formatPeriodCaption(return formatWeekRange(start, end, locale, withYear);}+/**+ * Shift and note days are stored as server-local midnight; sending that instant+ * would let each browser re-read it in its own timezone and land on the previous day.+ */+export function withCalendarDay<T extends { date: Date }>(+ row: T+): Omit<T, "date"> & { date: string } {+ return { ...row, date: formatDateToLocal(row.date) };+}+const LOCAL_DATE_PATTERN = /^\d{4}-\d{2}-\d{2}$/;/**