@labdigital/commercetools-mock 1.0.0 → 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.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 +1 -1
- package/src/repositories/product.ts +163 -3
- package/src/services/product.test.ts +124 -0
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,
|