This commit is contained in:
@@ -28,6 +28,7 @@ export async function POST(req: NextRequest, ctx: { params: Promise<{ id: string
|
||||
select: {
|
||||
id: true,
|
||||
status: true,
|
||||
kind: true,
|
||||
claimedByUserId: true,
|
||||
qcRequired: true,
|
||||
unitsCompleted: true,
|
||||
@@ -46,8 +47,18 @@ export async function POST(req: NextRequest, ctx: { params: Promise<{ id: string
|
||||
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");
|
||||
// Dedicated QC ops (kind="qc") are all-about-QC — always demand the inline
|
||||
// payload. Regular work ops only demand it when the template/op was flagged
|
||||
// qcRequired. Either way, without a QC block we short-circuit.
|
||||
const qcMandatory = existing.kind === "qc" || existing.qcRequired;
|
||||
if (qcMandatory && !body.qc) {
|
||||
throw new ApiError(
|
||||
400,
|
||||
"qc_required",
|
||||
existing.kind === "qc"
|
||||
? "QC result is required to complete an inspection step"
|
||||
: "This step requires an inline QC check before completing",
|
||||
);
|
||||
}
|
||||
|
||||
// --- Partial vs fully-done detection --------------------------------
|
||||
@@ -63,11 +74,23 @@ export async function POST(req: NextRequest, ctx: { params: Promise<{ id: string
|
||||
//
|
||||
// QC-required ops still demand a QC record even on partial close —
|
||||
// that's about material checks, not about finishing the batch.
|
||||
// QC ops don't track units at all; treat them as "done" by default.
|
||||
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";
|
||||
const isQcOp = existing.kind === "qc";
|
||||
const wouldFinish = isQcOp || units === 0 || units >= remaining;
|
||||
|
||||
// QC failure short-circuits the usual partial-vs-complete decision: the
|
||||
// step moves to `qc_failed` which blocks further work until an admin
|
||||
// clears it via /api/v1/operations/:id/qc-reset. We still log the QC
|
||||
// record + close the timelog so the failure is on the paper trail.
|
||||
const qcFailed = body.qc !== undefined && body.qc.passed === false;
|
||||
const nextStatus: "completed" | "partial" | "qc_failed" = qcFailed
|
||||
? "qc_failed"
|
||||
: wouldFinish
|
||||
? "completed"
|
||||
: "partial";
|
||||
|
||||
const now = new Date();
|
||||
await prisma.$transaction(async (tx) => {
|
||||
@@ -99,8 +122,9 @@ export async function POST(req: NextRequest, ctx: { params: Promise<{ id: string
|
||||
}
|
||||
// unitsCompleted is cumulative across pause/resume cycles; on close we
|
||||
// add this session's batch so the total reflects everything the step
|
||||
// actually produced. Partial close releases the claim so the next
|
||||
// operator can resume; completed close sets completedAt for reporting.
|
||||
// actually produced. Partial + qc_failed close release the claim so the
|
||||
// next operator (or admin, in the fail case) can act; completed close
|
||||
// sets completedAt for reporting.
|
||||
await tx.operation.update({
|
||||
where: { id },
|
||||
data: {
|
||||
@@ -114,9 +138,15 @@ export async function POST(req: NextRequest, ctx: { params: Promise<{ id: string
|
||||
});
|
||||
|
||||
const op = await prisma.operation.findUnique({ where: { id } });
|
||||
const action =
|
||||
nextStatus === "completed"
|
||||
? "close_op"
|
||||
: nextStatus === "qc_failed"
|
||||
? "qc_fail_op"
|
||||
: "partial_close_op";
|
||||
await audit({
|
||||
actorId: actor.id,
|
||||
action: nextStatus === "completed" ? "close_op" : "partial_close_op",
|
||||
action,
|
||||
entity: "Operation",
|
||||
entityId: id,
|
||||
after: {
|
||||
@@ -128,7 +158,11 @@ export async function POST(req: NextRequest, ctx: { params: Promise<{ id: string
|
||||
ipAddress: clientIp(req),
|
||||
});
|
||||
|
||||
return ok({ operation: op, partial: nextStatus === "partial" });
|
||||
return ok({
|
||||
operation: op,
|
||||
partial: nextStatus === "partial",
|
||||
qcFailed: nextStatus === "qc_failed",
|
||||
});
|
||||
} catch (err) {
|
||||
return errorResponse(err);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user