@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.d.ts +12 -3
- package/dist/index.global.js +480 -7
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +148 -3
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +148 -3
- package/dist/index.mjs.map +1 -1
- package/package.json +6 -6
- package/src/repositories/product.ts +163 -3
- package/src/repositories/standalone-price.ts +53 -15
- package/src/services/product.test.ts +124 -0
- package/src/services/standalone-price.test.ts +248 -10
package/dist/index.mjs
CHANGED
|
@@ -2754,6 +2754,122 @@ var ProductRepository = class extends AbstractResourceRepository {
|
|
|
2754
2754
|
}
|
|
2755
2755
|
checkForStagedChanges(resource);
|
|
2756
2756
|
return resource;
|
|
2757
|
+
},
|
|
2758
|
+
addPrice: (context, resource, { variantId, sku, price, staged }) => {
|
|
2759
|
+
const addVariantPrice = (data) => {
|
|
2760
|
+
const { variant, isMasterVariant, variantIndex } = getVariant(
|
|
2761
|
+
data,
|
|
2762
|
+
variantId,
|
|
2763
|
+
sku
|
|
2764
|
+
);
|
|
2765
|
+
if (!variant) {
|
|
2766
|
+
throw new Error(
|
|
2767
|
+
`Variant with id ${variantId} or sku ${sku} not found on product ${resource.id}`
|
|
2768
|
+
);
|
|
2769
|
+
}
|
|
2770
|
+
if (variant.prices === void 0) {
|
|
2771
|
+
variant.prices = [priceFromDraft(price)];
|
|
2772
|
+
} else {
|
|
2773
|
+
variant.prices.push(priceFromDraft(price));
|
|
2774
|
+
}
|
|
2775
|
+
if (isMasterVariant) {
|
|
2776
|
+
data.masterVariant = variant;
|
|
2777
|
+
} else {
|
|
2778
|
+
data.variants[variantIndex] = variant;
|
|
2779
|
+
}
|
|
2780
|
+
};
|
|
2781
|
+
const onlyStaged = staged !== void 0 ? staged : true;
|
|
2782
|
+
addVariantPrice(resource.masterData.staged);
|
|
2783
|
+
if (!onlyStaged) {
|
|
2784
|
+
addVariantPrice(resource.masterData.current);
|
|
2785
|
+
}
|
|
2786
|
+
checkForStagedChanges(resource);
|
|
2787
|
+
return resource;
|
|
2788
|
+
},
|
|
2789
|
+
changePrice: (context, resource, { priceId, price, staged }) => {
|
|
2790
|
+
const changeVariantPrice = (data) => {
|
|
2791
|
+
var _a;
|
|
2792
|
+
const allVariants = [data.masterVariant, ...data.variants ?? []];
|
|
2793
|
+
const priceVariant = allVariants.find(
|
|
2794
|
+
(variant2) => {
|
|
2795
|
+
var _a2;
|
|
2796
|
+
return (_a2 = variant2.prices) == null ? void 0 : _a2.some((x) => x.id === priceId);
|
|
2797
|
+
}
|
|
2798
|
+
);
|
|
2799
|
+
if (!priceVariant) {
|
|
2800
|
+
throw new Error(
|
|
2801
|
+
`Price with id ${priceId} not found on product ${resource.id}`
|
|
2802
|
+
);
|
|
2803
|
+
}
|
|
2804
|
+
const { variant, isMasterVariant, variantIndex } = getVariant(
|
|
2805
|
+
data,
|
|
2806
|
+
priceVariant.id,
|
|
2807
|
+
priceVariant.sku
|
|
2808
|
+
);
|
|
2809
|
+
if (!variant) {
|
|
2810
|
+
throw new Error(
|
|
2811
|
+
`Variant with id ${priceVariant.id} or sku ${priceVariant.sku} not found on product ${resource.id}`
|
|
2812
|
+
);
|
|
2813
|
+
}
|
|
2814
|
+
variant.prices = (_a = variant.prices) == null ? void 0 : _a.map((x) => {
|
|
2815
|
+
if (x.id === priceId) {
|
|
2816
|
+
return { ...x, ...price };
|
|
2817
|
+
}
|
|
2818
|
+
return x;
|
|
2819
|
+
});
|
|
2820
|
+
if (isMasterVariant) {
|
|
2821
|
+
data.masterVariant = variant;
|
|
2822
|
+
} else {
|
|
2823
|
+
data.variants[variantIndex] = variant;
|
|
2824
|
+
}
|
|
2825
|
+
};
|
|
2826
|
+
const onlyStaged = staged !== void 0 ? staged : true;
|
|
2827
|
+
changeVariantPrice(resource.masterData.staged);
|
|
2828
|
+
if (!onlyStaged) {
|
|
2829
|
+
changeVariantPrice(resource.masterData.current);
|
|
2830
|
+
}
|
|
2831
|
+
checkForStagedChanges(resource);
|
|
2832
|
+
return resource;
|
|
2833
|
+
},
|
|
2834
|
+
removePrice: (context, resource, { priceId, staged }) => {
|
|
2835
|
+
const removeVariantPrice = (data) => {
|
|
2836
|
+
var _a;
|
|
2837
|
+
const allVariants = [data.masterVariant, ...data.variants ?? []];
|
|
2838
|
+
const priceVariant = allVariants.find(
|
|
2839
|
+
(variant2) => {
|
|
2840
|
+
var _a2;
|
|
2841
|
+
return (_a2 = variant2.prices) == null ? void 0 : _a2.some((x) => x.id === priceId);
|
|
2842
|
+
}
|
|
2843
|
+
);
|
|
2844
|
+
if (!priceVariant) {
|
|
2845
|
+
throw new Error(
|
|
2846
|
+
`Price with id ${priceId} not found on product ${resource.id}`
|
|
2847
|
+
);
|
|
2848
|
+
}
|
|
2849
|
+
const { variant, isMasterVariant, variantIndex } = getVariant(
|
|
2850
|
+
data,
|
|
2851
|
+
priceVariant.id,
|
|
2852
|
+
priceVariant.sku
|
|
2853
|
+
);
|
|
2854
|
+
if (!variant) {
|
|
2855
|
+
throw new Error(
|
|
2856
|
+
`Variant with id ${priceVariant.id} or sku ${priceVariant.sku} not found on product ${resource.id}`
|
|
2857
|
+
);
|
|
2858
|
+
}
|
|
2859
|
+
variant.prices = (_a = variant.prices) == null ? void 0 : _a.filter((x) => x.id !== priceId);
|
|
2860
|
+
if (isMasterVariant) {
|
|
2861
|
+
data.masterVariant = variant;
|
|
2862
|
+
} else {
|
|
2863
|
+
data.variants[variantIndex] = variant;
|
|
2864
|
+
}
|
|
2865
|
+
};
|
|
2866
|
+
const onlyStaged = staged !== void 0 ? staged : true;
|
|
2867
|
+
removeVariantPrice(resource.masterData.staged);
|
|
2868
|
+
if (!onlyStaged) {
|
|
2869
|
+
removeVariantPrice(resource.masterData.current);
|
|
2870
|
+
}
|
|
2871
|
+
checkForStagedChanges(resource);
|
|
2872
|
+
return resource;
|
|
2757
2873
|
}
|
|
2758
2874
|
};
|
|
2759
2875
|
}
|
|
@@ -2847,6 +2963,7 @@ var variantFromDraft = (variantId, variant) => {
|
|
|
2847
2963
|
};
|
|
2848
2964
|
var priceFromDraft = (draft) => ({
|
|
2849
2965
|
id: uuidv46(),
|
|
2966
|
+
country: draft.country,
|
|
2850
2967
|
value: {
|
|
2851
2968
|
currencyCode: draft.value.currencyCode,
|
|
2852
2969
|
centAmount: draft.value.centAmount,
|
|
@@ -3898,21 +4015,49 @@ var StagedQuoteRepository = class extends AbstractResourceRepository {
|
|
|
3898
4015
|
var StandAlonePriceRepository = class extends AbstractResourceRepository {
|
|
3899
4016
|
constructor() {
|
|
3900
4017
|
super(...arguments);
|
|
3901
|
-
this.actions = {
|
|
4018
|
+
this.actions = {
|
|
4019
|
+
setActive: (context, resource, action) => {
|
|
4020
|
+
resource.active = action.active;
|
|
4021
|
+
},
|
|
4022
|
+
changeValue: (context, resource, action) => {
|
|
4023
|
+
resource.value = createTypedMoney(action.value);
|
|
4024
|
+
},
|
|
4025
|
+
setDiscountedPrice: (context, resource, action) => {
|
|
4026
|
+
resource.discounted = action.discounted ? this.transformDiscountDraft(action.discounted) : void 0;
|
|
4027
|
+
}
|
|
4028
|
+
};
|
|
3902
4029
|
}
|
|
3903
4030
|
getTypeId() {
|
|
3904
4031
|
return "standalone-price";
|
|
3905
4032
|
}
|
|
3906
4033
|
create(context, draft) {
|
|
4034
|
+
var _a;
|
|
3907
4035
|
const resource = {
|
|
3908
4036
|
...getBaseResourceProperties(),
|
|
3909
|
-
active: draft.active,
|
|
4037
|
+
active: draft.active ? draft.active : false,
|
|
3910
4038
|
sku: draft.sku,
|
|
3911
|
-
value: draft.value
|
|
4039
|
+
value: createTypedMoney(draft.value),
|
|
4040
|
+
country: draft.country,
|
|
4041
|
+
discounted: draft.discounted ? this.transformDiscountDraft(draft.discounted) : void 0,
|
|
4042
|
+
channel: ((_a = draft.channel) == null ? void 0 : _a.id) ? this.transformChannelReferenceDraft(draft.channel) : void 0,
|
|
4043
|
+
validFrom: draft.validFrom,
|
|
4044
|
+
validUntil: draft.validUntil
|
|
3912
4045
|
};
|
|
3913
4046
|
this.saveNew(context, resource);
|
|
3914
4047
|
return resource;
|
|
3915
4048
|
}
|
|
4049
|
+
transformChannelReferenceDraft(channel) {
|
|
4050
|
+
return {
|
|
4051
|
+
typeId: channel.typeId,
|
|
4052
|
+
id: channel.id
|
|
4053
|
+
};
|
|
4054
|
+
}
|
|
4055
|
+
transformDiscountDraft(discounted) {
|
|
4056
|
+
return {
|
|
4057
|
+
value: createTypedMoney(discounted.value),
|
|
4058
|
+
discount: discounted.discount
|
|
4059
|
+
};
|
|
4060
|
+
}
|
|
3916
4061
|
};
|
|
3917
4062
|
|
|
3918
4063
|
// src/repositories/state.ts
|