|
| 1 | +import { NextResponse } from 'next/server'; |
| 2 | +import dbConnect from '@/lib/mongodb'; |
| 3 | +import { StudentProfile } from '@/models/StudentProfile'; |
| 4 | +import { RateLimiter } from '@/lib/rate-limit'; |
| 5 | +import type { ParsedResume } from '@/types/student'; |
| 6 | + |
| 7 | +const confirmLimiter = new RateLimiter(10, 60000); |
| 8 | + |
| 9 | +export async function POST(req: Request) { |
| 10 | + const ip = req.headers.get('x-forwarded-for') || req.headers.get('x-real-ip') || 'unknown'; |
| 11 | + |
| 12 | + if (!(await confirmLimiter.check(ip))) { |
| 13 | + return NextResponse.json( |
| 14 | + { success: false, error: 'Too many requests, please try again later.' }, |
| 15 | + { status: 429 } |
| 16 | + ); |
| 17 | + } |
| 18 | + |
| 19 | + let body: unknown; |
| 20 | + |
| 21 | + try { |
| 22 | + body = await req.json(); |
| 23 | + } catch { |
| 24 | + return NextResponse.json( |
| 25 | + { success: false, error: 'Malformed JSON request body' }, |
| 26 | + { status: 400 } |
| 27 | + ); |
| 28 | + } |
| 29 | + |
| 30 | + const { githubUsername, data } = body as { |
| 31 | + githubUsername?: unknown; |
| 32 | + data?: unknown; |
| 33 | + }; |
| 34 | + |
| 35 | + if (!githubUsername || typeof githubUsername !== 'string') { |
| 36 | + return NextResponse.json( |
| 37 | + { success: false, error: 'Invalid or missing githubUsername' }, |
| 38 | + { status: 400 } |
| 39 | + ); |
| 40 | + } |
| 41 | + |
| 42 | + if (!data || typeof data !== 'object') { |
| 43 | + return NextResponse.json( |
| 44 | + { success: false, error: 'Invalid or missing profile data' }, |
| 45 | + { status: 400 } |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + const profileData = data as ParsedResume; |
| 50 | + |
| 51 | + if (!profileData.name || !profileData.email) { |
| 52 | + return NextResponse.json( |
| 53 | + { success: false, error: 'Name and email are required' }, |
| 54 | + { status: 400 } |
| 55 | + ); |
| 56 | + } |
| 57 | + |
| 58 | + try { |
| 59 | + if (!process.env.MONGODB_URI) { |
| 60 | + console.warn('MONGODB_URI is not set. Bypassing student profile save.'); |
| 61 | + return NextResponse.json({ success: true, bypassed: true }); |
| 62 | + } |
| 63 | + |
| 64 | + await dbConnect(); |
| 65 | + |
| 66 | + await StudentProfile.findOneAndUpdate( |
| 67 | + { githubUsername: githubUsername.trim().toLowerCase() }, |
| 68 | + { |
| 69 | + $set: { |
| 70 | + name: profileData.name, |
| 71 | + email: profileData.email, |
| 72 | + skills: profileData.skills || [], |
| 73 | + education: profileData.education || [], |
| 74 | + experience: profileData.experience || [], |
| 75 | + updatedAt: new Date(), |
| 76 | + }, |
| 77 | + }, |
| 78 | + { upsert: true, new: true } |
| 79 | + ); |
| 80 | + |
| 81 | + return NextResponse.json({ success: true }); |
| 82 | + } catch (error) { |
| 83 | + console.error('Error saving student profile:', error); |
| 84 | + return NextResponse.json( |
| 85 | + { success: false, error: 'Failed to save profile data' }, |
| 86 | + { status: 500 } |
| 87 | + ); |
| 88 | + } |
| 89 | +} |
0 commit comments