@final-commerce/common 2.8.1-preprod.1 → 2.8.1-preprod.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.mjs CHANGED
@@ -1706,6 +1706,48 @@ function compositeReservationMismatches(order) {
1706
1706
  });
1707
1707
  }
1708
1708
 
1709
+ // utils/line-stock-moves.util.ts
1710
+ function lineStockMoves(item, quantity, reservations = []) {
1711
+ const sign = Math.sign(quantity);
1712
+ const pool = (stockVariantId) => stockVariantId ? { stockVariantId: stockVariantId.toString() } : {};
1713
+ if (!item.components?.length)
1714
+ return [
1715
+ {
1716
+ productId: item.productId.toString(),
1717
+ variantId: item.variantId.toString(),
1718
+ fields: {
1719
+ quantity,
1720
+ ...item.stockQuantity != null ? { stockQuantity: sign * item.stockQuantity, unitId: item.unitId?.toString() } : {},
1721
+ ...pool(item.stockVariantId)
1722
+ }
1723
+ }
1724
+ ];
1725
+ const booked = new Set(
1726
+ reservations.filter((reservation) => reservation.lineItemInternalId && reservation.lineItemInternalId === item.internalId).map((reservation) => reservation.componentIndex)
1727
+ );
1728
+ return item.components.flatMap((component, componentIndex) => {
1729
+ if (booked.has(componentIndex)) return [];
1730
+ if (component.unitId && component.stockQuantity == null)
1731
+ throw new Error(
1732
+ `Composite ${item.productId?.toString()} component ${component.variantId.toString()} is sold in ${component.unitId.toString()} but carries no stockQuantity`
1733
+ );
1734
+ return [
1735
+ {
1736
+ productId: component.productId.toString(),
1737
+ variantId: component.variantId.toString(),
1738
+ componentIndex,
1739
+ fields: {
1740
+ quantity: component.quantity * quantity,
1741
+ ...component.stockQuantity != null ? { stockQuantity: component.stockQuantity * quantity, unitId: component.unitId?.toString() } : {},
1742
+ ...pool(component.stockVariantId),
1743
+ ...item.productId ? { compositeProductId: item.productId.toString() } : {},
1744
+ ...item.name ? { compositeName: item.name } : {}
1745
+ }
1746
+ }
1747
+ ];
1748
+ });
1749
+ }
1750
+
1709
1751
  // units/family-code.ts
1710
1752
  var MAX_SLUG_LENGTH = 32;
1711
1753
  var FALLBACK_SLUG = "family";
@@ -2854,6 +2896,39 @@ function isInCart(order) {
2854
2896
  return (order.status ?? "").trim().toLowerCase() === "in-cart";
2855
2897
  }
2856
2898
 
2899
+ // order-state-machine/assigned-users.ts
2900
+ function orderAssignments(order) {
2901
+ const value = order?.assignedUser;
2902
+ if (Array.isArray(value)) return value.filter((entry) => entry?.userId);
2903
+ return value?.userId ? [value] : [];
2904
+ }
2905
+ var sameLabel = (entry, label) => (entry.label ?? null) === (label ?? null);
2906
+ function currentAssignees(order, label) {
2907
+ return orderAssignments(order).filter(
2908
+ (entry) => !entry.unassignedAt && (label === void 0 || sameLabel(entry, label))
2909
+ );
2910
+ }
2911
+ function applyAssignment(order, change) {
2912
+ const { userId, at } = change;
2913
+ const label = change.label?.trim() || null;
2914
+ const entries = orderAssignments(order);
2915
+ if (userId === null) {
2916
+ const all = change.label === void 0;
2917
+ return entries.map(
2918
+ (entry) => !entry.unassignedAt && (all || sameLabel(entry, label)) ? { ...entry, unassignedAt: at } : entry
2919
+ );
2920
+ }
2921
+ if (entries.some((entry) => !entry.unassignedAt && entry.userId === userId && sameLabel(entry, label))) {
2922
+ return entries;
2923
+ }
2924
+ return [
2925
+ ...entries.map(
2926
+ (entry) => !entry.unassignedAt && sameLabel(entry, label) ? { ...entry, unassignedAt: at } : entry
2927
+ ),
2928
+ { userId, assignedAt: at, ...label ? { label } : {} }
2929
+ ];
2930
+ }
2931
+
2857
2932
  // order-state-machine/order-status.ts
2858
2933
  var STATUS_KEY = /^[a-z0-9][a-z0-9_-]*$/;
2859
2934
  function validateOrderStatuses(statuses) {
@@ -4127,6 +4202,7 @@ export {
4127
4202
  adyenTaxNumberFormat,
4128
4203
  amountToString,
4129
4204
  ancestorChain,
4205
+ applyAssignment,
4130
4206
  assertUnitSane,
4131
4207
  availableIn,
4132
4208
  baseFor,
@@ -4152,6 +4228,7 @@ export {
4152
4228
  convertFixedFeeToLocalCurrency,
4153
4229
  createAmount,
4154
4230
  crossAxisRuleApplies,
4231
+ currentAssignees,
4155
4232
  deriveDisplayState,
4156
4233
  deriveFamilyCode,
4157
4234
  deriveUnitId,
@@ -4198,6 +4275,7 @@ export {
4198
4275
  isSystemFamilyCode,
4199
4276
  isValidQuantity,
4200
4277
  isWhole,
4278
+ lineStockMoves,
4201
4279
  listCountries,
4202
4280
  listProvinces,
4203
4281
  mapToLegacyStatus,
@@ -4212,6 +4290,7 @@ export {
4212
4290
  normalizePostCode,
4213
4291
  normalizeProvince,
4214
4292
  occupancyIn,
4293
+ orderAssignments,
4215
4294
  parseAmountToMinorUnits,
4216
4295
  parseCurrencyCode,
4217
4296
  parseStripeCurrencyCode,