@labdigital/commercetools-mock 1.0.0 → 1.1.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.global.js +117 -0
- package/dist/index.global.js.map +1 -1
- package/dist/index.js +117 -0
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +117 -0
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -3
- package/src/repositories/product.ts +163 -3
- package/src/services/product.test.ts +124 -0
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,
|