shoc-frontend-new/src/domain/work-orders/utils/wo-closability.ts

43 lines
1.5 KiB
TypeScript
Raw Normal View History

import type { WorkOrderTableRow } from "@/domain/work-orders/types/work-order-table-row";
const PENDING_UPLIFT_GAP = "Pending uplift approval";
/** Fields required to evaluate whether a WO may be marked Completed or generate a comp doc. */
export type WoClosabilityInput = Pick<WorkOrderTableRow, "upliftSummary" | "hasPendingUplift">;
export function hasPendingUplift(row: WoClosabilityInput): boolean {
if (row.hasPendingUplift === true) {
return true;
}
return (row.upliftSummary?.pendingCount ?? 0) > 0;
}
/**
* Returns human-readable labels for requirements still missing before Completed is allowed.
* Empty array means the WO may be marked Completed (for closability gates implemented here).
*/
export function getClosabilityGaps(row: WoClosabilityInput): string[] {
if (hasPendingUplift(row)) {
return [PENDING_UPLIFT_GAP];
}
return [];
}
export function formatClosabilityMissingTitle(gaps: string[]): string {
return `Missing: ${gaps.join(", ")}`;
}
export type CompDocGateResult = { allowed: true } | { allowed: false; reason: string };
/**
* Gate for completion-document generation. CompDocDialog (not yet on `dev`) should call this
* before opening generate flow and surface `reason` when blocked.
*/
export function canGenerateCompDoc(row: WoClosabilityInput): CompDocGateResult {
const gaps = getClosabilityGaps(row);
if (gaps.length > 0) {
return { allowed: false, reason: formatClosabilityMissingTitle(gaps) };
}
return { allowed: true };
}