diff --git a/backend/emails/emails.js b/backend/emails/emails.js index 601234e..fea4378 100644 --- a/backend/emails/emails.js +++ b/backend/emails/emails.js @@ -15,7 +15,7 @@ const currencyFormatter = new Intl.NumberFormat('en-CA', { }) const getEmailToSection = async (reporter_id, recipients) => { - const emailToSet = new Set(['jw4he@watonomous.ca']) + const emailToSet = new Set(['jw4he@watonomous.ca', 'v2zheng@watonomous.ca']) if (recipients.includes(EMAIL_RECIPIENTS.admin)) { // TODO: use ADMIN_IDENTIFIERS (rename to ADMIN_EMAILS) after migrating to new onboarding data @@ -139,6 +139,44 @@ const sendEmailUPRCreatedToApprovers = async (upr) => { }) } +const sendEmailUPRPurchasedToReporter = async (upr) => { + const Subject = `[Purchased] ${upr.codename}` + const HTMLPart = + getMainMessageHTML( + `Your UW Finance Purchase Request has been purchased! When the item is ready to be picked up, we will let you know.` + ) + + (await getUPRTicketInfoHTML(upr)) + + +getTicketLinkHTML(upr.path) + const To = await getEmailToSection(upr.reporter_id, [ + EMAIL_RECIPIENTS.reporter, + ]) + + await sendEmail({ + Subject, + HTMLPart, + To, + }) +} + +const sendEmailUPRPurchasedToCoordinator = async (upr) => { + const Subject = `[Purchased] ${upr.codename}` + const HTMLPart = + getMainMessageHTML( + 'Thanks for purchasing the item(s)! When the item is ready to be picked up, please update the ticket below.' + ) + + (await getUPRTicketInfoHTML(upr)) + + getTicketLinkHTML(upr.path) + const To = await getEmailToSection(upr.reporter_id, [ + EMAIL_RECIPIENTS.coordinator, + ]) + + await sendEmail({ + Subject, + HTMLPart, + To, + }) +} + const sendEmailPPRApprovedToReporter = async (ppr) => { const Subject = `[Ready to Buy] ${ppr.codename}` const HTMLPart = @@ -500,10 +538,12 @@ const PaidPersonalPurchaseReimbursementClaimInstructions = ( } module.exports = { - sendEmailPPRCreatedToApprovers, sendEmailUPRCreatedToApprovers, - sendEmailPPRApprovedToReporter, sendEmailUPRApprovedToCoordinator, + sendEmailUPRPurchasedToReporter, + sendEmailUPRPurchasedToCoordinator, + sendEmailPPRApprovedToReporter, + sendEmailPPRCreatedToApprovers, PurchaseRequestInvalidated, PersonalPurchaseApproved, UWFinancePurchaseApproved, diff --git a/backend/service/automation.js b/backend/service/automation.js deleted file mode 100644 index 9605f5e..0000000 --- a/backend/service/automation.js +++ /dev/null @@ -1,14 +0,0 @@ -// import { PersonalPurchaseApproved } from '../emails/emails' -// import { createEmail } from './createEmail' - -// ticket of type personalPurchase or uwFinancePurchase -const transitionReadyToBuy = async (ticketType, ticketId) => { - const ticket = ticketType.findByIdAndUpdate(ticketId, { - status: 'READY_TO_BUY', - }) - return ticket -} - -module.exports = { - transitionReadyToBuy, -} diff --git a/backend/service/sendEmail.js b/backend/service/sendEmail.js index eb972b7..580ddcf 100644 --- a/backend/service/sendEmail.js +++ b/backend/service/sendEmail.js @@ -21,7 +21,7 @@ const sendEmail = async ({ To, Subject, HTMLPart }) => { }) console.log('sendEmail success!') } catch (err) { - console.log(`Uh oh! bad sendEmail: ${err}`) + console.log(`sendEmail failed: ${err}`) } } diff --git a/backend/service/uwfinancepurchases.service.js b/backend/service/uwfinancepurchases.service.js index f6b29f9..66e1969 100644 --- a/backend/service/uwfinancepurchases.service.js +++ b/backend/service/uwfinancepurchases.service.js @@ -7,6 +7,8 @@ const { const { sendEmailUPRCreatedToApprovers, sendEmailUPRApprovedToCoordinator, + sendEmailUPRPurchasedToCoordinator, + sendEmailUPRPurchasedToReporter, } = require('../emails/emails') const getAllUWFinancePurchases = () => { @@ -30,10 +32,28 @@ const createNewUWFinancePurchase = async (body) => { return annotatedUPR } -const updateUWFinancePurchase = (id, body) => { - return UWFinancePurchase.findByIdAndUpdate(id, body, { - new: true, - }) +const updateUWFinancePurchase = async (id, body) => { + const existingPurchaseTicket = await getUWFinancePurchase(id) + // SENT_TO_COORDINATOR -> ORDERED + const newPurchaseTicket = await UWFinancePurchase.findByIdAndUpdate( + id, + body, + { + new: true, + } + ) + if ( + existingPurchaseTicket.status === 'SENT_TO_COORDINATOR' && + body.status === 'ORDERED' + ) { + const annotatedUPR = await getUWFinancePurchase(id) + const emails = [ + sendEmailUPRPurchasedToCoordinator(annotatedUPR), + sendEmailUPRPurchasedToReporter(annotatedUPR), + ] + await Promise.all(emails) + } + return newPurchaseTicket } const updateFILinkUWFinancePurchase = async (id, new_fi_link) => { diff --git a/frontend/src/components/CreateTicketModal.js b/frontend/src/components/CreateTicketModal.js index e139973..6ca5ebb 100644 --- a/frontend/src/components/CreateTicketModal.js +++ b/frontend/src/components/CreateTicketModal.js @@ -24,12 +24,14 @@ import { TICKET_ENDPOINTS, TICKET_TYPES } from '../constants' import { useRecoilState } from 'recoil' import { allTicketsState } from '../state/atoms' import { getAllTickets } from '../utils/globalSetters' +import { usePreserveParamsNavigate } from '../hooks/hooks' export function CreateTicketModal({ isOpen, onClose }) { const [allTickets, setAllTickets] = useRecoilState(allTicketsState) const [ticketType, setTicketType] = useState('') const { control, register, handleSubmit, reset } = useForm() const [isLoading, setIsLoading] = useState(false) + const preserveParamsNavigate = usePreserveParamsNavigate() const auth = useAuth() const displayTicketType = () => { @@ -99,9 +101,13 @@ export function CreateTicketModal({ isOpen, onClose }) { } else if (ticketType === TICKET_TYPES.SF) { payload.status = 'ALLOCATED' } - await axiosPreset.post(TICKET_ENDPOINTS[ticketType], payload) + const createdTicket = await axiosPreset.post( + TICKET_ENDPOINTS[ticketType], + payload + ) await getAllTickets(setAllTickets) onClose() + preserveParamsNavigate(`/${ticketType}/${createdTicket.data._id}`) } catch (err) { console.log(err) } finally { diff --git a/frontend/src/components/TicketContent/UPRAdminContentTable.js b/frontend/src/components/TicketContent/UPRAdminContentTable.js index e032912..6eaaa38 100644 --- a/frontend/src/components/TicketContent/UPRAdminContentTable.js +++ b/frontend/src/components/TicketContent/UPRAdminContentTable.js @@ -39,6 +39,20 @@ const UPRAdminContentTable = () => { setChanged(false) } + const transitionToPurchased = async () => { + const payload = { + status: 'ORDERED', + po_number: currentTicket.po_number, + requisition_number: currentTicket.requisition_number, + } + await axiosPreset.patch( + `${TICKET_ENDPOINTS.UPR}/${currentTicket._id}`, + payload + ) + await getAllTickets(setAllTickets) + setChanged(false) + } + return ( {
- - - + {currentTicket.status === 'SENT_TO_COORDINATOR' ? ( + + ) : ( + + )}
)