@labdigital/commercetools-mock 2.55.0 → 2.56.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.js CHANGED
@@ -760,7 +760,88 @@ const selectPrice = ({ prices, currency, country }) => {
760
760
  });
761
761
  };
762
762
  const calculateLineItemTotalPrice = (lineItem) => lineItem.price?.value.centAmount * lineItem.quantity;
763
- const calculateCartTotalPrice = (cart) => cart.lineItems.reduce((cur, item) => cur + item.totalPrice.centAmount, 0);
763
+ const calculateCartTotalPrice = (cart) => {
764
+ const lineItemsTotal = cart.lineItems.reduce((cur, item) => cur + item.totalPrice.centAmount, 0);
765
+ const customLineItemsTotal = cart.customLineItems.reduce((cur, item) => cur + item.totalPrice.centAmount, 0);
766
+ return lineItemsTotal + customLineItemsTotal;
767
+ };
768
+ const calculateTaxedPrice = (amount, taxCategory, currency, country) => {
769
+ if (!taxCategory || !taxCategory.rates.length) return void 0;
770
+ const taxRate = taxCategory.rates.find((rate) => !rate.country || rate.country === country) || taxCategory.rates[0];
771
+ if (!taxRate) return void 0;
772
+ let netAmount;
773
+ let grossAmount;
774
+ let taxAmount;
775
+ if (taxRate.includedInPrice) {
776
+ grossAmount = amount;
777
+ taxAmount = Math.round(grossAmount * taxRate.amount / (1 + taxRate.amount));
778
+ netAmount = grossAmount - taxAmount;
779
+ } else {
780
+ netAmount = amount;
781
+ taxAmount = Math.round(netAmount * taxRate.amount);
782
+ grossAmount = netAmount + taxAmount;
783
+ }
784
+ return {
785
+ totalNet: {
786
+ type: "centPrecision",
787
+ currencyCode: currency,
788
+ centAmount: netAmount,
789
+ fractionDigits: 2
790
+ },
791
+ totalGross: {
792
+ type: "centPrecision",
793
+ currencyCode: currency,
794
+ centAmount: grossAmount,
795
+ fractionDigits: 2
796
+ },
797
+ taxPortions: taxAmount > 0 ? [{
798
+ rate: taxRate.amount,
799
+ amount: {
800
+ type: "centPrecision",
801
+ currencyCode: currency,
802
+ centAmount: taxAmount,
803
+ fractionDigits: 2
804
+ },
805
+ name: taxRate.name
806
+ }] : [],
807
+ totalTax: taxAmount > 0 ? {
808
+ type: "centPrecision",
809
+ currencyCode: currency,
810
+ centAmount: taxAmount,
811
+ fractionDigits: 2
812
+ } : void 0
813
+ };
814
+ };
815
+ const createCustomLineItemFromDraft = (projectKey, draft, storage, country) => {
816
+ const quantity = draft.quantity ?? 1;
817
+ const taxCategoryRef = draft.taxCategory ? getReferenceFromResourceIdentifier(draft.taxCategory, projectKey, storage) : void 0;
818
+ let taxCategory = void 0;
819
+ if (taxCategoryRef) try {
820
+ taxCategory = storage.get(projectKey, "tax-category", taxCategoryRef.id, {}) || void 0;
821
+ } catch (error) {}
822
+ const totalPrice = createCentPrecisionMoney({
823
+ ...draft.money,
824
+ centAmount: (draft.money.centAmount ?? 0) * quantity
825
+ });
826
+ const taxedPrice = taxCategory ? calculateTaxedPrice(totalPrice.centAmount, taxCategory, totalPrice.currencyCode, country) : void 0;
827
+ return {
828
+ id: v4(),
829
+ key: draft.key,
830
+ name: draft.name,
831
+ money: createTypedMoney(draft.money),
832
+ slug: draft.slug,
833
+ quantity: draft.quantity ?? 1,
834
+ state: [],
835
+ taxCategory: taxCategoryRef,
836
+ taxedPrice,
837
+ custom: createCustomFields(draft.custom, projectKey, storage),
838
+ discountedPricePerQuantity: [],
839
+ perMethodTaxRate: [],
840
+ priceMode: draft.priceMode ?? "Standard",
841
+ totalPrice,
842
+ taxedPricePortions: []
843
+ };
844
+ };
764
845
 
765
846
  //#endregion
766
847
  //#region src/repositories/cart/actions.ts
@@ -886,6 +967,94 @@ var CartUpdateHandler = class extends AbstractUpdateHandler {
886
967
  });
887
968
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
888
969
  }
970
+ addCustomLineItem(context, resource, { money, name, slug, quantity = 1, taxCategory, custom, priceMode = "Standard", key }) {
971
+ const customLineItem = createCustomLineItemFromDraft(context.projectKey, {
972
+ money,
973
+ name,
974
+ slug,
975
+ quantity,
976
+ taxCategory,
977
+ custom,
978
+ priceMode,
979
+ key
980
+ }, this._storage, resource.country);
981
+ resource.customLineItems.push(customLineItem);
982
+ resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
983
+ }
984
+ removeCustomLineItem(context, resource, { customLineItemId, customLineItemKey }) {
985
+ let customLineItem;
986
+ if (!customLineItemId && !customLineItemKey) throw new CommercetoolsError({
987
+ code: "General",
988
+ message: "Either customLineItemId or customLineItemKey needs to be provided."
989
+ });
990
+ if (customLineItemId) {
991
+ customLineItem = resource.customLineItems.find((x) => x.id === customLineItemId);
992
+ if (!customLineItem) throw new CommercetoolsError({
993
+ code: "General",
994
+ message: `A custom line item with ID '${customLineItemId}' not found.`
995
+ });
996
+ resource.customLineItems = resource.customLineItems.filter((x) => x.id !== customLineItemId);
997
+ }
998
+ if (customLineItemKey) {
999
+ customLineItem = resource.customLineItems.find((x) => x.key === customLineItemKey);
1000
+ if (!customLineItem) throw new CommercetoolsError({
1001
+ code: "General",
1002
+ message: `A custom line item with key '${customLineItemKey}' not found.`
1003
+ });
1004
+ resource.customLineItems = resource.customLineItems.filter((x) => x.key !== customLineItemKey);
1005
+ }
1006
+ resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1007
+ }
1008
+ changeCustomLineItemQuantity(context, resource, { customLineItemId, customLineItemKey, quantity }) {
1009
+ let customLineItem;
1010
+ if (!customLineItemId && !customLineItemKey) throw new CommercetoolsError({
1011
+ code: "General",
1012
+ message: "Either customLineItemId or customLineItemKey needs to be provided."
1013
+ });
1014
+ const setQuantity = (customLineItem$1) => {
1015
+ if (!customLineItem$1) throw new CommercetoolsError({
1016
+ code: "General",
1017
+ message: `A custom line item with ${customLineItemId ? `ID '${customLineItemId}'` : `key '${customLineItemKey}'`} not found.`
1018
+ });
1019
+ customLineItem$1.quantity = quantity;
1020
+ customLineItem$1.totalPrice = createCentPrecisionMoney({
1021
+ ...customLineItem$1.money,
1022
+ centAmount: (customLineItem$1.money.centAmount ?? 0) * quantity
1023
+ });
1024
+ };
1025
+ if (customLineItemId) {
1026
+ customLineItem = resource.customLineItems.find((x) => x.id === customLineItemId);
1027
+ setQuantity(customLineItem);
1028
+ }
1029
+ if (customLineItemKey) {
1030
+ customLineItem = resource.customLineItems.find((x) => x.key === customLineItemKey);
1031
+ setQuantity(customLineItem);
1032
+ }
1033
+ resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1034
+ }
1035
+ changeCustomLineItemMoney(context, resource, { customLineItemId, customLineItemKey, money }) {
1036
+ let customLineItem;
1037
+ const setMoney = (customLineItem$1) => {
1038
+ if (!customLineItem$1) throw new CommercetoolsError({
1039
+ code: "General",
1040
+ message: `A custom line item with ${customLineItemId ? `ID '${customLineItemId}'` : `key '${customLineItemKey}'`} not found.`
1041
+ });
1042
+ customLineItem$1.money = createTypedMoney(money);
1043
+ customLineItem$1.totalPrice = createCentPrecisionMoney({
1044
+ ...money,
1045
+ centAmount: (money.centAmount ?? 0) * customLineItem$1.quantity
1046
+ });
1047
+ };
1048
+ if (customLineItemId) {
1049
+ customLineItem = resource.customLineItems.find((x) => x.id === customLineItemId);
1050
+ setMoney(customLineItem);
1051
+ }
1052
+ if (customLineItemKey) {
1053
+ customLineItem = resource.customLineItems.find((x) => x.key === customLineItemKey);
1054
+ setMoney(customLineItem);
1055
+ }
1056
+ resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1057
+ }
889
1058
  setAnonymousId(_context, resource, { anonymousId }) {
890
1059
  resource.anonymousId = anonymousId;
891
1060
  resource.customerId = void 0;
@@ -1110,6 +1279,7 @@ var CartRepository = class extends AbstractResourceRepository {
1110
1279
  key: draft.businessUnit.key
1111
1280
  });
1112
1281
  const lineItems = draft.lineItems?.map((draftLineItem) => this.draftLineItemtoLineItem(context.projectKey, draftLineItem, draft.currency, draft.country)) ?? [];
1282
+ const customLineItems = draft.customLineItems?.map((draftCustomLineItem) => createCustomLineItemFromDraft(context.projectKey, draftCustomLineItem, this._storage, draft.country)) ?? [];
1113
1283
  const resource = {
1114
1284
  ...getBaseResourceProperties(),
1115
1285
  anonymousId: draft.anonymousId,
@@ -1122,7 +1292,7 @@ var CartRepository = class extends AbstractResourceRepository {
1122
1292
  country: draft.country,
1123
1293
  customerId: draft.customerId,
1124
1294
  customerEmail: draft.customerEmail,
1125
- customLineItems: [],
1295
+ customLineItems,
1126
1296
  directDiscounts: [],
1127
1297
  discountCodes: [],
1128
1298
  inventoryMode: "None",
@@ -4864,13 +5034,15 @@ const generateMatchFunc = (predicate) => {
4864
5034
  default: throw new Error("Unexpected");
4865
5035
  }
4866
5036
  }).led("IN", 20, ({ left, bp }) => {
5037
+ const firstToken = lexer.peek();
4867
5038
  const expr = parser.parse({ terminals: [bp - 1] });
4868
- lexer.expect(")");
5039
+ if (firstToken.match === "(") lexer.expect(")");
4869
5040
  return (obj, vars) => {
4870
5041
  let symbols = expr;
4871
5042
  if (!Array.isArray(symbols)) symbols = [expr];
4872
5043
  const inValues = symbols.flatMap((item) => resolveSymbol(item, vars));
4873
5044
  const value = resolveValue(obj, left);
5045
+ if (Array.isArray(value)) return inValues.some((inValue) => value.includes(inValue));
4874
5046
  return inValues.includes(value);
4875
5047
  };
4876
5048
  }).led("MATCHES_IGNORE_CASE", 20, ({ left, bp }) => {