Skip to content

Commit

Permalink
fix modify order and retail rescue items
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 f7306c7 commit 974dec9
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 4 deletions.
1 change: 1 addition & 0 deletions client/src/OrderPage/ModifyOrderForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions client/src/OrderPage/OrderPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ function OrderPage() {
_id: '',
advanced: false,
organization: '',
email: '',
produce: 0,
meat: 0,
vito: 0,
Expand Down
1 change: 1 addition & 0 deletions client/src/util/types/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ interface IRetailRescueItem {
interface IOrder {
_id: string;
advanced: boolean;
email: string;
organization: string;
produce: number;
meat: any;
Expand Down
8 changes: 6 additions & 2 deletions server/src/controllers/order.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

/**
Expand Down Expand Up @@ -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}`,
Expand Down
40 changes: 38 additions & 2 deletions server/src/services/mail.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down Expand Up @@ -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 || '[email protected]',
name: senderName,
},
to: order.email,
subject: 'Order Modified',
html:
`<h1 style="color:black;">Your order has been modified</h1>` +
`<h2 style="color:black;">Your order summary:</h2>${formatOrderToEmail(
order,
)}`,
};
const adminEmail: MailDataRequired = {
from: {
email: process.env.SENDGRID_EMAIL_ADDRESS || '[email protected]',
name: senderName,
},
to: email,
subject: 'Modified Order',
html:
`<h1 style="color:black;">You modified an order</h1>` +
`<h2 style="color:black;">Order summary:</h2>${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: {
Expand Down Expand Up @@ -201,4 +236,5 @@ export {
emailInviteLink,
emailApproveOrder,
emailRejectOrder,
emailModifyOrder,
};

0 comments on commit 974dec9

Please sign in to comment.