diff --git a/packages/core/src/event-bus/events/customer-address-event.ts b/packages/core/src/event-bus/events/customer-address-event.ts new file mode 100644 index 0000000000..3acf89ef53 --- /dev/null +++ b/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(); + } +} diff --git a/packages/core/src/event-bus/events/customer-event.ts b/packages/core/src/event-bus/events/customer-event.ts new file mode 100644 index 0000000000..7c63de214d --- /dev/null +++ b/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(); + } +} diff --git a/packages/core/src/event-bus/index.ts b/packages/core/src/event-bus/index.ts index d99440dfa8..3693506c2e 100644 --- a/packages/core/src/event-bus/index.ts +++ b/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'; diff --git a/packages/core/src/service/services/customer.service.ts b/packages/core/src/service/services/customer.service.ts index ba7a8d9da0..99cb1f7d69 100644 --- a/packages/core/src/service/services/customer.service.ts +++ b/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, };