Browse Source

fix(core, elasticsearch-plugin) Reindex variants on stock movement events (#3113)

Leftovers.today 1 year ago
parent
commit
82512eec31

+ 8 - 0
packages/core/src/plugin/default-search-plugin/default-search-plugin.ts

@@ -19,6 +19,7 @@ import { JobQueueService } from '../../job-queue/job-queue.service';
 import { PluginCommonModule } from '../plugin-common.module';
 import { VendurePlugin } from '../vendure-plugin';
 
+import { StockMovementEvent } from '../../event-bus/events/stock-movement-event';
 import { stockStatusExtension } from './api/api-extensions';
 import { AdminFulltextSearchResolver, ShopFulltextSearchResolver } from './api/fulltext-search.resolver';
 import { BUFFER_SEARCH_INDEX_UPDATES, PLUGIN_INIT_OPTIONS } from './constants';
@@ -166,6 +167,13 @@ export class DefaultSearchPlugin implements OnApplicationBootstrap, OnApplicatio
             }
         });
 
+        this.eventBus.ofType(StockMovementEvent).subscribe(event => {
+            return this.searchIndexService.updateVariants(
+                event.ctx,
+                event.stockMovements.map(m => m.productVariant),
+            );
+        });
+
         // TODO: Remove this buffering logic because because we have dedicated buffering based on #1137
         const collectionModification$ = this.eventBus.ofType(CollectionModificationEvent);
         const closingNotifier$ = collectionModification$.pipe(debounceTime(50));

+ 8 - 0
packages/elasticsearch-plugin/src/plugin.ts

@@ -15,6 +15,7 @@ import {
     ProductVariantChannelEvent,
     ProductVariantEvent,
     SearchJobBufferService,
+    StockMovementEvent,
     TaxRateModificationEvent,
     Type,
     VendurePlugin,
@@ -349,6 +350,13 @@ export class ElasticsearchPlugin implements OnApplicationBootstrap {
             }
         });
 
+        this.eventBus.ofType(StockMovementEvent).subscribe(event => {
+            return this.elasticsearchIndexService.updateVariants(
+                event.ctx,
+                event.stockMovements.map(m => m.productVariant),
+            );
+        });
+
         // TODO: Remove this buffering logic because because we have dedicated buffering based on #1137
         const collectionModification$ = this.eventBus.ofType(CollectionModificationEvent);
         const closingNotifier$ = collectionModification$.pipe(debounceTime(50));