@nuskin/ns-product-lib 2.17.0-cx24-5822.6 → 2.17.0-cx24-5822.7
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +1 -1
- package/src/graph-ql/product.js +39 -17
package/package.json
CHANGED
package/src/graph-ql/product.js
CHANGED
@@ -107,7 +107,7 @@ async function getProducts(ids, market, locale, config) {
|
|
107
107
|
variables: {
|
108
108
|
ids,
|
109
109
|
market,
|
110
|
-
locale,
|
110
|
+
language: locale,
|
111
111
|
...(okta !== null && { okta }),
|
112
112
|
quantity: 1 // set default to 1
|
113
113
|
}
|
@@ -115,22 +115,14 @@ async function getProducts(ids, market, locale, config) {
|
|
115
115
|
|
116
116
|
try {
|
117
117
|
const { data } = await graphQlHttpRequest(payload, config);
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
config.sku = ids[index];
|
127
|
-
const newProduct = mapProduct(product, market, locale, config);
|
128
|
-
return new Product(newProduct);
|
129
|
-
})
|
130
|
-
|
131
|
-
productsData.count = productsData.products.length;
|
132
|
-
}
|
133
|
-
|
118
|
+
const mappedProducts = mapSkusToProducts(ids,data);
|
119
|
+
//use to get default variant based on requested sku
|
120
|
+
mappedProducts.forEach(({sku , product})=>{
|
121
|
+
config.sku = sku;
|
122
|
+
const newProduct = mapProduct(product, market, locale, config);
|
123
|
+
productsData.products.push(new Product(newProduct));
|
124
|
+
});
|
125
|
+
productsData.count = productsData.products.length;
|
134
126
|
} catch (e) {
|
135
127
|
console.log(`Unable to fetch products details for SKUs ${ids}`);
|
136
128
|
}
|
@@ -145,6 +137,36 @@ async function getProducts(ids, market, locale, config) {
|
|
145
137
|
}
|
146
138
|
|
147
139
|
|
140
|
+
/**
|
141
|
+
* Map skus array to products array
|
142
|
+
* @param {string[]} skus
|
143
|
+
* @param {any} products
|
144
|
+
*/
|
145
|
+
|
146
|
+
function mapSkusToProducts(skus , products){
|
147
|
+
const productsData = [];
|
148
|
+
if (products?.data?.productsById?.products?.length) {
|
149
|
+
skus.forEach((sku)=>{
|
150
|
+
const matchedProducts = products.data.productsById.products.filter((product)=>{
|
151
|
+
let matched = false;
|
152
|
+
if(product?.id) {matched = product.id === sku;}
|
153
|
+
if(product?.variants?.length){
|
154
|
+
product.variants.forEach((variant)=>{
|
155
|
+
matched |= variant.sku === sku;
|
156
|
+
});
|
157
|
+
}
|
158
|
+
return matched;
|
159
|
+
});
|
160
|
+
if(matchedProducts.length > 0){
|
161
|
+
productsData.push({ sku: sku , product: matchedProducts[0]});
|
162
|
+
}
|
163
|
+
})
|
164
|
+
}
|
165
|
+
return productsData;
|
166
|
+
|
167
|
+
}
|
168
|
+
|
169
|
+
|
148
170
|
/**
|
149
171
|
* Map product prices
|
150
172
|
* @param {*} product
|