@final-commerce/common 2.2.0-staging.1 → 2.2.0-staging.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1592,6 +1592,8 @@ var STATE_SCHEMA = Object.freeze({
1592
1592
  // order-state-machine/financial-invariants.ts
1593
1593
  var REFUND_ALLOWED_FULFILLMENT = /* @__PURE__ */ new Set(["fulfilled", "returned", "partially_returned", "cancelled"]);
1594
1594
  var PAYMENT_COLLECTION_ORDER = ["unpaid", "partially_paid", "paid"];
1595
+ var UNRETURNED_MONEY_STATES = /* @__PURE__ */ new Set(["partially_paid", "paid", "partially_refunded"]);
1596
+ var REFUNDED_FULFILLMENT_LANDINGS = /* @__PURE__ */ new Set(["returned", "cancelled"]);
1595
1597
  var FINANCIAL_INVARIANTS = [
1596
1598
  {
1597
1599
  id: "no-leave-refunded",
@@ -1622,6 +1624,26 @@ var FINANCIAL_INVARIANTS = [
1622
1624
  const toIdx = PAYMENT_COLLECTION_ORDER.indexOf(to.payment);
1623
1625
  return fromIdx !== -1 && toIdx !== -1 && fromIdx > toIdx;
1624
1626
  }
1627
+ },
1628
+ {
1629
+ id: "no-cancel-with-unreturned-payments",
1630
+ description: "Fulfillment cannot enter cancelled while the payment state carries unreturned captured money (paid / partially_paid / partially_refunded) \u2014 return the money first: a full refund of an unfulfilled order lands refunded \xD7 cancelled, a void lands voided \xD7 cancelled. Scoped to the cancelling MOVE (from.fulfillment !== cancelled) so payment-axis edges evaluated against an already-cancelled fulfillment do not trip it",
1631
+ check: (from, to) => from !== null && from.fulfillment !== "cancelled" && to.fulfillment === "cancelled" && UNRETURNED_MONEY_STATES.has(to.payment)
1632
+ },
1633
+ {
1634
+ id: "no-draft-regression-with-captured-payments",
1635
+ description: "Fulfillment cannot regress to draft once money has been captured \u2014 a money-carrying order is no longer a cart (bug #31: resume lands deposit orders on in_progress for the same reason). New orders and draft-to-draft moves are exempt, so partially_paid \xD7 draft remains declarable as an initial state",
1636
+ check: (from, to) => from !== null && from.fulfillment !== "draft" && to.fulfillment === "draft" && to.payment !== "unpaid"
1637
+ },
1638
+ {
1639
+ id: "no-reopen-voided-fulfillment",
1640
+ description: "A voided order exists only at cancelled fulfillment: a voided order cannot re-enter fulfillment, and a voided \xD7 non-cancelled pair cannot be materialized from nothing (complements no-leave-voided, which only freezes the payment axis). Moves INTO voided from a live payment state are the void flow\u2019s concern, not this rule\u2019s",
1641
+ check: (from, to) => (from === null || from.payment === "voided") && to.payment === "voided" && to.fulfillment !== "cancelled"
1642
+ },
1643
+ {
1644
+ id: "no-reopen-refunded-fulfillment",
1645
+ description: "A fully refunded order cannot re-enter fulfillment \u2014 it may only relabel between its canonical landings, returned and cancelled (complements no-leave-refunded, which only freezes the payment axis)",
1646
+ check: (from, to) => from?.payment === "refunded" && !REFUNDED_FULFILLMENT_LANDINGS.has(to.fulfillment)
1625
1647
  }
1626
1648
  ];
1627
1649
  function getFinancialInvariantViolations(from, to) {
@@ -1709,24 +1731,17 @@ var DISPLAY_LABEL_OVERRIDES = {
1709
1731
  "paid|on_hold": "Parked - Paid",
1710
1732
  "paid|fulfilled": "Completed",
1711
1733
  "partially_refunded|fulfilled": "Partially Refunded",
1712
- "partially_refunded|partially_returned": "Partially Refunded",
1713
- "refunded|fulfilled": "Refunded",
1714
- "refunded|returned": "Refunded",
1715
- // Full refund of a never-fulfilled order (kaching refund op lands on
1716
- // cancelled when nothing was delivered). Terminal payment states emit no
1717
- // generic rows, so without this the pair fell through to the raw
1718
- // "refunded / cancelled" engine fallback.
1719
- "refunded|cancelled": "Refunded",
1720
- "voided|cancelled": "Cancelled"
1734
+ "partially_refunded|partially_returned": "Partially Refunded"
1735
+ };
1736
+ var TERMINAL_LABEL_OVERRIDES = {
1737
+ voided: "Cancelled"
1721
1738
  };
1722
1739
  function buildDefaultDisplayStateMap() {
1723
1740
  const rules = [];
1724
1741
  for (const p of PAYMENT_STATES) {
1742
+ const terminalLabel = TERMINAL_PAYMENT_STATES.has(p) ? TERMINAL_LABEL_OVERRIDES[p] ?? PAYMENT_LABELS.get(p) : void 0;
1725
1743
  for (const f of FULFILLMENT_STATES) {
1726
- const key = `${p}|${f}`;
1727
- const override = DISPLAY_LABEL_OVERRIDES[key];
1728
- if (TERMINAL_PAYMENT_STATES.has(p) && !override) continue;
1729
- const label = override ?? `${PAYMENT_LABELS.get(p)} / ${FULFILLMENT_LABELS.get(f)}`;
1744
+ const label = DISPLAY_LABEL_OVERRIDES[`${p}|${f}`] ?? terminalLabel ?? `${PAYMENT_LABELS.get(p)} / ${FULFILLMENT_LABELS.get(f)}`;
1730
1745
  rules.push({ paymentState: p, fulfillmentState: f, label });
1731
1746
  }
1732
1747
  }
@@ -1849,6 +1864,9 @@ function buildOrderContext(order, extras) {
1849
1864
  }
1850
1865
 
1851
1866
  // order-state-machine/display-state.ts
1867
+ function atomLabel(atom, defs) {
1868
+ return defs.find((d) => d.id === atom)?.label ?? atom;
1869
+ }
1852
1870
  function matchesOptional(actual, rule) {
1853
1871
  if (rule === void 0) return true;
1854
1872
  return Array.isArray(rule) ? rule.includes(actual) : rule === actual;
@@ -1859,7 +1877,9 @@ function deriveDisplayState(payment, fulfillment, config) {
1859
1877
  return { label: rule.label, color: rule.color, icon: rule.icon };
1860
1878
  }
1861
1879
  }
1862
- return { label: `${payment} / ${fulfillment}` };
1880
+ return {
1881
+ label: `${atomLabel(payment, STATE_SCHEMA.paymentStates)} / ${atomLabel(fulfillment, STATE_SCHEMA.fulfillmentStates)}`
1882
+ };
1863
1883
  }
1864
1884
  function displayRuleMatchesPair(rule, payment, fulfillment) {
1865
1885
  return matchesOptional(payment, rule.paymentState) && matchesOptional(fulfillment, rule.fulfillmentState);
@@ -2095,13 +2115,11 @@ function labelForFulfillment(id, config) {
2095
2115
  return config.fulfillmentStates.find((s) => s.id === id)?.label ?? id;
2096
2116
  }
2097
2117
  function transitionDisplayLabel(to, config) {
2098
- const pay = labelForPayment(to.payment, config);
2099
- const ful = labelForFulfillment(to.fulfillment, config);
2100
2118
  const derived = deriveDisplayState(to.payment, to.fulfillment, config).label;
2101
- if (derived && derived !== `${to.payment} / ${to.fulfillment}`) {
2119
+ if (derived) {
2102
2120
  return derived;
2103
2121
  }
2104
- return `${pay} \xB7 ${ful}`;
2122
+ return `${labelForPayment(to.payment, config)} \xB7 ${labelForFulfillment(to.fulfillment, config)}`;
2105
2123
  }
2106
2124
  function conditionMetLines(config, from, to, context) {
2107
2125
  const lines = [];
@@ -2130,20 +2148,12 @@ function conditionMetLines(config, from, to, context) {
2130
2148
  }
2131
2149
  function getAvailableTransitions(current, config, context) {
2132
2150
  const candidates = [];
2133
- for (const s of config.paymentStates) {
2134
- if (s.id === current.payment) continue;
2135
- candidates.push({ payment: s.id, fulfillment: current.fulfillment });
2136
- }
2137
2151
  for (const s of config.fulfillmentStates) {
2138
2152
  if (s.id === current.fulfillment) continue;
2139
2153
  candidates.push({ payment: current.payment, fulfillment: s.id });
2140
2154
  }
2141
2155
  const out = [];
2142
- const seen = /* @__PURE__ */ new Set();
2143
2156
  for (const to of candidates) {
2144
- const key = `${to.payment}:${to.fulfillment}`;
2145
- if (seen.has(key)) continue;
2146
- seen.add(key);
2147
2157
  const result = canTransition({
2148
2158
  from: current,
2149
2159
  to,