|
| 1 | +import { type ActionFunctionArgs, type LoaderFunctionArgs, json } from "@remix-run/server-runtime"; |
| 2 | +import { z } from "zod"; |
| 3 | +import { prisma } from "~/db.server"; |
| 4 | +import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server"; |
| 5 | + |
| 6 | +async function requireAdmin(request: Request) { |
| 7 | + const authResult = await authenticateApiRequestWithPersonalAccessToken(request); |
| 8 | + if (!authResult) { |
| 9 | + throw json({ error: "Invalid or Missing API key" }, { status: 401 }); |
| 10 | + } |
| 11 | + |
| 12 | + const user = await prisma.user.findUnique({ where: { id: authResult.userId } }); |
| 13 | + if (!user?.admin) { |
| 14 | + throw json({ error: "You must be an admin to perform this action" }, { status: 403 }); |
| 15 | + } |
| 16 | + |
| 17 | + return user; |
| 18 | +} |
| 19 | + |
| 20 | +export async function loader({ request, params }: LoaderFunctionArgs) { |
| 21 | + await requireAdmin(request); |
| 22 | + |
| 23 | + const model = await prisma.llmModel.findUnique({ |
| 24 | + where: { id: params.modelId }, |
| 25 | + include: { |
| 26 | + pricingTiers: { |
| 27 | + include: { prices: true }, |
| 28 | + orderBy: { priority: "asc" }, |
| 29 | + }, |
| 30 | + }, |
| 31 | + }); |
| 32 | + |
| 33 | + if (!model) { |
| 34 | + return json({ error: "Model not found" }, { status: 404 }); |
| 35 | + } |
| 36 | + |
| 37 | + return json({ model }); |
| 38 | +} |
| 39 | + |
| 40 | +const UpdateModelSchema = z.object({ |
| 41 | + modelName: z.string().min(1).optional(), |
| 42 | + matchPattern: z.string().min(1).optional(), |
| 43 | + startDate: z.string().nullable().optional(), |
| 44 | + pricingTiers: z |
| 45 | + .array( |
| 46 | + z.object({ |
| 47 | + name: z.string().min(1), |
| 48 | + isDefault: z.boolean().default(true), |
| 49 | + priority: z.number().int().default(0), |
| 50 | + conditions: z |
| 51 | + .array( |
| 52 | + z.object({ |
| 53 | + usageDetailPattern: z.string(), |
| 54 | + operator: z.enum(["gt", "gte", "lt", "lte", "eq", "neq"]), |
| 55 | + value: z.number(), |
| 56 | + }) |
| 57 | + ) |
| 58 | + .default([]), |
| 59 | + prices: z.record(z.string(), z.number()), |
| 60 | + }) |
| 61 | + ) |
| 62 | + .optional(), |
| 63 | +}); |
| 64 | + |
| 65 | +export async function action({ request, params }: ActionFunctionArgs) { |
| 66 | + await requireAdmin(request); |
| 67 | + |
| 68 | + const modelId = params.modelId!; |
| 69 | + |
| 70 | + if (request.method === "DELETE") { |
| 71 | + const existing = await prisma.llmModel.findUnique({ where: { id: modelId } }); |
| 72 | + if (!existing) { |
| 73 | + return json({ error: "Model not found" }, { status: 404 }); |
| 74 | + } |
| 75 | + |
| 76 | + await prisma.llmModel.delete({ where: { id: modelId } }); |
| 77 | + return json({ success: true }); |
| 78 | + } |
| 79 | + |
| 80 | + if (request.method !== "PUT") { |
| 81 | + return json({ error: "Method not allowed" }, { status: 405 }); |
| 82 | + } |
| 83 | + |
| 84 | + const body = await request.json(); |
| 85 | + const parsed = UpdateModelSchema.safeParse(body); |
| 86 | + |
| 87 | + if (!parsed.success) { |
| 88 | + return json({ error: "Invalid request body", details: parsed.error.issues }, { status: 400 }); |
| 89 | + } |
| 90 | + |
| 91 | + const { modelName, matchPattern, startDate, pricingTiers } = parsed.data; |
| 92 | + |
| 93 | + // Validate regex if provided |
| 94 | + if (matchPattern) { |
| 95 | + try { |
| 96 | + new RegExp(matchPattern); |
| 97 | + } catch { |
| 98 | + return json({ error: "Invalid regex in matchPattern" }, { status: 400 }); |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + // Update model fields |
| 103 | + const model = await prisma.llmModel.update({ |
| 104 | + where: { id: modelId }, |
| 105 | + data: { |
| 106 | + ...(modelName !== undefined && { modelName }), |
| 107 | + ...(matchPattern !== undefined && { matchPattern }), |
| 108 | + ...(startDate !== undefined && { startDate: startDate ? new Date(startDate) : null }), |
| 109 | + }, |
| 110 | + }); |
| 111 | + |
| 112 | + // If pricing tiers provided, replace them entirely |
| 113 | + if (pricingTiers) { |
| 114 | + // Delete existing tiers (cascades to prices) |
| 115 | + await prisma.llmPricingTier.deleteMany({ where: { modelId } }); |
| 116 | + |
| 117 | + // Create new tiers |
| 118 | + for (const tier of pricingTiers) { |
| 119 | + await prisma.llmPricingTier.create({ |
| 120 | + data: { |
| 121 | + modelId, |
| 122 | + name: tier.name, |
| 123 | + isDefault: tier.isDefault, |
| 124 | + priority: tier.priority, |
| 125 | + conditions: tier.conditions, |
| 126 | + prices: { |
| 127 | + create: Object.entries(tier.prices).map(([usageType, price]) => ({ |
| 128 | + modelId, |
| 129 | + usageType, |
| 130 | + price, |
| 131 | + })), |
| 132 | + }, |
| 133 | + }, |
| 134 | + }); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + const updated = await prisma.llmModel.findUnique({ |
| 139 | + where: { id: modelId }, |
| 140 | + include: { |
| 141 | + pricingTiers: { |
| 142 | + include: { prices: true }, |
| 143 | + orderBy: { priority: "asc" }, |
| 144 | + }, |
| 145 | + }, |
| 146 | + }); |
| 147 | + |
| 148 | + return json({ model: updated }); |
| 149 | +} |
0 commit comments