Files
mrp-qrcode/app/api/v1/projects/[id]/assemblies/route.ts
T
2026-04-21 08:56:51 -05:00

54 lines
1.6 KiB
TypeScript

import { type NextRequest } from "next/server";
import { prisma } from "@/lib/prisma";
import { ok, errorResponse, requireRole, parseJson, ApiError } from "@/lib/api";
import { CreateAssemblySchema } 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 assemblies = await prisma.assembly.findMany({
where: { projectId: id },
orderBy: { code: "asc" },
include: { _count: { select: { parts: true } } },
});
return ok({ assemblies });
} catch (err) {
return errorResponse(err);
}
}
export async function POST(req: NextRequest, ctx: { params: Promise<{ id: string }> }) {
try {
const actor = await requireRole("admin");
const { id } = await ctx.params;
const body = await parseJson(req, CreateAssemblySchema);
const project = await prisma.project.findUnique({ where: { id } });
if (!project) throw new ApiError(404, "not_found", "Project not found");
const created = await prisma.assembly.create({
data: {
projectId: id,
code: body.code,
name: body.name,
qty: body.qty,
notes: body.notes ?? null,
},
});
await audit({
actorId: actor.id,
action: "create",
entity: "Assembly",
entityId: created.id,
after: created,
ipAddress: clientIp(req),
});
return ok({ assembly: created }, { status: 201 });
} catch (err) {
return errorResponse(err);
}
}