fix cursor pagination + flatten variant options field
This commit is contained in:
1 parent
451e3bcdcf
commit
307b1c9342
4 files changed
+43
-8
No files matched your search
@@ -60,7 +60,15 @@ class ProductSearchSubscriber {
|
||||
"created_at",
|
||||
"updated_at",
|
||||
],
|
||||
relations: ["variants", "tags", "type", "collection"],
|
||||
relations: [
|
||||
"variants",
|
||||
"tags",
|
||||
"type",
|
||||
"collection",
|
||||
"variants.prices",
|
||||
"variants.options",
|
||||
"options",
|
||||
],
|
||||
})
|
||||
const transformedProduct = transformProduct(product)
|
||||
return transformedProduct
|
||||
|
||||
@@ -1,4 +1,12 @@
|
||||
const variantKeys = ["sku", "title", "upc", "ean", "mid_code", "hs_code"]
|
||||
const variantKeys = [
|
||||
"sku",
|
||||
"title",
|
||||
"upc",
|
||||
"ean",
|
||||
"mid_code",
|
||||
"hs_code",
|
||||
"options",
|
||||
]
|
||||
const prefix = `variant`
|
||||
|
||||
export const transformProduct = (product) => {
|
||||
@@ -6,17 +14,25 @@ export const transformProduct = (product) => {
|
||||
obj[`${prefix}_${key}`] = []
|
||||
return obj
|
||||
}, {})
|
||||
initialObj[`${prefix}_options_value`] = []
|
||||
|
||||
const flattenedVariantFields = product.variants.reduce((obj, variant) => {
|
||||
variantKeys.forEach(
|
||||
(k) => variant[k] && obj[`${prefix}_${k}`].push(variant[k])
|
||||
)
|
||||
variantKeys.forEach((k) => {
|
||||
if (k === "options" && variant[k]) {
|
||||
const values = variant[k].map((option) => option.value)
|
||||
obj[`${prefix}_options_value`] = obj[`${prefix}_options_value`].concat(
|
||||
values
|
||||
)
|
||||
return
|
||||
}
|
||||
return variant[k] && obj[`${prefix}_${k}`].push(variant[k])
|
||||
})
|
||||
return obj
|
||||
}, initialObj)
|
||||
|
||||
product.type_value = product.type && product.type.value
|
||||
product.collection_title = product.collection && product.collection.title
|
||||
product.tags_value = product.tags.map((t) => t.value)
|
||||
product.tags_value = product.tags ? product.tags.map((t) => t.value) : []
|
||||
|
||||
return {
|
||||
...product,
|
||||
|
||||
Reference in new issue
Block a user