This commit is contained in:
@@ -25,7 +25,19 @@ export async function POST(req: NextRequest, ctx: { params: Promise<{ id: string
|
|||||||
|
|
||||||
const existing = await prisma.operation.findUnique({
|
const existing = await prisma.operation.findUnique({
|
||||||
where: { id },
|
where: { id },
|
||||||
select: { id: true, status: true, claimedByUserId: true, qcRequired: true },
|
select: {
|
||||||
|
id: true,
|
||||||
|
status: true,
|
||||||
|
claimedByUserId: true,
|
||||||
|
qcRequired: true,
|
||||||
|
unitsCompleted: true,
|
||||||
|
part: {
|
||||||
|
select: {
|
||||||
|
qty: true,
|
||||||
|
assembly: { select: { qty: true } },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
});
|
});
|
||||||
if (!existing) throw new ApiError(404, "not_found", "Operation not found");
|
if (!existing) throw new ApiError(404, "not_found", "Operation not found");
|
||||||
if (existing.claimedByUserId !== actor.id) {
|
if (existing.claimedByUserId !== actor.id) {
|
||||||
@@ -38,6 +50,25 @@ export async function POST(req: NextRequest, ctx: { params: Promise<{ id: string
|
|||||||
throw new ApiError(400, "qc_required", "This step requires an inline QC check before completing");
|
throw new ApiError(400, "qc_required", "This step requires an inline QC check before completing");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- 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";
|
||||||
|
|
||||||
const now = new Date();
|
const now = new Date();
|
||||||
await prisma.$transaction(async (tx) => {
|
await prisma.$transaction(async (tx) => {
|
||||||
const openLog = await tx.timeLog.findFirst({
|
const openLog = await tx.timeLog.findFirst({
|
||||||
@@ -68,13 +99,13 @@ export async function POST(req: NextRequest, ctx: { params: Promise<{ id: string
|
|||||||
}
|
}
|
||||||
// unitsCompleted is cumulative across pause/resume cycles; on close we
|
// unitsCompleted is cumulative across pause/resume cycles; on close we
|
||||||
// add this session's batch so the total reflects everything the step
|
// add this session's batch so the total reflects everything the step
|
||||||
// actually produced.
|
// actually produced. Partial close releases the claim so the next
|
||||||
const units = body.unitsProcessed ?? 0;
|
// operator can resume; completed close sets completedAt for reporting.
|
||||||
await tx.operation.update({
|
await tx.operation.update({
|
||||||
where: { id },
|
where: { id },
|
||||||
data: {
|
data: {
|
||||||
status: "completed",
|
status: nextStatus,
|
||||||
completedAt: now,
|
completedAt: nextStatus === "completed" ? now : null,
|
||||||
claimedByUserId: null,
|
claimedByUserId: null,
|
||||||
claimedAt: null,
|
claimedAt: null,
|
||||||
...(units > 0 ? { unitsCompleted: { increment: units } } : {}),
|
...(units > 0 ? { unitsCompleted: { increment: units } } : {}),
|
||||||
@@ -85,18 +116,19 @@ export async function POST(req: NextRequest, ctx: { params: Promise<{ id: string
|
|||||||
const op = await prisma.operation.findUnique({ where: { id } });
|
const op = await prisma.operation.findUnique({ where: { id } });
|
||||||
await audit({
|
await audit({
|
||||||
actorId: actor.id,
|
actorId: actor.id,
|
||||||
action: "close_op",
|
action: nextStatus === "completed" ? "close_op" : "partial_close_op",
|
||||||
entity: "Operation",
|
entity: "Operation",
|
||||||
entityId: id,
|
entityId: id,
|
||||||
after: {
|
after: {
|
||||||
status: "completed",
|
status: nextStatus,
|
||||||
unitsProcessed: body.unitsProcessed ?? null,
|
unitsProcessed: body.unitsProcessed ?? null,
|
||||||
|
unitsRemaining: Math.max(0, remaining - units),
|
||||||
qcPassed: body.qc?.passed ?? null,
|
qcPassed: body.qc?.passed ?? null,
|
||||||
},
|
},
|
||||||
ipAddress: clientIp(req),
|
ipAddress: clientIp(req),
|
||||||
});
|
});
|
||||||
|
|
||||||
return ok({ operation: op });
|
return ok({ operation: op, partial: nextStatus === "partial" });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
return errorResponse(err);
|
return errorResponse(err);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -397,6 +397,28 @@ export default function ScanClient({ initialOp, viewer }: { initialOp: ScanOp; v
|
|||||||
</div>
|
</div>
|
||||||
) : null}
|
) : null}
|
||||||
|
|
||||||
|
{/*
|
||||||
|
Preview what Done will do. Keeps the match in lockstep with the
|
||||||
|
close route's partial-detection logic: units blank or >=
|
||||||
|
remaining means "fully done", anything less is a partial
|
||||||
|
handoff that releases the claim so the next operator can pick
|
||||||
|
it up.
|
||||||
|
*/}
|
||||||
|
{(() => {
|
||||||
|
const typed = units ? Number(units) : 0;
|
||||||
|
const remaining = Math.max(0, totalUnits - op.unitsCompleted);
|
||||||
|
const willPartial = typed > 0 && typed < remaining;
|
||||||
|
return willPartial ? (
|
||||||
|
<div className="rounded-md bg-orange-50 border border-orange-200 text-orange-900 text-xs px-3 py-2">
|
||||||
|
Pressing <span className="font-semibold">Done</span> with {typed} of {remaining}{" "}
|
||||||
|
remaining will mark this step <span className="font-semibold">Partial</span> and
|
||||||
|
release the claim so another operator can resume. Enter{" "}
|
||||||
|
<span className="font-mono">{remaining}</span> (or leave blank) if you actually
|
||||||
|
finished the batch.
|
||||||
|
</div>
|
||||||
|
) : null;
|
||||||
|
})()}
|
||||||
|
|
||||||
<div className="grid grid-cols-2 gap-3">
|
<div className="grid grid-cols-2 gap-3">
|
||||||
<button
|
<button
|
||||||
onClick={onRelease}
|
onClick={onRelease}
|
||||||
|
|||||||
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user