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
@@ -0,0 +1,84 @@
import { type NextRequest } from "next/server";
import { prisma } from "@/lib/prisma";
import { ok, errorResponse, requireRole, parseJson, ApiError } from "@/lib/api";
import { UpdateTemplateSchema } 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 template = await prisma.operationTemplate.findUnique({
where: { id },
include: { machine: true },
});
if (!template) throw new ApiError(404, "not_found", "Template not found");
return ok({ template });
} 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, UpdateTemplateSchema);
const before = await prisma.operationTemplate.findUnique({ where: { id } });
if (!before) throw new ApiError(404, "not_found", "Template not found");
const updated = await prisma.operationTemplate.update({
where: { id },
data: {
...(body.name !== undefined ? { name: body.name } : {}),
...(body.machineId !== undefined ? { machineId: body.machineId } : {}),
...(body.defaultSettings !== undefined ? { defaultSettings: body.defaultSettings } : {}),
...(body.defaultInstructions !== undefined
? { defaultInstructions: body.defaultInstructions }
: {}),
...(body.qcRequired !== undefined ? { qcRequired: body.qcRequired } : {}),
...(body.active !== undefined ? { active: body.active } : {}),
},
});
await audit({
actorId: actor.id,
action: "update",
entity: "OperationTemplate",
entityId: id,
before,
after: updated,
ipAddress: clientIp(req),
});
return ok({ template: 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.operationTemplate.findUnique({ where: { id } });
if (!before) throw new ApiError(404, "not_found", "Template not found");
const updated = await prisma.operationTemplate.update({
where: { id },
data: { active: false },
});
await audit({
actorId: actor.id,
action: "deactivate",
entity: "OperationTemplate",
entityId: id,
before,
after: updated,
ipAddress: clientIp(req),
});
return ok({ ok: true });
} catch (err) {
return errorResponse(err);
}
}
+48
View File
@@ -0,0 +1,48 @@
import { type NextRequest } from "next/server";
import { prisma } from "@/lib/prisma";
import { ok, errorResponse, requireRole, parseJson } from "@/lib/api";
import { CreateTemplateSchema } 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 templates = await prisma.operationTemplate.findMany({
where: includeInactive ? undefined : { active: true },
include: { machine: { select: { id: true, name: true, kind: true, active: true } } },
orderBy: [{ active: "desc" }, { name: "asc" }],
});
return ok({ templates });
} catch (err) {
return errorResponse(err);
}
}
export async function POST(req: NextRequest) {
try {
const actor = await requireRole("admin");
const body = await parseJson(req, CreateTemplateSchema);
const created = await prisma.operationTemplate.create({
data: {
name: body.name,
machineId: body.machineId ?? null,
defaultSettings: body.defaultSettings ?? null,
defaultInstructions: body.defaultInstructions ?? null,
qcRequired: body.qcRequired,
},
});
await audit({
actorId: actor.id,
action: "create",
entity: "OperationTemplate",
entityId: created.id,
after: created,
ipAddress: clientIp(req),
});
return ok({ template: created }, { status: 201 });
} catch (err) {
return errorResponse(err);
}
}