|
| 1 | +/** |
| 2 | + * PATCH /api/v1/admin/users/:id/modules |
| 3 | + * |
| 4 | + * Toggle per-user feature module flags (e.g. ourmoji). |
| 5 | + * Merges the supplied flags into the existing `enabled_modules` JSON field |
| 6 | + * on `user_preferences`, creating the row if it doesn't exist. |
| 7 | + * |
| 8 | + * Body: { "ourmoji": true, "someOtherFlag": false } |
| 9 | + */ |
| 10 | + |
| 11 | +import * as z from "zod"; |
| 12 | +import { nanoid } from "nanoid"; |
| 13 | +import { requireAdmin } from "~/server/utils/admin"; |
| 14 | +import { success, notFound, validationError } from "~/server/utils/response"; |
| 15 | +import { logAuthEvent } from "~/server/utils/authEvents"; |
| 16 | +import { db } from "~/server/db"; |
| 17 | +import { users, userPreferences } from "~/server/db/schema"; |
| 18 | +import { eq, sql } from "drizzle-orm"; |
| 19 | + |
| 20 | +const bodySchema = z.record(z.string(), z.boolean()).refine( |
| 21 | + (obj) => Object.keys(obj).length > 0, |
| 22 | + { message: "At least one module flag is required" }, |
| 23 | +); |
| 24 | + |
| 25 | +export default defineEventHandler(async (event) => { |
| 26 | + requireAdmin(event, "admin:users:write"); |
| 27 | + |
| 28 | + const userId = getRouterParam(event, "id"); |
| 29 | + if (!userId) { |
| 30 | + throw createError(notFound(event, "User")); |
| 31 | + } |
| 32 | + |
| 33 | + // Verify user exists |
| 34 | + const user = await db |
| 35 | + .select({ id: users.id }) |
| 36 | + .from(users) |
| 37 | + .where(eq(users.id, userId)) |
| 38 | + .limit(1); |
| 39 | + |
| 40 | + if (user.length === 0) { |
| 41 | + throw createError(notFound(event, "User")); |
| 42 | + } |
| 43 | + |
| 44 | + const body = await readBody(event); |
| 45 | + const parseResult = bodySchema.safeParse(body); |
| 46 | + |
| 47 | + if (!parseResult.success) { |
| 48 | + const errors: Record<string, string[]> = {}; |
| 49 | + for (const issue of parseResult.error.issues) { |
| 50 | + const path = issue.path.join(".") || "_root"; |
| 51 | + if (!errors[path]) errors[path] = []; |
| 52 | + errors[path].push(issue.message); |
| 53 | + } |
| 54 | + throw createError(validationError(event, errors)); |
| 55 | + } |
| 56 | + |
| 57 | + const flags = parseResult.data; |
| 58 | + |
| 59 | + // Fetch existing preferences row |
| 60 | + const existing = await db |
| 61 | + .select() |
| 62 | + .from(userPreferences) |
| 63 | + .where(eq(userPreferences.userId, userId)) |
| 64 | + .limit(1); |
| 65 | + |
| 66 | + let previousModules: Record<string, boolean> = {}; |
| 67 | + let updatedModules: Record<string, boolean>; |
| 68 | + |
| 69 | + if (existing.length === 0) { |
| 70 | + // Create preferences row with the supplied flags |
| 71 | + updatedModules = { ...flags }; |
| 72 | + await db.insert(userPreferences).values({ |
| 73 | + id: nanoid(), |
| 74 | + userId, |
| 75 | + enabledModules: updatedModules, |
| 76 | + }); |
| 77 | + } else { |
| 78 | + // Merge flags into existing enabled_modules |
| 79 | + const current = existing[0].enabledModules; |
| 80 | + previousModules = |
| 81 | + current && typeof current === "object" ? { ...current } : {}; |
| 82 | + updatedModules = { ...previousModules, ...flags }; |
| 83 | + |
| 84 | + await db |
| 85 | + .update(userPreferences) |
| 86 | + .set({ |
| 87 | + enabledModules: updatedModules, |
| 88 | + updatedAt: sql`(datetime('now'))`, |
| 89 | + }) |
| 90 | + .where(eq(userPreferences.userId, userId)); |
| 91 | + } |
| 92 | + |
| 93 | + const auth = event.context.auth!; |
| 94 | + await logAuthEvent({ |
| 95 | + event, |
| 96 | + userId: auth.userId, |
| 97 | + eventType: "admin:user_updated", |
| 98 | + metadata: { |
| 99 | + targetUserId: userId, |
| 100 | + action: "modules_updated", |
| 101 | + changes: flags, |
| 102 | + previousModules, |
| 103 | + }, |
| 104 | + }); |
| 105 | + |
| 106 | + return success(event, { enabledModules: updatedModules }); |
| 107 | +}); |
0 commit comments