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

feat(email-plugin): Allow specify metadata for EmailSendEvent #2963

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions packages/email-plugin/src/email-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}
}
Expand Down
3 changes: 2 additions & 1 deletion packages/email-plugin/src/email-send-event.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { RequestContext, VendureEvent } from '@vendure/core';

import { EmailDetails } from './types';
import { EmailDetails, EmailMetadata } from './types';

/**
* @description
Expand All @@ -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();
}
Expand Down
17 changes: 17 additions & 0 deletions packages/email-plugin/src/handler/event-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
IntermediateEmailDetails,
LoadDataFn,
SetAttachmentsFn,
SetMetadataFn,
SetOptionalAddressFieldsFn,
SetSubjectFn,
SetTemplateVarsFn,
Expand Down Expand Up @@ -140,6 +141,7 @@ export class EmailEventHandler<T extends string = string, Event extends EventWit
private setTemplateVarsFn: SetTemplateVarsFn<Event>;
private setAttachmentsFn?: SetAttachmentsFn<Event>;
private setOptionalAddressFieldsFn?: SetOptionalAddressFieldsFn<Event>;
private setMetadataFn?: SetMetadataFn<Event>;
private filterFns: Array<(event: Event) => boolean> = [];
private configurations: EmailTemplateConfig[] = [];
private defaultSubject: string;
Expand Down Expand Up @@ -246,6 +248,17 @@ export class EmailEventHandler<T extends string = string, Event extends EventWit
return this;
}

/**
* @description
* A function which allows {@link EmailMetadata} to be specified for the email.
*
* @since 3.1.0
*/
setMetadata(optionalSetMetadataFn: SetMetadataFn<Event>) {
this.setMetadataFn = optionalSetMetadataFn;
return this;
}

/**
* @description
* Defines one or more files to be attached to the email. An attachment can be specified
Expand Down Expand Up @@ -322,6 +335,7 @@ export class EmailEventHandler<T extends string = string, Event extends EventWit
asyncHandler.setTemplateVarsFn = this.setTemplateVarsFn;
asyncHandler.setAttachmentsFn = this.setAttachmentsFn;
asyncHandler.setOptionalAddressFieldsFn = this.setOptionalAddressFieldsFn;
asyncHandler.setMetadataFn = this.setMetadataFn;
asyncHandler.filterFns = this.filterFns;
asyncHandler.configurations = this.configurations;
asyncHandler.defaultSubject = this.defaultSubject;
Expand Down Expand Up @@ -397,6 +411,8 @@ export class EmailEventHandler<T extends string = string, Event extends EventWit
}
const attachments = await serializeAttachments(attachmentsArray);
const optionalAddressFields = (await this.setOptionalAddressFieldsFn?.(event)) ?? {};
const metadata = this.setMetadataFn ? await this.setMetadataFn(event) : {};

return {
ctx: event.ctx.serialize(),
type: this.type,
Expand All @@ -406,6 +422,7 @@ export class EmailEventHandler<T extends string = string, Event extends EventWit
subject,
templateFile: configuration ? configuration.templateFile : 'body.hbs',
attachments,
metadata,
...optionalAddressFields,
};
}
Expand Down
14 changes: 14 additions & 0 deletions packages/email-plugin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ export type IntermediateEmailDetails = {
cc?: string;
bcc?: string;
replyTo?: string;
metadata?: EmailMetadata;
};

/**
Expand Down Expand Up @@ -475,3 +476,16 @@ export interface OptionalAddressFields {
export type SetOptionalAddressFieldsFn<Event> = (
event: Event,
) => OptionalAddressFields | Promise<OptionalAddressFields>;

/**
* @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: Event) => EmailMetadata | Promise<EmailMetadata>;

export type EmailMetadata = Record<string, any>;
Loading