feat(medusa): Disable subscriber when no search engine is installed (#2958)

This commit is contained in:
Adrien de Peretti
2023-01-07 15:58:06 +01:00
committed by GitHub
parent a93d5d437c
commit baeacd1cc5
3 changed files with 23 additions and 4 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
feat: Deactivate search service product subscribers when search engine not enabled

View File

@@ -52,6 +52,8 @@ type PluginDetails = {
version: string
}
export const isSearchEngineInstalledResolutionKey = "isSearchEngineInstalled"
/**
* Registers all services in the services directory
*/
@@ -447,6 +449,8 @@ export async function registerServices(
),
[`searchService`]: aliasTo(name),
})
container.register(isSearchEngineInstalledResolutionKey, asValue(true))
} else if (loaded.prototype instanceof AbstractTaxService) {
container.registerAdd(
"taxProviders",

View File

@@ -1,6 +1,7 @@
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",
@@ -31,12 +32,21 @@ const searchRelations = [
]
class ProductSearchSubscriber {
constructor({ eventBusService, searchService, productService }) {
this.eventBus_ = eventBusService
constructor(container) {
this.eventBus_ = container.eventBusService
this.searchService_ = container.searchService
this.productService_ = container.productService
this.searchService_ = searchService
/**
* 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
*/
this.productService_ = productService
try {
container[isSearchEngineInstalledResolutionKey]
} catch (e) {
return this
}
this.eventBus_.subscribe(
ProductService.Events.CREATED,