diff --git a/lib/check-in/service.ts b/lib/check-in/service.ts index 4fda3aa..63334fd 100644 --- a/lib/check-in/service.ts +++ b/lib/check-in/service.ts @@ -1,5 +1,6 @@ import { getAuthenticatedUser } from "@/lib/auth/session"; import { deriveBudgetSnapshot } from "@/lib/check-in/budget"; +import { getLocalDateForTimezone } from "@/lib/dates"; import { ensureProfileBundleForCurrentUser } from "@/lib/profile/service"; import { createClient } from "@/lib/supabase/server"; import type { @@ -53,26 +54,6 @@ function mapMorningCheckInRow(row: MorningCheckInRow): MorningCheckInRecord { }; } -function getLocalDateForTimezone(timezone: string, date = new Date()) { - const formatter = new Intl.DateTimeFormat("en-CA", { - timeZone: timezone, - year: "numeric", - month: "2-digit", - day: "2-digit", - }); - - const parts = formatter.formatToParts(date); - const year = parts.find((part) => part.type === "year")?.value; - const month = parts.find((part) => part.type === "month")?.value; - const day = parts.find((part) => part.type === "day")?.value; - - if (!year || !month || !day) { - throw new Error("Lokale datum voor timezone kon niet worden bepaald."); - } - - return `${year}-${month}-${day}`; -} - async function readMorningCheckInByDate( supabase: SupabaseServerClient, userId: string, diff --git a/lib/dates.ts b/lib/dates.ts new file mode 100644 index 0000000..f1950df --- /dev/null +++ b/lib/dates.ts @@ -0,0 +1,19 @@ +export function getLocalDateForTimezone(timezone: string, date = new Date()) { + const formatter = new Intl.DateTimeFormat("en-CA", { + timeZone: timezone, + year: "numeric", + month: "2-digit", + day: "2-digit", + }); + + const parts = formatter.formatToParts(date); + const year = parts.find((part) => part.type === "year")?.value; + const month = parts.find((part) => part.type === "month")?.value; + const day = parts.find((part) => part.type === "day")?.value; + + if (!year || !month || !day) { + throw new Error("Lokale datum voor timezone kon niet worden bepaald."); + } + + return `${year}-${month}-${day}`; +} diff --git a/lib/planning/service.ts b/lib/planning/service.ts index f226276..e5e11b2 100644 --- a/lib/planning/service.ts +++ b/lib/planning/service.ts @@ -1,4 +1,5 @@ import { getAuthenticatedUser } from "@/lib/auth/session"; +import { getLocalDateForTimezone } from "@/lib/dates"; import { calculateDayOverviewSnapshot } from "@/lib/planning/day-overview"; import type { ActivityCategory, @@ -104,26 +105,6 @@ function mapActivityRow(row: ActivityRow): ActivityRecord { }; } -function getLocalDateForTimezone(timezone: string, date = new Date()) { - const formatter = new Intl.DateTimeFormat("en-CA", { - timeZone: timezone, - year: "numeric", - month: "2-digit", - day: "2-digit", - }); - - const parts = formatter.formatToParts(date); - const year = parts.find((part) => part.type === "year")?.value; - const month = parts.find((part) => part.type === "month")?.value; - const day = parts.find((part) => part.type === "day")?.value; - - if (!year || !month || !day) { - throw new Error("Lokale plandatum voor timezone kon niet worden bepaald."); - } - - return `${year}-${month}-${day}`; -} - function assertIsoDate(value: string) { if (!ISO_DATE_PATTERN.test(value)) { throw new Error("Ongeldige plandatum. Gebruik het formaat YYYY-MM-DD.");