fix: use type to choose transformer before adding or replacing documents

This commit is contained in:
zakariaelas committed 2021-09-24 11:34:14 +01:00
1 parent 307b1c9342
commit 24eecd2922
6 files changed
+45 -36

No files matched your search

@@ -1,5 +1,6 @@
import { SearchService } from "medusa-interfaces"
import { MeiliSearch } from "meilisearch"
import { indexTypes } from "medusa-core-utils"
import { transformProduct } from "../utils/transform-product"
class MeiliSearchService extends SearchService {
@@ -19,12 +20,14 @@ class MeiliSearchService extends SearchService {
return this.client_.index(indexName)
}
addDocuments(indexName, documents) {
return this.client_.index(indexName).addDocuments(documents)
addDocuments(indexName, documents, type) {
const transformedDocuments = this.getTransformedDocuments(type, documents)
return this.client_.index(indexName).addDocuments(transformedDocuments)
}
replaceDocuments(indexName, documents) {
return this.client_.index(indexName).addDocuments(documents)
replaceDocuments(indexName, documents, type) {
const transformedDocuments = this.getTransformedDocuments(type, documents)
return this.client_.index(indexName).addDocuments(transformedDocuments)
}
deleteDocument(indexName, document_id) {
@@ -43,6 +46,15 @@ class MeiliSearchService extends SearchService {
return this.client_.index(indexName).updateSettings(settings)
}
getTransformedDocuments(type, documents) {
switch (type) {
case indexTypes.products:
return this.transformProducts(documents)
default:
return documents
}
}
transformProducts(products) {
if (!products) return []
return products.map(transformProduct)
@@ -1,3 +1,4 @@
import { indexTypes } from "medusa-core-utils"
import { transformProduct } from "../utils/transform-product"
class ProductSearchSubscriber {
@@ -37,9 +38,11 @@ class ProductSearchSubscriber {
handleProductCreation = async (data) => {
const product = await this.retrieveProduct_(data.id)
await this.meilisearchService_.addDocuments(this.productIndexName, [
product,
])
await this.meilisearchService_.addDocuments(
this.productIndexName,
[product],
indexTypes.products
)
}
retrieveProduct_ = async (product_id) => {
@@ -76,9 +79,11 @@ class ProductSearchSubscriber {
handleProductUpdate = async (data) => {
const product = await this.retrieveProduct_(data.id)
await this.meilisearchService_.addDocuments(this.productIndexName, [
product,
])
await this.meilisearchService_.addDocuments(
this.productIndexName,
[product],
indexTypes.products
)
}
handleProductDeletion = async (data) => {
@@ -90,9 +95,11 @@ class ProductSearchSubscriber {
handleProductVariantChange = async (data) => {
const product = await this.retrieveProduct_(data.product_id)
await this.meilisearchService_.addDocuments(this.productIndexName, [
product,
])
await this.meilisearchService_.addDocuments(
this.productIndexName,
[product],
indexTypes.products
)
}
}