1
0
Эх сурвалжийг харах

Merge branch 'feature/search-event'

Michael Bromley 3 жил өмнө
parent
commit
0592f94246

+ 1 - 1
packages/core/src/event-bus/events/role-event.ts

@@ -9,7 +9,7 @@ type RoleInputTypes = CreateRoleInput | UpdateRoleInput | ID;
 
 /**
  * @description
- * This event is fired whenever one {@link Role} is  is added, updated or deleted.
+ * This event is fired whenever one {@link Role} is added, updated or deleted.
  *
  * @docsCategory events
  * @docsPage Event Types

+ 22 - 0
packages/core/src/event-bus/events/search-event.ts

@@ -0,0 +1,22 @@
+import { SearchInput } from '@vendure/common/lib/generated-types';
+
+import { RequestContext } from '../../api/common/request-context';
+import { VendureEvent } from '../vendure-event';
+
+type ExtendedSearchInput = SearchInput & {
+    [extendedInputField: string]: any;
+};
+
+/**
+ * @description
+ * This event is fired whenever a search query is executed.
+ *
+ * @docsCategory events
+ * @docsPage Event Types
+ * @since 1.6.0
+ */
+export class SearchEvent extends VendureEvent {
+    constructor(public ctx: RequestContext, public input: ExtendedSearchInput) {
+        super();
+    }
+}

+ 1 - 0
packages/core/src/event-bus/index.ts

@@ -47,6 +47,7 @@ export * from './events/promotion-event';
 export * from './events/refund-state-transition-event';
 export * from './events/role-change-event';
 export * from './events/role-event';
+export * from './events/search-event';
 export * from './events/shipping-method-event';
 export * from './events/stock-movement-event';
 export * from './events/tax-category-event';

+ 3 - 0
packages/core/src/plugin/default-search-plugin/fulltext-search.service.ts

@@ -7,6 +7,7 @@ import { InternalServerError } from '../../common/error/errors';
 import { TransactionalConnection } from '../../connection/transactional-connection';
 import { Collection, FacetValue } from '../../entity';
 import { EventBus } from '../../event-bus/event-bus';
+import { SearchEvent } from '../../event-bus/events/search-event';
 import { Job } from '../../job-queue/job';
 import { CollectionService } from '../../service/services/collection.service';
 import { FacetValueService } from '../../service/services/facet-value.service';
@@ -54,6 +55,8 @@ export class FulltextSearchService {
     ): Promise<Omit<Omit<SearchResponse, 'facetValues'>, 'collections'>> {
         const items = await this._searchStrategy.getSearchResults(ctx, input, enabledOnly);
         const totalItems = await this._searchStrategy.getTotalCount(ctx, input, enabledOnly);
+        this.eventBus.publish(new SearchEvent(ctx, input));
+
         return {
             items,
             totalItems,

+ 5 - 0
packages/elasticsearch-plugin/src/elasticsearch.service.ts

@@ -6,12 +6,14 @@ import {
     CollectionService,
     ConfigService,
     DeepRequired,
+    EventBus,
     FacetValue,
     FacetValueService,
     InternalServerError,
     Job,
     Logger,
     RequestContext,
+    SearchEvent,
     SearchService,
 } from '@vendure/core';
 import equal from 'fast-deep-equal/es6';
@@ -46,6 +48,7 @@ export class ElasticsearchService implements OnModuleInit, OnModuleDestroy {
         private configService: ConfigService,
         private facetValueService: FacetValueService,
         private collectionService: CollectionService,
+        private eventBus: EventBus,
     ) {
         searchService.adopt(this);
     }
@@ -191,6 +194,7 @@ export class ElasticsearchService implements OnModuleInit, OnModuleDestroy {
                     body: elasticSearchBody,
                 });
                 const totalItems = await this.totalHits(ctx, input, groupByProduct);
+                this.eventBus.publish(new SearchEvent(ctx, input));
                 return {
                     items: body.hits.hits.map(hit => this.mapProductToSearchResult(hit)),
                     totalItems,
@@ -215,6 +219,7 @@ export class ElasticsearchService implements OnModuleInit, OnModuleDestroy {
                     index: indexPrefix + VARIANT_INDEX_NAME,
                     body: elasticSearchBody,
                 });
+                this.eventBus.publish(new SearchEvent(ctx, input));
                 return {
                     items: body.hits.hits.map(hit => this.mapVariantToSearchResult(hit)),
                     totalItems: body.hits.total ? body.hits.total.value : 0,