phase 2 and 3
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user