Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | import { NextResponse, type NextRequest } from 'next/server' import { withAuth } from '@/lib/auth/withAuth' import { db } from '@/db' import { userNotificationSettings, userPushSubscriptions } from '@/db/schema' import { eq } from 'drizzle-orm' import type { TypeOverridesMap } from '@/db/schema/user-notification-settings' /** GET /api/settings/notification-preferences — get current user's notification settings */ export const GET = withAuth(async (_request: NextRequest, { userId }) => { const [settings] = await db .select() .from(userNotificationSettings) .where(eq(userNotificationSettings.userId, userId)) .limit(1) const pushSubs = await db .select({ id: userPushSubscriptions.id, deviceLabel: userPushSubscriptions.deviceLabel, createdAt: userPushSubscriptions.createdAt, lastUsedAt: userPushSubscriptions.lastUsedAt, }) .from(userPushSubscriptions) .where(eq(userPushSubscriptions.userId, userId)) return NextResponse.json({ settings: settings ?? { userId, inAppEnabled: true, pushEnabled: false, emailEnabled: false, notificationEmail: null, typeOverrides: null, }, pushDevices: pushSubs, }) }) /** PUT /api/settings/notification-preferences — update notification settings */ export const PUT = withAuth(async (request: NextRequest, { userId }) => { const body = await request.json() const { inAppEnabled, pushEnabled, emailEnabled, notificationEmail, typeOverrides } = body as { inAppEnabled?: boolean pushEnabled?: boolean emailEnabled?: boolean notificationEmail?: string | null typeOverrides?: TypeOverridesMap | null } const now = new Date() // Check if settings exist const [existing] = await db .select({ userId: userNotificationSettings.userId }) .from(userNotificationSettings) .where(eq(userNotificationSettings.userId, userId)) .limit(1) if (existing) { const updates: Record<string, unknown> = { updatedAt: now } if (inAppEnabled !== undefined) updates.inAppEnabled = inAppEnabled if (pushEnabled !== undefined) updates.pushEnabled = pushEnabled if (emailEnabled !== undefined) updates.emailEnabled = emailEnabled if (notificationEmail !== undefined) updates.notificationEmail = notificationEmail if (typeOverrides !== undefined) updates.typeOverrides = typeOverrides await db .update(userNotificationSettings) .set(updates) .where(eq(userNotificationSettings.userId, userId)) } else { await db.insert(userNotificationSettings).values({ userId, inAppEnabled: inAppEnabled ?? true, pushEnabled: pushEnabled ?? false, emailEnabled: emailEnabled ?? false, notificationEmail: notificationEmail ?? null, typeOverrides: typeOverrides ?? null, createdAt: now, updatedAt: now, }) } // Return updated settings const [updated] = await db .select() .from(userNotificationSettings) .where(eq(userNotificationSettings.userId, userId)) .limit(1) return NextResponse.json({ settings: updated }) }) |