fixes
This commit is contained in:
@@ -15,6 +15,7 @@ import type {
|
||||
WorkOrderOperationTimerInput,
|
||||
WorkOrderMaterialIssueInput,
|
||||
WorkOrderStatus,
|
||||
WorkOrderStatusUpdateInput,
|
||||
WorkOrderSummaryDto,
|
||||
} from "@mrp/shared";
|
||||
|
||||
@@ -41,6 +42,7 @@ type WorkOrderRecord = {
|
||||
id: string;
|
||||
workOrderNumber: string;
|
||||
status: string;
|
||||
holdReason: string | null;
|
||||
quantity: number;
|
||||
completedQuantity: number;
|
||||
dueDate: Date | null;
|
||||
@@ -390,6 +392,7 @@ function mapDetail(
|
||||
return {
|
||||
...mapSummary(record),
|
||||
notes: record.notes,
|
||||
holdReason: record.holdReason,
|
||||
createdAt: record.createdAt.toISOString(),
|
||||
itemType: record.item.type,
|
||||
itemUnitOfMeasure: record.item.unitOfMeasure,
|
||||
@@ -1294,12 +1297,18 @@ export async function updateWorkOrder(workOrderId: string, payload: WorkOrderInp
|
||||
return workOrder ? { ok: true as const, workOrder } : { ok: false as const, reason: "Unable to load saved work order." };
|
||||
}
|
||||
|
||||
export async function updateWorkOrderStatus(workOrderId: string, status: WorkOrderStatus, actorId?: string | null) {
|
||||
export async function updateWorkOrderStatus(
|
||||
workOrderId: string,
|
||||
payload: WorkOrderStatusUpdateInput,
|
||||
actorId?: string | null
|
||||
) {
|
||||
const existing = await workOrderModel.findUnique({
|
||||
where: { id: workOrderId },
|
||||
select: {
|
||||
id: true,
|
||||
workOrderNumber: true,
|
||||
status: true,
|
||||
holdReason: true,
|
||||
quantity: true,
|
||||
completedQuantity: true,
|
||||
},
|
||||
@@ -1309,18 +1318,24 @@ export async function updateWorkOrderStatus(workOrderId: string, status: WorkOrd
|
||||
return { ok: false as const, reason: "Work order was not found." };
|
||||
}
|
||||
|
||||
if (existing.status === "COMPLETE" && status !== "COMPLETE") {
|
||||
if (existing.status === "COMPLETE" && payload.status !== "COMPLETE") {
|
||||
return { ok: false as const, reason: "Completed work orders cannot be reopened from quick actions." };
|
||||
}
|
||||
|
||||
if (status === "COMPLETE" && existing.completedQuantity < existing.quantity) {
|
||||
if (payload.status === "COMPLETE" && existing.completedQuantity < existing.quantity) {
|
||||
return { ok: false as const, reason: "Use the completion action to finish a work order." };
|
||||
}
|
||||
|
||||
const nextHoldReason = payload.reason?.trim() ?? "";
|
||||
if (payload.status === "ON_HOLD" && nextHoldReason.length === 0) {
|
||||
return { ok: false as const, reason: "An On Hold reason is required before the work order can be paused." };
|
||||
}
|
||||
|
||||
await workOrderModel.update({
|
||||
where: { id: workOrderId },
|
||||
data: {
|
||||
status,
|
||||
status: payload.status,
|
||||
holdReason: payload.status === "ON_HOLD" ? nextHoldReason : null,
|
||||
},
|
||||
});
|
||||
|
||||
@@ -1333,10 +1348,12 @@ export async function updateWorkOrderStatus(workOrderId: string, status: WorkOrd
|
||||
entityType: "work-order",
|
||||
entityId: workOrderId,
|
||||
action: "status.updated",
|
||||
summary: `Updated work order ${workOrder.workOrderNumber} to ${status}.`,
|
||||
summary: `Updated work order ${workOrder.workOrderNumber} to ${payload.status}.`,
|
||||
metadata: {
|
||||
workOrderNumber: workOrder.workOrderNumber,
|
||||
status,
|
||||
previousStatus: existing.status,
|
||||
status: payload.status,
|
||||
holdReason: payload.status === "ON_HOLD" ? nextHoldReason : null,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user