-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathroute.ts
More file actions
141 lines (124 loc) · 4.35 KB
/
route.ts
File metadata and controls
141 lines (124 loc) · 4.35 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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { db } from '@sim/db'
import { settings } from '@sim/db/schema'
import { createLogger } from '@sim/logger'
import { eq } from 'drizzle-orm'
import { nanoid } from 'nanoid'
import { NextResponse } from 'next/server'
import { z } from 'zod'
import { getSession } from '@/lib/auth'
import { generateRequestId } from '@/lib/core/utils/request'
const logger = createLogger('UserSettingsAPI')
const SettingsSchema = z.object({
theme: z.enum(['system', 'light', 'dark']).optional(),
autoConnect: z.boolean().optional(),
telemetryEnabled: z.boolean().optional(),
emailPreferences: z
.object({
unsubscribeAll: z.boolean().optional(),
unsubscribeMarketing: z.boolean().optional(),
unsubscribeUpdates: z.boolean().optional(),
unsubscribeNotifications: z.boolean().optional(),
})
.optional(),
billingUsageNotificationsEnabled: z.boolean().optional(),
showTrainingControls: z.boolean().optional(),
superUserModeEnabled: z.boolean().optional(),
errorNotificationsEnabled: z.boolean().optional(),
snapToGridSize: z.number().min(0).max(50).optional(),
showActionBar: z.boolean().optional(),
})
const defaultSettings = {
theme: 'system',
autoConnect: true,
telemetryEnabled: true,
emailPreferences: {},
billingUsageNotificationsEnabled: true,
showTrainingControls: false,
superUserModeEnabled: false,
errorNotificationsEnabled: true,
snapToGridSize: 0,
showActionBar: true,
}
export async function GET() {
const requestId = generateRequestId()
try {
const session = await getSession()
if (!session?.user?.id) {
logger.info(`[${requestId}] Returning default settings for unauthenticated user`)
return NextResponse.json({ data: defaultSettings }, { status: 200 })
}
const userId = session.user.id
const result = await db.select().from(settings).where(eq(settings.userId, userId)).limit(1)
if (!result.length) {
return NextResponse.json({ data: defaultSettings }, { status: 200 })
}
const userSettings = result[0]
return NextResponse.json(
{
data: {
theme: userSettings.theme,
autoConnect: userSettings.autoConnect,
telemetryEnabled: userSettings.telemetryEnabled,
emailPreferences: userSettings.emailPreferences ?? {},
billingUsageNotificationsEnabled: userSettings.billingUsageNotificationsEnabled ?? true,
showTrainingControls: userSettings.showTrainingControls ?? false,
superUserModeEnabled: userSettings.superUserModeEnabled ?? true,
errorNotificationsEnabled: userSettings.errorNotificationsEnabled ?? true,
snapToGridSize: userSettings.snapToGridSize ?? 0,
showActionBar: userSettings.showActionBar ?? true,
},
},
{ status: 200 }
)
} catch (error: any) {
logger.error(`[${requestId}] Settings fetch error`, error)
return NextResponse.json({ data: defaultSettings }, { status: 200 })
}
}
export async function PATCH(request: Request) {
const requestId = generateRequestId()
try {
const session = await getSession()
if (!session?.user?.id) {
logger.info(
`[${requestId}] Settings update attempted by unauthenticated user - acknowledged without saving`
)
return NextResponse.json({ success: true }, { status: 200 })
}
const userId = session.user.id
const body = await request.json()
try {
const validatedData = SettingsSchema.parse(body)
await db
.insert(settings)
.values({
id: nanoid(),
userId,
...validatedData,
updatedAt: new Date(),
})
.onConflictDoUpdate({
target: [settings.userId],
set: {
...validatedData,
updatedAt: new Date(),
},
})
return NextResponse.json({ success: true }, { status: 200 })
} catch (validationError) {
if (validationError instanceof z.ZodError) {
logger.warn(`[${requestId}] Invalid settings data`, {
errors: validationError.errors,
})
return NextResponse.json(
{ error: 'Invalid settings data', details: validationError.errors },
{ status: 400 }
)
}
throw validationError
}
} catch (error: any) {
logger.error(`[${requestId}] Settings update error`, error)
return NextResponse.json({ success: true }, { status: 200 })
}
}