|
| 1 | +import { ActionFunctionArgs, json } from "@remix-run/server-runtime"; |
| 2 | +import { prisma } from "~/db.server"; |
| 3 | +import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server"; |
| 4 | +import { getRunsReplicationGlobal } from "~/services/runsReplicationGlobal.server"; |
| 5 | +import { runsReplicationInstance } from "~/services/runsReplicationInstance.server"; |
| 6 | +import { |
| 7 | + makeSetFlags, |
| 8 | + setFlags, |
| 9 | + FeatureFlagCatalogSchema, |
| 10 | + validateAllFeatureFlags, |
| 11 | + validatePartialFeatureFlags, |
| 12 | + makeSetMultipleFlags, |
| 13 | +} from "~/v3/featureFlags.server"; |
| 14 | +import { z } from "zod"; |
| 15 | + |
| 16 | +export async function action({ request }: ActionFunctionArgs) { |
| 17 | + // Next authenticate the request |
| 18 | + const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request); |
| 19 | + |
| 20 | + if (!authenticationResult) { |
| 21 | + return json({ error: "Invalid or Missing API key" }, { status: 401 }); |
| 22 | + } |
| 23 | + |
| 24 | + const user = await prisma.user.findUnique({ |
| 25 | + where: { |
| 26 | + id: authenticationResult.userId, |
| 27 | + }, |
| 28 | + }); |
| 29 | + |
| 30 | + if (!user) { |
| 31 | + return json({ error: "Invalid or Missing API key" }, { status: 401 }); |
| 32 | + } |
| 33 | + |
| 34 | + if (!user.admin) { |
| 35 | + return json({ error: "You must be an admin to perform this action" }, { status: 403 }); |
| 36 | + } |
| 37 | + |
| 38 | + try { |
| 39 | + // Parse the request body |
| 40 | + const body = await request.json(); |
| 41 | + |
| 42 | + // Validate the input using the partial schema |
| 43 | + const validationResult = validatePartialFeatureFlags(body as Record<string, unknown>); |
| 44 | + if (!validationResult.success) { |
| 45 | + return json( |
| 46 | + { |
| 47 | + error: "Invalid feature flags data", |
| 48 | + details: validationResult.error.issues, |
| 49 | + }, |
| 50 | + { status: 400 } |
| 51 | + ); |
| 52 | + } |
| 53 | + |
| 54 | + const featureFlags = validationResult.data; |
| 55 | + const setMultipleFlags = makeSetMultipleFlags(prisma); |
| 56 | + const updatedFlags = await setMultipleFlags(featureFlags); |
| 57 | + |
| 58 | + return json({ |
| 59 | + success: true, |
| 60 | + updatedFlags, |
| 61 | + message: `Updated ${updatedFlags.length} feature flag(s)`, |
| 62 | + }); |
| 63 | + } catch (error) { |
| 64 | + return json( |
| 65 | + { |
| 66 | + error: error instanceof Error ? error.message : String(error), |
| 67 | + }, |
| 68 | + { status: 400 } |
| 69 | + ); |
| 70 | + } |
| 71 | +} |
0 commit comments