@labdigital/commercetools-mock 0.14.1 → 1.1.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
@@ -2787,6 +2787,122 @@ var ProductRepository = class extends AbstractResourceRepository {
2787
2787
  }
2788
2788
  checkForStagedChanges(resource);
2789
2789
  return resource;
2790
+ },
2791
+ addPrice: (context, resource, { variantId, sku, price, staged }) => {
2792
+ const addVariantPrice = (data) => {
2793
+ const { variant, isMasterVariant, variantIndex } = getVariant(
2794
+ data,
2795
+ variantId,
2796
+ sku
2797
+ );
2798
+ if (!variant) {
2799
+ throw new Error(
2800
+ `Variant with id ${variantId} or sku ${sku} not found on product ${resource.id}`
2801
+ );
2802
+ }
2803
+ if (variant.prices === void 0) {
2804
+ variant.prices = [priceFromDraft(price)];
2805
+ } else {
2806
+ variant.prices.push(priceFromDraft(price));
2807
+ }
2808
+ if (isMasterVariant) {
2809
+ data.masterVariant = variant;
2810
+ } else {
2811
+ data.variants[variantIndex] = variant;
2812
+ }
2813
+ };
2814
+ const onlyStaged = staged !== void 0 ? staged : true;
2815
+ addVariantPrice(resource.masterData.staged);
2816
+ if (!onlyStaged) {
2817
+ addVariantPrice(resource.masterData.current);
2818
+ }
2819
+ checkForStagedChanges(resource);
2820
+ return resource;
2821
+ },
2822
+ changePrice: (context, resource, { priceId, price, staged }) => {
2823
+ const changeVariantPrice = (data) => {
2824
+ var _a;
2825
+ const allVariants = [data.masterVariant, ...data.variants ?? []];
2826
+ const priceVariant = allVariants.find(
2827
+ (variant2) => {
2828
+ var _a2;
2829
+ return (_a2 = variant2.prices) == null ? void 0 : _a2.some((x) => x.id === priceId);
2830
+ }
2831
+ );
2832
+ if (!priceVariant) {
2833
+ throw new Error(
2834
+ `Price with id ${priceId} not found on product ${resource.id}`
2835
+ );
2836
+ }
2837
+ const { variant, isMasterVariant, variantIndex } = getVariant(
2838
+ data,
2839
+ priceVariant.id,
2840
+ priceVariant.sku
2841
+ );
2842
+ if (!variant) {
2843
+ throw new Error(
2844
+ `Variant with id ${priceVariant.id} or sku ${priceVariant.sku} not found on product ${resource.id}`
2845
+ );
2846
+ }
2847
+ variant.prices = (_a = variant.prices) == null ? void 0 : _a.map((x) => {
2848
+ if (x.id === priceId) {
2849
+ return { ...x, ...price };
2850
+ }
2851
+ return x;
2852
+ });
2853
+ if (isMasterVariant) {
2854
+ data.masterVariant = variant;
2855
+ } else {
2856
+ data.variants[variantIndex] = variant;
2857
+ }
2858
+ };
2859
+ const onlyStaged = staged !== void 0 ? staged : true;
2860
+ changeVariantPrice(resource.masterData.staged);
2861
+ if (!onlyStaged) {
2862
+ changeVariantPrice(resource.masterData.current);
2863
+ }
2864
+ checkForStagedChanges(resource);
2865
+ return resource;
2866
+ },
2867
+ removePrice: (context, resource, { priceId, staged }) => {
2868
+ const removeVariantPrice = (data) => {
2869
+ var _a;
2870
+ const allVariants = [data.masterVariant, ...data.variants ?? []];
2871
+ const priceVariant = allVariants.find(
2872
+ (variant2) => {
2873
+ var _a2;
2874
+ return (_a2 = variant2.prices) == null ? void 0 : _a2.some((x) => x.id === priceId);
2875
+ }
2876
+ );
2877
+ if (!priceVariant) {
2878
+ throw new Error(
2879
+ `Price with id ${priceId} not found on product ${resource.id}`
2880
+ );
2881
+ }
2882
+ const { variant, isMasterVariant, variantIndex } = getVariant(
2883
+ data,
2884
+ priceVariant.id,
2885
+ priceVariant.sku
2886
+ );
2887
+ if (!variant) {
2888
+ throw new Error(
2889
+ `Variant with id ${priceVariant.id} or sku ${priceVariant.sku} not found on product ${resource.id}`
2890
+ );
2891
+ }
2892
+ variant.prices = (_a = variant.prices) == null ? void 0 : _a.filter((x) => x.id !== priceId);
2893
+ if (isMasterVariant) {
2894
+ data.masterVariant = variant;
2895
+ } else {
2896
+ data.variants[variantIndex] = variant;
2897
+ }
2898
+ };
2899
+ const onlyStaged = staged !== void 0 ? staged : true;
2900
+ removeVariantPrice(resource.masterData.staged);
2901
+ if (!onlyStaged) {
2902
+ removeVariantPrice(resource.masterData.current);
2903
+ }
2904
+ checkForStagedChanges(resource);
2905
+ return resource;
2790
2906
  }
2791
2907
  };
2792
2908
  }
@@ -2880,6 +2996,7 @@ var variantFromDraft = (variantId, variant) => {
2880
2996
  };
2881
2997
  var priceFromDraft = (draft) => ({
2882
2998
  id: (0, import_uuid6.v4)(),
2999
+ country: draft.country,
2883
3000
  value: {
2884
3001
  currencyCode: draft.value.currencyCode,
2885
3002
  centAmount: draft.value.centAmount,
@@ -3931,21 +4048,49 @@ var StagedQuoteRepository = class extends AbstractResourceRepository {
3931
4048
  var StandAlonePriceRepository = class extends AbstractResourceRepository {
3932
4049
  constructor() {
3933
4050
  super(...arguments);
3934
- this.actions = {};
4051
+ this.actions = {
4052
+ setActive: (context, resource, action) => {
4053
+ resource.active = action.active;
4054
+ },
4055
+ changeValue: (context, resource, action) => {
4056
+ resource.value = createTypedMoney(action.value);
4057
+ },
4058
+ setDiscountedPrice: (context, resource, action) => {
4059
+ resource.discounted = action.discounted ? this.transformDiscountDraft(action.discounted) : void 0;
4060
+ }
4061
+ };
3935
4062
  }
3936
4063
  getTypeId() {
3937
4064
  return "standalone-price";
3938
4065
  }
3939
4066
  create(context, draft) {
4067
+ var _a;
3940
4068
  const resource = {
3941
4069
  ...getBaseResourceProperties(),
3942
- active: draft.active,
4070
+ active: draft.active ? draft.active : false,
3943
4071
  sku: draft.sku,
3944
- value: draft.value
4072
+ value: createTypedMoney(draft.value),
4073
+ country: draft.country,
4074
+ discounted: draft.discounted ? this.transformDiscountDraft(draft.discounted) : void 0,
4075
+ channel: ((_a = draft.channel) == null ? void 0 : _a.id) ? this.transformChannelReferenceDraft(draft.channel) : void 0,
4076
+ validFrom: draft.validFrom,
4077
+ validUntil: draft.validUntil
3945
4078
  };
3946
4079
  this.saveNew(context, resource);
3947
4080
  return resource;
3948
4081
  }
4082
+ transformChannelReferenceDraft(channel) {
4083
+ return {
4084
+ typeId: channel.typeId,
4085
+ id: channel.id
4086
+ };
4087
+ }
4088
+ transformDiscountDraft(discounted) {
4089
+ return {
4090
+ value: createTypedMoney(discounted.value),
4091
+ discount: discounted.discount
4092
+ };
4093
+ }
3949
4094
  };
3950
4095
 
3951
4096
  // src/repositories/state.ts