import type { PoolClient } from 'pg'; import { pool } from '../../../../lib/postgres/connection.js'; import type { OrderShipmentRollup } from '../../types/orderShipmentRollup.js'; /** * The "ambient" data the rollup needs about the order — read it once and * pass it in so callers can short-circuit without an extra DB roundtrip. */ export interface RollupOrderSummary { order_id: number; order_status: string | null; } /** * Aggregate stats produced by the item-level math. The booleans drive the * predicate vocabulary `all:` / `any:`. `shippableItemsCount` * lets the all-digital short-circuit trip without a second query. * * See wiki/multi-shipment-design.md → "Item-based rollup math". */ export interface RollupStats { shippableItemsCount: number; allDelivered: boolean; anyDelivered: boolean; allShipped: boolean; anyShipped: boolean; allCanceled: boolean; anyCanceled: boolean; allPending: boolean; } /** * Per-(order_item, shipment_status) row returned by the rollup SQL. App code * buckets these into phases via the status registration in * `getConfig('oms.order.shipmentStatus')`. */ interface RawRollupRow { order_item_id: number; qty_ordered: number; status: string | null; qty: number | null; } /** * Aggregate the raw rows into per-item totals by phase, then into the boolean * predicates the rule resolver consumes. Canceled quantities are tracked * separately (`qty_canceled`) so the canceled rules can fire, but a canceled * shipment still contributes zero to `qty_shipped` / `qty_delivered` — so an * item that was canceled and then re-shipped reads as shipped (the rule order * checks shipped/delivered before canceled). Unshipped items (no shipment_item * row) contribute to none of the three. */ export declare function aggregateRollupStats(rows: RawRollupRow[]): RollupStats; /** * Compute the rollup for an order, hitting the DB once for stats and once * for the order summary (if not provided). * * **Hookable** via `hookBefore('resolveShipmentRollup')` / * `hookAfter('resolveShipmentRollup')` so an extension can short-circuit or * override the math (e.g. for returns). */ export declare function resolveShipmentRollupForOrder(orderId: number, connection?: PoolClient | typeof pool, preloadedOrder?: RollupOrderSummary): Promise; export declare function hookBeforeResolveShipmentRollup(callback: (this: { order: RollupOrderSummary; stats: RollupStats; }, ...args: [RollupOrderSummary, RollupStats]) => void | Promise, priority?: number): void; export declare function hookAfterResolveShipmentRollup(callback: (this: { order: RollupOrderSummary; stats: RollupStats; }, ...args: [RollupOrderSummary, RollupStats]) => void | Promise, priority?: number): void; export {};