Browse Source

feat(core): Add CustomerEvent and CustomerAddressEvent

Co-authored-by: Elena Trubetskaia <elena.trubetskaia@ekreative.com>
Elena Trubetskaia 4 years ago
parent
commit
480de31ba7

+ 21 - 0
packages/core/src/event-bus/events/customer-address-event.ts

@@ -0,0 +1,21 @@
+import { RequestContext } from '../../api/common/request-context';
+import { Address } from '../../entity/address/address.entity';
+import { VendureEvent } from '../vendure-event';
+
+/**
+ * @description
+ * This event is fired whenever a {@link Customer} is added, updated
+ * or deleted.
+ *
+ * @docsCategory events
+ * @docsPage Event Types
+ */
+export class CustomerAddressEvent extends VendureEvent {
+    constructor(
+        public ctx: RequestContext,
+        public address: Address,
+        public type: 'created' | 'updated' | 'deleted',
+    ) {
+        super();
+    }
+}

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

@@ -0,0 +1,21 @@
+import { RequestContext } from '../../api/common/request-context';
+import { Customer } from '../../entity/customer/customer.entity';
+import { VendureEvent } from '../vendure-event';
+
+/**
+ * @description
+ * This event is fired whenever a {@link Customer} is added, updated
+ * or deleted.
+ *
+ * @docsCategory events
+ * @docsPage Event Types
+ */
+export class CustomerEvent extends VendureEvent {
+    constructor(
+        public ctx: RequestContext,
+        public customer: Customer,
+        public type: 'created' | 'updated' | 'deleted',
+    ) {
+        super();
+    }
+}

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

@@ -7,6 +7,8 @@ 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/customer-event';
+export * from './events/customer-address-event';
 export * from './events/fulfillment-state-transition-event';
 export * from './events/identifier-change-event';
 export * from './events/identifier-change-request-event';

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

@@ -45,6 +45,8 @@ import { HistoryEntry } from '../../entity/history-entry/history-entry.entity';
 import { User } from '../../entity/user/user.entity';
 import { EventBus } from '../../event-bus/event-bus';
 import { AccountRegistrationEvent } from '../../event-bus/events/account-registration-event';
+import { CustomerAddressEvent } from '../../event-bus/events/customer-address-event';
+import { CustomerEvent } from '../../event-bus/events/customer-event';
 import { IdentifierChangeEvent } from '../../event-bus/events/identifier-change-event';
 import { IdentifierChangeRequestEvent } from '../../event-bus/events/identifier-change-request-event';
 import { PasswordResetEvent } from '../../event-bus/events/password-reset-event';
@@ -228,6 +230,7 @@ export class CustomerService {
                 },
             });
         }
+        this.eventBus.publish(new CustomerEvent(ctx, createdCustomer, 'created'));
         return createdCustomer;
     }
 
@@ -273,6 +276,7 @@ export class CustomerService {
                 input,
             },
         });
+        this.eventBus.publish(new CustomerEvent(ctx, customer, 'updated'));
         return assertFound(this.findOne(ctx, customer.id));
     }
 
@@ -564,6 +568,7 @@ export class CustomerService {
             type: HistoryEntryType.CUSTOMER_ADDRESS_CREATED,
             data: { address: addressToLine(createdAddress) },
         });
+        this.eventBus.publish(new CustomerAddressEvent(ctx, createdAddress, 'created'));
         return createdAddress;
     }
 
@@ -599,6 +604,7 @@ export class CustomerService {
                 input,
             },
         });
+        this.eventBus.publish(new CustomerAddressEvent(ctx, updatedAddress, 'updated'));
         return updatedAddress;
     }
 
@@ -626,6 +632,7 @@ export class CustomerService {
             },
         });
         await this.connection.getRepository(ctx, Address).remove(address);
+        this.eventBus.publish(new CustomerAddressEvent(ctx, address, 'deleted'));
         return true;
     }
 
@@ -638,6 +645,7 @@ export class CustomerService {
             .update({ id: customerId }, { deletedAt: new Date() });
         // tslint:disable-next-line:no-non-null-assertion
         await this.userService.softDelete(ctx, customer.user!.id);
+        this.eventBus.publish(new CustomerEvent(ctx, customer, 'deleted'));
         return {
             result: DeletionResult.DELETED,
         };