2026-08-25 07:51:38 +02:00
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
import { z } from "zod";
|
|
|
|
|
import { deletePlaySession, endPlaySession, getPlaySessionDetail } from "@/lib/db/queries/play-sessions";
|
2026-08-25 21:26:18 +02:00
|
|
|
import { getRecordingsForSession } from "@/lib/db/queries/recordings";
|
2026-08-26 08:45:15 +02:00
|
|
|
import { withRouteLogging } from "@/lib/api/with-route-logging";
|
|
|
|
|
import { createLogger } from "@/lib/logger";
|
2026-08-25 07:51:38 +02:00
|
|
|
|
2026-08-26 08:45:15 +02:00
|
|
|
const log = createLogger("play-sessions");
|
2026-08-25 07:51:38 +02:00
|
|
|
const patchSchema = z.object({ status: z.enum(["completed", "aborted"]) });
|
|
|
|
|
|
2026-08-26 08:45:15 +02:00
|
|
|
export const GET = withRouteLogging(
|
|
|
|
|
"play-sessions.get",
|
|
|
|
|
async (_req: Request, { params }: { params: Promise<{ id: string }> }) => {
|
|
|
|
|
const { id } = await params;
|
|
|
|
|
const detail = await getPlaySessionDetail(Number(id));
|
|
|
|
|
if (!detail) return NextResponse.json({ error: "not found" }, { status: 404 });
|
|
|
|
|
return NextResponse.json(detail);
|
|
|
|
|
},
|
|
|
|
|
);
|
2026-08-25 07:51:38 +02:00
|
|
|
|
2026-08-26 08:45:15 +02:00
|
|
|
export const PATCH = withRouteLogging(
|
|
|
|
|
"play-sessions.end",
|
|
|
|
|
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: "status must be completed or aborted" }, { status: 400 });
|
2026-08-25 07:51:38 +02:00
|
|
|
}
|
2026-08-26 08:45:15 +02:00
|
|
|
const updated = await endPlaySession(Number(id), parsed.data);
|
|
|
|
|
if (!updated) return NextResponse.json({ error: "not found" }, { status: 404 });
|
|
|
|
|
return NextResponse.json({ playSession: updated });
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
export const DELETE = withRouteLogging(
|
|
|
|
|
"play-sessions.delete",
|
|
|
|
|
async (req: Request, { params }: { params: Promise<{ id: string }> }) => {
|
|
|
|
|
const { id } = await params;
|
|
|
|
|
const cascade = new URL(req.url).searchParams.get("cascade") === "true";
|
|
|
|
|
try {
|
|
|
|
|
await deletePlaySession(Number(id), { cascade });
|
|
|
|
|
return NextResponse.json({ ok: true });
|
|
|
|
|
} catch (err) {
|
|
|
|
|
const message = err instanceof Error ? err.message : String(err);
|
|
|
|
|
if (message.includes("FOREIGN KEY") || message.includes("SQLITE_CONSTRAINT")) {
|
|
|
|
|
log.warn("blocked session delete due to referencing recording", { sessionId: id, cascade });
|
|
|
|
|
const blockingRecordings = await getRecordingsForSession(Number(id));
|
|
|
|
|
return NextResponse.json(
|
|
|
|
|
{ error: "cannot delete a session that a saved recording still references", recordings: blockingRecordings },
|
|
|
|
|
{ status: 409 },
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
throw err;
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
);
|