Files
mrp-qrcode/app/api/v1/operations/[id]/close/route.ts
T

136 lines
5.1 KiB
TypeScript
Raw Normal View History

2026-04-21 09:29:44 -05:00
import { type NextRequest } from "next/server";
import { prisma } from "@/lib/prisma";
import { ok, errorResponse, requireRole, parseJson, ApiError } from "@/lib/api";
import { CloseOperationSchema } from "@/lib/schemas";
import { audit } from "@/lib/audit";
import { clientIp } from "@/lib/request";
/**
* Complete an operation. Only the current claim holder may close, and if
* the operation is flagged qcRequired the payload must include an inline
* QC block (pass/fail + optional measurements). Close does four things
* atomically:
*
* 1. marks the operation completed + records completedAt,
* 2. closes the open TimeLog with unitsProcessed / note if provided,
* 3. writes a QCRecord if this op requires QC (or if the operator passed
* one in voluntarily),
* 4. audits the close for later reporting.
*/
export async function POST(req: NextRequest, ctx: { params: Promise<{ id: string }> }) {
try {
const actor = await requireRole("operator");
const { id } = await ctx.params;
const body = await parseJson(req, CloseOperationSchema);
const existing = await prisma.operation.findUnique({
where: { id },
2026-04-22 07:15:56 -05:00
select: {
id: true,
status: true,
claimedByUserId: true,
qcRequired: true,
unitsCompleted: true,
part: {
select: {
qty: true,
assembly: { select: { qty: true } },
},
},
},
2026-04-21 09:29:44 -05:00
});
if (!existing) throw new ApiError(404, "not_found", "Operation not found");
if (existing.claimedByUserId !== actor.id) {
throw new ApiError(409, "not_claim_holder", "Only the current operator can complete this step");
}
if (existing.status !== "in_progress") {
throw new ApiError(409, "op_not_active", "Step is not active");
}
if (existing.qcRequired && !body.qc) {
throw new ApiError(400, "qc_required", "This step requires an inline QC check before completing");
}
2026-04-22 07:15:56 -05:00
// --- Partial vs fully-done detection --------------------------------
// The operator hits "Done" but may have only produced some of the
// assembly.qty × part.qty total units (e.g. end-of-shift handoff). In
// that case the step should end up `partial` so the NEXT operator can
// resume it — NOT `completed`, which locks it and requires admin to
// reset. We infer the operator's intent from the units they typed in:
//
// units == 0 (blank) → "trust me, it's done" → completed
// units >= remaining → math agrees → completed
// units < remaining → partial (clear claim, no completedAt)
//
// QC-required ops still demand a QC record even on partial close —
// that's about material checks, not about finishing the batch.
const units = body.unitsProcessed ?? 0;
const totalUnits = existing.part.assembly.qty * existing.part.qty;
const remaining = Math.max(0, totalUnits - existing.unitsCompleted);
const wouldFinish = units === 0 || units >= remaining;
const nextStatus: "completed" | "partial" = wouldFinish ? "completed" : "partial";
2026-04-21 09:29:44 -05:00
const now = new Date();
await prisma.$transaction(async (tx) => {
const openLog = await tx.timeLog.findFirst({
where: { operationId: id, operatorId: actor.id, endedAt: null },
orderBy: { startedAt: "desc" },
});
if (openLog) {
await tx.timeLog.update({
where: { id: openLog.id },
data: {
endedAt: now,
unitsProcessed: body.unitsProcessed ?? null,
note: body.note ?? null,
},
});
}
if (body.qc) {
await tx.qCRecord.create({
data: {
operationId: id,
operatorId: actor.id,
kind: "inline",
passed: body.qc.passed,
measurements: body.qc.measurements ?? null,
notes: body.qc.notes ?? null,
},
});
}
2026-04-21 20:59:55 -05:00
// unitsCompleted is cumulative across pause/resume cycles; on close we
// add this session's batch so the total reflects everything the step
2026-04-22 07:15:56 -05:00
// actually produced. Partial close releases the claim so the next
// operator can resume; completed close sets completedAt for reporting.
2026-04-21 09:29:44 -05:00
await tx.operation.update({
where: { id },
data: {
2026-04-22 07:15:56 -05:00
status: nextStatus,
completedAt: nextStatus === "completed" ? now : null,
2026-04-21 09:29:44 -05:00
claimedByUserId: null,
claimedAt: null,
2026-04-21 20:59:55 -05:00
...(units > 0 ? { unitsCompleted: { increment: units } } : {}),
2026-04-21 09:29:44 -05:00
},
});
});
const op = await prisma.operation.findUnique({ where: { id } });
await audit({
actorId: actor.id,
2026-04-22 07:15:56 -05:00
action: nextStatus === "completed" ? "close_op" : "partial_close_op",
2026-04-21 09:29:44 -05:00
entity: "Operation",
entityId: id,
after: {
2026-04-22 07:15:56 -05:00
status: nextStatus,
2026-04-21 09:29:44 -05:00
unitsProcessed: body.unitsProcessed ?? null,
2026-04-22 07:15:56 -05:00
unitsRemaining: Math.max(0, remaining - units),
2026-04-21 09:29:44 -05:00
qcPassed: body.qc?.passed ?? null,
},
ipAddress: clientIp(req),
});
2026-04-22 07:15:56 -05:00
return ok({ operation: op, partial: nextStatus === "partial" });
2026-04-21 09:29:44 -05:00
} catch (err) {
return errorResponse(err);
}
}