From 974dec9aa650e0d40cfa2c98017e0ff7717a58d9 Mon Sep 17 00:00:00 2001 From: Ricky Raup Date: Thu, 16 May 2024 20:55:01 -0400 Subject: [PATCH] fix modify order and retail rescue items --- client/src/OrderPage/ModifyOrderForm.tsx | 1 + client/src/OrderPage/OrderPage.tsx | 1 + client/src/util/types/order.ts | 1 + server/src/controllers/order.controller.ts | 8 +++-- server/src/services/mail.service.ts | 40 ++++++++++++++++++++-- 5 files changed, 47 insertions(+), 4 deletions(-) diff --git a/client/src/OrderPage/ModifyOrderForm.tsx b/client/src/OrderPage/ModifyOrderForm.tsx index 5fcadbb3..6f458a12 100644 --- a/client/src/OrderPage/ModifyOrderForm.tsx +++ b/client/src/OrderPage/ModifyOrderForm.tsx @@ -123,6 +123,7 @@ function ModifyOrderForm({ // eslint-disable-next-line no-underscore-dangle _id: order._id, advanced: order.advanced, + email: order.email, organization: order.organization, status: order.status, produce: values.produce, diff --git a/client/src/OrderPage/OrderPage.tsx b/client/src/OrderPage/OrderPage.tsx index 14d0bdbb..7b5cf430 100644 --- a/client/src/OrderPage/OrderPage.tsx +++ b/client/src/OrderPage/OrderPage.tsx @@ -28,6 +28,7 @@ function OrderPage() { _id: '', advanced: false, organization: '', + email: '', produce: 0, meat: 0, vito: 0, diff --git a/client/src/util/types/order.ts b/client/src/util/types/order.ts index b0c5782b..c4706b51 100644 --- a/client/src/util/types/order.ts +++ b/client/src/util/types/order.ts @@ -10,6 +10,7 @@ interface IRetailRescueItem { interface IOrder { _id: string; advanced: boolean; + email: string; organization: string; produce: number; meat: any; diff --git a/server/src/controllers/order.controller.ts b/server/src/controllers/order.controller.ts index 114cac32..af935f5a 100644 --- a/server/src/controllers/order.controller.ts +++ b/server/src/controllers/order.controller.ts @@ -17,7 +17,11 @@ import { getAllActiveOrdersInDateRange, getAllApprovedOrdersInDateRange, } from '../services/order.service'; -import { emailApproveOrder, emailRejectOrder } from '../services/mail.service'; +import { + emailApproveOrder, + emailRejectOrder, + emailModifyOrder, +} from '../services/mail.service'; import { ISettings, Settings } from '../models/settings.model'; /** @@ -340,7 +344,7 @@ const modifyAndApproveOrder = async ( updateOrderById(id, order) .then(() => { - emailApproveOrder(organizationUser.email, order) + emailModifyOrder(organizationUser.email, order) .then(() => res.status(StatusCode.CREATED).send({ message: `Email has been sent to ${organizationUser.email}`, diff --git a/server/src/services/mail.service.ts b/server/src/services/mail.service.ts index 70f7c07c..b44221df 100644 --- a/server/src/services/mail.service.ts +++ b/server/src/services/mail.service.ts @@ -90,8 +90,11 @@ const emailInviteLink = async (email: string, token: string) => { const parseArray = (array: any) => { return array - .map((element: retailRescueItem) => `${element.item} ${element.comment}`) - .join(); + .map( + (element: retailRescueItem) => + `Item: ${element.item} - Comment: ${element.comment}`, + ) + .join(', '); }; const formatOrderToEmail = (order: IOrder) => { @@ -130,6 +133,38 @@ const formatOrderToEmail = (order: IOrder) => { ); }; +const emailModifyOrder = async (email: string, order: IOrder) => { + const userEmail: MailDataRequired = { + from: { + email: process.env.SENDGRID_EMAIL_ADDRESS || 'missing@mail.com', + name: senderName, + }, + to: order.email, + subject: 'Order Modified', + html: + `

Your order has been modified

` + + `

Your order summary:

${formatOrderToEmail( + order, + )}`, + }; + const adminEmail: MailDataRequired = { + from: { + email: process.env.SENDGRID_EMAIL_ADDRESS || 'missing@mail.com', + name: senderName, + }, + to: email, + subject: 'Modified Order', + html: + `

You modified an order

` + + `

Order summary:

${formatOrderToEmail( + order, + )}`, + }; + // Send the email and propogate the error up if one exists + await SGmail.send(userEmail); + await SGmail.send(adminEmail); +}; + const emailApproveOrder = async (email: string, order: IOrder) => { const userEmail: MailDataRequired = { from: { @@ -201,4 +236,5 @@ export { emailInviteLink, emailApproveOrder, emailRejectOrder, + emailModifyOrder, };