2026-08-25 07:51:38 +02:00
|
|
|
import { NextResponse } from "next/server";
|
|
|
|
|
import { z } from "zod";
|
|
|
|
|
import { renameDevice } from "@/lib/db/queries/devices";
|
2026-08-26 08:45:15 +02:00
|
|
|
import { withRouteLogging } from "@/lib/api/with-route-logging";
|
2026-08-25 07:51:38 +02:00
|
|
|
|
|
|
|
|
const bodySchema = z.object({ displayName: z.string().min(1).max(120) });
|
|
|
|
|
|
2026-08-26 08:45:15 +02:00
|
|
|
export const PATCH = withRouteLogging(
|
|
|
|
|
"devices.rename",
|
|
|
|
|
async (req: Request, { params }: { params: Promise<{ id: string }> }) => {
|
|
|
|
|
const { id } = await params;
|
|
|
|
|
const parsed = bodySchema.safeParse(await req.json().catch(() => null));
|
|
|
|
|
if (!parsed.success) {
|
|
|
|
|
return NextResponse.json({ error: "displayName is required" }, { status: 400 });
|
|
|
|
|
}
|
|
|
|
|
const updated = await renameDevice(Number(id), parsed.data.displayName);
|
|
|
|
|
if (!updated) return NextResponse.json({ error: "not found" }, { status: 404 });
|
|
|
|
|
return NextResponse.json({ device: updated });
|
|
|
|
|
},
|
|
|
|
|
);
|