Skip to content

Commit

Permalink
fix times
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricky Raup authored and Ricky Raup committed May 17, 2024
1 parent 974dec9 commit fa1e498
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 14 deletions.
15 changes: 11 additions & 4 deletions server/src/controllers/order.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import {
import {
emailApproveOrder,
emailRejectOrder,
emailModifyApproveOrder,
emailModifyOrder,
} from '../services/mail.service';
import { ISettings, Settings } from '../models/settings.model';
Expand Down Expand Up @@ -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}`,
Expand Down Expand Up @@ -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.'));
Expand Down
47 changes: 37 additions & 10 deletions server/src/services/mail.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ');
};
Expand Down Expand Up @@ -126,23 +130,27 @@ const formatOrderToEmail = (order: IOrder) => {
order.retailRescue,
)}</div>` +
`<div style="color:black;">Status: ${order.status}</div>` +
`<div style="color:black;">Pickup: ${pickupDate.toLocaleTimeString([], {
hour: '2-digit',
minute: '2-digit',
})}</div>`
`<div style="color:black;">Pickup Date: ${pickupDate.toDateString()}</div>` +
`<div style="color:black;">Pickup Time: ${pickupDate.toLocaleTimeString(
[],
{
hour: '2-digit',
minute: '2-digit',
},
)}</div>`
);
};

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:
`<h1 style="color:black;">Your order has been modified</h1>` +
`<h1 style="color:black;">Your order has been modified and approved</h1>` +
`<h2 style="color:black;">Your order summary:</h2>${formatOrderToEmail(
order,
)}`,
Expand All @@ -153,9 +161,9 @@ const emailModifyOrder = async (email: string, order: IOrder) => {
name: senderName,
},
to: email,
subject: 'Modified Order',
subject: 'Modified/Approved Order',
html:
`<h1 style="color:black;">You modified an order</h1>` +
`<h1 style="color:black;">You modified/approved an order</h1>` +
`<h2 style="color:black;">Order summary:</h2>${formatOrderToEmail(
order,
)}`,
Expand All @@ -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:
`<h1 style="color:black;">You have modified your order</h1>` +
`<h2 style="color:black;">Your order summary:</h2>${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: {
Expand Down Expand Up @@ -236,5 +262,6 @@ export {
emailInviteLink,
emailApproveOrder,
emailRejectOrder,
emailModifyApproveOrder,
emailModifyOrder,
};

0 comments on commit fa1e498

Please sign in to comment.