phase 2 and 3

This commit is contained in:
jason
2026-04-21 08:56:51 -05:00
parent b98837a72c
commit d79aaf6ef8
42 changed files with 4962 additions and 19 deletions
+79
View File
@@ -0,0 +1,79 @@
import { type NextRequest } from "next/server";
import { prisma } from "@/lib/prisma";
import { ok, errorResponse, requireRole, parseJson, ApiError } from "@/lib/api";
import { UpdateMachineSchema } from "@/lib/schemas";
import { audit } from "@/lib/audit";
import { clientIp } from "@/lib/request";
export async function GET(_req: NextRequest, ctx: { params: Promise<{ id: string }> }) {
try {
await requireRole("admin");
const { id } = await ctx.params;
const machine = await prisma.machine.findUnique({ where: { id } });
if (!machine) throw new ApiError(404, "not_found", "Machine not found");
return ok({ machine });
} catch (err) {
return errorResponse(err);
}
}
export async function PATCH(req: NextRequest, ctx: { params: Promise<{ id: string }> }) {
try {
const actor = await requireRole("admin");
const { id } = await ctx.params;
const body = await parseJson(req, UpdateMachineSchema);
const before = await prisma.machine.findUnique({ where: { id } });
if (!before) throw new ApiError(404, "not_found", "Machine not found");
const updated = await prisma.machine.update({
where: { id },
data: {
...(body.name !== undefined ? { name: body.name } : {}),
...(body.kind !== undefined ? { kind: body.kind } : {}),
...(body.location !== undefined ? { location: body.location } : {}),
...(body.notes !== undefined ? { notes: body.notes } : {}),
...(body.active !== undefined ? { active: body.active } : {}),
},
});
await audit({
actorId: actor.id,
action: "update",
entity: "Machine",
entityId: id,
before,
after: updated,
ipAddress: clientIp(req),
});
return ok({ machine: updated });
} catch (err) {
return errorResponse(err);
}
}
export async function DELETE(req: NextRequest, ctx: { params: Promise<{ id: string }> }) {
try {
const actor = await requireRole("admin");
const { id } = await ctx.params;
const before = await prisma.machine.findUnique({ where: { id } });
if (!before) throw new ApiError(404, "not_found", "Machine not found");
// soft-delete to preserve references on historical operations
const updated = await prisma.machine.update({
where: { id },
data: { active: false },
});
await audit({
actorId: actor.id,
action: "deactivate",
entity: "Machine",
entityId: id,
before,
after: updated,
ipAddress: clientIp(req),
});
return ok({ ok: true });
} catch (err) {
return errorResponse(err);
}
}
+46
View File
@@ -0,0 +1,46 @@
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);
}
}