Skip to content

Commit ac0baf9

Browse files
authored
feat(email-plugin): Allow specifying metadata for EmailSendEvent (#2963)
1 parent 620eeb1 commit ac0baf9

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

packages/email-plugin/src/email-processor.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,9 @@ export class EmailProcessor {
7575
};
7676
const transportSettings = await this.getTransportSettings(ctx);
7777
await this.emailSender.send(emailDetails, transportSettings);
78-
await this.eventBus.publish(new EmailSendEvent(ctx, emailDetails, true));
78+
await this.eventBus.publish(
79+
new EmailSendEvent(ctx, emailDetails, true, undefined, data.metadata),
80+
);
7981
return true;
8082
} catch (err: unknown) {
8183
if (err instanceof Error) {
@@ -84,7 +86,9 @@ export class EmailProcessor {
8486
Logger.error(String(err), loggerCtx);
8587
}
8688

87-
await this.eventBus.publish(new EmailSendEvent(ctx, emailDetails, false, err as Error));
89+
await this.eventBus.publish(
90+
new EmailSendEvent(ctx, emailDetails, false, err as Error, data.metadata),
91+
);
8892
throw err;
8993
}
9094
}

packages/email-plugin/src/email-send-event.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { RequestContext, VendureEvent } from '@vendure/core';
22

3-
import { EmailDetails } from './types';
3+
import { EmailDetails, EmailMetadata } from './types';
44

55
/**
66
* @description
@@ -17,6 +17,7 @@ export class EmailSendEvent extends VendureEvent {
1717
public readonly details: EmailDetails,
1818
public readonly success: boolean,
1919
public readonly error?: Error,
20+
public readonly metadata?: EmailMetadata,
2021
) {
2122
super();
2223
}

packages/email-plugin/src/handler/event-handler.ts

+17
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
IntermediateEmailDetails,
1414
LoadDataFn,
1515
SetAttachmentsFn,
16+
SetMetadataFn,
1617
SetOptionalAddressFieldsFn,
1718
SetSubjectFn,
1819
SetTemplateVarsFn,
@@ -140,6 +141,7 @@ export class EmailEventHandler<T extends string = string, Event extends EventWit
140141
private setTemplateVarsFn: SetTemplateVarsFn<Event>;
141142
private setAttachmentsFn?: SetAttachmentsFn<Event>;
142143
private setOptionalAddressFieldsFn?: SetOptionalAddressFieldsFn<Event>;
144+
private setMetadataFn?: SetMetadataFn<Event>;
143145
private filterFns: Array<(event: Event) => boolean> = [];
144146
private configurations: EmailTemplateConfig[] = [];
145147
private defaultSubject: string;
@@ -246,6 +248,17 @@ export class EmailEventHandler<T extends string = string, Event extends EventWit
246248
return this;
247249
}
248250

251+
/**
252+
* @description
253+
* A function which allows {@link EmailMetadata} to be specified for the email.
254+
*
255+
* @since 3.1.0
256+
*/
257+
setMetadata(optionalSetMetadataFn: SetMetadataFn<Event>) {
258+
this.setMetadataFn = optionalSetMetadataFn;
259+
return this;
260+
}
261+
249262
/**
250263
* @description
251264
* Defines one or more files to be attached to the email. An attachment can be specified
@@ -322,6 +335,7 @@ export class EmailEventHandler<T extends string = string, Event extends EventWit
322335
asyncHandler.setTemplateVarsFn = this.setTemplateVarsFn;
323336
asyncHandler.setAttachmentsFn = this.setAttachmentsFn;
324337
asyncHandler.setOptionalAddressFieldsFn = this.setOptionalAddressFieldsFn;
338+
asyncHandler.setMetadataFn = this.setMetadataFn;
325339
asyncHandler.filterFns = this.filterFns;
326340
asyncHandler.configurations = this.configurations;
327341
asyncHandler.defaultSubject = this.defaultSubject;
@@ -397,6 +411,8 @@ export class EmailEventHandler<T extends string = string, Event extends EventWit
397411
}
398412
const attachments = await serializeAttachments(attachmentsArray);
399413
const optionalAddressFields = (await this.setOptionalAddressFieldsFn?.(event)) ?? {};
414+
const metadata = this.setMetadataFn ? await this.setMetadataFn(event) : {};
415+
400416
return {
401417
ctx: event.ctx.serialize(),
402418
type: this.type,
@@ -406,6 +422,7 @@ export class EmailEventHandler<T extends string = string, Event extends EventWit
406422
subject,
407423
templateFile: configuration ? configuration.templateFile : 'body.hbs',
408424
attachments,
425+
metadata,
409426
...optionalAddressFields,
410427
};
411428
}

packages/email-plugin/src/types.ts

+14
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,7 @@ export type IntermediateEmailDetails = {
356356
cc?: string;
357357
bcc?: string;
358358
replyTo?: string;
359+
metadata?: EmailMetadata;
359360
};
360361

361362
/**
@@ -475,3 +476,16 @@ export interface OptionalAddressFields {
475476
export type SetOptionalAddressFieldsFn<Event> = (
476477
event: Event,
477478
) => OptionalAddressFields | Promise<OptionalAddressFields>;
479+
480+
/**
481+
* @description
482+
* A function used to set the {@link EmailMetadata}.
483+
*
484+
* @since 3.1.0
485+
* @docsCategory core plugins/EmailPlugin
486+
* @docsPage Email Plugin Types
487+
*
488+
*/
489+
export type SetMetadataFn<Event> = (event: Event) => EmailMetadata | Promise<EmailMetadata>;
490+
491+
export type EmailMetadata = Record<string, any>;

0 commit comments

Comments
 (0)