Skip to content

Commit

Permalink
feat(core): Add RefundEvent & publish on Refund creation (#2832)
Browse files Browse the repository at this point in the history
Closes #2830
  • Loading branch information
vjung28 authored May 31, 2024
1 parent 213a26b commit cb08427
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
21 changes: 21 additions & 0 deletions packages/core/src/event-bus/events/refund-event.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { RequestContext } from '../../api/common/request-context';
import { Order, Refund } from '../../entity';
import { VendureEvent } from '../vendure-event';

/**
* @description
* This event is fired whenever a {@link Refund} is created
*
* @docsCategory events
* @docsPage Event Types
*/
export class RefundEvent extends VendureEvent {
constructor(
public ctx: RequestContext,
public order: Order,
public refund: Refund,
public type: 'created',
) {
super();
}
}
1 change: 1 addition & 0 deletions packages/core/src/event-bus/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ export * from './events/product-variant-channel-event';
export * from './events/product-variant-event';
export * from './events/product-variant-price-event';
export * from './events/promotion-event';
export * from './events/refund-event';
export * from './events/refund-state-transition-event';
export * from './events/role-change-event';
export * from './events/role-event';
Expand Down
8 changes: 7 additions & 1 deletion packages/core/src/service/services/order.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ import { CouponCodeEvent } from '../../event-bus/events/coupon-code-event';
import { OrderEvent } from '../../event-bus/events/order-event';
import { OrderLineEvent } from '../../event-bus/events/order-line-event';
import { OrderStateTransitionEvent } from '../../event-bus/events/order-state-transition-event';
import { RefundEvent } from '../../event-bus/events/refund-event';
import { RefundStateTransitionEvent } from '../../event-bus/events/refund-state-transition-event';
import { CustomFieldRelationService } from '../helpers/custom-field-relation/custom-field-relation.service';
import { FulfillmentState } from '../helpers/fulfillment-state-machine/fulfillment-state';
Expand Down Expand Up @@ -1419,7 +1420,12 @@ export class OrderService {
return new RefundOrderStateError({ orderState: order.state });
}

return await this.paymentService.createRefund(ctx, input, order, payment);
const createdRefund = await this.paymentService.createRefund(ctx, input, order, payment);

if (createdRefund instanceof Refund) {
await this.eventBus.publish(new RefundEvent(ctx, order, createdRefund, 'created'));
}
return createdRefund;
}

/**
Expand Down

0 comments on commit cb08427

Please sign in to comment.