get-layout-data.ts 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. role: string;
  12. isSuperAdmin: boolean;
  13. emailVerified: boolean;
  14. companyLogo: string | undefined;
  15. dateFormat: string | undefined;
  16. timeFormat: string | undefined;
  17. timezone: string | undefined;
  18. serviceType: string | undefined;
  19. organizations: { id: string; name: string; role: string }[];
  20. };
  21. export async function getLayoutData(): Promise<AuthResult> {
  22. const session = await getCachedSession();
  23. if (!session?.user?.id) return { status: "unauthenticated" };
  24. const [membership, user] = await Promise.all([
  25. getCachedMembership(session.user.id),
  26. db.user.findUnique({
  27. where: { id: session.user.id },
  28. select: { isSuperAdmin: true, emailVerified: true },
  29. }),
  30. ]);
  31. // User record deleted (e.g. admin removed the account) but session cookie still cached
  32. if (!user) return { status: "unauthenticated" };
  33. const isSuperAdmin = user.isSuperAdmin;
  34. if (!membership && !isSuperAdmin) return { status: "no-organization" };
  35. const [orgSettings, memberships] = await Promise.all([
  36. membership
  37. ? db.appSetting.findMany({
  38. where: {
  39. organizationId: membership.organizationId,
  40. key: {
  41. in: [
  42. SETTING_KEYS.COMPANY_LOGO,
  43. SETTING_KEYS.DATE_FORMAT,
  44. SETTING_KEYS.TIME_FORMAT,
  45. SETTING_KEYS.TIMEZONE,
  46. SETTING_KEYS.SERVICE_TYPE,
  47. ],
  48. },
  49. },
  50. select: { key: true, value: true },
  51. })
  52. : null,
  53. db.organizationMember.findMany({
  54. where: { userId: session.user.id },
  55. select: {
  56. role: true,
  57. organization: { select: { id: true, name: true } },
  58. },
  59. }),
  60. ]);
  61. const orgMap = new Map(orgSettings?.map((s) => [s.key, s.value]) ?? []);
  62. return {
  63. status: "ok",
  64. userId: session.user.id,
  65. organizationId: membership?.organizationId ?? "",
  66. role: isSuperAdmin ? "super_admin" : (membership?.role ?? "member"),
  67. isSuperAdmin,
  68. emailVerified: user?.emailVerified ?? false,
  69. companyLogo: orgMap.get(SETTING_KEYS.COMPANY_LOGO) || undefined,
  70. dateFormat: orgMap.get(SETTING_KEYS.DATE_FORMAT) || undefined,
  71. timeFormat: orgMap.get(SETTING_KEYS.TIME_FORMAT) || undefined,
  72. timezone: orgMap.get(SETTING_KEYS.TIMEZONE) || undefined,
  73. serviceType: orgMap.get(SETTING_KEYS.SERVICE_TYPE) || undefined,
  74. organizations: memberships.map((m) => ({
  75. id: m.organization.id,
  76. name: m.organization.name,
  77. role: m.role,
  78. })),
  79. };
  80. }