-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathroute.ts
More file actions
107 lines (94 loc) · 3.67 KB
/
route.ts
File metadata and controls
107 lines (94 loc) · 3.67 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import { auth, currentUser } from '@clerk/nextjs/server'
import { NextRequest, NextResponse } from 'next/server'
import { connectDB } from '@/lib/mongodb'
import { Teacher } from '@/models/Teacher'
export async function GET() {
const { userId } = await auth()
if (!userId) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
try {
await connectDB()
let teacher = await Teacher.findOne({ clerkId: userId }).lean()
if (!teacher) {
const clerkUser = await currentUser()
const created = await Teacher.create({
clerkId: userId,
name: clerkUser?.fullName ?? '',
email: clerkUser?.emailAddresses[0]?.emailAddress ?? '',
department: '',
subjects: [],
})
teacher = created.toObject()
}
return NextResponse.json(teacher)
} catch (error) {
console.error('GET /api/profile error:', error instanceof Error ? error.message : error)
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
}
}
export async function PUT(req: NextRequest) {
const { userId } = await auth()
if (!userId) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
try {
await connectDB()
let body
try {
body = await req.json()
} catch {
return NextResponse.json({ error: 'Invalid JSON in request body' }, { status: 400 })
}
const { name, department, subjects, phone, bio, academicHistory } = body
// Validate input
if (typeof name !== 'string' || !name.trim()) {
return NextResponse.json({ error: 'name must be a non-empty string' }, { status: 400 })
}
if (department !== undefined && typeof department !== 'string') {
return NextResponse.json({ error: 'department must be a string' }, { status: 400 })
}
if (!Array.isArray(subjects) || !subjects.every((s) => typeof s === 'string')) {
return NextResponse.json({ error: 'subjects must be an array of strings' }, { status: 400 })
}
if (phone !== undefined && typeof phone !== 'string') {
return NextResponse.json({ error: 'phone must be a string' }, { status: 400 })
}
if (bio !== undefined && typeof bio !== 'string') {
return NextResponse.json({ error: 'bio must be a string' }, { status: 400 })
}
if (academicHistory !== undefined) {
if (
!Array.isArray(academicHistory) ||
academicHistory.length > 20 ||
!academicHistory.every(
(entry: unknown) =>
entry !== null &&
typeof entry === 'object' &&
typeof (entry as Record<string, unknown>).year === 'string' &&
typeof (entry as Record<string, unknown>).title === 'string',
)
) {
return NextResponse.json(
{ error: 'academicHistory must be an array of objects with string year and title (max 20 items)' },
{ status: 400 },
)
}
}
const updatePayload: Record<string, unknown> = { name, subjects }
if (department !== undefined) updatePayload.department = department
if (phone !== undefined) updatePayload.phone = phone
if (bio !== undefined) updatePayload.bio = bio
if (academicHistory !== undefined) updatePayload.academicHistory = academicHistory
const teacher = await Teacher.findOneAndUpdate(
{ clerkId: userId },
{ $set: updatePayload },
{ new: true }
)
if (!teacher) {
return NextResponse.json({ error: 'Teacher not found' }, { status: 404 })
}
return NextResponse.json(teacher)
} catch (error) {
if (error instanceof Error) {
console.error('PUT /api/profile error:', error.message)
}
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
}
}