47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { type NextRequest } from "next/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { ok, errorResponse, requireRole, parseJson } from "@/lib/api";
|
|
import { CreateMachineSchema } from "@/lib/schemas";
|
|
import { audit } from "@/lib/audit";
|
|
import { clientIp } from "@/lib/request";
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
await requireRole("admin");
|
|
const includeInactive = req.nextUrl.searchParams.get("includeInactive") === "1";
|
|
const machines = await prisma.machine.findMany({
|
|
where: includeInactive ? undefined : { active: true },
|
|
orderBy: [{ active: "desc" }, { name: "asc" }],
|
|
});
|
|
return ok({ machines });
|
|
} catch (err) {
|
|
return errorResponse(err);
|
|
}
|
|
}
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const actor = await requireRole("admin");
|
|
const body = await parseJson(req, CreateMachineSchema);
|
|
const created = await prisma.machine.create({
|
|
data: {
|
|
name: body.name,
|
|
kind: body.kind,
|
|
location: body.location ?? null,
|
|
notes: body.notes ?? null,
|
|
},
|
|
});
|
|
await audit({
|
|
actorId: actor.id,
|
|
action: "create",
|
|
entity: "Machine",
|
|
entityId: created.id,
|
|
after: created,
|
|
ipAddress: clientIp(req),
|
|
});
|
|
return ok({ machine: created }, { status: 201 });
|
|
} catch (err) {
|
|
return errorResponse(err);
|
|
}
|
|
}
|