All files / apps/work-orders-service/src/work-orders work-order-transitions.ts

100% Statements 7/7
100% Branches 0/0
100% Functions 2/2
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 393x                 3x                           3x       10x     3x         3x 5x    
import { WorkOrderStatus } from './work-order.enums';
 
/**
 * Lifecycle: open → assigned → in_progress → completed, with cancellation
 * possible from any non-terminal state. Entering `assigned` is deliberately
 * excluded here — it only happens through assignment, which guarantees an
 * assignee is present (mirroring the DB CHECK constraint).
 */
const ALLOWED_TRANSITIONS: Record<WorkOrderStatus, readonly WorkOrderStatus[]> =
  {
    [WorkOrderStatus.OPEN]: [WorkOrderStatus.CANCELLED],
    [WorkOrderStatus.ASSIGNED]: [
      WorkOrderStatus.IN_PROGRESS,
      WorkOrderStatus.CANCELLED,
    ],
    [WorkOrderStatus.IN_PROGRESS]: [
      WorkOrderStatus.COMPLETED,
      WorkOrderStatus.CANCELLED,
    ],
    [WorkOrderStatus.COMPLETED]: [],
    [WorkOrderStatus.CANCELLED]: [],
  };
 
export function canTransition(
  from: WorkOrderStatus,
  to: WorkOrderStatus,
): boolean {
  return ALLOWED_TRANSITIONS[from].includes(to);
}
 
const ASSIGNABLE_STATUSES: readonly WorkOrderStatus[] = [
  WorkOrderStatus.OPEN,
  WorkOrderStatus.ASSIGNED, // reassignment is allowed until work starts
];
 
export function canAssign(status: WorkOrderStatus): boolean {
  return ASSIGNABLE_STATUSES.includes(status);
}