57 lines
2.0 KiB
TypeScript
57 lines
2.0 KiB
TypeScript
import { NextResponse } from "next/server";
|
|||
|
|
import { z } from "zod";
|
||
|
|
import { deleteSession, endSession, getSessionDetail, renameSession } from "@/lib/db/queries/sessions";
|
||
|
|
import { withRouteLogging } from "@/lib/api/with-route-logging";
|
||
|
|
|
||
|
|
const patchSchema = z
|
||
|
|
.object({
|
||
|
|
status: z.enum(["completed", "aborted"]).optional(),
|
||
|
|
name: z.string().min(1).max(160).optional(),
|
||
|
|
description: z.string().max(2000).optional(),
|
||
|
|
})
|
||
|
|
.refine((v) => v.status !== undefined || v.name !== undefined || v.description !== undefined, {
|
||
|
|
message: "at least one of status, name, description is required",
|
||
|
|
});
|
||
|
|
|
||
|
|
export const GET = withRouteLogging(
|
||
|
|
"sessions.get",
|
||
|
|
async (_req: Request, { params }: { params: Promise<{ id: string }> }) => {
|
||
|
|
const { id } = await params;
|
||
|
|
const detail = await getSessionDetail(Number(id));
|
||
|
|
if (!detail) return NextResponse.json({ error: "not found" }, { status: 404 });
|
||
|
|
return NextResponse.json(detail);
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
export const PATCH = withRouteLogging(
|
||
|
|
"sessions.update",
|
||
|
|
async (req: Request, { params }: { params: Promise<{ id: string }> }) => {
|
||
|
|
const { id } = await params;
|
||
|
|
const parsed = patchSchema.safeParse(await req.json().catch(() => null));
|
||
|
|
if (!parsed.success) {
|
||
|
|
return NextResponse.json({ error: parsed.error.message }, { status: 400 });
|
||
|
|
}
|
||
|
|
const { status, name, description } = parsed.data;
|
||
|
|
|
||
|
|
let updated;
|
||
|
|
if (status !== undefined) {
|
||
|
|
updated = await endSession(Number(id), { status });
|
||
|
|
}
|
||
|
|
if (name !== undefined || description !== undefined) {
|
||
|
|
updated = await renameSession(Number(id), { name, description });
|
||
|
|
}
|
||
|
|
|
||
|
|
if (!updated) return NextResponse.json({ error: "not found" }, { status: 404 });
|
||
|
|
return NextResponse.json({ session: updated });
|
||
|
|
},
|
||
|
|
);
|
||
|
|
|
||
|
|
export const DELETE = withRouteLogging(
|
||
|
|
"sessions.delete",
|
||
|
|
async (_req: Request, { params }: { params: Promise<{ id: string }> }) => {
|
||
|
|
const { id } = await params;
|
||
|
|
await deleteSession(Number(id));
|
||
|
|
return NextResponse.json({ ok: true });
|
||
|
|
},
|
||
|
|
);
|