From dfb190435ee3d3561cb469f8c08e84539dab27d0 Mon Sep 17 00:00:00 2001 From: Daveed Date: Wed, 22 Jan 2025 21:37:45 -0500 Subject: [PATCH] Created Better Noticiation email structure, skeleton RSVP confirmation --- src/api/markket/services/notification.ts | 43 ---- .../markket/services/notification/index.ts | 65 +++++ .../markket/services/notification/template.ts | 231 ++++++++++++++++++ src/api/rsvp/middlewares/notification.ts | 8 + src/api/rsvp/routes/rsvp.ts | 13 +- 5 files changed, 316 insertions(+), 44 deletions(-) delete mode 100644 src/api/markket/services/notification.ts create mode 100644 src/api/markket/services/notification/index.ts create mode 100644 src/api/markket/services/notification/template.ts create mode 100644 src/api/rsvp/middlewares/notification.ts diff --git a/src/api/markket/services/notification.ts b/src/api/markket/services/notification.ts deleted file mode 100644 index fc18413..0000000 --- a/src/api/markket/services/notification.ts +++ /dev/null @@ -1,43 +0,0 @@ -const SENDGRID_FROM_EMAIL = process.env.SENDGRID_FROM_EMAIL || ''; -const SENDGRID_REPLY_TO_EMAIL = process.env.SENDGRID_REPLY_TO_EMAIL || ''; - -const OrderNotificationHTml = (order: any) => { - //

Order Amount: ${((order?.data?.object?.total_amount || 0) / 100)}

- return ` -

Order Confirmation

-

Thank you for your order!

-

Order ID: ${order?.data?.object?.id}

-

- - View Receipt - -

- `; -}; - -export const sendOrderNotification = async ({ strapi, order }) => { - console.info('notification::stripe:checkout.session.completed', { - order, - strapi: !!strapi - }); - - if (!SENDGRID_FROM_EMAIL || !SENDGRID_REPLY_TO_EMAIL) { - return; - } - - if (!order?.data?.object?.customer_details?.email) { - return; - } - - const customer = order?.data?.object?.customer_details; - - return await strapi.plugins['email'].services.email.send({ - to: customer.email, - from: SENDGRID_FROM_EMAIL, //e.g. single sender verification in SendGrid - cc: SENDGRID_REPLY_TO_EMAIL, - replyTo: SENDGRID_REPLY_TO_EMAIL, - subject: 'Markkët: Order Confirmation', - text: 'Thank you for your order!', - html: OrderNotificationHTml(order), - }); -}; diff --git a/src/api/markket/services/notification/index.ts b/src/api/markket/services/notification/index.ts new file mode 100644 index 0000000..a025fdb --- /dev/null +++ b/src/api/markket/services/notification/index.ts @@ -0,0 +1,65 @@ +const SENDGRID_FROM_EMAIL = process.env.SENDGRID_FROM_EMAIL || ''; +const SENDGRID_REPLY_TO_EMAIL = process.env.SENDGRID_REPLY_TO_EMAIL || ''; +import { OrderNotificationHTml, RSVPNotificationHTml } from './template'; + + +export const sendRSVPNotification = async ({ strapi, rsvp, event }) => { + console.info('notification::rsvp:created', { + rsvp: !!rsvp, + event: !!event, + strapi: !!strapi, + from: !!SENDGRID_FROM_EMAIL, + reply_to: !!SENDGRID_REPLY_TO_EMAIL, + }); + + if (!SENDGRID_FROM_EMAIL || !SENDGRID_REPLY_TO_EMAIL) { + return; + } + + // if (!order?.data?.object?.customer_details?.email) { + // return; + // } + + // const customer = event?.data?.object?.customer_details; + const customer = { email: rsvp.email }; + + return await strapi.plugins['email'].services.email.send({ + to: customer.email, + from: SENDGRID_FROM_EMAIL, //e.g. single sender verification in SendGrid + cc: SENDGRID_REPLY_TO_EMAIL, + replyTo: SENDGRID_REPLY_TO_EMAIL, + subject: 'Markkët: RSVP Confirmation', + text: 'RSVP confirmation!', + html: RSVPNotificationHTml(event), + }); +}; + + +export const sendOrderNotification = async ({ strapi, order }) => { + console.info('notification::stripe:checkout.session.completed', { + order, + strapi: !!strapi, + from: !!SENDGRID_FROM_EMAIL, + reply_to: !!SENDGRID_REPLY_TO_EMAIL, + }); + + if (!SENDGRID_FROM_EMAIL || !SENDGRID_REPLY_TO_EMAIL) { + return; + } + + if (!order?.data?.object?.customer_details?.email) { + return; + } + + const customer = order?.data?.object?.customer_details; + + return await strapi.plugins['email'].services.email.send({ + to: customer.email, + from: SENDGRID_FROM_EMAIL, //e.g. single sender verification in SendGrid + cc: SENDGRID_REPLY_TO_EMAIL, + replyTo: SENDGRID_REPLY_TO_EMAIL, + subject: 'Markkët: Order Confirmation', + text: 'Thank you for your order!', + html: OrderNotificationHTml(order), + }); +}; diff --git a/src/api/markket/services/notification/template.ts b/src/api/markket/services/notification/template.ts new file mode 100644 index 0000000..c592969 --- /dev/null +++ b/src/api/markket/services/notification/template.ts @@ -0,0 +1,231 @@ + +type EmailLayout = { + content: string; + title: string; +}; + +export const emailLayout = ({ content, title }: EmailLayout) => { + return ` + + + + + + RSVP Confirmation - Markkët + + + +
+ +
+

Markkët

+
+ + +
+ + +
+

${title} 🎉

+ + ${content} + +
+

If you need to make any changes, please don't hesitate to contact us.

+

+ Best regards,
+ The Markkët Team +

+
+
+ + +
+

+ Powered by + Markkët + & SendGrid +

+
+
+ + + `; +}; + +/** + * Order Notification Email Template + * + * @param order + * @returns + */ +export const OrderNotificationHTml = (order: any) => { + //

Order Amount: ${((order?.data?.object?.total_amount || 0) / 100)}

+ const content = ` +

Thank you for your order!

+
+

Order

+

Order ID: ${order?.data?.object?.id}

+

Dear buyer,

+

Thank you for your order!

+ + View Receipt + +

The seller has been notified and will reach out if more information is needed.

+
+ `; + + const title = 'Markkët: Order Confirmation'; + + return emailLayout({ content, title }); +}; + +/** + * RSVP Notification Email Template + * + * @param event + * @returns + */ +export const RSVPNotificationHTml = (event: any) => { + //

Order Amount: ${((order?.data?.object?.total_amount || 0) / 100)}

+ const content = ` + +

Dear {{name}},

+

Thank you for confirming your attendance to our upcoming event. We're excited to have you join us!

+
+

Event Details

+

Event: {{eventName}}

+

Date: {{eventDate}}

+

Time: {{startTime}} - {{endTime}}

+

Your Email: {{email}}

+
+ `; + + const title = 'Markkët: RSVP Confirmation'; + + return emailLayout({ content, title }); +}; diff --git a/src/api/rsvp/middlewares/notification.ts b/src/api/rsvp/middlewares/notification.ts new file mode 100644 index 0000000..4daf4cb --- /dev/null +++ b/src/api/rsvp/middlewares/notification.ts @@ -0,0 +1,8 @@ +import { sendRSVPNotification } from "../../markket/services/notification"; + +export default (config, { strapi }) => { + return async (context, next) => { + await next(); + await sendRSVPNotification({ strapi, rsvp: context.body?.data, event: {} }); + }; +}; \ No newline at end of file diff --git a/src/api/rsvp/routes/rsvp.ts b/src/api/rsvp/routes/rsvp.ts index 45ed63c..e60bf3c 100644 --- a/src/api/rsvp/routes/rsvp.ts +++ b/src/api/rsvp/routes/rsvp.ts @@ -4,4 +4,15 @@ import { factories } from '@strapi/strapi'; -export default factories.createCoreRouter('api::rsvp.rsvp'); +export default factories.createCoreRouter('api::rsvp.rsvp', { + config: { + create: { + middlewares: [ + { + name: 'api::rsvp.notification', + config: {} + } + ] + } + } +});