From ac0baf9b5eed141a3c76b81e056b11f4a31726df Mon Sep 17 00:00:00 2001 From: Andrii Baran <59182007+Anddrrew@users.noreply.github.com> Date: Mon, 22 Jul 2024 11:34:56 +0300 Subject: [PATCH] feat(email-plugin): Allow specifying metadata for EmailSendEvent (#2963) --- packages/email-plugin/src/email-processor.ts | 8 ++++++-- packages/email-plugin/src/email-send-event.ts | 3 ++- .../email-plugin/src/handler/event-handler.ts | 17 +++++++++++++++++ packages/email-plugin/src/types.ts | 14 ++++++++++++++ 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/packages/email-plugin/src/email-processor.ts b/packages/email-plugin/src/email-processor.ts index 277c0c3c9f..686b50edbf 100644 --- a/packages/email-plugin/src/email-processor.ts +++ b/packages/email-plugin/src/email-processor.ts @@ -75,7 +75,9 @@ export class EmailProcessor { }; const transportSettings = await this.getTransportSettings(ctx); await this.emailSender.send(emailDetails, transportSettings); - await this.eventBus.publish(new EmailSendEvent(ctx, emailDetails, true)); + await this.eventBus.publish( + new EmailSendEvent(ctx, emailDetails, true, undefined, data.metadata), + ); return true; } catch (err: unknown) { if (err instanceof Error) { @@ -84,7 +86,9 @@ export class EmailProcessor { Logger.error(String(err), loggerCtx); } - await this.eventBus.publish(new EmailSendEvent(ctx, emailDetails, false, err as Error)); + await this.eventBus.publish( + new EmailSendEvent(ctx, emailDetails, false, err as Error, data.metadata), + ); throw err; } } diff --git a/packages/email-plugin/src/email-send-event.ts b/packages/email-plugin/src/email-send-event.ts index fe35beabb9..81a2de9768 100644 --- a/packages/email-plugin/src/email-send-event.ts +++ b/packages/email-plugin/src/email-send-event.ts @@ -1,6 +1,6 @@ import { RequestContext, VendureEvent } from '@vendure/core'; -import { EmailDetails } from './types'; +import { EmailDetails, EmailMetadata } from './types'; /** * @description @@ -17,6 +17,7 @@ export class EmailSendEvent extends VendureEvent { public readonly details: EmailDetails, public readonly success: boolean, public readonly error?: Error, + public readonly metadata?: EmailMetadata, ) { super(); } diff --git a/packages/email-plugin/src/handler/event-handler.ts b/packages/email-plugin/src/handler/event-handler.ts index 4764f0c153..7a19276503 100644 --- a/packages/email-plugin/src/handler/event-handler.ts +++ b/packages/email-plugin/src/handler/event-handler.ts @@ -13,6 +13,7 @@ import { IntermediateEmailDetails, LoadDataFn, SetAttachmentsFn, + SetMetadataFn, SetOptionalAddressFieldsFn, SetSubjectFn, SetTemplateVarsFn, @@ -140,6 +141,7 @@ export class EmailEventHandler; private setAttachmentsFn?: SetAttachmentsFn; private setOptionalAddressFieldsFn?: SetOptionalAddressFieldsFn; + private setMetadataFn?: SetMetadataFn; private filterFns: Array<(event: Event) => boolean> = []; private configurations: EmailTemplateConfig[] = []; private defaultSubject: string; @@ -246,6 +248,17 @@ export class EmailEventHandler) { + this.setMetadataFn = optionalSetMetadataFn; + return this; + } + /** * @description * Defines one or more files to be attached to the email. An attachment can be specified @@ -322,6 +335,7 @@ export class EmailEventHandler = ( event: Event, ) => OptionalAddressFields | Promise; + +/** + * @description + * A function used to set the {@link EmailMetadata}. + * + * @since 3.1.0 + * @docsCategory core plugins/EmailPlugin + * @docsPage Email Plugin Types + * + */ +export type SetMetadataFn = (event: Event) => EmailMetadata | Promise; + +export type EmailMetadata = Record;