idosell 0.4.13 → 0.4.17
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/changelog.md +6 -0
- package/dist/enums.d.ts +28 -0
- package/dist/enums.js +33 -1
- package/dist/gateways.d.ts +6 -5
- package/dist/helpers.js +1 -1
- package/dist/reqparams.d.ts +9 -5
- package/dist/responses.d.ts +328 -393
- package/dist/utils.d.ts +13 -14
- package/dist/utils.js +204 -81
- package/package.json +1 -1
- package/tests/util.data.ts +343 -0
- package/tests/utilMapProductCodes.test.js +24 -0
- package/tests/utilMapProductLocations.test.js +29 -0
- package/tests/utilMapSizeQuantites.test.js +48 -0
- package/tests/utilSumProductQuantities.test.js +48 -0
package/dist/utils.d.ts
CHANGED
|
@@ -1,31 +1,30 @@
|
|
|
1
|
+
import type { PRODUCT_SIZE_COUNTABLE, PRODUCT_SIZE_LOCATIONS, PRODUCT_SIZE_CODES } from "./enums.d";
|
|
1
2
|
import type { SearchProductsResponse } from "./responses.d.ts"
|
|
2
3
|
|
|
3
|
-
export enum QunatityType {
|
|
4
|
-
ProductSizeQuantityOwnStock = "productSizeQuantityOwnStock",
|
|
5
|
-
ProductSizeQuantityOutsideStock = "productSizeQuantityOutsideStock",
|
|
6
|
-
ProductSizeQuantityAllStocks = "productSizeQuantityAllStocks",
|
|
7
|
-
ProductStocksQuantities = "productStocksQuantities",
|
|
8
|
-
ProductOrdersUnfinishedQuantities = "productOrdersUnfinishedQuantities",
|
|
9
|
-
ProductSizesDeliveries = "productSizesDeliveries",
|
|
10
|
-
ProductSizesDispositionsInAuctions = "productSizesDispositionsInAuctions",
|
|
11
|
-
}
|
|
12
|
-
|
|
13
4
|
export type IdosellProduct = SearchProductsResponse['results'][0];
|
|
14
5
|
|
|
15
6
|
export type GetIaICodeFunction = (productId: number|string, sizeId: string) => string;
|
|
16
7
|
|
|
17
|
-
export type SumProductQuantitiesFunction = (productStocksData: IdosellProduct['productStocksData'], stockType?:
|
|
8
|
+
export type SumProductQuantitiesFunction = (productStocksData: IdosellProduct['productStocksData'], stockType?: PRODUCT_SIZE_COUNTABLE | `${PRODUCT_SIZE_COUNTABLE}`) => number;
|
|
9
|
+
|
|
10
|
+
export type MapSizeQuantitesFunction = (product: IdosellProduct, stockType?: `${PRODUCT_SIZE_COUNTABLE}`) => Record<string,number>;
|
|
11
|
+
|
|
12
|
+
export type MapProductCodesFunction = (product: IdosellProduct, codeType?: `${PRODUCT_SIZE_CODES}`) => Record<string,string>;
|
|
13
|
+
|
|
14
|
+
export type MapProductLocationsFunction = (product: IdosellProduct, stockId?: number, codeType?: `${PRODUCT_SIZE_LOCATIONS}`) => Record<string,string[]>;
|
|
18
15
|
|
|
19
|
-
export type
|
|
16
|
+
export type GetLangDataFunction = <T extends { langId: string}>(array: T[], langId?: string) => T|undefined;
|
|
20
17
|
|
|
21
|
-
export type
|
|
18
|
+
export type ClearParametersLangDataFunction = (products: SearchProductsResponse['results'], langId: string = 'pol') => SearchProductsResponse['results'];
|
|
22
19
|
|
|
23
20
|
declare namespace utils {
|
|
24
|
-
const QUANTITY_TYPE_ENUM: typeof QunatityType;
|
|
25
21
|
const getIaiCode: GetIaICodeFunction;
|
|
26
22
|
const sumProductQuantities: SumProductQuantitiesFunction;
|
|
27
23
|
const mapSizeQuantites: MapSizeQuantitesFunction;
|
|
28
24
|
const mapProductCodes: MapProductCodesFunction;
|
|
25
|
+
const getLangData: GetLangDataFunction;
|
|
26
|
+
const clearParametersLangData: ClearParametersLangDataFunction;
|
|
27
|
+
const mapProductLocations: MapProductLocationsFunction;
|
|
29
28
|
}
|
|
30
29
|
|
|
31
30
|
export default utils;
|
package/dist/utils.js
CHANGED
|
@@ -1,99 +1,222 @@
|
|
|
1
|
-
|
|
2
|
-
(
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
QunatityType["ProductStocksQuantities"] = "productStocksQuantities";
|
|
7
|
-
QunatityType["ProductOrdersUnfinishedQuantities"] = "productOrdersUnfinishedQuantities";
|
|
8
|
-
QunatityType["ProductSizesDeliveries"] = "productSizesDeliveries";
|
|
9
|
-
QunatityType["ProductSizesDispositionsInAuctions"] = "productSizesDispositionsInAuctions";
|
|
10
|
-
})(QunatityType || (QunatityType = {}));
|
|
11
|
-
const productQuantityPathMap = {
|
|
12
|
-
// ----- Product Dispositions -----
|
|
13
|
-
[QunatityType.ProductSizeQuantityOwnStock]: ["productSizesDispositions", "productSizesDispositionsInSales", "productSizeQuantityOwnStock"],
|
|
14
|
-
[QunatityType.ProductSizeQuantityOutsideStock]: ["productSizesDispositions", "productSizesDispositionsInSales", "productSizeQuantityOutsideStock"],
|
|
15
|
-
[QunatityType.ProductSizeQuantityAllStocks]: ["productSizesDispositions", "productSizesDispositionsInSales", "productSizeQuantityAllStocks"],
|
|
16
|
-
[QunatityType.ProductSizesDispositionsInAuctions]: ["productSizesDispositions", "productSizesDispositionsInAuctions", "productSizeQuantity"],
|
|
17
|
-
// ----- Stocks & related quantities -----
|
|
18
|
-
[QunatityType.ProductStocksQuantities]: ["productStocksQuantities", "productSizeQuantity"],
|
|
19
|
-
[QunatityType.ProductOrdersUnfinishedQuantities]: ["productOrdersUnfinishedQuantities", "productSizeQuantity"],
|
|
20
|
-
[QunatityType.ProductSizesDeliveries]: ["productSizesDeliveries", "productSizeQuantity"],
|
|
21
|
-
};
|
|
22
|
-
const getProductSizeArrayNode = (productStocksData, key) => {
|
|
23
|
-
const pathNode = productQuantityPathMap[key];
|
|
24
|
-
if (!pathNode)
|
|
25
|
-
throw new Error("Invalid stock type " + key);
|
|
26
|
-
const path = pathNode.slice(0, -1);
|
|
27
|
-
const node = pathNode[pathNode.length - 1];
|
|
28
|
-
const arr = getProductSizeArray(productStocksData, path);
|
|
29
|
-
return { arr, node };
|
|
1
|
+
import ENUMS from "./enums.js";
|
|
2
|
+
const getIaiCode = (productId, sizeId) => {
|
|
3
|
+
if (sizeId === 'uniw')
|
|
4
|
+
return productId.toString();
|
|
5
|
+
return [productId, sizeId].join('-');
|
|
30
6
|
};
|
|
31
|
-
const
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
7
|
+
const traverseNodes = (node, pathIndex, path, results = []) => {
|
|
8
|
+
if (!node) {
|
|
9
|
+
return;
|
|
10
|
+
}
|
|
11
|
+
if (pathIndex >= path.length) {
|
|
12
|
+
results.push(node);
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const key = path[pathIndex];
|
|
16
|
+
if (key === 'array') {
|
|
17
|
+
if (Array.isArray(node)) {
|
|
18
|
+
node.forEach(item => traverseNodes(item, pathIndex + 1, path, results));
|
|
39
19
|
}
|
|
40
20
|
}
|
|
41
|
-
|
|
42
|
-
if (
|
|
43
|
-
|
|
21
|
+
else {
|
|
22
|
+
if (node[key] !== undefined) {
|
|
23
|
+
traverseNodes(node[key], pathIndex + 1, path, results);
|
|
44
24
|
}
|
|
45
|
-
return current;
|
|
46
25
|
}
|
|
47
|
-
return [];
|
|
48
26
|
};
|
|
49
|
-
const
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
27
|
+
const productSizesPathMap = {
|
|
28
|
+
[ENUMS.PRODUCT_SIZE_COUNTABLE.QUANTITY]: [
|
|
29
|
+
'productStocksData',
|
|
30
|
+
'productStocksQuantities',
|
|
31
|
+
'array',
|
|
32
|
+
'productSizesData',
|
|
33
|
+
'array'
|
|
34
|
+
],
|
|
35
|
+
[ENUMS.PRODUCT_SIZE_COUNTABLE.QUANTITY_OWN]: [
|
|
36
|
+
'productStocksData',
|
|
37
|
+
'productSizesDispositions',
|
|
38
|
+
'productSizesDispositionsInSales',
|
|
39
|
+
'array'
|
|
40
|
+
],
|
|
41
|
+
[ENUMS.PRODUCT_SIZE_COUNTABLE.QUANTITY_OUTSIDE]: [
|
|
42
|
+
'productStocksData',
|
|
43
|
+
'productSizesDispositions',
|
|
44
|
+
'productSizesDispositionsInSales',
|
|
45
|
+
'array'
|
|
46
|
+
],
|
|
47
|
+
[ENUMS.PRODUCT_SIZE_COUNTABLE.QUANTITY_ALL]: [
|
|
48
|
+
'productStocksData',
|
|
49
|
+
'productSizesDispositions',
|
|
50
|
+
'productSizesDispositionsInSales',
|
|
51
|
+
'array'
|
|
52
|
+
],
|
|
53
|
+
[ENUMS.PRODUCT_SIZE_COUNTABLE.QUANTITY_ORDERS_UNFINISHED]: [
|
|
54
|
+
'productStocksData',
|
|
55
|
+
'productOrdersUnfinishedQuantities',
|
|
56
|
+
'array',
|
|
57
|
+
'productSizesData',
|
|
58
|
+
'array'
|
|
59
|
+
],
|
|
60
|
+
[ENUMS.PRODUCT_SIZE_COUNTABLE.DELIVERIES]: [
|
|
61
|
+
'productStocksData',
|
|
62
|
+
'productSizesDeliveries',
|
|
63
|
+
'array'
|
|
64
|
+
],
|
|
65
|
+
[ENUMS.PRODUCT_SIZE_COUNTABLE.AUCTIONS]: [
|
|
66
|
+
'productStocksData',
|
|
67
|
+
'productSizesDispositions',
|
|
68
|
+
'productSizesDispositionsInAuctions',
|
|
69
|
+
'array'
|
|
70
|
+
],
|
|
71
|
+
[ENUMS.PRODUCT_SIZE_CODES.NAME]: [
|
|
72
|
+
'productSizes',
|
|
73
|
+
'array'
|
|
74
|
+
],
|
|
75
|
+
[ENUMS.PRODUCT_SIZE_CODES.CODE_PRODUCER]: [
|
|
76
|
+
'productSizesAttributes',
|
|
77
|
+
'array'
|
|
78
|
+
],
|
|
79
|
+
[ENUMS.PRODUCT_SIZE_CODES.CODE_EXTERNAL]: [
|
|
80
|
+
'productStocksData',
|
|
81
|
+
'productStocksQuantities',
|
|
82
|
+
'array',
|
|
83
|
+
'productSizesData',
|
|
84
|
+
'array'
|
|
85
|
+
],
|
|
86
|
+
[ENUMS.PRODUCT_SIZE_LOCATIONS.NAME]: [
|
|
87
|
+
'productStocksData',
|
|
88
|
+
'productSizesStocksLocations',
|
|
89
|
+
'array',
|
|
90
|
+
'productSizesLocation',
|
|
91
|
+
'array'
|
|
92
|
+
],
|
|
93
|
+
[ENUMS.PRODUCT_SIZE_LOCATIONS.CODE]: [
|
|
94
|
+
'productStocksData',
|
|
95
|
+
'productSizesStocksLocations',
|
|
96
|
+
'array',
|
|
97
|
+
'productSizesLocation',
|
|
98
|
+
'array'
|
|
99
|
+
],
|
|
100
|
+
[ENUMS.PRODUCT_SIZE_LOCATIONS.ID]: [
|
|
101
|
+
'productStocksData',
|
|
102
|
+
'productSizesStocksLocations',
|
|
103
|
+
'array',
|
|
104
|
+
'productSizesLocation',
|
|
105
|
+
'array'
|
|
106
|
+
],
|
|
57
107
|
};
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
108
|
+
const getTraversedPathElements = (product, nodeName) => {
|
|
109
|
+
const path = productSizesPathMap[nodeName];
|
|
110
|
+
if (!path)
|
|
111
|
+
throw new Error(`Invalid path for ${nodeName}`);
|
|
112
|
+
const results = [];
|
|
113
|
+
traverseNodes(product, 0, path, results);
|
|
114
|
+
return results;
|
|
62
115
|
};
|
|
63
|
-
const
|
|
64
|
-
const
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
116
|
+
const sumProductQuantities = (productStocksData, nodeName = ENUMS.PRODUCT_SIZE_COUNTABLE.QUANTITY_OWN) => {
|
|
117
|
+
const results = getTraversedPathElements({ productStocksData, productId: 0 }, nodeName);
|
|
118
|
+
let sum = 0;
|
|
119
|
+
if (['productOrdersUnfinishedQuantities', 'productSizesDeliveries', 'productSizesDispositionsInAuctions'].includes(nodeName))
|
|
120
|
+
nodeName = 'productSizeQuantity';
|
|
121
|
+
results.forEach(item => {
|
|
122
|
+
if (item?.sizeId && item[nodeName] !== undefined) {
|
|
123
|
+
const value = Number(item[nodeName]) || 0;
|
|
124
|
+
sum += value;
|
|
125
|
+
}
|
|
126
|
+
});
|
|
127
|
+
return sum;
|
|
128
|
+
};
|
|
129
|
+
const mapSizeQuantites = (product, nodeName = ENUMS.PRODUCT_SIZE_COUNTABLE.QUANTITY_OWN) => {
|
|
130
|
+
const results = getTraversedPathElements(product, nodeName);
|
|
131
|
+
const aggregated = {};
|
|
132
|
+
if (['productOrdersUnfinishedQuantities', 'productSizesDeliveries', 'productSizesDispositionsInAuctions'].includes(nodeName))
|
|
133
|
+
nodeName = 'productSizeQuantity';
|
|
134
|
+
results.forEach(item => {
|
|
135
|
+
if (item?.sizeId && item[nodeName] !== undefined) {
|
|
136
|
+
const sizeId = String(item.sizeId);
|
|
137
|
+
const value = Number(item[nodeName]) || 0;
|
|
138
|
+
aggregated[sizeId] = (aggregated[sizeId] || 0) + value;
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
return aggregated;
|
|
142
|
+
};
|
|
143
|
+
const mapProductCodes = (product, codeType = ENUMS.PRODUCT_SIZE_CODES.NAME) => {
|
|
144
|
+
const results = getTraversedPathElements(product, codeType);
|
|
145
|
+
const mapped = {};
|
|
146
|
+
results.forEach(item => {
|
|
147
|
+
if (item?.sizeId && item[codeType] !== undefined) {
|
|
148
|
+
const sizeId = String(item.sizeId);
|
|
149
|
+
const value = String(item[codeType] || '');
|
|
150
|
+
if (!mapped[sizeId] || value) {
|
|
151
|
+
mapped[sizeId] = value;
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
return mapped;
|
|
156
|
+
};
|
|
157
|
+
const SUB_TYPE_NODES = {
|
|
158
|
+
stockLocationTextId: 'stockAdditionalLocationTextId',
|
|
159
|
+
stockLocationId: 'stockAdditionalLocationId',
|
|
160
|
+
stockLocationCode: 'stockAdditionalLocationCode'
|
|
161
|
+
};
|
|
162
|
+
const mapProductLocations = (product, stockId, locationType = ENUMS.PRODUCT_SIZE_LOCATIONS.NAME) => {
|
|
163
|
+
let results = getTraversedPathElements(product, locationType);
|
|
164
|
+
if (stockId) {
|
|
165
|
+
const prefix = 'M' + stockId;
|
|
166
|
+
results = results.filter((location) => location.stockLocationTextId.startsWith(prefix));
|
|
167
|
+
}
|
|
168
|
+
const mapped = {};
|
|
169
|
+
const subNode = SUB_TYPE_NODES[locationType];
|
|
170
|
+
for (const item of results) {
|
|
171
|
+
const { sizeId } = item;
|
|
172
|
+
if (!mapped[sizeId]) {
|
|
173
|
+
mapped[sizeId] = [];
|
|
174
|
+
}
|
|
175
|
+
// main location
|
|
176
|
+
const mainLocation = item[locationType];
|
|
177
|
+
if (mainLocation) {
|
|
178
|
+
mapped[sizeId].push(mainLocation.toString());
|
|
179
|
+
}
|
|
180
|
+
// additional locations
|
|
181
|
+
if (item.stockAdditionalLocations?.length) {
|
|
182
|
+
for (const additional of item.stockAdditionalLocations) {
|
|
183
|
+
const value = additional[subNode];
|
|
184
|
+
if (value) {
|
|
185
|
+
mapped[sizeId].push(value.toString());
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
}
|
|
77
189
|
}
|
|
78
|
-
return
|
|
190
|
+
return mapped;
|
|
191
|
+
};
|
|
192
|
+
const getLangData = (arr, langId = 'pol') => {
|
|
193
|
+
if (!arr.length)
|
|
194
|
+
return undefined;
|
|
195
|
+
if (!arr[0].langId)
|
|
196
|
+
throw new Error('Array has no langId');
|
|
197
|
+
return arr.find(item => item.langId === langId);
|
|
79
198
|
};
|
|
80
|
-
const
|
|
81
|
-
const
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
if (!
|
|
199
|
+
const clearParametersLangData = (products, langId = 'pol') => {
|
|
200
|
+
for (const product of products) {
|
|
201
|
+
if (!product.productParameters)
|
|
202
|
+
continue;
|
|
203
|
+
for (const parameter of product.productParameters) {
|
|
204
|
+
if (!parameter.parameterValues)
|
|
86
205
|
continue;
|
|
87
|
-
|
|
88
|
-
|
|
206
|
+
parameter.parameterDescriptionsLangData = parameter.parameterDescriptionsLangData.filter(lang => lang.langId === langId);
|
|
207
|
+
for (const value of parameter.parameterValues) {
|
|
208
|
+
value.parameterValueDescriptionsLangData = value.parameterValueDescriptionsLangData.filter(lang => lang.langId === langId).map((item) => { item.parameterValueShopsData = []; return item; });
|
|
209
|
+
}
|
|
89
210
|
}
|
|
90
211
|
}
|
|
91
|
-
return
|
|
212
|
+
return products;
|
|
92
213
|
};
|
|
93
214
|
export default {
|
|
94
|
-
QUANTITY_TYPE_ENUM: QunatityType,
|
|
95
|
-
getIaiCode,
|
|
96
215
|
sumProductQuantities,
|
|
216
|
+
getIaiCode,
|
|
97
217
|
mapSizeQuantites,
|
|
98
|
-
mapProductCodes
|
|
218
|
+
mapProductCodes,
|
|
219
|
+
mapProductLocations,
|
|
220
|
+
getLangData,
|
|
221
|
+
clearParametersLangData
|
|
99
222
|
};
|