Skip to content

Commit

Permalink
Update email automation
Browse files Browse the repository at this point in the history
  • Loading branch information
ansonjwhe committed Jul 15, 2024
1 parent b211f5a commit 4f82be3
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 57 deletions.
94 changes: 39 additions & 55 deletions backend/emails/emails.js
Original file line number Diff line number Diff line change
@@ -1,39 +1,27 @@
const { EMAIL_RECIPIENTS } = require('../models/constants')
const { EMAIL_RECIPIENTS, ADMIN_GROUP_EMAIL } = require('../models/constants')
const { sendEmail } = require('../service/sendEmail')
const { getUserByUID } = require('../service/users.service')

const FINANCE_GROUP_EMAIL = 'finance@watonomous.ca'
const DivisionEmails = {
software: 'software-leads@watonomous.ca',
mechanical: 'mechanical-leads@watonomous.ca',
business: 'business-leads@watonomous.ca',
electrical: 'electrical-leads@watonomous.ca',
}

const currencyFormatter = new Intl.NumberFormat('en-CA', {
style: 'currency',
currency: 'CAD',
})

const getEmailToSection = async (reporter_id, recipients) => {
const emailToSet = new Set([FINANCE_GROUP_EMAIL])
const emailToSet = new Set([ADMIN_GROUP_EMAIL])

if (recipients.includes(EMAIL_RECIPIENTS.admin)) {
// TODO: use ADMIN_IDENTIFIERS (rename to ADMIN_EMAILS) after migrating to new onboarding data
emailToSet.add(ADMIN_GROUP_EMAIL)
}

if (recipients.includes(EMAIL_RECIPIENTS.coordinator)) {
// TODO: determine which is the correct coordinator email
// emailToSet.add('studentdesigncentreengineering@uwaterloo.ca')
// emailToSet.add('srfeeney@uwaterloo.ca')
}

if (recipients.includes(EMAIL_RECIPIENTS.director)) {
// TODO: get director emails
}

if (recipients.includes(EMAIL_RECIPIENTS.faculty_advisor)) {
// emailToSet.add('drayside@uwaterloo.ca')
// TODO: emailToSet.add('drayside@uwaterloo.ca')
}

if (recipients.includes(EMAIL_RECIPIENTS.finance)) {
Expand All @@ -47,10 +35,6 @@ const getEmailToSection = async (reporter_id, recipients) => {
}
}

if (recipients.includes(EMAIL_RECIPIENTS.team_captain)) {
// TODO: get team captain emails
}

return [...emailToSet].map((Email) => ({ Email }))
}

Expand Down Expand Up @@ -129,18 +113,19 @@ const getTicketLinkHTML = (ticketPath) => `
</p>
`

const sendEmailPPRCreatedToApprovers = async (ppr) => {
const Subject = `[Seeking Approval] ${ppr.codename}`
// ******************** UPR EMAILS ********************

const sendEmailUPRCreatedToApprovers = async (upr) => {
const Subject = `[Seeking Approval] ${upr.codename}`
const HTMLPart =
getMainMessageHTML(
'A new Personal Purchase Request needs your approval!'
'A new UW Finance Purchase Request needs your approval!'
) +
(await getPPRTicketInfoHTML(ppr)) +
getTicketLinkHTML(ppr.path)
const To = await getEmailToSection(ppr.reporter_id, [
(await getUPRTicketInfoHTML(upr)) +
getTicketLinkHTML(upr.path)
const To = await getEmailToSection(upr.reporter_id, [
EMAIL_RECIPIENTS.faculty_advisor,
EMAIL_RECIPIENTS.team_captain,
EMAIL_RECIPIENTS.director,
EMAIL_RECIPIENTS.admin,
])

await sendEmail({
Expand All @@ -150,18 +135,17 @@ const sendEmailPPRCreatedToApprovers = async (ppr) => {
})
}

const sendEmailUPRCreatedToApprovers = async (upr) => {
const Subject = `[Seeking Approval] ${upr.codename}`
const sendEmailUPRApprovedToCoordinator = async (upr) => {
const Subject = `[Ready to Buy] ${upr.codename}`
const HTMLPart =
getMainMessageHTML(
'A new UW Finance Purchase Request needs your approval!'
`A new UW Finance Purchase Request has been approved! Please purchase the approved item(s).<br>
After purchase, update the purchase order number and requisition number at the ticket link below.`
) +
(await getUPRTicketInfoHTML(upr)) +
getTicketLinkHTML(upr.path)
const To = await getEmailToSection(upr.reporter_id, [
EMAIL_RECIPIENTS.faculty_advisor,
EMAIL_RECIPIENTS.team_captain,
EMAIL_RECIPIENTS.director,
EMAIL_RECIPIENTS.coordinator,
])

await sendEmail({
Expand Down Expand Up @@ -209,7 +193,7 @@ const sendEmailUPRPurchasedToCoordinator = async (upr) => {
})
}

const sendEmailUPRReadyForPickupToCoordinator = async (upr) => {
const sendEmailUPRReadyForPickupToReporter = async (upr) => {
const Subject = `[Ready for pickup] ${upr.codename}`
const HTMLPart =
getMainMessageHTML(
Expand All @@ -228,17 +212,19 @@ const sendEmailUPRReadyForPickupToCoordinator = async (upr) => {
})
}

const sendEmailPPRApprovedToReporter = async (ppr) => {
const Subject = `[Ready to Buy] ${ppr.codename}`
// ******************** PPR EMAILS ********************

const sendEmailPPRCreatedToApprovers = async (ppr) => {
const Subject = `[Seeking Approval] ${ppr.codename}`
const HTMLPart =
getMainMessageHTML(
`Your Personal Purchase Request has been approved! Please purchase the approved item(s).<br>
Upload your proof of purchase at the ticket link below to request reimbursement.`
'A new Personal Purchase Request needs your approval!'
) +
(await getPPRTicketInfoHTML(ppr)) +
getTicketLinkHTML(ppr.path)
const To = await getEmailToSection(ppr.reporter_id, [
EMAIL_RECIPIENTS.reporter,
EMAIL_RECIPIENTS.faculty_advisor,
EMAIL_RECIPIENTS.admin,
])

await sendEmail({
Expand All @@ -248,17 +234,17 @@ const sendEmailPPRApprovedToReporter = async (ppr) => {
})
}

const sendEmailUPRApprovedToCoordinator = async (upr) => {
const Subject = `[Ready to Buy] ${upr.codename}`
const sendEmailPPRApprovedToReporter = async (ppr) => {
const Subject = `[Ready to Buy] ${ppr.codename}`
const HTMLPart =
getMainMessageHTML(
`A new UW Finance Purchase Request has been approved! Please purchase the approved item(s).<br>
After purchase, update the purchase order number and requisition number at the ticket link below.`
`Your Personal Purchase Request has been approved! Please purchase the approved item(s).<br>
Upload your proof of purchase at the ticket link below to request reimbursement.`
) +
(await getUPRTicketInfoHTML(upr)) +
getTicketLinkHTML(upr.path)
const To = await getEmailToSection(upr.reporter_id, [
EMAIL_RECIPIENTS.coordinator,
(await getPPRTicketInfoHTML(ppr)) +
getTicketLinkHTML(ppr.path)
const To = await getEmailToSection(ppr.reporter_id, [
EMAIL_RECIPIENTS.reporter,
])

await sendEmail({
Expand All @@ -277,7 +263,6 @@ const sendEmailPPRPurchasedAndReceiptsSubmittedToCoordinator = async (ppr) => {
(await getPPRTicketInfoHTML(ppr)) +
getTicketLinkHTML(ppr.path)
const To = await getEmailToSection(ppr.reporter_id, [
EMAIL_RECIPIENTS.finance,
EMAIL_RECIPIENTS.coordinator,
])
await sendEmail({
Expand All @@ -296,8 +281,7 @@ const sendEmailPPRReimbursedToReporter = async (ppr) => {
(await getPPRTicketInfoHTML(ppr)) +
getTicketLinkHTML(ppr.path)
const To = await getEmailToSection(ppr.reporter_id, [
EMAIL_RECIPIENTS.finance,
EMAIL_RECIPIENTS.coordinator,
EMAIL_RECIPIENTS.reporter,
])
await sendEmail({
Subject,
Expand All @@ -306,6 +290,8 @@ const sendEmailPPRReimbursedToReporter = async (ppr) => {
})
}

// ******************** SF EMAILS ********************

const sendEmailSFReimbursementRequestToCoordinator = async (sf) => {
const Subject = `[Action Needed] Submit Reimbursement Request ${sf.codename}`
const HTMLPart =
Expand Down Expand Up @@ -349,9 +335,7 @@ const sendEmailSFReimbursementReceivedToTeam = async (sf) => {
(await getSFTicketInfoHTML(sf)) +
getTicketLinkHTML(sf.path)
const To = await getEmailToSection(sf.reporter_id, [
EMAIL_RECIPIENTS.finance,
EMAIL_RECIPIENTS.coordinator,
EMAIL_RECIPIENTS.team_captain,
])
await sendEmail({
Subject,
Expand All @@ -365,9 +349,9 @@ module.exports = {
sendEmailUPRApprovedToCoordinator,
sendEmailUPRPurchasedToReporter,
sendEmailUPRPurchasedToCoordinator,
sendEmailUPRReadyForPickupToCoordinator,
sendEmailPPRApprovedToReporter,
sendEmailUPRReadyForPickupToReporter,
sendEmailPPRCreatedToApprovers,
sendEmailPPRApprovedToReporter,
sendEmailPPRPurchasedAndReceiptsSubmittedToCoordinator,
sendEmailPPRReimbursedToReporter,
sendEmailSFReimbursementRequestToCoordinator,
Expand Down
2 changes: 2 additions & 0 deletions backend/models/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ const TICKET_ENDPOINTS = Object.freeze({
})

const ADMIN_IDENTIFIERS = ['drayside', 'v2zheng', 'jw4he', 'william.li']
const ADMIN_GROUP_EMAIL = 'finance-admin@watonomous.ca'

module.exports = {
ENDOWMENT_FUNDS,
Expand All @@ -68,4 +69,5 @@ module.exports = {
AUTH_ROLES,
EMAIL_RECIPIENTS,
ADMIN_IDENTIFIERS,
ADMIN_GROUP_EMAIL,
}
4 changes: 2 additions & 2 deletions backend/service/uwfinancepurchases.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const {
sendEmailUPRApprovedToCoordinator,
sendEmailUPRPurchasedToCoordinator,
sendEmailUPRPurchasedToReporter,
sendEmailUPRReadyForPickupToCoordinator,
sendEmailUPRReadyForPickupToReporter,
} = require('../emails/emails')

const getAllUWFinancePurchases = () => {
Expand Down Expand Up @@ -53,7 +53,7 @@ const updateUWFinancePurchase = async (id, body) => {
// ORDERED -> READY_FOR_PICKUP
if (body.status === 'READY_FOR_PICKUP') {
const annotatedUPR = await getUWFinancePurchase(id)
await sendEmailUPRReadyForPickupToCoordinator(annotatedUPR)
await sendEmailUPRReadyForPickupToReporter(annotatedUPR)
}
return newPurchaseTicket
}
Expand Down

0 comments on commit 4f82be3

Please sign in to comment.