Browse Source

feat(core): Emit event when assigning/removing Customer to/from group

Michael Bromley 5 years ago
parent
commit
66763357de

+ 23 - 0
packages/core/src/event-bus/events/customer-group-event.ts

@@ -0,0 +1,23 @@
+import { RequestContext } from '../../api/common/request-context';
+import { CustomerGroup } from '../../entity/customer-group/customer-group.entity';
+import { Customer } from '../../entity/customer/customer.entity';
+import { VendureEvent } from '../vendure-event';
+
+/**
+ * @description
+ * This event is fired whenever one or more {@link Customer} is assigned to or removed from a
+ * {@link CustomerGroup}.
+ *
+ * @docsCategory events
+ * @docsPage Event Types
+ */
+export class CustomerGroupEvent extends VendureEvent {
+    constructor(
+        public ctx: RequestContext,
+        public customers: Customer[],
+        public customGroup: CustomerGroup,
+        public type: 'assigned' | 'removed',
+    ) {
+        super();
+    }
+}

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

@@ -6,6 +6,7 @@ export * from './events/account-registration-event';
 export * from './events/asset-event';
 export * from './events/attempted-login-event';
 export * from './events/collection-modification-event';
+export * from './events/customer-group-event';
 export * from './events/fulfillment-state-transition-event';
 export * from './events/identifier-change-event';
 export * from './events/identifier-change-request-event';

+ 5 - 0
packages/core/src/service/services/customer-group.service.ts

@@ -17,6 +17,8 @@ import { UserInputError } from '../../common/error/errors';
 import { assertFound, idsAreEqual } from '../../common/utils';
 import { CustomerGroup } from '../../entity/customer-group/customer-group.entity';
 import { Customer } from '../../entity/customer/customer.entity';
+import { EventBus } from '../../event-bus/event-bus';
+import { CustomerGroupEvent } from '../../event-bus/events/customer-group-event';
 import { ListQueryBuilder } from '../helpers/list-query-builder/list-query-builder';
 import { patchEntity } from '../helpers/utils/patch-entity';
 import { TransactionalConnection } from '../transaction/transactional-connection';
@@ -29,6 +31,7 @@ export class CustomerGroupService {
         private connection: TransactionalConnection,
         private listQueryBuilder: ListQueryBuilder,
         private historyService: HistoryService,
+        private eventBus: EventBus,
     ) {}
 
     findAll(ctx: RequestContext, options?: CustomerGroupListOptions): Promise<PaginatedList<CustomerGroup>> {
@@ -122,6 +125,7 @@ export class CustomerGroupService {
         }
 
         await this.connection.getRepository(ctx, Customer).save(customers, { reload: false });
+        this.eventBus.publish(new CustomerGroupEvent(ctx, customers, group, 'assigned'));
         return assertFound(this.findOne(ctx, group.id));
     }
 
@@ -146,6 +150,7 @@ export class CustomerGroupService {
             });
         }
         await this.connection.getRepository(ctx, Customer).save(customers, { reload: false });
+        this.eventBus.publish(new CustomerGroupEvent(ctx, customers, group, 'removed'));
         return assertFound(this.findOne(ctx, group.id));
     }