@final-commerce/common 2.2.0-beta.2 → 2.2.0-staging.2

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.mjs CHANGED
@@ -199,6 +199,7 @@ var Platform = /* @__PURE__ */ ((Platform2) => {
199
199
  var OrderPlatform = /* @__PURE__ */ ((OrderPlatform2) => {
200
200
  OrderPlatform2["POS"] = "in-store";
201
201
  OrderPlatform2["WOO_COMMERCE"] = "woo-commerce";
202
+ OrderPlatform2["PUBLIC_API"] = "public-api";
202
203
  return OrderPlatform2;
203
204
  })(OrderPlatform || {});
204
205
  var CustomerPlatform = /* @__PURE__ */ ((CustomerPlatform2) => {
@@ -1276,10 +1277,7 @@ function negateAtPaths(value, path, paths) {
1276
1277
  function padHex(value, length) {
1277
1278
  return value.toString(16).padStart(length, "0");
1278
1279
  }
1279
- var PROCESS_RANDOM = Array.from(
1280
- { length: 5 },
1281
- () => padHex(Math.floor(Math.random() * 256), 2)
1282
- ).join("");
1280
+ var PROCESS_RANDOM = Array.from({ length: 5 }, () => padHex(Math.floor(Math.random() * 256), 2)).join("");
1283
1281
  var objectIdCounter = Math.floor(Math.random() * 16777215);
1284
1282
  function generateMongoID() {
1285
1283
  const timestamp = padHex(Math.floor(Date.now() / 1e3), 8);
@@ -1424,6 +1422,8 @@ var STATE_SCHEMA = Object.freeze({
1424
1422
  // order-state-machine/financial-invariants.ts
1425
1423
  var REFUND_ALLOWED_FULFILLMENT = /* @__PURE__ */ new Set(["fulfilled", "returned", "partially_returned", "cancelled"]);
1426
1424
  var PAYMENT_COLLECTION_ORDER = ["unpaid", "partially_paid", "paid"];
1425
+ var UNRETURNED_MONEY_STATES = /* @__PURE__ */ new Set(["partially_paid", "paid", "partially_refunded"]);
1426
+ var REFUNDED_FULFILLMENT_LANDINGS = /* @__PURE__ */ new Set(["returned", "cancelled"]);
1427
1427
  var FINANCIAL_INVARIANTS = [
1428
1428
  {
1429
1429
  id: "no-leave-refunded",
@@ -1454,6 +1454,26 @@ var FINANCIAL_INVARIANTS = [
1454
1454
  const toIdx = PAYMENT_COLLECTION_ORDER.indexOf(to.payment);
1455
1455
  return fromIdx !== -1 && toIdx !== -1 && fromIdx > toIdx;
1456
1456
  }
1457
+ },
1458
+ {
1459
+ id: "no-cancel-with-unreturned-payments",
1460
+ 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",
1461
+ check: (from, to) => from !== null && from.fulfillment !== "cancelled" && to.fulfillment === "cancelled" && UNRETURNED_MONEY_STATES.has(to.payment)
1462
+ },
1463
+ {
1464
+ id: "no-draft-regression-with-captured-payments",
1465
+ 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",
1466
+ check: (from, to) => from !== null && from.fulfillment !== "draft" && to.fulfillment === "draft" && to.payment !== "unpaid"
1467
+ },
1468
+ {
1469
+ id: "no-reopen-voided-fulfillment",
1470
+ 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",
1471
+ check: (from, to) => (from === null || from.payment === "voided") && to.payment === "voided" && to.fulfillment !== "cancelled"
1472
+ },
1473
+ {
1474
+ id: "no-reopen-refunded-fulfillment",
1475
+ 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)",
1476
+ check: (from, to) => from?.payment === "refunded" && !REFUNDED_FULFILLMENT_LANDINGS.has(to.fulfillment)
1457
1477
  }
1458
1478
  ];
1459
1479
  function getFinancialInvariantViolations(from, to) {
@@ -1541,24 +1561,17 @@ var DISPLAY_LABEL_OVERRIDES = {
1541
1561
  "paid|on_hold": "Parked - Paid",
1542
1562
  "paid|fulfilled": "Completed",
1543
1563
  "partially_refunded|fulfilled": "Partially Refunded",
1544
- "partially_refunded|partially_returned": "Partially Refunded",
1545
- "refunded|fulfilled": "Refunded",
1546
- "refunded|returned": "Refunded",
1547
- // Full refund of a never-fulfilled order (kaching refund op lands on
1548
- // cancelled when nothing was delivered). Terminal payment states emit no
1549
- // generic rows, so without this the pair fell through to the raw
1550
- // "refunded / cancelled" engine fallback.
1551
- "refunded|cancelled": "Refunded",
1552
- "voided|cancelled": "Cancelled"
1564
+ "partially_refunded|partially_returned": "Partially Refunded"
1565
+ };
1566
+ var TERMINAL_LABEL_OVERRIDES = {
1567
+ voided: "Cancelled"
1553
1568
  };
1554
1569
  function buildDefaultDisplayStateMap() {
1555
1570
  const rules = [];
1556
1571
  for (const p of PAYMENT_STATES) {
1572
+ const terminalLabel = TERMINAL_PAYMENT_STATES.has(p) ? TERMINAL_LABEL_OVERRIDES[p] ?? PAYMENT_LABELS.get(p) : void 0;
1557
1573
  for (const f of FULFILLMENT_STATES) {
1558
- const key = `${p}|${f}`;
1559
- const override = DISPLAY_LABEL_OVERRIDES[key];
1560
- if (TERMINAL_PAYMENT_STATES.has(p) && !override) continue;
1561
- const label = override ?? `${PAYMENT_LABELS.get(p)} / ${FULFILLMENT_LABELS.get(f)}`;
1574
+ const label = DISPLAY_LABEL_OVERRIDES[`${p}|${f}`] ?? terminalLabel ?? `${PAYMENT_LABELS.get(p)} / ${FULFILLMENT_LABELS.get(f)}`;
1562
1575
  rules.push({ paymentState: p, fulfillmentState: f, label });
1563
1576
  }
1564
1577
  }
@@ -1681,6 +1694,9 @@ function buildOrderContext(order, extras) {
1681
1694
  }
1682
1695
 
1683
1696
  // order-state-machine/display-state.ts
1697
+ function atomLabel(atom, defs) {
1698
+ return defs.find((d) => d.id === atom)?.label ?? atom;
1699
+ }
1684
1700
  function matchesOptional(actual, rule) {
1685
1701
  if (rule === void 0) return true;
1686
1702
  return Array.isArray(rule) ? rule.includes(actual) : rule === actual;
@@ -1691,7 +1707,9 @@ function deriveDisplayState(payment, fulfillment, config) {
1691
1707
  return { label: rule.label, color: rule.color, icon: rule.icon };
1692
1708
  }
1693
1709
  }
1694
- return { label: `${payment} / ${fulfillment}` };
1710
+ return {
1711
+ label: `${atomLabel(payment, STATE_SCHEMA.paymentStates)} / ${atomLabel(fulfillment, STATE_SCHEMA.fulfillmentStates)}`
1712
+ };
1695
1713
  }
1696
1714
  function displayRuleMatchesPair(rule, payment, fulfillment) {
1697
1715
  return matchesOptional(payment, rule.paymentState) && matchesOptional(fulfillment, rule.fulfillmentState);
@@ -1927,13 +1945,11 @@ function labelForFulfillment(id, config) {
1927
1945
  return config.fulfillmentStates.find((s) => s.id === id)?.label ?? id;
1928
1946
  }
1929
1947
  function transitionDisplayLabel(to, config) {
1930
- const pay = labelForPayment(to.payment, config);
1931
- const ful = labelForFulfillment(to.fulfillment, config);
1932
1948
  const derived = deriveDisplayState(to.payment, to.fulfillment, config).label;
1933
- if (derived && derived !== `${to.payment} / ${to.fulfillment}`) {
1949
+ if (derived) {
1934
1950
  return derived;
1935
1951
  }
1936
- return `${pay} \xB7 ${ful}`;
1952
+ return `${labelForPayment(to.payment, config)} \xB7 ${labelForFulfillment(to.fulfillment, config)}`;
1937
1953
  }
1938
1954
  function conditionMetLines(config, from, to, context) {
1939
1955
  const lines = [];
@@ -1962,20 +1978,12 @@ function conditionMetLines(config, from, to, context) {
1962
1978
  }
1963
1979
  function getAvailableTransitions(current, config, context) {
1964
1980
  const candidates = [];
1965
- for (const s of config.paymentStates) {
1966
- if (s.id === current.payment) continue;
1967
- candidates.push({ payment: s.id, fulfillment: current.fulfillment });
1968
- }
1969
1981
  for (const s of config.fulfillmentStates) {
1970
1982
  if (s.id === current.fulfillment) continue;
1971
1983
  candidates.push({ payment: current.payment, fulfillment: s.id });
1972
1984
  }
1973
1985
  const out = [];
1974
- const seen = /* @__PURE__ */ new Set();
1975
1986
  for (const to of candidates) {
1976
- const key = `${to.payment}:${to.fulfillment}`;
1977
- if (seen.has(key)) continue;
1978
- seen.add(key);
1979
1987
  const result = canTransition({
1980
1988
  from: current,
1981
1989
  to,
@@ -2424,6 +2432,27 @@ function inferStatesFromLegacyStatus(order) {
2424
2432
  return { payment: "unpaid", fulfillment: "pending" };
2425
2433
  }
2426
2434
 
2435
+ // order-state-machine/write-strategy.ts
2436
+ var LegacyWriteStrategy = class {
2437
+ applyState(_order) {
2438
+ }
2439
+ };
2440
+ var DualWriteStrategy = class {
2441
+ constructor(config) {
2442
+ this.config = config;
2443
+ }
2444
+ applyState(order) {
2445
+ if (order.paymentState && order.fulfillmentState) {
2446
+ setOrderState(order, order.paymentState, order.fulfillmentState, this.config);
2447
+ return;
2448
+ }
2449
+ if (order.status) {
2450
+ const inferred = inferStatesFromLegacyStatus(order);
2451
+ setOrderState(order, inferred.payment, inferred.fulfillment, this.config);
2452
+ }
2453
+ }
2454
+ };
2455
+
2427
2456
  // order-state-machine/resolve-template.ts
2428
2457
  function resolveTemplate(templateJson, params, values = {}) {
2429
2458
  const resolvedValues = {};
@@ -2725,27 +2754,6 @@ var ORDER_STATE_EVENTS = {
2725
2754
  stateTransitionBlocked: "state-transition-blocked",
2726
2755
  configUpdated: "config-updated"
2727
2756
  };
2728
-
2729
- // order-state-machine/write-strategy.ts
2730
- var LegacyWriteStrategy = class {
2731
- applyState(_order) {
2732
- }
2733
- };
2734
- var DualWriteStrategy = class {
2735
- constructor(config) {
2736
- this.config = config;
2737
- }
2738
- applyState(order) {
2739
- if (order.paymentState && order.fulfillmentState) {
2740
- setOrderState(order, order.paymentState, order.fulfillmentState, this.config);
2741
- return;
2742
- }
2743
- if (order.status) {
2744
- const inferred = inferStatesFromLegacyStatus(order);
2745
- setOrderState(order, inferred.payment, inferred.fulfillment, this.config);
2746
- }
2747
- }
2748
- };
2749
2757
  export {
2750
2758
  AttributeType,
2751
2759
  CONDITION_OPERATORS,