@final-commerce/common 2.2.0-beta.1 → 2.2.0-preprod.1

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) => {
@@ -229,6 +230,7 @@ var InventorySpecificActionType = /* @__PURE__ */ ((InventorySpecificActionType2
229
230
  InventorySpecificActionType2["REFUND_DAMAGE"] = "REFUND_DAMAGE";
230
231
  InventorySpecificActionType2["SALE"] = "SALE";
231
232
  InventorySpecificActionType2["COMPOSED_FOR_SALE"] = "COMPOSED_FOR_SALE";
233
+ InventorySpecificActionType2["UNIT_FAMILY_CHANGED"] = "UNIT_FAMILY_CHANGED";
232
234
  InventorySpecificActionType2["TRANSFER"] = "TRANSFER";
233
235
  InventorySpecificActionType2["BULK_RECOUNT"] = "BULK_RECOUNT";
234
236
  InventorySpecificActionType2["APPLIED_FROM_WOO"] = "APPLIED_FROM_WOO";
@@ -1276,10 +1278,7 @@ function negateAtPaths(value, path, paths) {
1276
1278
  function padHex(value, length) {
1277
1279
  return value.toString(16).padStart(length, "0");
1278
1280
  }
1279
- var PROCESS_RANDOM = Array.from(
1280
- { length: 5 },
1281
- () => padHex(Math.floor(Math.random() * 256), 2)
1282
- ).join("");
1281
+ var PROCESS_RANDOM = Array.from({ length: 5 }, () => padHex(Math.floor(Math.random() * 256), 2)).join("");
1283
1282
  var objectIdCounter = Math.floor(Math.random() * 16777215);
1284
1283
  function generateMongoID() {
1285
1284
  const timestamp = padHex(Math.floor(Date.now() / 1e3), 8);
@@ -1424,6 +1423,8 @@ var STATE_SCHEMA = Object.freeze({
1424
1423
  // order-state-machine/financial-invariants.ts
1425
1424
  var REFUND_ALLOWED_FULFILLMENT = /* @__PURE__ */ new Set(["fulfilled", "returned", "partially_returned", "cancelled"]);
1426
1425
  var PAYMENT_COLLECTION_ORDER = ["unpaid", "partially_paid", "paid"];
1426
+ var UNRETURNED_MONEY_STATES = /* @__PURE__ */ new Set(["partially_paid", "paid", "partially_refunded"]);
1427
+ var REFUNDED_FULFILLMENT_LANDINGS = /* @__PURE__ */ new Set(["returned", "cancelled"]);
1427
1428
  var FINANCIAL_INVARIANTS = [
1428
1429
  {
1429
1430
  id: "no-leave-refunded",
@@ -1454,6 +1455,26 @@ var FINANCIAL_INVARIANTS = [
1454
1455
  const toIdx = PAYMENT_COLLECTION_ORDER.indexOf(to.payment);
1455
1456
  return fromIdx !== -1 && toIdx !== -1 && fromIdx > toIdx;
1456
1457
  }
1458
+ },
1459
+ {
1460
+ id: "no-cancel-with-unreturned-payments",
1461
+ 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",
1462
+ check: (from, to) => from !== null && from.fulfillment !== "cancelled" && to.fulfillment === "cancelled" && UNRETURNED_MONEY_STATES.has(to.payment)
1463
+ },
1464
+ {
1465
+ id: "no-draft-regression-with-captured-payments",
1466
+ 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",
1467
+ check: (from, to) => from !== null && from.fulfillment !== "draft" && to.fulfillment === "draft" && to.payment !== "unpaid"
1468
+ },
1469
+ {
1470
+ id: "no-reopen-voided-fulfillment",
1471
+ 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",
1472
+ check: (from, to) => (from === null || from.payment === "voided") && to.payment === "voided" && to.fulfillment !== "cancelled"
1473
+ },
1474
+ {
1475
+ id: "no-reopen-refunded-fulfillment",
1476
+ 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)",
1477
+ check: (from, to) => from?.payment === "refunded" && !REFUNDED_FULFILLMENT_LANDINGS.has(to.fulfillment)
1457
1478
  }
1458
1479
  ];
1459
1480
  function getFinancialInvariantViolations(from, to) {
@@ -1541,24 +1562,17 @@ var DISPLAY_LABEL_OVERRIDES = {
1541
1562
  "paid|on_hold": "Parked - Paid",
1542
1563
  "paid|fulfilled": "Completed",
1543
1564
  "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"
1565
+ "partially_refunded|partially_returned": "Partially Refunded"
1566
+ };
1567
+ var TERMINAL_LABEL_OVERRIDES = {
1568
+ voided: "Cancelled"
1553
1569
  };
1554
1570
  function buildDefaultDisplayStateMap() {
1555
1571
  const rules = [];
1556
1572
  for (const p of PAYMENT_STATES) {
1573
+ const terminalLabel = TERMINAL_PAYMENT_STATES.has(p) ? TERMINAL_LABEL_OVERRIDES[p] ?? PAYMENT_LABELS.get(p) : void 0;
1557
1574
  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)}`;
1575
+ const label = DISPLAY_LABEL_OVERRIDES[`${p}|${f}`] ?? terminalLabel ?? `${PAYMENT_LABELS.get(p)} / ${FULFILLMENT_LABELS.get(f)}`;
1562
1576
  rules.push({ paymentState: p, fulfillmentState: f, label });
1563
1577
  }
1564
1578
  }
@@ -1681,6 +1695,9 @@ function buildOrderContext(order, extras) {
1681
1695
  }
1682
1696
 
1683
1697
  // order-state-machine/display-state.ts
1698
+ function atomLabel(atom, defs) {
1699
+ return defs.find((d) => d.id === atom)?.label ?? atom;
1700
+ }
1684
1701
  function matchesOptional(actual, rule) {
1685
1702
  if (rule === void 0) return true;
1686
1703
  return Array.isArray(rule) ? rule.includes(actual) : rule === actual;
@@ -1691,7 +1708,9 @@ function deriveDisplayState(payment, fulfillment, config) {
1691
1708
  return { label: rule.label, color: rule.color, icon: rule.icon };
1692
1709
  }
1693
1710
  }
1694
- return { label: `${payment} / ${fulfillment}` };
1711
+ return {
1712
+ label: `${atomLabel(payment, STATE_SCHEMA.paymentStates)} / ${atomLabel(fulfillment, STATE_SCHEMA.fulfillmentStates)}`
1713
+ };
1695
1714
  }
1696
1715
  function displayRuleMatchesPair(rule, payment, fulfillment) {
1697
1716
  return matchesOptional(payment, rule.paymentState) && matchesOptional(fulfillment, rule.fulfillmentState);
@@ -1927,13 +1946,11 @@ function labelForFulfillment(id, config) {
1927
1946
  return config.fulfillmentStates.find((s) => s.id === id)?.label ?? id;
1928
1947
  }
1929
1948
  function transitionDisplayLabel(to, config) {
1930
- const pay = labelForPayment(to.payment, config);
1931
- const ful = labelForFulfillment(to.fulfillment, config);
1932
1949
  const derived = deriveDisplayState(to.payment, to.fulfillment, config).label;
1933
- if (derived && derived !== `${to.payment} / ${to.fulfillment}`) {
1950
+ if (derived) {
1934
1951
  return derived;
1935
1952
  }
1936
- return `${pay} \xB7 ${ful}`;
1953
+ return `${labelForPayment(to.payment, config)} \xB7 ${labelForFulfillment(to.fulfillment, config)}`;
1937
1954
  }
1938
1955
  function conditionMetLines(config, from, to, context) {
1939
1956
  const lines = [];
@@ -1962,20 +1979,12 @@ function conditionMetLines(config, from, to, context) {
1962
1979
  }
1963
1980
  function getAvailableTransitions(current, config, context) {
1964
1981
  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
1982
  for (const s of config.fulfillmentStates) {
1970
1983
  if (s.id === current.fulfillment) continue;
1971
1984
  candidates.push({ payment: current.payment, fulfillment: s.id });
1972
1985
  }
1973
1986
  const out = [];
1974
- const seen = /* @__PURE__ */ new Set();
1975
1987
  for (const to of candidates) {
1976
- const key = `${to.payment}:${to.fulfillment}`;
1977
- if (seen.has(key)) continue;
1978
- seen.add(key);
1979
1988
  const result = canTransition({
1980
1989
  from: current,
1981
1990
  to,
@@ -2424,6 +2433,27 @@ function inferStatesFromLegacyStatus(order) {
2424
2433
  return { payment: "unpaid", fulfillment: "pending" };
2425
2434
  }
2426
2435
 
2436
+ // order-state-machine/write-strategy.ts
2437
+ var LegacyWriteStrategy = class {
2438
+ applyState(_order) {
2439
+ }
2440
+ };
2441
+ var DualWriteStrategy = class {
2442
+ constructor(config) {
2443
+ this.config = config;
2444
+ }
2445
+ applyState(order) {
2446
+ if (order.paymentState && order.fulfillmentState) {
2447
+ setOrderState(order, order.paymentState, order.fulfillmentState, this.config);
2448
+ return;
2449
+ }
2450
+ if (order.status) {
2451
+ const inferred = inferStatesFromLegacyStatus(order);
2452
+ setOrderState(order, inferred.payment, inferred.fulfillment, this.config);
2453
+ }
2454
+ }
2455
+ };
2456
+
2427
2457
  // order-state-machine/resolve-template.ts
2428
2458
  function resolveTemplate(templateJson, params, values = {}) {
2429
2459
  const resolvedValues = {};
@@ -2725,27 +2755,6 @@ var ORDER_STATE_EVENTS = {
2725
2755
  stateTransitionBlocked: "state-transition-blocked",
2726
2756
  configUpdated: "config-updated"
2727
2757
  };
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
2758
  export {
2750
2759
  AttributeType,
2751
2760
  CONDITION_OPERATORS,