@nuskin/ns-product-lib 2.17.0-cx24-5822.5 → 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 CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nuskin/ns-product-lib",
3
- "version": "2.17.0-cx24-5822.5",
3
+ "version": "2.17.0-cx24-5822.7",
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,19 +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 && data.data.productsById && data.data.productsById.products) {
120
-
121
- //use to get default variant based on requested sku
122
- productsData.products = data.data.productsById.products.map((product,index)=>{
123
- config.sku = ids[index];
124
- const newProduct = mapProduct(product, market, locale, config);
125
- return new Product(newProduct);
126
- })
127
-
128
- productsData.count = productsData.products.length;
129
- }
130
-
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;
131
126
  } catch (e) {
132
127
  console.log(`Unable to fetch products details for SKUs ${ids}`);
133
128
  }
@@ -142,6 +137,36 @@ async function getProducts(ids, market, locale, config) {
142
137
  }
143
138
 
144
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
+
145
170
  /**
146
171
  * Map product prices
147
172
  * @param {*} product