import { NextResponse } from 'next/server'; import { auth } from '@/lib/auth'; import { getUserNotificationSettings, updateUserNotificationSettings, } from '@/lib/notification-settings-db'; // GET /api/users/[id]/notification-settings export async function GET( request: Request, { params }: { params: Promise<{ id: string }> } ) { try { const session = await auth(); if (!session?.user) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const { id: userId } = await params; const currentUserId = (session.user as any).id; // Users can only view their own settings if (userId !== currentUserId) { return NextResponse.json({ error: 'Forbidden' }, { status: 403 }); } const settings = getUserNotificationSettings(userId); return NextResponse.json({ settings }); } catch (error) { console.error('[GET /api/users/[id]/notification-settings]', error); return NextResponse.json( { error: 'Failed to get settings' }, { status: 500 } ); } } // PATCH /api/users/[id]/notification-settings export async function PATCH( request: Request, { params }: { params: Promise<{ id: string }> } ) { try { const session = await auth(); if (!session?.user) { return NextResponse.json({ error: 'Unauthorized' }, { status: 401 }); } const { id: userId } = await params; const currentUserId = (session.user as any).id; // Users can only update their own settings if (userId !== currentUserId) { return NextResponse.json({ error: 'Forbidden' }, { status: 403 }); } const body = await request.json(); const { email_enabled, telegram_enabled, telegram_chat_id } = body; const settings = updateUserNotificationSettings(userId, { email_enabled, telegram_enabled, telegram_chat_id, }); return NextResponse.json({ settings }); } catch (error) { console.error('[PATCH /api/users/[id]/notification-settings]', error); return NextResponse.json( { error: 'Failed to update settings' }, { status: 500 } ); } }