mirror of
https://github.com/Tony0410/quietthanks.git
synced 2026-05-24 21:31:41 +08:00
97 lines
2.9 KiB
TypeScript
97 lines
2.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { db, schema } from "@/lib/db";
|
|
import { getSession } from "@/lib/auth";
|
|
import { eq } from "drizzle-orm";
|
|
import { encrypt } from "@/lib/crypto";
|
|
|
|
// GET /api/settings - Get current user settings
|
|
export async function GET() {
|
|
const user = await getSession();
|
|
if (!user) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
const users = await db
|
|
.select({
|
|
reminderEnabled: schema.users.reminderEnabled,
|
|
reminderTime: schema.users.reminderTime,
|
|
reminderAlways: schema.users.reminderAlways,
|
|
llmProvider: schema.users.llmProvider,
|
|
llmApiKey: schema.users.llmApiKey,
|
|
llmModel: schema.users.llmModel,
|
|
})
|
|
.from(schema.users)
|
|
.where(eq(schema.users.id, user.id))
|
|
.limit(1);
|
|
|
|
if (users.length === 0) {
|
|
return NextResponse.json({ error: "User not found" }, { status: 404 });
|
|
}
|
|
|
|
return NextResponse.json({
|
|
reminderEnabled: users[0].reminderEnabled === 1,
|
|
reminderTime: users[0].reminderTime || "20:00",
|
|
reminderAlways: users[0].reminderAlways === 1,
|
|
llmProvider: users[0].llmProvider || null,
|
|
hasLlmKey: Boolean(users[0].llmApiKey),
|
|
llmModel: users[0].llmModel || null,
|
|
});
|
|
} catch (error) {
|
|
console.error("Failed to fetch settings:", error);
|
|
return NextResponse.json({ error: "Failed to fetch settings" }, { status: 500 });
|
|
}
|
|
}
|
|
|
|
// PATCH /api/settings - Update user settings
|
|
export async function PATCH(request: NextRequest) {
|
|
const user = await getSession();
|
|
if (!user) {
|
|
return NextResponse.json({ error: "Unauthorized" }, { status: 401 });
|
|
}
|
|
|
|
try {
|
|
const body = await request.json();
|
|
const updates: Record<string, unknown> = {};
|
|
|
|
if (typeof body.reminderEnabled === "boolean") {
|
|
updates.reminderEnabled = body.reminderEnabled ? 1 : 0;
|
|
}
|
|
|
|
if (typeof body.reminderAlways === "boolean") {
|
|
updates.reminderAlways = body.reminderAlways ? 1 : 0;
|
|
}
|
|
|
|
if (typeof body.reminderTime === "string") {
|
|
updates.reminderTime = body.reminderTime;
|
|
}
|
|
|
|
if (typeof body.llmProvider === "string") {
|
|
updates.llmProvider = body.llmProvider || null;
|
|
}
|
|
|
|
if (typeof body.llmApiKey === "string") {
|
|
// Encrypt the API key before storing
|
|
updates.llmApiKey = body.llmApiKey ? encrypt(body.llmApiKey) : null;
|
|
}
|
|
|
|
if (typeof body.llmModel === "string") {
|
|
updates.llmModel = body.llmModel || null;
|
|
}
|
|
|
|
if (Object.keys(updates).length === 0) {
|
|
return NextResponse.json({ error: "No updates provided" }, { status: 400 });
|
|
}
|
|
|
|
await db
|
|
.update(schema.users)
|
|
.set(updates)
|
|
.where(eq(schema.users.id, user.id));
|
|
|
|
return NextResponse.json({ success: true });
|
|
} catch (error) {
|
|
console.error("Failed to update settings:", error);
|
|
return NextResponse.json({ error: "Failed to update settings" }, { status: 500 });
|
|
}
|
|
}
|