|
| 1 | +import { type Prisma, type WorkloadType } from "@trigger.dev/database"; |
| 2 | +import { type PrismaClientOrTransaction } from "~/db.server"; |
| 3 | +import { FEATURE_FLAG } from "./featureFlags"; |
| 4 | +import { makeFlag } from "./featureFlags.server"; |
| 5 | + |
| 6 | +/** |
| 7 | + * Resolves whether an org has compute access based on feature flags. |
| 8 | + */ |
| 9 | +export async function resolveComputeAccess( |
| 10 | + prisma: PrismaClientOrTransaction, |
| 11 | + orgFeatureFlags: unknown |
| 12 | +): Promise<boolean> { |
| 13 | + const flag = makeFlag(prisma); |
| 14 | + return flag({ |
| 15 | + key: FEATURE_FLAG.hasComputeAccess, |
| 16 | + defaultValue: false, |
| 17 | + overrides: (orgFeatureFlags as Record<string, unknown>) ?? {}, |
| 18 | + }); |
| 19 | +} |
| 20 | + |
| 21 | +/** |
| 22 | + * Builds a visibility filter for non-admin, non-allowlisted users. |
| 23 | + * Without compute access, MICROVM regions are excluded entirely. |
| 24 | + * With compute access, hidden flag works normally (existing behavior). |
| 25 | + */ |
| 26 | +export function defaultVisibilityFilter( |
| 27 | + hasComputeAccess: boolean |
| 28 | +): Prisma.WorkerInstanceGroupWhereInput { |
| 29 | + if (hasComputeAccess) { |
| 30 | + return { hidden: false }; |
| 31 | + } |
| 32 | + |
| 33 | + return { hidden: false, workloadType: { not: "MICROVM" } }; |
| 34 | +} |
| 35 | + |
| 36 | +/** |
| 37 | + * Whether a region is accessible given compute access. |
| 38 | + * MICROVM regions require compute access; all other types pass through. |
| 39 | + */ |
| 40 | +export function isComputeRegionAccessible( |
| 41 | + region: { workloadType: WorkloadType }, |
| 42 | + hasComputeAccess: boolean |
| 43 | +): boolean { |
| 44 | + if (region.workloadType !== "MICROVM") { |
| 45 | + return true; |
| 46 | + } |
| 47 | + |
| 48 | + // Allow access to any MICROVM region if the org has compute access |
| 49 | + return hasComputeAccess; |
| 50 | +} |
0 commit comments