@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 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.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,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,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