23 lines
1 KiB
TypeScript
23 lines
1 KiB
TypeScript
|
|
import type { OrderItem } from "@/lib/types";
|
||
|
|
|
||
|
|
/**
|
||
|
|
* An order is "Complete" iff it has at least one line item and every
|
||
|
|
* line item is `delivered`. This is the only order-level indicator we
|
||
|
|
* render — there is no stored `orders.status` field to keep in sync.
|
||
|
|
*
|
||
|
|
* Why this is its own helper instead of inlined at the call site:
|
||
|
|
* - Centralises the rule (one place to change if "complete" ever
|
||
|
|
* gains another condition, e.g. "all items delivered AND invoiced").
|
||
|
|
* - Locks the contract with a unit test (see order-completion.test.ts).
|
||
|
|
* - Pure function — easy to test without touching PocketBase.
|
||
|
|
*
|
||
|
|
* Note: an order with **zero** items is intentionally NOT complete.
|
||
|
|
* The /orders list treats no-items as a degenerate case (the row
|
||
|
|
* collapses to an empty item list) — we don't want an empty draft
|
||
|
|
* order to wear a green "Complete" badge.
|
||
|
|
*/
|
||
|
|
export function isOrderComplete(items: OrderItem[] | null | undefined): boolean {
|
||
|
|
if (!items || items.length === 0) return false;
|
||
|
|
return items.every((i) => i.item_status === "delivered");
|
||
|
|
}
|