Skip to content

Commit

Permalink
fix(email-plugin): Only call loadData() function after filters run
Browse files Browse the repository at this point in the history
Fixes #518
  • Loading branch information
michaelbromley committed Oct 21, 2020
1 parent ef99c22 commit e22db7e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 8 deletions.
8 changes: 8 additions & 0 deletions packages/email-plugin/src/event-handler.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { LanguageCode } from '@vendure/common/lib/generated-types';
import { Type } from '@vendure/common/lib/shared-types';
import { Injector } from '@vendure/core';

import { EmailEventListener, EmailTemplateConfig, SetTemplateVarsFn } from './event-listener';
import { EventWithAsyncData, EventWithContext, IntermediateEmailDetails, LoadDataFn } from './types';
Expand Down Expand Up @@ -169,12 +170,19 @@ export class EmailEventHandler<T extends string = string, Event extends EventWit
async handle(
event: Event,
globals: { [key: string]: any } = {},
injector: Injector,
): Promise<IntermediateEmailDetails | undefined> {
for (const filterFn of this.filterFns) {
if (!filterFn(event)) {
return;
}
}
if (this instanceof EmailEventHandlerWithAsyncData) {
(event as EventWithAsyncData<Event, any>).data = await this._loadDataFn({
event,
injector,
});
}
if (!this.setRecipientFn) {
throw new Error(
`No setRecipientFn has been defined. ` +
Expand Down
18 changes: 18 additions & 0 deletions packages/email-plugin/src/plugin.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,24 @@ describe('EmailPlugin', () => {
expect(onSend.mock.calls[0][0].from).toBe('"test from" <[email protected]>');
expect(onSend.mock.calls[0][0].recipient).toBe('[email protected]');
});

it('only executes for filtered events', async () => {
let callCount = 0;
const handler = new EmailEventListener('test')
.on(MockEvent)
.filter(event => event.shouldSend === true)
.loadData(async ({ injector }) => {
callCount++;
});

await initPluginWithHandlers([handler]);

eventBus.publish(new MockEvent(RequestContext.empty(), false));
eventBus.publish(new MockEvent(RequestContext.empty(), true));
await pause();

expect(callCount).toBe(1);
});
});

describe('orderConfirmationHandler', () => {
Expand Down
14 changes: 6 additions & 8 deletions packages/email-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,14 +233,12 @@ export class EmailPlugin implements OnVendureBootstrap, OnVendureClose {
Logger.debug(`Handling event "${handler.type}"`, 'EmailPlugin');
const { type } = handler;
try {
if (handler instanceof EmailEventHandlerWithAsyncData) {
const injector = new Injector(this.moduleRef);
(event as EventWithAsyncData<EventWithContext, any>).data = await handler._loadDataFn({
event,
injector,
});
}
const result = await handler.handle(event as any, EmailPlugin.options.globalTemplateVars);
const injector = new Injector(this.moduleRef);
const result = await handler.handle(
event as any,
EmailPlugin.options.globalTemplateVars,
injector,
);
if (!result) {
return;
}
Expand Down

0 comments on commit e22db7e

Please sign in to comment.