fix(medusa,utils): Searching indexing product subscriber (#3905)

This commit is contained in:
Oliver Windall Juhl
2023-04-23 13:16:54 +02:00
committed by GitHub
parent aec7ae219d
commit 491566df6b
6 changed files with 106 additions and 132 deletions

View File

@@ -0,0 +1,6 @@
---
"@medusajs/medusa": patch
"@medusajs/utils": patch
---
fix(medusa,utils): Searching indexing product subscriber

View File

@@ -1,121 +0,0 @@
import ProductVariantService from "../services/product-variant"
import ProductService from "../services/product"
import { indexTypes } from "medusa-core-utils"
import { isSearchEngineInstalledResolutionKey } from "../loaders/plugins"
const searchFields = [
"id",
"title",
"subtitle",
"status",
"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(container) {
this.eventBus_ = container.eventBusService
this.searchService_ = container.searchService
this.productService_ = container.productService
/**
* Do not subscribe to any event in case no search engine have been installed.
* If some events need to be subscribed out of the search engine reason, they can be subscribed above this comment
*/
try {
container[isSearchEngineInstalledResolutionKey]
} catch (e) {
return this
}
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(
ProductVariantService.Events.CREATED,
this.handleProductVariantChange
)
this.eventBus_.subscribe(
ProductVariantService.Events.UPDATED,
this.handleProductVariantChange
)
this.eventBus_.subscribe(
ProductVariantService.Events.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.searchService_.addDocuments(
ProductService.IndexName,
[product],
indexTypes.products
)
}
handleProductDeletion = async (data) => {
await this.searchService_.deleteDocument(ProductService.IndexName, data.id)
}
handleProductVariantChange = async (data) => {
const product = await this.retrieveProduct_(data.product_id)
await this.searchService_.addDocuments(
ProductService.IndexName,
[product],
indexTypes.products
)
}
}
export default ProductSearchSubscriber

View File

@@ -0,0 +1,87 @@
import { IEventBusService, ISearchService } from "@medusajs/types"
import { defaultSearchIndexingProductRelations } from "@medusajs/utils"
import { indexTypes } from "medusa-core-utils"
import { isSearchEngineInstalledResolutionKey } from "../loaders/plugins"
import ProductService from "../services/product"
import ProductVariantService from "../services/product-variant"
type InjectedDependencies = {
eventBusService: IEventBusService
searchService: ISearchService
productService: ProductService
}
class ProductSearchSubscriber {
private readonly eventBusService_: IEventBusService
private readonly searchService_: ISearchService
private readonly productService_: ProductService
constructor(container: InjectedDependencies) {
this.eventBusService_ = container.eventBusService
this.searchService_ = container.searchService
this.productService_ = container.productService
/**
* Do not subscribe to any event in case no search engine have been installed.
* If some events need to be subscribed out of the search engine reason, they can be subscribed above this comment
*/
try {
container[isSearchEngineInstalledResolutionKey]
} catch (e) {
return this
}
this.eventBusService_
.subscribe(ProductService.Events.CREATED, this.handleProductCreation)
.subscribe(ProductService.Events.UPDATED, this.handleProductUpdate)
.subscribe(ProductService.Events.DELETED, this.handleProductDeletion)
.subscribe(
ProductVariantService.Events.CREATED,
this.handleProductVariantChange
)
.subscribe(
ProductVariantService.Events.UPDATED,
this.handleProductVariantChange
)
.subscribe(
ProductVariantService.Events.DELETED,
this.handleProductVariantChange
)
}
handleProductCreation = async (data) => {
const product = await this.productService_.retrieve(data.id, {
relations: defaultSearchIndexingProductRelations,
})
await this.searchService_.addDocuments(
ProductService.IndexName,
[product],
indexTypes.products
)
}
handleProductUpdate = async (data) => {
const product = await this.productService_.retrieve(data.id)
await this.searchService_.addDocuments(
ProductService.IndexName,
[product],
indexTypes.products
)
}
handleProductDeletion = async (data) => {
await this.searchService_.deleteDocument(ProductService.IndexName, data.id)
}
handleProductVariantChange = async (data) => {
const product = await this.productService_.retrieve(data.product_id)
await this.searchService_.addDocuments(
ProductService.IndexName,
[product],
indexTypes.products
)
}
}
export default ProductSearchSubscriber

View File

@@ -1,4 +1,5 @@
import { IEventBusService, ISearchService } from "@medusajs/types"
import { defaultSearchIndexingProductRelations } from "@medusajs/utils"
import { indexTypes } from "medusa-core-utils"
import ProductCategoryFeatureFlag from "../loaders/feature-flags/product-categories"
import { SEARCH_INDEX_EVENT } from "../loaders/search-index"
@@ -59,17 +60,7 @@ class SearchIndexingSubscriber {
lastSeenId: string,
take: number
): Promise<Product[]> {
const relations = [
"variants",
"tags",
"type",
"collection",
"variants.prices",
"images",
"variants.options",
"options",
]
const relations = [...defaultSearchIndexingProductRelations]
if (
this.featureFlagRouter_.isFeatureEnabled(ProductCategoryFeatureFlag.key)
) {

View File

@@ -1,3 +1,4 @@
export * from "./abstract-service"
export * from "./is-search-service"
export * from "./search-relations"

View File

@@ -0,0 +1,10 @@
export const defaultSearchIndexingProductRelations = [
"variants",
"tags",
"type",
"collection",
"variants.prices",
"images",
"variants.options",
"options",
]