92 lines
3.0 KiB
TypeScript
92 lines
3.0 KiB
TypeScript
import { type NextRequest } from "next/server";
|
|
import { prisma } from "@/lib/prisma";
|
|
import { ok, errorResponse, requireRole, parseJson, ApiError } from "@/lib/api";
|
|
import { UpdatePartSchema } 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 part = await prisma.part.findUnique({
|
|
where: { id },
|
|
include: {
|
|
assembly: {
|
|
include: { project: { select: { id: true, code: true, name: true } } },
|
|
},
|
|
stepFile: true,
|
|
drawingFile: true,
|
|
cutFile: true,
|
|
operations: {
|
|
orderBy: { sequence: "asc" },
|
|
include: { machine: { select: { id: true, name: true } } },
|
|
},
|
|
},
|
|
});
|
|
if (!part) throw new ApiError(404, "not_found", "Part not found");
|
|
return ok({ part });
|
|
} 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, UpdatePartSchema);
|
|
|
|
const before = await prisma.part.findUnique({ where: { id } });
|
|
if (!before) throw new ApiError(404, "not_found", "Part not found");
|
|
|
|
const updated = await prisma.part.update({
|
|
where: { id },
|
|
data: {
|
|
...(body.code !== undefined ? { code: body.code } : {}),
|
|
...(body.name !== undefined ? { name: body.name } : {}),
|
|
...(body.material !== undefined ? { material: body.material } : {}),
|
|
...(body.qty !== undefined ? { qty: body.qty } : {}),
|
|
...(body.notes !== undefined ? { notes: body.notes } : {}),
|
|
...(body.stepFileId !== undefined ? { stepFileId: body.stepFileId } : {}),
|
|
...(body.drawingFileId !== undefined ? { drawingFileId: body.drawingFileId } : {}),
|
|
...(body.cutFileId !== undefined ? { cutFileId: body.cutFileId } : {}),
|
|
...(body.thumbnailFileId !== undefined ? { thumbnailFileId: body.thumbnailFileId } : {}),
|
|
},
|
|
});
|
|
await audit({
|
|
actorId: actor.id,
|
|
action: "update",
|
|
entity: "Part",
|
|
entityId: id,
|
|
before,
|
|
after: updated,
|
|
ipAddress: clientIp(req),
|
|
});
|
|
return ok({ part: 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.part.findUnique({ where: { id } });
|
|
if (!before) throw new ApiError(404, "not_found", "Part not found");
|
|
await prisma.part.delete({ where: { id } });
|
|
await audit({
|
|
actorId: actor.id,
|
|
action: "delete",
|
|
entity: "Part",
|
|
entityId: id,
|
|
before,
|
|
ipAddress: clientIp(req),
|
|
});
|
|
return ok({ ok: true });
|
|
} catch (err) {
|
|
return errorResponse(err);
|
|
}
|
|
}
|