get-layout-data.ts 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. import { getCachedSession, getCachedMembership } from './cached-session'
  2. import { db } from './db'
  3. import { SETTING_KEYS } from '@/features/settings/Schema/settingsSchema'
  4. type AuthResult =
  5. | { status: 'unauthenticated' }
  6. | { status: 'no-organization' }
  7. | {
  8. status: 'ok'
  9. userId: string
  10. organizationId: string
  11. /** When the active workshop signed up, for age-gated announcements. */
  12. organizationCreatedAt: Date | null
  13. role: string
  14. isSuperAdmin: boolean
  15. emailVerified: boolean
  16. lastSeenVersion: string | null
  17. companyLogo: string | undefined
  18. dateFormat: string | undefined
  19. timeFormat: string | undefined
  20. timezone: string | undefined
  21. weekStartDay: number
  22. serviceType: string | undefined
  23. currencyCode: string | undefined
  24. currencyFormat: string | undefined
  25. organizations: { id: string; name: string; role: string }[]
  26. }
  27. export async function getLayoutData(): Promise<AuthResult> {
  28. const session = await getCachedSession()
  29. if (!session?.user?.id) return { status: 'unauthenticated' }
  30. const [membership, user] = await Promise.all([
  31. getCachedMembership(session.user.id),
  32. db.user.findUnique({
  33. where: { id: session.user.id },
  34. select: { isSuperAdmin: true, emailVerified: true, lastSeenVersion: true },
  35. }),
  36. ])
  37. // User record deleted (e.g. admin removed the account) but session cookie still cached
  38. if (!user) return { status: 'unauthenticated' }
  39. const isSuperAdmin = user.isSuperAdmin
  40. if (!membership && !isSuperAdmin) return { status: 'no-organization' }
  41. const [orgSettings, memberships] = await Promise.all([
  42. membership
  43. ? db.appSetting.findMany({
  44. where: {
  45. organizationId: membership.organizationId,
  46. key: {
  47. in: [
  48. SETTING_KEYS.COMPANY_LOGO,
  49. SETTING_KEYS.DATE_FORMAT,
  50. SETTING_KEYS.TIME_FORMAT,
  51. SETTING_KEYS.TIMEZONE,
  52. SETTING_KEYS.WORKBOARD_WEEK_START_DAY,
  53. SETTING_KEYS.SERVICE_TYPE,
  54. SETTING_KEYS.CURRENCY_CODE,
  55. SETTING_KEYS.CURRENCY_FORMAT,
  56. ],
  57. },
  58. },
  59. select: { key: true, value: true },
  60. })
  61. : null,
  62. db.organizationMember.findMany({
  63. where: { userId: session.user.id },
  64. select: {
  65. role: true,
  66. organization: { select: { id: true, name: true, createdAt: true } },
  67. },
  68. }),
  69. ])
  70. const orgMap = new Map(orgSettings?.map((s) => [s.key, s.value]) ?? [])
  71. return {
  72. status: 'ok',
  73. userId: session.user.id,
  74. organizationId: membership?.organizationId ?? '',
  75. organizationCreatedAt:
  76. memberships.find((m) => m.organization.id === membership?.organizationId)?.organization
  77. .createdAt ?? null,
  78. role: isSuperAdmin ? 'super_admin' : (membership?.role ?? 'member'),
  79. isSuperAdmin,
  80. emailVerified: user?.emailVerified ?? false,
  81. lastSeenVersion: user?.lastSeenVersion ?? null,
  82. companyLogo: orgMap.get(SETTING_KEYS.COMPANY_LOGO) || undefined,
  83. dateFormat: orgMap.get(SETTING_KEYS.DATE_FORMAT) || undefined,
  84. timeFormat: orgMap.get(SETTING_KEYS.TIME_FORMAT) || undefined,
  85. timezone: orgMap.get(SETTING_KEYS.TIMEZONE) || undefined,
  86. weekStartDay: parseInt(orgMap.get(SETTING_KEYS.WORKBOARD_WEEK_START_DAY) || '1', 10),
  87. serviceType: orgMap.get(SETTING_KEYS.SERVICE_TYPE) || undefined,
  88. currencyCode: orgMap.get(SETTING_KEYS.CURRENCY_CODE) || undefined,
  89. currencyFormat: orgMap.get(SETTING_KEYS.CURRENCY_FORMAT) || undefined,
  90. organizations: memberships.map((m) => ({
  91. id: m.organization.id,
  92. name: m.organization.name,
  93. role: m.role,
  94. })),
  95. }
  96. }