@labdigital/commercetools-mock 2.62.0 → 2.63.0

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
@@ -551,18 +551,15 @@ const roundDecimal = (decimal, roundingMode) => {
551
551
  default: throw new Error(`Unknown rounding mode: ${roundingMode}`);
552
552
  }
553
553
  };
554
- const createCentPrecisionMoney = (value) => {
555
- let fractionDigits = 2;
556
- switch (value.currencyCode.toUpperCase()) {
554
+ const getCurrencyFractionDigits = (currencyCode) => {
555
+ switch (currencyCode.toUpperCase()) {
557
556
  case "BHD":
558
557
  case "IQD":
559
558
  case "JOD":
560
559
  case "KWD":
561
560
  case "LYD":
562
561
  case "OMR":
563
- case "TND":
564
- fractionDigits = 3;
565
- break;
562
+ case "TND": return 3;
566
563
  case "CVE":
567
564
  case "DJF":
568
565
  case "GNF":
@@ -577,22 +574,49 @@ const createCentPrecisionMoney = (value) => {
577
574
  case "VUV":
578
575
  case "XAF":
579
576
  case "XOF":
580
- case "XPF":
581
- fractionDigits = 0;
582
- break;
583
- default: fractionDigits = 2;
577
+ case "XPF": return 0;
578
+ default: return 2;
584
579
  }
585
- if (value.preciseAmount) throw new Error("HighPrecisionMoney not supported");
580
+ };
581
+ const calculateCentAmountFromPreciseAmount = (preciseAmount, fractionDigits, currencyCode, roundingMode = "HalfEven") => {
582
+ const diff = fractionDigits - getCurrencyFractionDigits(currencyCode);
583
+ const scale = new Decimal(10).pow(Math.abs(diff));
584
+ return roundDecimal(diff >= 0 ? new Decimal(preciseAmount).div(scale) : new Decimal(preciseAmount).mul(scale), roundingMode).toNumber();
585
+ };
586
+ const createCentPrecisionMoney = (value) => {
587
+ const fractionDigits = getCurrencyFractionDigits(value.currencyCode);
588
+ const preciseValue = value;
589
+ let centAmount;
590
+ centAmount = value.centAmount ?? 0;
591
+ if (preciseValue.preciseAmount !== void 0 && preciseValue.fractionDigits !== void 0) centAmount = calculateCentAmountFromPreciseAmount(preciseValue.preciseAmount, preciseValue.fractionDigits, value.currencyCode, "HalfEven");
586
592
  return {
587
593
  type: "centPrecision",
588
- centAmount: value.centAmount ?? 0,
594
+ centAmount,
589
595
  currencyCode: value.currencyCode,
590
596
  fractionDigits
591
597
  };
592
598
  };
599
+ const createHighPrecisionMoney = (value) => {
600
+ if (value.preciseAmount === void 0) throw new Error("HighPrecisionMoney requires preciseAmount");
601
+ if (value.fractionDigits === void 0) throw new Error("HighPrecisionMoney requires fractionDigits");
602
+ return {
603
+ type: "highPrecision",
604
+ centAmount: value.centAmount ?? calculateCentAmountFromPreciseAmount(value.preciseAmount, value.fractionDigits, value.currencyCode, "HalfEven"),
605
+ currencyCode: value.currencyCode,
606
+ fractionDigits: value.fractionDigits,
607
+ preciseAmount: value.preciseAmount
608
+ };
609
+ };
593
610
  const createTypedMoney = (value) => {
611
+ const preciseValue = value;
612
+ if ("type" in value && value.type === "highPrecision" || preciseValue.preciseAmount !== void 0) return createHighPrecisionMoney(value);
594
613
  return createCentPrecisionMoney(value);
595
614
  };
615
+ const calculateMoneyTotalCentAmount = (money, quantity, roundingMode = "HalfEven") => {
616
+ const preciseValue = money;
617
+ if (preciseValue.preciseAmount === void 0 || preciseValue.fractionDigits === void 0) return (money.centAmount ?? 0) * quantity;
618
+ return calculateCentAmountFromPreciseAmount(new Decimal(preciseValue.preciseAmount).mul(quantity).toNumber(), preciseValue.fractionDigits, money.currencyCode, roundingMode);
619
+ };
596
620
  const resolveStoreReference = (ref, projectKey, storage) => {
597
621
  if (!ref) return void 0;
598
622
  const resource = storage.getByResourceIdentifier(projectKey, ref);
@@ -666,11 +690,11 @@ const getBusinessUnitKeyReference = (id, projectKey, storage) => {
666
690
  typeId: "business-unit",
667
691
  key: id.key
668
692
  };
669
- const value = getReferenceFromResourceIdentifier(id, projectKey, storage);
670
- if (!value.obj?.key) throw new Error("No business-unit found for reference");
693
+ const resource = storage.getByResourceIdentifier(projectKey, id);
694
+ if (!resource?.key) throw new Error("No business-unit found for reference");
671
695
  return {
672
696
  typeId: "business-unit",
673
- key: value.obj?.key
697
+ key: resource.key
674
698
  };
675
699
  };
676
700
 
@@ -911,7 +935,10 @@ const selectPrice = ({ prices, currency, country }) => {
911
935
  return countryMatch && currencyMatch;
912
936
  });
913
937
  };
914
- const calculateLineItemTotalPrice = (lineItem) => lineItem.price?.value.centAmount * lineItem.quantity;
938
+ const calculateLineItemTotalPrice = (lineItem) => {
939
+ if (!lineItem.price?.value) return 0;
940
+ return calculateMoneyTotalCentAmount(lineItem.price.value, lineItem.quantity);
941
+ };
915
942
  const calculateCartTotalPrice = (cart) => {
916
943
  return cart.lineItems.reduce((cur, item) => cur + item.totalPrice.centAmount, 0) + cart.customLineItems.reduce((cur, item) => cur + item.totalPrice.centAmount, 0);
917
944
  };
@@ -923,8 +950,8 @@ const createCustomLineItemFromDraft = (projectKey, draft, storage, country) => {
923
950
  taxCategory = storage.get(projectKey, "tax-category", taxCategoryRef.id, {}) || void 0;
924
951
  } catch (_error) {}
925
952
  const totalPrice = createCentPrecisionMoney({
926
- ...draft.money,
927
- centAmount: (draft.money.centAmount ?? 0) * quantity
953
+ currencyCode: draft.money.currencyCode,
954
+ centAmount: calculateMoneyTotalCentAmount(draft.money, quantity)
928
955
  });
929
956
  const taxedPrice = taxCategory ? calculateTaxedPrice(totalPrice.centAmount, taxCategory, totalPrice.currencyCode, country) : void 0;
930
957
  const taxRate = taxCategory ? taxCategory.rates.find((rate) => !rate.country || rate.country === country) : void 0;
@@ -947,6 +974,22 @@ const createCustomLineItemFromDraft = (projectKey, draft, storage, country) => {
947
974
  taxedPricePortions: []
948
975
  };
949
976
  };
977
+ const createDiscountCodeInfoFromCode = (projectKey, storage, code) => {
978
+ const discountCodes = storage.query(projectKey, "discount-code", { where: `code="${code}"` });
979
+ if (discountCodes.count === 0) throw new CommercetoolsError({
980
+ code: "DiscountCodeNonApplicable",
981
+ message: `The discount code '${code}' was not found.`,
982
+ reason: "DoesNotExist",
983
+ discountCode: "nonexistent"
984
+ });
985
+ return {
986
+ discountCode: {
987
+ typeId: "discount-code",
988
+ id: discountCodes.results[0].id
989
+ },
990
+ state: "MatchesCart"
991
+ };
992
+ };
950
993
 
951
994
  //#endregion
952
995
  //#region src/repositories/cart/actions.ts
@@ -998,6 +1041,10 @@ var CartUpdateHandler = class extends AbstractUpdateHandler {
998
1041
  country: resource.country
999
1042
  });
1000
1043
  if (!price) throw new Error(`No valid price found for ${productId} for country ${resource.country} and currency ${currency}`);
1044
+ const totalPrice = createCentPrecisionMoney({
1045
+ currencyCode: price.value.currencyCode,
1046
+ centAmount: calculateMoneyTotalCentAmount(price.value, quantity)
1047
+ });
1001
1048
  resource.lineItems.push({
1002
1049
  id: v4(),
1003
1050
  key,
@@ -1011,11 +1058,7 @@ var CartUpdateHandler = class extends AbstractUpdateHandler {
1011
1058
  price,
1012
1059
  taxedPricePortions: [],
1013
1060
  perMethodTaxRate: [],
1014
- totalPrice: {
1015
- ...price.value,
1016
- type: "centPrecision",
1017
- centAmount: price.value.centAmount * quantity
1018
- },
1061
+ totalPrice,
1019
1062
  quantity,
1020
1063
  discountedPricePerQuantity: [],
1021
1064
  lineItemMode: "Standard",
@@ -1057,6 +1100,10 @@ var CartUpdateHandler = class extends AbstractUpdateHandler {
1057
1100
  resource.taxRoundingMode = taxRoundingMode;
1058
1101
  }
1059
1102
  recalculate() {}
1103
+ addDiscountCode(context, resource, { code }) {
1104
+ const info = createDiscountCodeInfoFromCode(context.projectKey, this._storage, code);
1105
+ if (!resource.discountCodes.map((dc) => dc.discountCode.id).includes(info.discountCode.id)) resource.discountCodes.push(info);
1106
+ }
1060
1107
  removeDiscountCode(context, resource, { discountCode }) {
1061
1108
  resource.discountCodes = resource.discountCodes.filter((code) => code.discountCode.id !== discountCode.id);
1062
1109
  }
@@ -1126,8 +1173,8 @@ var CartUpdateHandler = class extends AbstractUpdateHandler {
1126
1173
  });
1127
1174
  customLineItem$1.quantity = quantity;
1128
1175
  customLineItem$1.totalPrice = createCentPrecisionMoney({
1129
- ...customLineItem$1.money,
1130
- centAmount: (customLineItem$1.money.centAmount ?? 0) * quantity
1176
+ currencyCode: customLineItem$1.money.currencyCode,
1177
+ centAmount: calculateMoneyTotalCentAmount(customLineItem$1.money, quantity)
1131
1178
  });
1132
1179
  };
1133
1180
  if (customLineItemId) {
@@ -1149,8 +1196,8 @@ var CartUpdateHandler = class extends AbstractUpdateHandler {
1149
1196
  });
1150
1197
  customLineItem$1.money = createTypedMoney(money);
1151
1198
  customLineItem$1.totalPrice = createCentPrecisionMoney({
1152
- ...money,
1153
- centAmount: (money.centAmount ?? 0) * customLineItem$1.quantity
1199
+ currencyCode: money.currencyCode,
1200
+ centAmount: calculateMoneyTotalCentAmount(money, customLineItem$1.quantity)
1154
1201
  });
1155
1202
  };
1156
1203
  if (customLineItemId) {
@@ -1207,7 +1254,7 @@ var CartUpdateHandler = class extends AbstractUpdateHandler {
1207
1254
  shippingMethodName,
1208
1255
  price: createCentPrecisionMoney(shippingRate.price),
1209
1256
  shippingRate: {
1210
- price: createTypedMoney(shippingRate.price),
1257
+ price: createCentPrecisionMoney(shippingRate.price),
1211
1258
  tiers: []
1212
1259
  },
1213
1260
  taxCategory: tax ? {
@@ -1293,7 +1340,7 @@ var CartUpdateHandler = class extends AbstractUpdateHandler {
1293
1340
  }
1294
1341
  const lineItemTotal = calculateLineItemTotalPrice(lineItem);
1295
1342
  lineItem.totalPrice = createCentPrecisionMoney({
1296
- ...lineItem.price.value,
1343
+ currencyCode: lineItem.price.value.currencyCode,
1297
1344
  centAmount: lineItemTotal
1298
1345
  });
1299
1346
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
@@ -1384,6 +1431,10 @@ var CartRepository = class extends AbstractResourceRepository {
1384
1431
  });
1385
1432
  const lineItems = draft.lineItems?.map((draftLineItem) => this.draftLineItemtoLineItem(context.projectKey, draftLineItem, draft.currency, draft.country)) ?? [];
1386
1433
  const customLineItems = draft.customLineItems?.map((draftCustomLineItem) => createCustomLineItemFromDraft(context.projectKey, draftCustomLineItem, this._storage, draft.shippingAddress?.country ?? draft.country)) ?? [];
1434
+ const discountCodeInfo = [];
1435
+ if (draft.discountCodes?.length) draft.discountCodes.forEach((code) => {
1436
+ discountCodeInfo.push(createDiscountCodeInfoFromCode(context.projectKey, this._storage, code));
1437
+ });
1387
1438
  const resource = {
1388
1439
  ...getBaseResourceProperties(),
1389
1440
  anonymousId: draft.anonymousId,
@@ -1398,7 +1449,7 @@ var CartRepository = class extends AbstractResourceRepository {
1398
1449
  customerEmail: draft.customerEmail,
1399
1450
  customLineItems,
1400
1451
  directDiscounts: [],
1401
- discountCodes: [],
1452
+ discountCodes: discountCodeInfo,
1402
1453
  inventoryMode: "None",
1403
1454
  itemShippingAddresses: [],
1404
1455
  lineItems,
@@ -1464,6 +1515,10 @@ var CartRepository = class extends AbstractResourceRepository {
1464
1515
  country
1465
1516
  });
1466
1517
  if (!price) throw new Error(`No valid price found for ${productId} for country ${country} and currency ${currency}`);
1518
+ const totalPrice = createCentPrecisionMoney({
1519
+ currencyCode: price.value.currencyCode,
1520
+ centAmount: calculateMoneyTotalCentAmount(price.value, quant)
1521
+ });
1467
1522
  return {
1468
1523
  id: v4(),
1469
1524
  productId: product.id,
@@ -1473,12 +1528,7 @@ var CartRepository = class extends AbstractResourceRepository {
1473
1528
  name: product.masterData.current.name,
1474
1529
  variant,
1475
1530
  price,
1476
- totalPrice: {
1477
- type: "centPrecision",
1478
- currencyCode: price.value.currencyCode,
1479
- fractionDigits: price.value.fractionDigits,
1480
- centAmount: price.value.centAmount * quant
1481
- },
1531
+ totalPrice,
1482
1532
  taxedPricePortions: [],
1483
1533
  perMethodTaxRate: [],
1484
1534
  quantity: quant,
@@ -1799,8 +1849,8 @@ var OrderRepository = class extends AbstractResourceRepository {
1799
1849
  } else throw new Error("No product found");
1800
1850
  const quantity = draft.quantity ?? 1;
1801
1851
  const totalPrice = createCentPrecisionMoney({
1802
- ...draft.price.value,
1803
- centAmount: (draft.price.value.centAmount ?? 0) * quantity
1852
+ currencyCode: draft.price.value.currencyCode,
1853
+ centAmount: calculateMoneyTotalCentAmount(draft.price.value, quantity)
1804
1854
  });
1805
1855
  return {
1806
1856
  ...getBaseResourceProperties(),
@@ -1830,8 +1880,8 @@ var OrderRepository = class extends AbstractResourceRepository {
1830
1880
  customLineItemFromImportDraft(context, draft) {
1831
1881
  const quantity = draft.quantity ?? 1;
1832
1882
  const totalPrice = createCentPrecisionMoney({
1833
- ...draft.money,
1834
- centAmount: (draft.money.centAmount ?? 0) * quantity
1883
+ currencyCode: draft.money.currencyCode,
1884
+ centAmount: calculateMoneyTotalCentAmount(draft.money, quantity)
1835
1885
  });
1836
1886
  return {
1837
1887
  ...getBaseResourceProperties(),
@@ -6308,8 +6358,8 @@ var ReviewUpdateHandler = class extends AbstractUpdateHandler {
6308
6358
  //#endregion
6309
6359
  //#region src/repositories/shipping-method/helpers.ts
6310
6360
  const transformShippingRate = (rate) => ({
6311
- price: createTypedMoney(rate.price),
6312
- freeAbove: rate.freeAbove && createTypedMoney(rate.freeAbove),
6361
+ price: createCentPrecisionMoney(rate.price),
6362
+ freeAbove: rate.freeAbove && createCentPrecisionMoney(rate.freeAbove),
6313
6363
  tiers: rate.tiers || []
6314
6364
  });
6315
6365