Browse Source

feat(core): Add StockLocationEvent

Michael Bromley 1 year ago
parent
commit
5cff832708

+ 27 - 0
packages/core/src/event-bus/events/stock-location-event.ts

@@ -0,0 +1,27 @@
+import { CreateStockLocationInput, UpdateStockLocationInput } from '@vendure/common/lib/generated-types';
+import { ID } from '@vendure/common/lib/shared-types';
+
+import { RequestContext } from '../../api/common/request-context';
+import { StockLocation } from '../../entity';
+import { VendureEntityEvent } from '../vendure-entity-event';
+
+type StockLocationInputTypes = CreateStockLocationInput | UpdateStockLocationInput | ID;
+
+/**
+ * @description
+ * This event is fired whenever a {@link StockLocation} is added, updated
+ * or deleted.
+ *
+ * @docsCategory events
+ * @docsPage Event Types
+ */
+export class StockLocationEvent extends VendureEntityEvent<StockLocation, StockLocationInputTypes> {
+    constructor(
+        ctx: RequestContext,
+        entity: StockLocation,
+        type: 'created' | 'updated' | 'deleted',
+        input?: StockLocationInputTypes,
+    ) {
+        super(entity, type, ctx, input);
+    }
+}

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

@@ -54,6 +54,7 @@ export * from './events/role-event';
 export * from './events/search-event';
 export * from './events/seller-event';
 export * from './events/shipping-method-event';
+export * from './events/stock-location-event';
 export * from './events/stock-movement-event';
 export * from './events/tax-category-event';
 export * from './events/tax-rate-event';

+ 8 - 0
packages/core/src/service/services/stock-location.service.ts

@@ -22,6 +22,7 @@ import { TransactionalConnection } from '../../connection/transactional-connecti
 import { OrderLine } from '../../entity/order-line/order-line.entity';
 import { StockLevel } from '../../entity/stock-level/stock-level.entity';
 import { StockLocation } from '../../entity/stock-location/stock-location.entity';
+import { EventBus, StockLocationEvent } from '../../event-bus/index';
 import { CustomFieldRelationService } from '../helpers/custom-field-relation/custom-field-relation.service';
 import { ListQueryBuilder } from '../helpers/list-query-builder/list-query-builder';
 import { RequestContextService } from '../helpers/request-context/request-context.service';
@@ -41,6 +42,7 @@ export class StockLocationService {
         private configService: ConfigService,
         private requestContextCache: RequestContextCacheService,
         private customFieldRelationService: CustomFieldRelationService,
+        private eventBus: EventBus,
     ) {}
 
     async initStockLocations() {
@@ -81,6 +83,7 @@ export class StockLocationService {
         );
         await this.channelService.assignToCurrentChannel(stockLocation, ctx);
         await this.connection.getRepository(ctx, StockLocation).save(stockLocation);
+        await this.eventBus.publish(new StockLocationEvent(ctx, stockLocation, 'created', input));
         return stockLocation;
     }
 
@@ -94,6 +97,7 @@ export class StockLocationService {
             input,
             updatedStockLocation,
         );
+        await this.eventBus.publish(new StockLocationEvent(ctx, updatedStockLocation, 'updated', input));
         return assertFound(this.findOne(ctx, updatedStockLocation.id));
     }
 
@@ -147,7 +151,11 @@ export class StockLocationService {
             }
         }
         try {
+            const deletedStockLocation = new StockLocation(stockLocation);
             await this.connection.getRepository(ctx, StockLocation).remove(stockLocation);
+            await this.eventBus.publish(
+                new StockLocationEvent(ctx, deletedStockLocation, 'deleted', input.id),
+            );
         } catch (e: any) {
             return {
                 result: DeletionResult.NOT_DELETED,