workshop-timezone.ts 1.1 KB

123456789101112131415161718192021222324252627282930
  1. import { SETTING_KEYS } from '@/features/settings/Schema/settingsSchema'
  2. import { db } from '@/lib/db'
  3. import { safeTimeZone } from '@/lib/timezone'
  4. /**
  5. * The zone server code reads workshop times in.
  6. *
  7. * The localisation page offers "automatic", which the browser resolves and
  8. * the server cannot. Saving that page also stores what the browser resolved,
  9. * so a workshop that never chose a zone still gets the right one here rather
  10. * than UTC. An explicit choice always wins.
  11. */
  12. export function resolveWorkshopTimeZone(
  13. explicit: string | null | undefined,
  14. detected: string | null | undefined
  15. ): string {
  16. return safeTimeZone(explicit?.trim() || detected?.trim() || null)
  17. }
  18. export async function workshopTimeZone(organizationId: string): Promise<string> {
  19. const rows = await db.appSetting.findMany({
  20. where: { organizationId, key: { in: [SETTING_KEYS.TIMEZONE, SETTING_KEYS.TIMEZONE_DETECTED] } },
  21. select: { key: true, value: true },
  22. })
  23. const map = new Map(rows.map((r) => [r.key, r.value]))
  24. return resolveWorkshopTimeZone(
  25. map.get(SETTING_KEYS.TIMEZONE),
  26. map.get(SETTING_KEYS.TIMEZONE_DETECTED)
  27. )
  28. }