|
| 1 | +import { type LoaderFunctionArgs } from "@remix-run/server-runtime"; |
| 2 | +import { typedjson } from "remix-typedjson"; |
| 3 | +import { requireUser } from "~/services/session.server"; |
| 4 | +import { prisma } from "~/db.server"; |
| 5 | +import { FEATURE_FLAG, validateFeatureFlagValue } from "~/v3/featureFlags.server"; |
| 6 | +import { OrganizationParamsSchema } from "~/utils/pathBuilder"; |
| 7 | + |
| 8 | +async function hasLogsPageAccess( |
| 9 | + userId: string, |
| 10 | + isAdmin: boolean, |
| 11 | + isImpersonating: boolean, |
| 12 | + organizationSlug: string |
| 13 | +): Promise<boolean> { |
| 14 | + if (isAdmin || isImpersonating) { |
| 15 | + return true; |
| 16 | + } |
| 17 | + |
| 18 | + const organization = await prisma.organization.findFirst({ |
| 19 | + where: { |
| 20 | + slug: organizationSlug, |
| 21 | + members: { some: { userId } }, |
| 22 | + }, |
| 23 | + select: { |
| 24 | + featureFlags: true, |
| 25 | + }, |
| 26 | + }); |
| 27 | + |
| 28 | + if (!organization?.featureFlags) { |
| 29 | + return false; |
| 30 | + } |
| 31 | + |
| 32 | + const flags = organization.featureFlags as Record<string, unknown>; |
| 33 | + const hasLogsPageAccessResult = validateFeatureFlagValue( |
| 34 | + FEATURE_FLAG.hasLogsPageAccess, |
| 35 | + flags.hasLogsPageAccess |
| 36 | + ); |
| 37 | + |
| 38 | + return hasLogsPageAccessResult.success && hasLogsPageAccessResult.data === true; |
| 39 | +} |
| 40 | + |
| 41 | +export const loader = async ({ request, params }: LoaderFunctionArgs) => { |
| 42 | + const user = await requireUser(request); |
| 43 | + const { organizationSlug } = OrganizationParamsSchema.parse(params); |
| 44 | + |
| 45 | + const canViewLogsPage = user.admin || user.isImpersonating || await hasLogsPageAccess( |
| 46 | + user.id, |
| 47 | + user.admin, |
| 48 | + user.isImpersonating, |
| 49 | + organizationSlug |
| 50 | + ); |
| 51 | + |
| 52 | + return typedjson({ canViewLogsPage }); |
| 53 | +}; |
0 commit comments