@labdigital/commercetools-mock 2.55.0 → 2.56.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.js CHANGED
@@ -760,7 +760,90 @@ 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
+ const taxRate = taxCategory ? taxCategory.rates.find((rate) => !rate.country || rate.country === country) : void 0;
828
+ return {
829
+ id: v4(),
830
+ key: draft.key,
831
+ name: draft.name,
832
+ money: createTypedMoney(draft.money),
833
+ slug: draft.slug,
834
+ quantity: draft.quantity ?? 1,
835
+ state: [],
836
+ taxCategory: taxCategoryRef,
837
+ taxRate,
838
+ taxedPrice,
839
+ custom: createCustomFields(draft.custom, projectKey, storage),
840
+ discountedPricePerQuantity: [],
841
+ perMethodTaxRate: [],
842
+ priceMode: draft.priceMode ?? "Standard",
843
+ totalPrice,
844
+ taxedPricePortions: []
845
+ };
846
+ };
764
847
 
765
848
  //#endregion
766
849
  //#region src/repositories/cart/actions.ts
@@ -886,6 +969,94 @@ var CartUpdateHandler = class extends AbstractUpdateHandler {
886
969
  });
887
970
  resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
888
971
  }
972
+ addCustomLineItem(context, resource, { money, name, slug, quantity = 1, taxCategory, custom, priceMode = "Standard", key }) {
973
+ const customLineItem = createCustomLineItemFromDraft(context.projectKey, {
974
+ money,
975
+ name,
976
+ slug,
977
+ quantity,
978
+ taxCategory,
979
+ custom,
980
+ priceMode,
981
+ key
982
+ }, this._storage, resource.shippingAddress?.country ?? resource.country);
983
+ resource.customLineItems.push(customLineItem);
984
+ resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
985
+ }
986
+ removeCustomLineItem(context, resource, { customLineItemId, customLineItemKey }) {
987
+ let customLineItem;
988
+ if (!customLineItemId && !customLineItemKey) throw new CommercetoolsError({
989
+ code: "General",
990
+ message: "Either customLineItemId or customLineItemKey needs to be provided."
991
+ });
992
+ if (customLineItemId) {
993
+ customLineItem = resource.customLineItems.find((x) => x.id === customLineItemId);
994
+ if (!customLineItem) throw new CommercetoolsError({
995
+ code: "General",
996
+ message: `A custom line item with ID '${customLineItemId}' not found.`
997
+ });
998
+ resource.customLineItems = resource.customLineItems.filter((x) => x.id !== customLineItemId);
999
+ }
1000
+ if (customLineItemKey) {
1001
+ customLineItem = resource.customLineItems.find((x) => x.key === customLineItemKey);
1002
+ if (!customLineItem) throw new CommercetoolsError({
1003
+ code: "General",
1004
+ message: `A custom line item with key '${customLineItemKey}' not found.`
1005
+ });
1006
+ resource.customLineItems = resource.customLineItems.filter((x) => x.key !== customLineItemKey);
1007
+ }
1008
+ resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1009
+ }
1010
+ changeCustomLineItemQuantity(context, resource, { customLineItemId, customLineItemKey, quantity }) {
1011
+ let customLineItem;
1012
+ if (!customLineItemId && !customLineItemKey) throw new CommercetoolsError({
1013
+ code: "General",
1014
+ message: "Either customLineItemId or customLineItemKey needs to be provided."
1015
+ });
1016
+ const setQuantity = (customLineItem$1) => {
1017
+ if (!customLineItem$1) throw new CommercetoolsError({
1018
+ code: "General",
1019
+ message: `A custom line item with ${customLineItemId ? `ID '${customLineItemId}'` : `key '${customLineItemKey}'`} not found.`
1020
+ });
1021
+ customLineItem$1.quantity = quantity;
1022
+ customLineItem$1.totalPrice = createCentPrecisionMoney({
1023
+ ...customLineItem$1.money,
1024
+ centAmount: (customLineItem$1.money.centAmount ?? 0) * quantity
1025
+ });
1026
+ };
1027
+ if (customLineItemId) {
1028
+ customLineItem = resource.customLineItems.find((x) => x.id === customLineItemId);
1029
+ setQuantity(customLineItem);
1030
+ }
1031
+ if (customLineItemKey) {
1032
+ customLineItem = resource.customLineItems.find((x) => x.key === customLineItemKey);
1033
+ setQuantity(customLineItem);
1034
+ }
1035
+ resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1036
+ }
1037
+ changeCustomLineItemMoney(context, resource, { customLineItemId, customLineItemKey, money }) {
1038
+ let customLineItem;
1039
+ const setMoney = (customLineItem$1) => {
1040
+ if (!customLineItem$1) throw new CommercetoolsError({
1041
+ code: "General",
1042
+ message: `A custom line item with ${customLineItemId ? `ID '${customLineItemId}'` : `key '${customLineItemKey}'`} not found.`
1043
+ });
1044
+ customLineItem$1.money = createTypedMoney(money);
1045
+ customLineItem$1.totalPrice = createCentPrecisionMoney({
1046
+ ...money,
1047
+ centAmount: (money.centAmount ?? 0) * customLineItem$1.quantity
1048
+ });
1049
+ };
1050
+ if (customLineItemId) {
1051
+ customLineItem = resource.customLineItems.find((x) => x.id === customLineItemId);
1052
+ setMoney(customLineItem);
1053
+ }
1054
+ if (customLineItemKey) {
1055
+ customLineItem = resource.customLineItems.find((x) => x.key === customLineItemKey);
1056
+ setMoney(customLineItem);
1057
+ }
1058
+ resource.totalPrice.centAmount = calculateCartTotalPrice(resource);
1059
+ }
889
1060
  setAnonymousId(_context, resource, { anonymousId }) {
890
1061
  resource.anonymousId = anonymousId;
891
1062
  resource.customerId = void 0;
@@ -1110,6 +1281,7 @@ var CartRepository = class extends AbstractResourceRepository {
1110
1281
  key: draft.businessUnit.key
1111
1282
  });
1112
1283
  const lineItems = draft.lineItems?.map((draftLineItem) => this.draftLineItemtoLineItem(context.projectKey, draftLineItem, draft.currency, draft.country)) ?? [];
1284
+ const customLineItems = draft.customLineItems?.map((draftCustomLineItem) => createCustomLineItemFromDraft(context.projectKey, draftCustomLineItem, this._storage, draft.shippingAddress?.country ?? draft.country)) ?? [];
1113
1285
  const resource = {
1114
1286
  ...getBaseResourceProperties(),
1115
1287
  anonymousId: draft.anonymousId,
@@ -1122,7 +1294,7 @@ var CartRepository = class extends AbstractResourceRepository {
1122
1294
  country: draft.country,
1123
1295
  customerId: draft.customerId,
1124
1296
  customerEmail: draft.customerEmail,
1125
- customLineItems: [],
1297
+ customLineItems,
1126
1298
  directDiscounts: [],
1127
1299
  discountCodes: [],
1128
1300
  inventoryMode: "None",
@@ -4864,13 +5036,15 @@ const generateMatchFunc = (predicate) => {
4864
5036
  default: throw new Error("Unexpected");
4865
5037
  }
4866
5038
  }).led("IN", 20, ({ left, bp }) => {
5039
+ const firstToken = lexer.peek();
4867
5040
  const expr = parser.parse({ terminals: [bp - 1] });
4868
- lexer.expect(")");
5041
+ if (firstToken.match === "(") lexer.expect(")");
4869
5042
  return (obj, vars) => {
4870
5043
  let symbols = expr;
4871
5044
  if (!Array.isArray(symbols)) symbols = [expr];
4872
5045
  const inValues = symbols.flatMap((item) => resolveSymbol(item, vars));
4873
5046
  const value = resolveValue(obj, left);
5047
+ if (Array.isArray(value)) return inValues.some((inValue) => value.includes(inValue));
4874
5048
  return inValues.includes(value);
4875
5049
  };
4876
5050
  }).led("MATCHES_IGNORE_CASE", 20, ({ left, bp }) => {