-
-
Notifications
You must be signed in to change notification settings - Fork 964
feat(api): add admin endpoint for updating org feature flags #2891
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
153 changes: 153 additions & 0 deletions
153
apps/webapp/app/routes/admin.api.v1.orgs.$organizationId.feature-flags.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,153 @@ | ||
| import { ActionFunctionArgs, LoaderFunctionArgs, json } from "@remix-run/server-runtime"; | ||
| import { z } from "zod"; | ||
| import { prisma } from "~/db.server"; | ||
| import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server"; | ||
| import { validatePartialFeatureFlags } from "~/v3/featureFlags.server"; | ||
|
|
||
| const ParamsSchema = z.object({ | ||
| organizationId: z.string(), | ||
| }); | ||
|
|
||
| async function authenticateAdmin(request: Request) { | ||
| const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request); | ||
|
|
||
| if (!authenticationResult) { | ||
| return { error: json({ error: "Invalid or Missing API key" }, { status: 401 }) }; | ||
| } | ||
|
|
||
| const user = await prisma.user.findUnique({ | ||
| where: { | ||
| id: authenticationResult.userId, | ||
| }, | ||
| }); | ||
|
|
||
| if (!user) { | ||
| return { error: json({ error: "Invalid or Missing API key" }, { status: 401 }) }; | ||
| } | ||
|
|
||
| if (!user.admin) { | ||
| return { error: json({ error: "You must be an admin to perform this action" }, { status: 403 }) }; | ||
| } | ||
|
|
||
| return { user }; | ||
| } | ||
|
|
||
| export async function loader({ request, params }: LoaderFunctionArgs) { | ||
| const authResult = await authenticateAdmin(request); | ||
|
|
||
| if ("error" in authResult) { | ||
| return authResult.error; | ||
| } | ||
|
|
||
| const { organizationId } = ParamsSchema.parse(params); | ||
|
|
||
| const organization = await prisma.organization.findUnique({ | ||
| where: { | ||
| id: organizationId, | ||
| }, | ||
| select: { | ||
| id: true, | ||
| slug: true, | ||
| featureFlags: true, | ||
| }, | ||
| }); | ||
|
|
||
| if (!organization) { | ||
| return json({ error: "Organization not found" }, { status: 404 }); | ||
| } | ||
|
|
||
| const flagsResult = organization.featureFlags | ||
| ? validatePartialFeatureFlags(organization.featureFlags as Record<string, unknown>) | ||
| : { success: false as const }; | ||
|
|
||
| const featureFlags = flagsResult.success ? flagsResult.data : {}; | ||
|
|
||
| return json({ | ||
| organizationId: organization.id, | ||
| organizationSlug: organization.slug, | ||
| featureFlags, | ||
| }); | ||
| } | ||
|
|
||
| export async function action({ request, params }: ActionFunctionArgs) { | ||
| const authResult = await authenticateAdmin(request); | ||
|
|
||
| if ("error" in authResult) { | ||
| return authResult.error; | ||
| } | ||
|
|
||
| const { organizationId } = ParamsSchema.parse(params); | ||
|
|
||
| const organization = await prisma.organization.findUnique({ | ||
| where: { | ||
| id: organizationId, | ||
| }, | ||
| select: { | ||
| id: true, | ||
| featureFlags: true, | ||
| }, | ||
| }); | ||
|
|
||
| if (!organization) { | ||
| return json({ error: "Organization not found" }, { status: 404 }); | ||
| } | ||
|
|
||
| try { | ||
| const body = await request.json(); | ||
|
|
||
| // Validate the input using the partial schema | ||
| const validationResult = validatePartialFeatureFlags(body as Record<string, unknown>); | ||
| if (!validationResult.success) { | ||
| return json( | ||
| { | ||
| error: "Invalid feature flags data", | ||
| details: validationResult.error.issues, | ||
| }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
|
|
||
| // Merge new flags with existing flags | ||
| const existingFlags = organization.featureFlags | ||
| ? validatePartialFeatureFlags(organization.featureFlags as Record<string, unknown>) | ||
| : { success: false as const }; | ||
|
|
||
| const mergedFlags = { | ||
| ...(existingFlags.success ? existingFlags.data : {}), | ||
| ...validationResult.data, | ||
| }; | ||
|
|
||
| // Update the organization's feature flags | ||
| const updatedOrganization = await prisma.organization.update({ | ||
| where: { | ||
| id: organizationId, | ||
| }, | ||
| data: { | ||
| featureFlags: mergedFlags, | ||
| }, | ||
| select: { | ||
| id: true, | ||
| slug: true, | ||
| featureFlags: true, | ||
| }, | ||
| }); | ||
|
|
||
| const updatedFlagsResult = updatedOrganization.featureFlags | ||
| ? validatePartialFeatureFlags(updatedOrganization.featureFlags as Record<string, unknown>) | ||
| : { success: false as const }; | ||
|
|
||
| return json({ | ||
| success: true, | ||
| organizationId: updatedOrganization.id, | ||
| organizationSlug: updatedOrganization.slug, | ||
| featureFlags: updatedFlagsResult.success ? updatedFlagsResult.data : {}, | ||
| }); | ||
| } catch (error) { | ||
| return json( | ||
| { | ||
| error: error instanceof Error ? error.message : String(error), | ||
| }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.