diff --git a/server/src/controllers/order.controller.ts b/server/src/controllers/order.controller.ts index af935f5a..95edb18b 100644 --- a/server/src/controllers/order.controller.ts +++ b/server/src/controllers/order.controller.ts @@ -20,6 +20,7 @@ import { import { emailApproveOrder, emailRejectOrder, + emailModifyApproveOrder, emailModifyOrder, } from '../services/mail.service'; import { ISettings, Settings } from '../models/settings.model'; @@ -344,7 +345,7 @@ const modifyAndApproveOrder = async ( updateOrderById(id, order) .then(() => { - emailModifyOrder(organizationUser.email, order) + emailModifyApproveOrder(organizationUser.email, order) .then(() => res.status(StatusCode.CREATED).send({ message: `Email has been sent to ${organizationUser.email}`, @@ -406,9 +407,15 @@ const modifyOrder = async ( updateOrderById(id, order) .then(() => { - res.status(StatusCode.CREATED).send({ - message: 'Order has been modified', - }); + emailModifyOrder(organizationUser.email, order) + .then(() => + res.status(StatusCode.CREATED).send({ + message: `Email has been sent to ${organizationUser.email}`, + }), + ) + .catch(() => { + next(ApiError.internal('Failed to send modify order email.')); + }); }) .catch(() => { next(ApiError.internal('Unable to modify order.')); diff --git a/server/src/services/mail.service.ts b/server/src/services/mail.service.ts index b44221df..c32a3401 100644 --- a/server/src/services/mail.service.ts +++ b/server/src/services/mail.service.ts @@ -92,7 +92,11 @@ const parseArray = (array: any) => { return array .map( (element: retailRescueItem) => - `Item: ${element.item} - Comment: ${element.comment}`, + `Item: ${element.item} - Comment: ${ + element.comment === undefined || element.comment.length === 0 + ? 'None' + : element.comment + }`, ) .join(', '); }; @@ -126,23 +130,27 @@ const formatOrderToEmail = (order: IOrder) => { order.retailRescue, )}` + `
Status: ${order.status}
` + - `
Pickup: ${pickupDate.toLocaleTimeString([], { - hour: '2-digit', - minute: '2-digit', - })}
` + `
Pickup Date: ${pickupDate.toDateString()}
` + + `
Pickup Time: ${pickupDate.toLocaleTimeString( + [], + { + hour: '2-digit', + minute: '2-digit', + }, + )}
` ); }; -const emailModifyOrder = async (email: string, order: IOrder) => { +const emailModifyApproveOrder = 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', + subject: 'Order Modified/Approved', html: - `

Your order has been modified

` + + `

Your order has been modified and approved

` + `

Your order summary:

${formatOrderToEmail( order, )}`, @@ -153,9 +161,9 @@ const emailModifyOrder = async (email: string, order: IOrder) => { name: senderName, }, to: email, - subject: 'Modified Order', + subject: 'Modified/Approved Order', html: - `

You modified an order

` + + `

You modified/approved an order

` + `

Order summary:

${formatOrderToEmail( order, )}`, @@ -165,6 +173,24 @@ const emailModifyOrder = async (email: string, order: IOrder) => { await SGmail.send(adminEmail); }; +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: + `

You have modified your order

` + + `

Your order summary:

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