From a430339d542cdbf33ac4f5fe0e7bc64f36ef09e7 Mon Sep 17 00:00:00 2001 From: 420coupe <35115310+420coupe@users.noreply.github.com> Date: Wed, 28 Aug 2024 02:39:41 -0400 Subject: [PATCH] feat(notification-sendgrid): include ability to handle attachments (#8729) * feat(notification-sendgrid): include ability to handle attachments array if passed to dynamicTemplateData.attachments * docs: update sendgrid page.mdx include attachments documentation in example * ability to set from email, must be verified sender * docs: update to include optional from property * first-class optional vars for from & attachments * docs: update for optional first-class vars * Update www/apps/resources/app/architectural-modules/notification/sendgrid/page.mdx Co-authored-by: Shahed Nasser --------- Co-authored-by: Stevche Radevski Co-authored-by: Shahed Nasser --- .../core/types/src/notification/common.ts | 36 ++++++++++++++++ .../core/types/src/notification/provider.ts | 10 +++++ .../src/services/sendgrid.ts | 17 +++++++- .../notification/sendgrid/page.mdx | 42 ++++++++++++------- 4 files changed, 89 insertions(+), 16 deletions(-) diff --git a/packages/core/types/src/notification/common.ts b/packages/core/types/src/notification/common.ts index 480a155efe..251d1aad28 100644 --- a/packages/core/types/src/notification/common.ts +++ b/packages/core/types/src/notification/common.ts @@ -1,6 +1,34 @@ import { BaseFilterable } from "../dal" import { OperatorMap } from "../dal/utils" +/** + * @interface + * + * The structure for attachments in a notification. + */ +export interface Attachment { + /** + * The content of the attachment, encoded as a base64 string. + */ + content: string + /** + * The filename of the attachment. + */ + filename: string + /** + * The MIME type of the attachment. + */ + content_type?: string + /** + * The disposition of the attachment, e.g., "inline" or "attachment". + */ + disposition?: string + /** + * The ID, if the attachment is meant to be referenced within the body of the message. + */ + id?: string +} + /** * @interface * @@ -15,6 +43,14 @@ export interface NotificationDTO { * The recipient of the notification. It can be email, phone number, or username, depending on the channel. */ to: string + /** + * The sender of the notification. It can be email, phone number, or username, depending on the channel. + */ + from?: string | null + /** + * Optional attachments for the notification. + */ + attachments?: Attachment[] | null /** * The channel through which the notification is sent, such as 'email' or 'sms' */ diff --git a/packages/core/types/src/notification/provider.ts b/packages/core/types/src/notification/provider.ts index 89bf3c5ade..1ba8207c39 100644 --- a/packages/core/types/src/notification/provider.ts +++ b/packages/core/types/src/notification/provider.ts @@ -1,3 +1,5 @@ +import { Attachment } from "./common" + /** * @interface * @@ -8,6 +10,14 @@ export type ProviderSendNotificationDTO = { * The recipient of the notification. It can be email, phone number, or username, depending on the channel. */ to: string + /** + * The sender of the notification. It can be email, phone number, or username, depending on the channel. + */ + from?: string | null + /** + * Optional attachments for the notification. + */ + attachments?: Attachment[] | null /** * The channel through which the notification is sent, such as 'email' or 'sms' */ diff --git a/packages/modules/providers/notification-sendgrid/src/services/sendgrid.ts b/packages/modules/providers/notification-sendgrid/src/services/sendgrid.ts index ee526c852f..834a1723d7 100644 --- a/packages/modules/providers/notification-sendgrid/src/services/sendgrid.ts +++ b/packages/modules/providers/notification-sendgrid/src/services/sendgrid.ts @@ -46,13 +46,26 @@ export class SendgridNotificationService extends AbstractNotificationProviderSer ) } - const message = { + const attachments = Array.isArray(notification.attachments) + ? notification.attachments.map((attachment) => ({ + content: attachment.content, // Base64 encoded string of the file + filename: attachment.filename, + content_type: attachment.content_type, // MIME type (e.g., 'application/pdf') + disposition: attachment.disposition ?? "attachment", // Default to 'attachment' + id: attachment.id ?? undefined, // Optional: unique identifier for inline attachments + })) + : undefined + + const from = notification.from?.trim() || this.config_.from + + const message: sendgrid.MailDataRequired = { to: notification.to, - from: this.config_.from, + from: from, templateId: notification.template, dynamicTemplateData: notification.data as | { [key: string]: any } | undefined, + attachments: attachments, } try { diff --git a/www/apps/resources/app/architectural-modules/notification/sendgrid/page.mdx b/www/apps/resources/app/architectural-modules/notification/sendgrid/page.mdx index 48887e505e..da8030f1f8 100644 --- a/www/apps/resources/app/architectural-modules/notification/sendgrid/page.mdx +++ b/www/apps/resources/app/architectural-modules/notification/sendgrid/page.mdx @@ -12,20 +12,22 @@ The SendGrid Notification Module Provider integrates [SendGrid](https://sendgrid ## Install the SendGrid Notification Module - + To install the SendGrid Notification Module Provider, run the following command in the directory of your Medusa application: @@ -154,9 +156,19 @@ export default async function productCreateHandler({ await notificationModuleService.createNotifications({ to: "test@gmail.com", + from: "test@medusajs.com", // Optional var, verified sender required channel: "email", template: "product-created", data, + attachments: [ // optional var + { + content: base64, + content_type: MIME type, + filename: filename.ext, + disposition: "attachment or inline attachment", + id: "id", // only needed for inline attachment + }, + ] }) } @@ -172,6 +184,8 @@ In this subscriber: - By specifying the `email` channel, the SendGrid Notification Module Provider is used to send the notification. - The `template` property of the `create` method's parameter specifies the ID of the template defined in SendGrid. - The `data` property allows you to pass data to the template in SendGrid. +- The `attachments` optional property allows you to pass attachments to the template in SendGrid. +- The `from` optional property allows you to pass a single sender-verified email. If not provided, the value of the `from` configuration of the module is used. Then, start the Medusa application: