From 700f8c39190469337c74d9bf3f046f293024e521 Mon Sep 17 00:00:00 2001 From: Sebastian Rindom Date: Mon, 18 Oct 2021 13:42:40 +0200 Subject: [PATCH] fix: move subscriber to core --- .eslintignore | 2 +- .../medusa-plugin-meilisearch/.eslintignore | 8 ++ .../src/loaders/index.js | 1 + .../src/subscribers/product-search.js | 106 ----------------- packages/medusa-plugin-meilisearch/yarn.lock | 60 ---------- packages/medusa/src/loaders/search-index.js | 30 ++--- packages/medusa/src/services/search.js | 2 +- packages/medusa/src/subscribers/product.js | 112 ++++++++++++++++++ 8 files changed, 139 insertions(+), 182 deletions(-) create mode 100644 packages/medusa-plugin-meilisearch/.eslintignore delete mode 100644 packages/medusa-plugin-meilisearch/src/subscribers/product-search.js create mode 100644 packages/medusa/src/subscribers/product.js diff --git a/.eslintignore b/.eslintignore index 3515221c4b..9b1552c693 100644 --- a/.eslintignore +++ b/.eslintignore @@ -23,6 +23,7 @@ /packages/medusa/src/subscribers/notification.js /packages/medusa/src/subscribers/order.js +/packages/medusa/src/subscribers/product.js /packages/medusa/src/loaders/api.js /packages/medusa/src/loaders/database.js @@ -75,7 +76,6 @@ /packages/medusa-plugin-economic /packages/medusa-plugin-ip-lookup /packages/medusa-plugin-mailchimp -/packages/medusa-plugin-meilisearch /packages/medusa-plugin-permissions /packages/medusa-plugin-restock-notification /packages/medusa-plugin-segment diff --git a/packages/medusa-plugin-meilisearch/.eslintignore b/packages/medusa-plugin-meilisearch/.eslintignore new file mode 100644 index 0000000000..9f560229da --- /dev/null +++ b/packages/medusa-plugin-meilisearch/.eslintignore @@ -0,0 +1,8 @@ +/src/subscribers + +/api +/services +/models +/subscribers +/loaders +/utils diff --git a/packages/medusa-plugin-meilisearch/src/loaders/index.js b/packages/medusa-plugin-meilisearch/src/loaders/index.js index b7ba033487..d6b3e76e11 100644 --- a/packages/medusa-plugin-meilisearch/src/loaders/index.js +++ b/packages/medusa-plugin-meilisearch/src/loaders/index.js @@ -8,6 +8,7 @@ export default async (container, options) => { ) ) } catch (err) { + // ignore console.log(err) } } diff --git a/packages/medusa-plugin-meilisearch/src/subscribers/product-search.js b/packages/medusa-plugin-meilisearch/src/subscribers/product-search.js deleted file mode 100644 index bcfdfbf55e..0000000000 --- a/packages/medusa-plugin-meilisearch/src/subscribers/product-search.js +++ /dev/null @@ -1,106 +0,0 @@ -import { indexTypes } from "medusa-core-utils" -import { transformProduct } from "../utils/transform-product" - -class ProductSearchSubscriber { - constructor( - { eventBusService, meilisearchService, productService }, - options - ) { - this.eventBus_ = eventBusService - - this.meilisearchService_ = meilisearchService - - this.productService_ = productService - - this.productIndexName = productService.constructor.IndexName - - this.eventBus_.subscribe("product.created", this.handleProductCreation) - - this.eventBus_.subscribe("product.updated", this.handleProductUpdate) - - this.eventBus_.subscribe("product.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.meilisearchService_.addDocuments( - this.productIndexName, - [product], - indexTypes.products - ) - } - - retrieveProduct_ = async (product_id) => { - const product = await this.productService_.retrieve(product_id, { - select: [ - "id", - "title", - "subtitle", - "description", - "handle", - "is_giftcard", - "discountable", - "thumbnail", - "profile_id", - "collection_id", - "type_id", - "origin_country", - "created_at", - "updated_at", - ], - relations: [ - "variants", - "tags", - "type", - "collection", - "variants.prices", - "variants.options", - "options", - ], - }) - const transformedProduct = transformProduct(product) - return transformedProduct - } - - handleProductUpdate = async (data) => { - const product = await this.retrieveProduct_(data.id) - await this.meilisearchService_.addDocuments( - this.productIndexName, - [product], - indexTypes.products - ) - } - - handleProductDeletion = async (data) => { - await this.meilisearchService_.deleteDocument( - this.productIndexName, - data.id - ) - } - - handleProductVariantChange = async (data) => { - const product = await this.retrieveProduct_(data.product_id) - await this.meilisearchService_.addDocuments( - this.productIndexName, - [product], - indexTypes.products - ) - } -} - -export default ProductSearchSubscriber diff --git a/packages/medusa-plugin-meilisearch/yarn.lock b/packages/medusa-plugin-meilisearch/yarn.lock index dec1edc80c..15a5be80b3 100644 --- a/packages/medusa-plugin-meilisearch/yarn.lock +++ b/packages/medusa-plugin-meilisearch/yarn.lock @@ -953,18 +953,6 @@ exec-sh "^0.3.2" minimist "^1.2.0" -"@hapi/hoek@^9.0.0": - version "9.2.0" - resolved "https://registry.yarnpkg.com/@hapi/hoek/-/hoek-9.2.0.tgz#f3933a44e365864f4dad5db94158106d511e8131" - integrity sha512-sqKVVVOe5ivCaXDWivIJYVSaEgdQK9ul7a4Kity5Iw7u9+wBAPbX1RMSnLLmp7O4Vzj0WOWwMAJsTL00xwaNug== - -"@hapi/topo@^5.0.0": - version "5.1.0" - resolved "https://registry.yarnpkg.com/@hapi/topo/-/topo-5.1.0.tgz#dc448e332c6c6e37a4dc02fd84ba8d44b9afb012" - integrity sha512-foQZKJig7Ob0BMAYBfcJk8d77QtOe7Wo4ox7ff1lQYoNNAb6jwcY1ncdoy2e9wQZzvNy7ODZCYJkK8kzmcAnAg== - dependencies: - "@hapi/hoek" "^9.0.0" - "@istanbuljs/load-nyc-config@^1.0.0": version "1.1.0" resolved "https://registry.yarnpkg.com/@istanbuljs/load-nyc-config/-/load-nyc-config-1.1.0.tgz#fd3db1d59ecf7cf121e80650bb86712f9b55eced" @@ -1166,23 +1154,6 @@ readdirp "^2.2.1" upath "^1.1.1" -"@sideway/address@^4.1.0": - version "4.1.2" - resolved "https://registry.yarnpkg.com/@sideway/address/-/address-4.1.2.tgz#811b84333a335739d3969cfc434736268170cad1" - integrity sha512-idTz8ibqWFrPU8kMirL0CoPH/A29XOzzAzpyN3zQ4kAWnzmNfFmRaoMNN6VI8ske5M73HZyhIaW4OuSFIdM4oA== - dependencies: - "@hapi/hoek" "^9.0.0" - -"@sideway/formula@^3.0.0": - version "3.0.0" - resolved "https://registry.yarnpkg.com/@sideway/formula/-/formula-3.0.0.tgz#fe158aee32e6bd5de85044be615bc08478a0a13c" - integrity sha512-vHe7wZ4NOXVfkoRb8T5otiENVlT7a3IAiw7H5M2+GO+9CDgcVUUsX1zalAztCmwyOr2RUTGJdgB+ZvSVqmdHmg== - -"@sideway/pinpoint@^2.0.0": - version "2.0.0" - resolved "https://registry.yarnpkg.com/@sideway/pinpoint/-/pinpoint-2.0.0.tgz#cff8ffadc372ad29fd3f78277aeb29e632cc70df" - integrity sha512-RNiOoTPkptFtSVzQevY/yWtZwf/RxyVnPy/OcA9HBM3MlGDnBEYL5B41H0MTn0Uec8Hi+2qUtTfG2WWZBmMejQ== - "@sinonjs/commons@^1.7.0": version "1.8.3" resolved "https://registry.yarnpkg.com/@sinonjs/commons/-/commons-1.8.3.tgz#3802ddd21a50a949b6721ddd72da36e67e7f1b2d" @@ -3531,22 +3502,6 @@ jest@^25.5.2: import-local "^3.0.2" jest-cli "^25.5.4" -joi-objectid@^3.0.1: - version "3.0.1" - resolved "https://registry.yarnpkg.com/joi-objectid/-/joi-objectid-3.0.1.tgz#63ace7860f8e1a993a28d40c40ffd8eff01a3668" - integrity sha512-V/3hbTlGpvJ03Me6DJbdBI08hBTasFOmipsauOsxOSnsF1blxV537WTl1zPwbfcKle4AK0Ma4OPnzMH4LlvTpQ== - -joi@^17.3.0: - version "17.4.2" - resolved "https://registry.yarnpkg.com/joi/-/joi-17.4.2.tgz#02f4eb5cf88e515e614830239379dcbbe28ce7f7" - integrity sha512-Lm56PP+n0+Z2A2rfRvsfWVDXGEWjXxatPopkQ8qQ5mxCEhwHG+Ettgg5o98FFaxilOxozoa14cFhrE/hOzh/Nw== - dependencies: - "@hapi/hoek" "^9.0.0" - "@hapi/topo" "^5.0.0" - "@sideway/address" "^4.1.0" - "@sideway/formula" "^3.0.0" - "@sideway/pinpoint" "^2.0.0" - js-tokens@^4.0.0: version "4.0.0" resolved "https://registry.yarnpkg.com/js-tokens/-/js-tokens-4.0.0.tgz#19203fb59991df98e3a287050d4647cdeaf32499" @@ -3777,21 +3732,6 @@ media-typer@0.3.0: resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748" integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g= -medusa-core-utils@^1.1.22: - version "1.1.22" - resolved "https://registry.yarnpkg.com/medusa-core-utils/-/medusa-core-utils-1.1.22.tgz#84ce0af0a7c672191d758ea462056e30a39d08b1" - integrity sha512-kMuRkWOuNG4Bw6epg/AYu95UJuE+rjHTeTWRLbEPrYGjWREV82tLWVDI21/QcccmaHmMU98Rkw2z9JwyFZIiyw== - dependencies: - joi "^17.3.0" - joi-objectid "^3.0.1" - -medusa-interfaces@^1.1.23: - version "1.1.23" - resolved "https://registry.yarnpkg.com/medusa-interfaces/-/medusa-interfaces-1.1.23.tgz#b552a8c1d0eaddeff30472ab238652b9e1a56e73" - integrity sha512-dHCOnsyYQvjrtRd3p0ZqQZ4M/zmo4M/BAgVfRrYSyGrMdQ86TK9Z1DQDCHEzM1216AxEfXz2JYUD7ilTfG2iHQ== - dependencies: - medusa-core-utils "^1.1.22" - meilisearch@^0.20.0: version "0.20.0" resolved "https://registry.yarnpkg.com/meilisearch/-/meilisearch-0.20.0.tgz#42899fec7a2ddefcd035e30ed5dd47aa65a6727f" diff --git a/packages/medusa/src/loaders/search-index.js b/packages/medusa/src/loaders/search-index.js index af57c034fd..8c01a2fdbc 100644 --- a/packages/medusa/src/loaders/search-index.js +++ b/packages/medusa/src/loaders/search-index.js @@ -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 + } } } diff --git a/packages/medusa/src/services/search.js b/packages/medusa/src/services/search.js index f0893e313b..1bca213329 100644 --- a/packages/medusa/src/services/search.js +++ b/packages/medusa/src/services/search.js @@ -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) { diff --git a/packages/medusa/src/subscribers/product.js b/packages/medusa/src/subscribers/product.js new file mode 100644 index 0000000000..6e5922fac6 --- /dev/null +++ b/packages/medusa/src/subscribers/product.js @@ -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