@nuskin/ns-product-lib 2.17.0-cx24-5822.6 → 2.17.0-cx24-5822.8

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuskin/ns-product-lib",
3
- "version": "2.17.0-cx24-5822.6",
3
+ "version": "2.17.0-cx24-5822.8",
4
4
  "description": "This project contains shared Product models and code between the backend and frontend.",
5
5
  "main": "src/index.js",
6
6
  "scripts": {
@@ -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
- if (data && data.data &&
120
- data.data.productsById &&
121
- data.data.productsById.products &&
122
- data.data.productsById.products.length) {
123
-
124
- //use to get default variant based on requested sku
125
- productsData.products = data.data.productsById.products.map((product,index)=>{
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,40 @@ 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 &&
149
+ products.data &&
150
+ products.data.productsById &&
151
+ products.data.productsById.products &&
152
+ products.data.productsById.products.length) {
153
+ skus.forEach((sku)=>{
154
+ const matchedProducts = products.data.productsById.products.filter((product)=>{
155
+ let matched = false;
156
+ if(product.id) {matched = product.id === sku;}
157
+ if(product.variants.length){
158
+ product.variants.forEach((variant)=>{
159
+ matched |= variant.sku === sku;
160
+ });
161
+ }
162
+ return matched;
163
+ });
164
+ if(matchedProducts.length > 0){
165
+ productsData.push({ sku: sku , product: matchedProducts[0]});
166
+ }
167
+ })
168
+ }
169
+ return productsData;
170
+
171
+ }
172
+
173
+
148
174
  /**
149
175
  * Map product prices
150
176
  * @param {*} product