@final-commerce/common 2.12.2-staging2.1 → 2.13.0-staging2.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.d.mts +76 -3
- package/dist/index.d.ts +76 -3
- package/dist/index.js +171 -40
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +156 -40
- package/dist/index.mjs.map +1 -1
- package/dist/pos-types/index.d.mts +12 -1
- package/dist/pos-types/index.d.ts +12 -1
- package/package.json +1 -1
package/dist/index.mjs
CHANGED
|
@@ -1019,6 +1019,16 @@ function resolveUnit(unitId, companyUnits = []) {
|
|
|
1019
1019
|
if (!unit2) throw new Error(`Unknown measurement unit: ${unitId}`);
|
|
1020
1020
|
return unit2;
|
|
1021
1021
|
}
|
|
1022
|
+
function attachResolvedUnits(variants, companyUnits, logLabel = "attachUnits") {
|
|
1023
|
+
for (const variant of variants)
|
|
1024
|
+
if (variant.unitId)
|
|
1025
|
+
try {
|
|
1026
|
+
variant.unit = resolveUnit(variant.unitId, companyUnits);
|
|
1027
|
+
} catch (error) {
|
|
1028
|
+
console.error(`[${logLabel}] unit ${variant.unitId} does not resolve. ${error.message}`);
|
|
1029
|
+
}
|
|
1030
|
+
return variants;
|
|
1031
|
+
}
|
|
1022
1032
|
function resolveFamily(familyId, companyFamilies = []) {
|
|
1023
1033
|
const family = SYSTEM_FAMILIES.find((f) => f.familyId === familyId) ?? companyFamilies.find((f) => f.familyId === familyId);
|
|
1024
1034
|
if (!family) throw new Error(`Unknown measurement unit family: ${familyId}`);
|
|
@@ -1559,6 +1569,66 @@ function hasSellableChoice(modifier, outletId) {
|
|
|
1559
1569
|
}
|
|
1560
1570
|
var modifiersSellable = (modifiers, outletId) => modifiers.every((modifier) => !modifier.required || hasSellableChoice(modifier, outletId));
|
|
1561
1571
|
|
|
1572
|
+
// utils/line-stock-moves.util.ts
|
|
1573
|
+
function lineStockMoves(item, quantity, reservations = []) {
|
|
1574
|
+
const sign = Math.sign(quantity);
|
|
1575
|
+
const pool = (stockVariantId) => stockVariantId ? { stockVariantId: stockVariantId.toString() } : {};
|
|
1576
|
+
if (!item.components?.length)
|
|
1577
|
+
return [
|
|
1578
|
+
{
|
|
1579
|
+
productId: item.productId.toString(),
|
|
1580
|
+
variantId: item.variantId.toString(),
|
|
1581
|
+
fields: {
|
|
1582
|
+
quantity,
|
|
1583
|
+
...item.stockQuantity != null ? { stockQuantity: sign * item.stockQuantity, unitId: item.unitId?.toString() } : {},
|
|
1584
|
+
...pool(item.stockVariantId)
|
|
1585
|
+
}
|
|
1586
|
+
}
|
|
1587
|
+
];
|
|
1588
|
+
const booked = new Set(
|
|
1589
|
+
reservations.filter((reservation) => reservation.lineItemInternalId && reservation.lineItemInternalId === item.internalId).map((reservation) => reservation.componentIndex)
|
|
1590
|
+
);
|
|
1591
|
+
return shelfComponents(item.components).flatMap(({ component, componentIndex, parentUnits }) => {
|
|
1592
|
+
if (booked.has(componentIndex)) return [];
|
|
1593
|
+
if (component.unitId && component.stockQuantity == null)
|
|
1594
|
+
throw new Error(
|
|
1595
|
+
`Composite ${item.productId?.toString()} component ${component.variantId.toString()} is sold in ${component.unitId.toString()} but carries no stockQuantity`
|
|
1596
|
+
);
|
|
1597
|
+
return [
|
|
1598
|
+
{
|
|
1599
|
+
productId: component.productId.toString(),
|
|
1600
|
+
variantId: component.variantId.toString(),
|
|
1601
|
+
componentIndex,
|
|
1602
|
+
fields: {
|
|
1603
|
+
quantity: component.quantity * parentUnits * quantity,
|
|
1604
|
+
...component.stockQuantity != null ? { stockQuantity: component.stockQuantity * parentUnits * quantity, unitId: component.unitId?.toString() } : {},
|
|
1605
|
+
...pool(component.stockVariantId),
|
|
1606
|
+
...item.productId ? { compositeProductId: item.productId.toString() } : {},
|
|
1607
|
+
...item.name ? { compositeName: item.name } : {}
|
|
1608
|
+
}
|
|
1609
|
+
}
|
|
1610
|
+
];
|
|
1611
|
+
});
|
|
1612
|
+
}
|
|
1613
|
+
function componentUnitsPerLine(components, index) {
|
|
1614
|
+
const component = components[index];
|
|
1615
|
+
if (!component) throw new Error(`No component ${index} on the line`);
|
|
1616
|
+
if (component.parentIndex == null) return component.quantity;
|
|
1617
|
+
if (component.parentIndex >= index)
|
|
1618
|
+
throw new Error(`Component ${index} points to parent ${component.parentIndex}, which is not before it`);
|
|
1619
|
+
return component.quantity * componentUnitsPerLine(components, component.parentIndex);
|
|
1620
|
+
}
|
|
1621
|
+
var isInnerComposite = (components, index) => components.some((child) => child.parentIndex === index);
|
|
1622
|
+
var shelfComponents = (components) => components.flatMap(
|
|
1623
|
+
(component, componentIndex) => isInnerComposite(components, componentIndex) ? [] : [
|
|
1624
|
+
{
|
|
1625
|
+
component,
|
|
1626
|
+
componentIndex,
|
|
1627
|
+
parentUnits: component.parentIndex == null ? 1 : componentUnitsPerLine(components, component.parentIndex)
|
|
1628
|
+
}
|
|
1629
|
+
]
|
|
1630
|
+
);
|
|
1631
|
+
|
|
1562
1632
|
// utils/composite-availability.util.ts
|
|
1563
1633
|
function poolStockAt(rows, outletId) {
|
|
1564
1634
|
if (!rows.some((row) => row.manageStock === true)) return null;
|
|
@@ -1697,7 +1767,9 @@ function poolDemand(lines) {
|
|
|
1697
1767
|
(demand.get(poolOf(line)) ?? 0) + (line.unit ? toBase(line.quantity * times, line.unit) : line.quantity * times)
|
|
1698
1768
|
);
|
|
1699
1769
|
for (const line of lines) {
|
|
1700
|
-
if (line.components)
|
|
1770
|
+
if (line.components)
|
|
1771
|
+
for (const { component, parentUnits } of shelfComponents(line.components))
|
|
1772
|
+
add(component, line.quantity * parentUnits);
|
|
1701
1773
|
else add(line, 1);
|
|
1702
1774
|
}
|
|
1703
1775
|
return demand;
|
|
@@ -1739,47 +1811,76 @@ function compositeReservationMismatches(order) {
|
|
|
1739
1811
|
});
|
|
1740
1812
|
}
|
|
1741
1813
|
|
|
1742
|
-
// utils/
|
|
1743
|
-
function
|
|
1744
|
-
|
|
1745
|
-
|
|
1746
|
-
|
|
1747
|
-
|
|
1748
|
-
|
|
1749
|
-
|
|
1750
|
-
|
|
1751
|
-
|
|
1752
|
-
|
|
1753
|
-
|
|
1754
|
-
|
|
1755
|
-
|
|
1756
|
-
|
|
1757
|
-
|
|
1758
|
-
|
|
1759
|
-
|
|
1760
|
-
);
|
|
1761
|
-
|
|
1762
|
-
|
|
1763
|
-
|
|
1764
|
-
|
|
1765
|
-
|
|
1766
|
-
|
|
1767
|
-
|
|
1768
|
-
|
|
1769
|
-
|
|
1770
|
-
|
|
1771
|
-
|
|
1772
|
-
|
|
1773
|
-
|
|
1774
|
-
|
|
1775
|
-
|
|
1776
|
-
|
|
1777
|
-
|
|
1778
|
-
|
|
1779
|
-
|
|
1780
|
-
];
|
|
1814
|
+
// utils/variant-cost.util.ts
|
|
1815
|
+
function variantCostAtSale({
|
|
1816
|
+
costPrice,
|
|
1817
|
+
costPriceUpdatedAt,
|
|
1818
|
+
landedCost,
|
|
1819
|
+
lastReceivedAt
|
|
1820
|
+
}) {
|
|
1821
|
+
if (landedCost == null) return costPrice ?? null;
|
|
1822
|
+
if (costPrice == null) return landedCost;
|
|
1823
|
+
if (!costPriceUpdatedAt || !lastReceivedAt) return costPrice;
|
|
1824
|
+
return new Date(lastReceivedAt).getTime() > new Date(costPriceUpdatedAt).getTime() ? landedCost : costPrice;
|
|
1825
|
+
}
|
|
1826
|
+
function compositeCostOfKnown(items) {
|
|
1827
|
+
const known = items.filter((item) => item.cost !== null);
|
|
1828
|
+
if (!known.length) return null;
|
|
1829
|
+
return known.reduce((sum, { cost, quantity }) => sum + extendPrice(cost, quantity), 0);
|
|
1830
|
+
}
|
|
1831
|
+
function compositeCostRange(parts) {
|
|
1832
|
+
const missing = new Set(parts.flatMap((part) => part.options.flatMap((option) => option.missing)));
|
|
1833
|
+
const [low, high] = [pickedEnd(parts, "low"), pickedEnd(parts, "high")];
|
|
1834
|
+
const [lowCost, highCost] = [low.length ? compositeCostOfKnown(low) : 0, compositeCostOfKnown(high)];
|
|
1835
|
+
return { range: lowCost === null || highCost === null ? null : [lowCost, highCost], missing: [...missing] };
|
|
1836
|
+
}
|
|
1837
|
+
function innerCompositeOption(name, quantity, cost) {
|
|
1838
|
+
const [low, high] = cost.range ?? [null, null];
|
|
1839
|
+
return { name, quantity, low, high, missing: cost.range === null && !cost.missing.length ? [name] : cost.missing };
|
|
1840
|
+
}
|
|
1841
|
+
function variantLabel(variant, variantCount) {
|
|
1842
|
+
if (variantCount === 1) return "";
|
|
1843
|
+
const attributes = (variant.attributes ?? []).map((attribute) => attribute.value ?? attribute.values?.[0] ?? "");
|
|
1844
|
+
return attributes.filter(Boolean).join(" / ") || variant.name || variant.sku || "";
|
|
1845
|
+
}
|
|
1846
|
+
function variantDisplayName(productName, variant, variantCount) {
|
|
1847
|
+
const label = variantLabel(variant, variantCount);
|
|
1848
|
+
return label ? `${productName} \xB7 ${label}` : productName;
|
|
1849
|
+
}
|
|
1850
|
+
function pickedEnd(parts, end) {
|
|
1851
|
+
return parts.flatMap((part) => {
|
|
1852
|
+
if (!part.options.length) return [];
|
|
1853
|
+
const byCost = part.options.flatMap(({ quantity, [end]: cost }) => cost === null ? [] : [{ cost, quantity }]).sort((a, b) => extendPrice(a.cost, a.quantity) - extendPrice(b.cost, b.quantity));
|
|
1854
|
+
const pick = (end === "low" ? byCost[0] : byCost[byCost.length - 1]) ?? { cost: null, quantity: 1 };
|
|
1855
|
+
return Array(end === "low" ? part.minPicks : part.maxPicks).fill(pick);
|
|
1781
1856
|
});
|
|
1782
1857
|
}
|
|
1858
|
+
function costPriceChangeStamp(next, stored) {
|
|
1859
|
+
if (next === void 0 || next === (stored ?? null)) return {};
|
|
1860
|
+
return { costPriceUpdatedAt: /* @__PURE__ */ new Date() };
|
|
1861
|
+
}
|
|
1862
|
+
function bankersRoundDiv(numerator, denominator) {
|
|
1863
|
+
if (denominator === 0n) return 0n;
|
|
1864
|
+
const negative = numerator < 0n !== denominator < 0n;
|
|
1865
|
+
const n = numerator < 0n ? -numerator : numerator;
|
|
1866
|
+
const d = denominator < 0n ? -denominator : denominator;
|
|
1867
|
+
const quotient = n / d;
|
|
1868
|
+
const twiceRemainder = n % d * 2n;
|
|
1869
|
+
let result;
|
|
1870
|
+
if (twiceRemainder < d) {
|
|
1871
|
+
result = quotient;
|
|
1872
|
+
} else if (twiceRemainder > d) {
|
|
1873
|
+
result = quotient + 1n;
|
|
1874
|
+
} else {
|
|
1875
|
+
result = quotient % 2n === 0n ? quotient : quotient + 1n;
|
|
1876
|
+
}
|
|
1877
|
+
return negative ? -result : result;
|
|
1878
|
+
}
|
|
1879
|
+
function landedCostPerUnit(landedValue, baseQuantity, ratioToBase = 1) {
|
|
1880
|
+
return Number(bankersRoundDiv(BigInt(Math.round(landedValue * ratioToBase)), BigInt(baseQuantity)));
|
|
1881
|
+
}
|
|
1882
|
+
var bookCostPerUnit = (book, ratioToBase) => book && book.totalQuantity > 0 && ratioToBase ? landedCostPerUnit(book.totalLandedValue, book.totalQuantity, ratioToBase) : null;
|
|
1883
|
+
var marginPercentOf = (grossProfit, costedGross) => grossProfit === null || !costedGross ? null : grossProfit * 100 / costedGross;
|
|
1783
1884
|
|
|
1784
1885
|
// units/family-code.ts
|
|
1785
1886
|
var MAX_SLUG_LENGTH = 32;
|
|
@@ -4256,11 +4357,14 @@ export {
|
|
|
4256
4357
|
ancestorChain,
|
|
4257
4358
|
applyAssignment,
|
|
4258
4359
|
assertUnitSane,
|
|
4360
|
+
attachResolvedUnits,
|
|
4259
4361
|
availableIn,
|
|
4362
|
+
bankersRoundDiv,
|
|
4260
4363
|
baseFor,
|
|
4261
4364
|
baseFulfillmentDefinitions,
|
|
4262
4365
|
baseOf,
|
|
4263
4366
|
basePaymentDefinitions,
|
|
4367
|
+
bookCostPerUnit,
|
|
4264
4368
|
buildAvailability,
|
|
4265
4369
|
buildGrid,
|
|
4266
4370
|
buildOrderContext,
|
|
@@ -4270,6 +4374,9 @@ export {
|
|
|
4270
4374
|
canTransition,
|
|
4271
4375
|
cheapestCompositeItem,
|
|
4272
4376
|
cheapestCompositePrice,
|
|
4377
|
+
componentUnitsPerLine,
|
|
4378
|
+
compositeCostOfKnown,
|
|
4379
|
+
compositeCostRange,
|
|
4273
4380
|
compositeItemCost,
|
|
4274
4381
|
compositeItemUnavailable,
|
|
4275
4382
|
compositeMakeable,
|
|
@@ -4278,6 +4385,7 @@ export {
|
|
|
4278
4385
|
compositeVariantInStock,
|
|
4279
4386
|
conditionSummary,
|
|
4280
4387
|
convertFixedFeeToLocalCurrency,
|
|
4388
|
+
costPriceChangeStamp,
|
|
4281
4389
|
createAmount,
|
|
4282
4390
|
crossAxisRuleApplies,
|
|
4283
4391
|
currentAssignees,
|
|
@@ -4315,6 +4423,7 @@ export {
|
|
|
4315
4423
|
hasSellableChoice,
|
|
4316
4424
|
holdExpiresAt,
|
|
4317
4425
|
inferStatesFromLegacyStatus,
|
|
4426
|
+
innerCompositeOption,
|
|
4318
4427
|
isAdyenRolloutCountry,
|
|
4319
4428
|
isAffirmCountry,
|
|
4320
4429
|
isAffirmPricePlan,
|
|
@@ -4322,16 +4431,19 @@ export {
|
|
|
4322
4431
|
isConditionOperator,
|
|
4323
4432
|
isFulfillmentState,
|
|
4324
4433
|
isInCart,
|
|
4434
|
+
isInnerComposite,
|
|
4325
4435
|
isPaymentState,
|
|
4326
4436
|
isSupportedCurrency,
|
|
4327
4437
|
isSystemFamilyCode,
|
|
4328
4438
|
isValidQuantity,
|
|
4329
4439
|
isWhole,
|
|
4440
|
+
landedCostPerUnit,
|
|
4330
4441
|
lineStockMoves,
|
|
4331
4442
|
listCountries,
|
|
4332
4443
|
listProvinces,
|
|
4333
4444
|
listShippingCountries,
|
|
4334
4445
|
mapToLegacyStatus,
|
|
4446
|
+
marginPercentOf,
|
|
4335
4447
|
maxPrecisionFor,
|
|
4336
4448
|
mergeFragment,
|
|
4337
4449
|
modifiersSellable,
|
|
@@ -4366,6 +4478,7 @@ export {
|
|
|
4366
4478
|
roundToPrecision,
|
|
4367
4479
|
sameFamily,
|
|
4368
4480
|
setOrderState,
|
|
4481
|
+
shelfComponents,
|
|
4369
4482
|
shipsToCountry,
|
|
4370
4483
|
shopDayKey,
|
|
4371
4484
|
shopDayRange,
|
|
@@ -4379,6 +4492,9 @@ export {
|
|
|
4379
4492
|
validateFragmentCompatibility,
|
|
4380
4493
|
validateOrderStatuses,
|
|
4381
4494
|
validateShippingZone,
|
|
4495
|
+
variantCostAtSale,
|
|
4496
|
+
variantDisplayName,
|
|
4497
|
+
variantLabel,
|
|
4382
4498
|
violatesFinancialInvariant,
|
|
4383
4499
|
windowEndsAt,
|
|
4384
4500
|
withinPresence
|