Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Vendure entity events #1222

Merged
merged 8 commits into from
Nov 23, 2021
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';
Draykee marked this conversation as resolved.
Show resolved Hide resolved
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;
}
}
29 changes: 29 additions & 0 deletions packages/core/src/event-bus/vendure-entity-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { RequestContext } from '../api';

import { VendureEvent } from './vendure-event';

/**
* @description
* The base class for all entity events used by the EventBus system.
*
* @docsCategory events
* */
export abstract class VendureEntityEvent<Entity, Input = any> extends VendureEvent {
public readonly entity: Entity;
public readonly type: 'created' | 'updated' | 'deleted';
public readonly ctx?: RequestContext;
public readonly input?: Input;

protected constructor(
entity: Entity,
type: 'created' | 'updated' | 'deleted',
ctx?: RequestContext,
input?: Input,
) {
super();
this.entity = entity;
this.type = type;
this.ctx = ctx;
this.input = input;
}
}
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, {
Draykee marked this conversation as resolved.
Show resolved Hide resolved
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