|
| 1 | +import { NextRequest, NextResponse } from 'next/server'; |
| 2 | +import dbConnect from '@/lib/mongodb'; |
| 3 | +import { Notification } from '@/models/Notification'; |
| 4 | +import { NotificationPayload, NotificationResponse } from '@/types/index'; |
| 5 | + |
| 6 | +// ─── POST /api/notify ──────────────────────────────────────────────────────── |
| 7 | +// Register or update email notification preferences for a user |
| 8 | +export async function POST(req: NextRequest): Promise<NextResponse<NotificationResponse>> { |
| 9 | + try { |
| 10 | + const body: NotificationPayload = await req.json(); |
| 11 | + const { username, email, frequency, preferences } = body; |
| 12 | + |
| 13 | + // Validate required fields |
| 14 | + if (!username || !email) { |
| 15 | + return NextResponse.json( |
| 16 | + { success: false, message: 'Username and email are required.' }, |
| 17 | + { status: 400 } |
| 18 | + ); |
| 19 | + } |
| 20 | + |
| 21 | + // Validate email format |
| 22 | + const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/; |
| 23 | + if (!emailRegex.test(email)) { |
| 24 | + return NextResponse.json( |
| 25 | + { success: false, message: 'Invalid email address.' }, |
| 26 | + { status: 400 } |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + // Validate frequency |
| 31 | + const validFrequencies = ['realtime', 'daily', 'weekly']; |
| 32 | + if (frequency && !validFrequencies.includes(frequency)) { |
| 33 | + return NextResponse.json( |
| 34 | + { success: false, message: 'Invalid frequency. Use realtime, daily, or weekly.' }, |
| 35 | + { status: 400 } |
| 36 | + ); |
| 37 | + } |
| 38 | + |
| 39 | + await dbConnect(); |
| 40 | + |
| 41 | + // Upsert notification preferences |
| 42 | + const notification = await Notification.findOneAndUpdate( |
| 43 | + { username: username.toLowerCase() }, |
| 44 | + { |
| 45 | + email: email.toLowerCase(), |
| 46 | + frequency: frequency ?? 'daily', |
| 47 | + notifyOnCommit: preferences?.notifyOnCommit ?? true, |
| 48 | + notifyOnStreak: preferences?.notifyOnStreak ?? true, |
| 49 | + notifyOnMilestone: preferences?.notifyOnMilestone ?? true, |
| 50 | + isActive: true, |
| 51 | + updatedAt: new Date(), |
| 52 | + }, |
| 53 | + { upsert: true, new: true } |
| 54 | + ); |
| 55 | + |
| 56 | + return NextResponse.json( |
| 57 | + { |
| 58 | + success: true, |
| 59 | + message: 'Notification preferences saved successfully.', |
| 60 | + data: { |
| 61 | + username: notification.username, |
| 62 | + email: notification.email, |
| 63 | + frequency: notification.frequency, |
| 64 | + preferences: { |
| 65 | + notifyOnCommit: notification.notifyOnCommit, |
| 66 | + notifyOnStreak: notification.notifyOnStreak, |
| 67 | + notifyOnMilestone: notification.notifyOnMilestone, |
| 68 | + }, |
| 69 | + }, |
| 70 | + }, |
| 71 | + { status: 200 } |
| 72 | + ); |
| 73 | + } catch (error) { |
| 74 | + console.error('[/api/notify] Error saving notification preferences:', error); |
| 75 | + return NextResponse.json( |
| 76 | + { success: false, message: 'Internal server error.' }, |
| 77 | + { status: 500 } |
| 78 | + ); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +// ─── GET /api/notify ───────────────────────────────────────────────────────── |
| 83 | +// Fetch notification preferences for a user |
| 84 | +export async function GET(req: NextRequest): Promise<NextResponse<NotificationResponse>> { |
| 85 | + try { |
| 86 | + const { searchParams } = new URL(req.url); |
| 87 | + const username = searchParams.get('user'); |
| 88 | + |
| 89 | + if (!username) { |
| 90 | + return NextResponse.json( |
| 91 | + { success: false, message: 'Username is required.' }, |
| 92 | + { status: 400 } |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + await dbConnect(); |
| 97 | + |
| 98 | + const notification = await Notification.findOne({ |
| 99 | + username: username.toLowerCase(), |
| 100 | + }); |
| 101 | + |
| 102 | + if (!notification) { |
| 103 | + return NextResponse.json( |
| 104 | + { success: false, message: 'No notification preferences found.' }, |
| 105 | + { status: 404 } |
| 106 | + ); |
| 107 | + } |
| 108 | + |
| 109 | + return NextResponse.json( |
| 110 | + { |
| 111 | + success: true, |
| 112 | + message: 'Notification preferences fetched successfully.', |
| 113 | + data: { |
| 114 | + username: notification.username, |
| 115 | + email: notification.email, |
| 116 | + frequency: notification.frequency, |
| 117 | + preferences: { |
| 118 | + notifyOnCommit: notification.notifyOnCommit, |
| 119 | + notifyOnStreak: notification.notifyOnStreak, |
| 120 | + notifyOnMilestone: notification.notifyOnMilestone, |
| 121 | + }, |
| 122 | + }, |
| 123 | + }, |
| 124 | + { status: 200 } |
| 125 | + ); |
| 126 | + } catch (error) { |
| 127 | + console.error('[/api/notify] Error fetching notification preferences:', error); |
| 128 | + return NextResponse.json( |
| 129 | + { success: false, message: 'Internal server error.' }, |
| 130 | + { status: 500 } |
| 131 | + ); |
| 132 | + } |
| 133 | +} |
0 commit comments