62 lines
1.5 KiB
TypeScript
62 lines
1.5 KiB
TypeScript
import { z } from "zod";
|
|
import { requireAdmin } from "@/lib/middleware/admin";
|
|
import {
|
|
getAllSystemSettings,
|
|
getSystemSetting,
|
|
upsertSystemSetting,
|
|
} from "@/lib/db/queries";
|
|
import { ChatSDKError } from "@/lib/errors";
|
|
|
|
const settingSchema = z.object({
|
|
key: z.string(),
|
|
value: z.any(),
|
|
});
|
|
|
|
export async function GET(request: Request) {
|
|
try {
|
|
await requireAdmin();
|
|
|
|
const { searchParams } = new URL(request.url);
|
|
const key = searchParams.get("key");
|
|
|
|
if (key) {
|
|
const setting = await getSystemSetting({ key });
|
|
return Response.json({ setting });
|
|
}
|
|
|
|
const settings = await getAllSystemSettings();
|
|
return Response.json({ settings });
|
|
} catch (error) {
|
|
if (error instanceof ChatSDKError) {
|
|
return error.toResponse();
|
|
}
|
|
return new ChatSDKError("offline:admin").toResponse();
|
|
}
|
|
}
|
|
|
|
export async function POST(request: Request) {
|
|
try {
|
|
const session = await requireAdmin();
|
|
|
|
const body = await request.json();
|
|
const { key, value } = settingSchema.parse(body);
|
|
|
|
const setting = await upsertSystemSetting({
|
|
key,
|
|
value,
|
|
updatedBy: session.user.id,
|
|
});
|
|
|
|
return Response.json({ setting: setting[0] });
|
|
} catch (error) {
|
|
if (error instanceof ChatSDKError) {
|
|
return error.toResponse();
|
|
}
|
|
if (error instanceof z.ZodError) {
|
|
return new ChatSDKError("bad_request:api", error.message).toResponse();
|
|
}
|
|
return new ChatSDKError("offline:admin").toResponse();
|
|
}
|
|
}
|
|
|