fix: move subscriber to core

This commit is contained in:
Sebastian Rindom committed 2021-10-18 13:42:40 +02:00
1 parent 4fd361fddd
commit 700f8c3919
8 files changed
+139 -182

No files matched your search

+16 -14
View File
@@ -6,13 +6,12 @@ async function loadProductsIntoSearchEngine(container) {
const productService = container.resolve("productService")
const TAKE = 20
const totalCount = await productService.count()
let iterCount = 0
let lastSeenId = ""
let skip = 0
let hasMore = true
while (iterCount < totalCount) {
while (hasMore) {
const products = await productService.list(
{ id: { gt: lastSeenId } },
{},
{
select: [
"id",
@@ -40,18 +39,21 @@ async function loadProductsIntoSearchEngine(container) {
"options",
],
take: TAKE,
order: { id: "ASC" },
skip,
order: { created_at: "ASC" },
}
)
await searchService.addDocuments(
ProductService.IndexName,
products,
indexTypes.products
)
iterCount += products.length
lastSeenId = products[products.length - 1].id
if (products.length > 0) {
await searchService.addDocuments(
ProductService.IndexName,
products,
indexTypes.products
)
skip += products.length
} else {
hasMore = false
}
}
}
+1 -1
View File
@@ -2,7 +2,7 @@ import { SearchService } from "medusa-interfaces"
/**
* Default class that implements SearchService but provides stuv implementation for all methods
* @implements SearchService
* @extends SearchService
*/
class DefaultSearchService extends SearchService {
constructor(container) {
+112
View File
@@ -0,0 +1,112 @@
import ProductService from "../services/product"
import { indexTypes } from "medusa-core-utils"
const searchFields = [
"id",
"title",
"subtitle",
"description",
"handle",
"is_giftcard",
"discountable",
"thumbnail",
"profile_id",
"collection_id",
"type_id",
"origin_country",
"created_at",
"updated_at",
]
const searchRelations = [
"variants",
"tags",
"type",
"collection",
"variants.prices",
"variants.options",
"options",
]
class ProductSearchSubscriber {
constructor({ eventBusService, searchService, productService }) {
this.eventBus_ = eventBusService
this.searchService_ = searchService
this.productService_ = productService
this.eventBus_.subscribe(
ProductService.Events.CREATED,
this.handleProductCreation
)
this.eventBus_.subscribe(
ProductService.Events.UPDATED,
this.handleProductUpdate
)
this.eventBus_.subscribe(
ProductService.Events.DELETED,
this.handleProductDeletion
)
this.eventBus_.subscribe(
"product-variant.created",
this.handleProductVariantChange
)
this.eventBus_.subscribe(
"product-variant.updated",
this.handleProductVariantChange
)
this.eventBus_.subscribe(
"product-variant.deleted",
this.handleProductVariantChange
)
}
handleProductCreation = async (data) => {
const product = await this.retrieveProduct_(data.id)
await this.searchService.addDocuments(
ProductService.IndexName,
[product],
indexTypes.products
)
}
retrieveProduct_ = async (product_id) => {
return await this.productService_.retrieve(product_id, {
select: searchFields,
relations: searchRelations,
})
}
handleProductUpdate = async (data) => {
const product = await this.retrieveProduct_(data.id)
await this.meilisearchService_.addDocuments(
ProductService.IndexName,
[product],
indexTypes.products
)
}
handleProductDeletion = async (data) => {
await this.meilisearchService_.deleteDocument(
ProductService.IndexName,
data.id
)
}
handleProductVariantChange = async (data) => {
const product = await this.retrieveProduct_(data.product_id)
await this.meilisearchService_.addDocuments(
ProductService.IndexName,
[product],
indexTypes.products
)
}
}
export default ProductSearchSubscriber