47 lines
1.5 KiB
TypeScript
47 lines
1.5 KiB
TypeScript
import { type NextRequest } from "next/server";
|
|
import { ok, errorResponse, requireRole, parseJson } from "@/lib/api";
|
|
import { DuplicateAssemblySchema } from "@/lib/schemas";
|
|
import { audit } from "@/lib/audit";
|
|
import { clientIp } from "@/lib/request";
|
|
import { duplicateAssembly } from "@/lib/duplicate";
|
|
|
|
/**
|
|
* Admin-only. Clone an Assembly (plus every child Part, plus every
|
|
* Operation when includeOperations is true) into the same project under a
|
|
* new `code`. Useful when a new project needs the same assembly with minor
|
|
* tweaks — duplicate then edit rather than rebuilding from scratch.
|
|
*/
|
|
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, DuplicateAssemblySchema);
|
|
|
|
const result = await duplicateAssembly({
|
|
sourceAssemblyId: id,
|
|
code: body.code,
|
|
name: body.name,
|
|
includeOperations: body.includeOperations ?? true,
|
|
});
|
|
|
|
await audit({
|
|
actorId: actor.id,
|
|
action: "duplicate",
|
|
entity: "Assembly",
|
|
entityId: result.id,
|
|
after: { sourceAssemblyId: id, ...result },
|
|
ipAddress: clientIp(req),
|
|
});
|
|
return ok(
|
|
{
|
|
assembly: { id: result.id },
|
|
partsCopied: result.partsCopied,
|
|
operationsCopied: result.operationsCopied,
|
|
},
|
|
{ status: 201 },
|
|
);
|
|
} catch (err) {
|
|
return errorResponse(err);
|
|
}
|
|
}
|