Skip to content

Commit

Permalink
feat(core): Implemented CustomerAddressEvent with new entity event ba…
Browse files Browse the repository at this point in the history
  • Loading branch information
Kevin committed Nov 15, 2021
1 parent b09b966 commit 469c6ac
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 14 deletions.
30 changes: 23 additions & 7 deletions packages/core/src/event-bus/events/customer-address-event.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,37 @@
import { RequestContext } from '../../api/common/request-context';
import { Address } from '../../entity/address/address.entity';
import { VendureEvent } from '../vendure-event';
import { CreateAddressInput, UpdateAddressInput } from '@vendure/common/lib/generated-types';
import { ID } from '@vendure/common/src/shared-types';

import { RequestContext } from '../../api';
import { Address } from '../../entity';
import { VendureEntityEvent } from '../vendure-entity-event';

type CustomerAddressInputTypes = CreateAddressInput | UpdateAddressInput | ID;

/**
* @description
* This event is fired whenever a {@link Customer} is added, updated
* This event is fired whenever a {@link Address} is added, updated
* or deleted.
*
* @docsCategory events
* @docsPage Event Types
* @since 1.4
*/
export class CustomerAddressEvent extends VendureEvent {
export class CustomerAddressEvent extends VendureEntityEvent<Address, CustomerAddressInputTypes> {
constructor(
public ctx: RequestContext,
public address: Address,
public entity: Address,
public type: 'created' | 'updated' | 'deleted',
public input?: CustomerAddressInputTypes,
) {
super();
super(entity, type, ctx);
}

/**
* Return an address field to become compatible with the
* deprecated old version of CustomerAddressEvent
* @deprecated Use `entity` instead
*/
get address(): Address {
return this.entity;
}
}
14 changes: 7 additions & 7 deletions packages/core/src/service/services/customer.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -668,15 +668,15 @@ export class CustomerService {
type: HistoryEntryType.CUSTOMER_ADDRESS_CREATED,
data: { address: addressToLine(createdAddress) },
});
this.eventBus.publish(new CustomerAddressEvent(ctx, createdAddress, 'created'));
this.eventBus.publish(new CustomerAddressEvent(ctx, createdAddress, 'created', input));
return createdAddress;
}

async updateAddress(ctx: RequestContext, input: UpdateAddressInput): Promise<Address> {
const address = await this.connection.getEntityOrThrow(ctx, Address, input.id, {
const address = await this.connection.getEntityOrThrow<Address>(ctx, Address, input.id, {
relations: ['customer', 'country'],
});
const customer = await this.connection.findOneInChannel(
const customer = await this.connection.findOneInChannel<Customer>(
ctx,
Customer,
address.customer.id,
Expand Down Expand Up @@ -704,15 +704,15 @@ export class CustomerService {
input,
},
});
this.eventBus.publish(new CustomerAddressEvent(ctx, updatedAddress, 'updated'));
this.eventBus.publish(new CustomerAddressEvent(ctx, updatedAddress, 'updated', input));
return updatedAddress;
}

async deleteAddress(ctx: RequestContext, id: ID): Promise<boolean> {
const address = await this.connection.getEntityOrThrow(ctx, Address, id, {
const address = await this.connection.getEntityOrThrow<Address>(ctx, Address, id, {
relations: ['customer', 'country'],
});
const customer = await this.connection.findOneInChannel(
const customer = await this.connection.findOneInChannel<Customer>(
ctx,
Customer,
address.customer.id,
Expand All @@ -732,7 +732,7 @@ export class CustomerService {
},
});
await this.connection.getRepository(ctx, Address).remove(address);
this.eventBus.publish(new CustomerAddressEvent(ctx, address, 'deleted'));
this.eventBus.publish(new CustomerAddressEvent(ctx, address, 'deleted', id));
return true;
}

Expand Down

0 comments on commit 469c6ac

Please sign in to comment.