broadcast.ts 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. import { cache } from 'react'
  2. import { db } from '@/lib/db'
  3. import {
  4. BROADCAST_LEVELS,
  5. BROADCAST_MAX_LENGTH,
  6. SYSTEM_SETTING_KEYS,
  7. type BroadcastLevel,
  8. } from '@/features/admin/Schema/systemSettingsSchema'
  9. /**
  10. * Pages a workshop's own customers see, where a platform notice must not go.
  11. *
  12. * Deliberately its own list rather than reuse of the locale helper, which
  13. * matches on an org id and so lets bare `/portal` and `/terms` through. This
  14. * one is a promise to every workshop, and it is a prefix check on purpose:
  15. * anything added under these roots is covered without anyone remembering to
  16. * come back here.
  17. *
  18. * Two reasons it matters. These pages carry the workshop's branding, not ours,
  19. * and a white-label licence exists precisely so Torqvoice does not appear on
  20. * that paperwork. And a notice aimed at staff, about maintenance or an
  21. * incident, is not something a customer opening an invoice should read.
  22. */
  23. const CUSTOMER_FACING = ['/portal', '/share', '/terms']
  24. export function isCustomerFacingPath(pathname: string | null | undefined): boolean {
  25. if (!pathname) return false
  26. return CUSTOMER_FACING.some((root) => pathname === root || pathname.startsWith(`${root}/`))
  27. }
  28. export type Broadcast = {
  29. message: string
  30. level: BroadcastLevel
  31. /** Changes whenever the notice does, which is what resets dismissals. */
  32. updatedAt: string
  33. }
  34. /**
  35. * The notice every visitor sees, if there is one.
  36. *
  37. * Read without authentication on purpose. An infrastructure incident is worth
  38. * saying on the sign-in page too, and someone who cannot get in is exactly the
  39. * person who most needs to know why.
  40. *
  41. * Cached per request: this runs in the root layout, so it is on the path of
  42. * every page in the app, and it must stay a single indexed lookup that returns
  43. * nothing almost every time.
  44. */
  45. export const getBroadcast = cache(async (): Promise<Broadcast | null> => {
  46. let rows: { key: string; value: string }[]
  47. try {
  48. rows = await db.systemSetting.findMany({
  49. where: {
  50. key: {
  51. in: [
  52. SYSTEM_SETTING_KEYS.BROADCAST_MESSAGE,
  53. SYSTEM_SETTING_KEYS.BROADCAST_LEVEL,
  54. SYSTEM_SETTING_KEYS.BROADCAST_UPDATED_AT,
  55. ],
  56. },
  57. },
  58. select: { key: true, value: true },
  59. })
  60. } catch {
  61. // The banner sits in the root layout, so a database that is down or not
  62. // yet migrated must not take every page down with it. No notice is the
  63. // right answer when we cannot read one.
  64. return null
  65. }
  66. const stored = new Map(rows.map((row) => [row.key, row.value]))
  67. const message = stored.get(SYSTEM_SETTING_KEYS.BROADCAST_MESSAGE)?.trim()
  68. if (!message) return null
  69. const level = stored.get(SYSTEM_SETTING_KEYS.BROADCAST_LEVEL) as BroadcastLevel | undefined
  70. return {
  71. message: message.slice(0, BROADCAST_MAX_LENGTH),
  72. level: level && BROADCAST_LEVELS.includes(level) ? level : 'info',
  73. // Falling back to the text itself still gives dismissals something stable
  74. // to key on, for a notice written before the timestamp existed.
  75. updatedAt: stored.get(SYSTEM_SETTING_KEYS.BROADCAST_UPDATED_AT) ?? message,
  76. }
  77. })