Skip to content

Commit

Permalink
feat(core): Add CustomerEvent and CustomerAddressEvent
Browse files Browse the repository at this point in the history
Co-authored-by: Elena Trubetskaia <[email protected]>
  • Loading branch information
eltrubetskaya and eltrubetskaia authored Mar 23, 2021
1 parent 3497e81 commit 480de31
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 0 deletions.
21 changes: 21 additions & 0 deletions packages/core/src/event-bus/events/customer-address-event.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 21 additions & 0 deletions packages/core/src/event-bus/events/customer-event.ts
Original file line number Diff line number Diff line change
@@ -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 changes: 2 additions & 0 deletions packages/core/src/event-bus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down
8 changes: 8 additions & 0 deletions packages/core/src/service/services/customer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -228,6 +230,7 @@ export class CustomerService {
},
});
}
this.eventBus.publish(new CustomerEvent(ctx, createdCustomer, 'created'));
return createdCustomer;
}

Expand Down Expand Up @@ -273,6 +276,7 @@ export class CustomerService {
input,
},
});
this.eventBus.publish(new CustomerEvent(ctx, customer, 'updated'));
return assertFound(this.findOne(ctx, customer.id));
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -599,6 +604,7 @@ export class CustomerService {
input,
},
});
this.eventBus.publish(new CustomerAddressEvent(ctx, updatedAddress, 'updated'));
return updatedAddress;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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,
};
Expand Down

0 comments on commit 480de31

Please sign in to comment.