From 2a5f742edaea8bb66f563fefe51c76687104a5e1 Mon Sep 17 00:00:00 2001 From: Peyton-McKee Date: Sat, 6 Jul 2024 13:04:59 -0400 Subject: [PATCH] Remove Need for Unnecessary Env Variables and Fix More CrId Slack Bugs --- .../src/services/change-requests.services.ts | 2 +- .../reimbursement-requests.services.ts | 10 +++++++- .../src/utils/change-requests.utils.ts | 2 +- .../src/utils/google-integration.utils.ts | 8 +++--- src/backend/src/utils/slack.utils.ts | 25 ++++++------------- 5 files changed, 23 insertions(+), 24 deletions(-) diff --git a/src/backend/src/services/change-requests.services.ts b/src/backend/src/services/change-requests.services.ts index fcfbf9cb4b..949fd36d83 100644 --- a/src/backend/src/services/change-requests.services.ts +++ b/src/backend/src/services/change-requests.services.ts @@ -152,7 +152,7 @@ export default class ChangeRequestsService { await sendCRSubmitterReviewedNotification(foundCR); // send a reply to a CR's notifications of its updated status - await sendSlackCRStatusToThread(updated.notificationSlackThreads, foundCR.crId, accepted); + await sendSlackCRStatusToThread(updated.notificationSlackThreads, foundCR.crId, foundCR.identifier accepted); return updated.crId; } diff --git a/src/backend/src/services/reimbursement-requests.services.ts b/src/backend/src/services/reimbursement-requests.services.ts index 118add7e2b..611bb39849 100644 --- a/src/backend/src/services/reimbursement-requests.services.ts +++ b/src/backend/src/services/reimbursement-requests.services.ts @@ -466,7 +466,15 @@ export default class ReimbursementRequestService { text: `The following reimbursement requests need to be approved by you: ${saboNumbers.join(', ')}` }; - await sendMailToAdvisor(mailOptions.subject, mailOptions.text); + const organization = await prisma.organization.findUnique({ + where: { organizationId }, + include: { advisor: true } + }); + + if (!organization) throw new NotFoundException('Organization', organizationId); + if (!organization.advisor) throw new HttpException(400, 'Organization does not have an advisor'); + + await sendMailToAdvisor(mailOptions.subject, mailOptions.text, organization.advisor); reimbursementRequests.forEach((reimbursementRequest) => { prisma.reimbursement_Status.create({ diff --git a/src/backend/src/utils/change-requests.utils.ts b/src/backend/src/utils/change-requests.utils.ts index 001d8864a4..0efdc20a2f 100644 --- a/src/backend/src/utils/change-requests.utils.ts +++ b/src/backend/src/utils/change-requests.utils.ts @@ -561,7 +561,7 @@ export const sendCRSubmitterReviewedNotification = async ( const creatorUserSettings = await prisma.user_Settings.findUnique({ where: { userId: foundCR.submitterId } }); if (creatorUserSettings && creatorUserSettings.slackId) { try { - await sendSlackCRReviewedNotification(creatorUserSettings.slackId, foundCR.crId); + await sendSlackCRReviewedNotification(creatorUserSettings.slackId, foundCR.crId, foundCR.identifier); } catch (err: unknown) { if (err instanceof Error) { throw new HttpException(500, `Failed to send slack notification: ${err.message}`); diff --git a/src/backend/src/utils/google-integration.utils.ts b/src/backend/src/utils/google-integration.utils.ts index faaeb5e9f0..3831840cc4 100644 --- a/src/backend/src/utils/google-integration.utils.ts +++ b/src/backend/src/utils/google-integration.utils.ts @@ -4,6 +4,7 @@ import SMTPTransport from 'nodemailer/lib/smtp-transport'; import { HttpException } from './errors.utils'; import stream, { Readable } from 'stream'; import concat from 'concat-stream'; +import { User } from '@prisma/client'; const { OAuth2 } = google.auth; const { @@ -12,8 +13,7 @@ const { GOOGLE_CLIENT_SECRET, EMAIL_REFRESH_TOKEN, USER_EMAIL, - DRIVE_REFRESH_TOKEN, - ADVISOR_EMAIL + DRIVE_REFRESH_TOKEN } = process.env; const oauth2Client = new OAuth2(GOOGLE_CLIENT_ID, GOOGLE_CLIENT_SECRET, 'https://developers.google.com/oauthplayground'); @@ -49,12 +49,12 @@ const createTransporter = async () => { } }; -export const sendMailToAdvisor = async (subject: string, text: string) => { +export const sendMailToAdvisor = async (subject: string, text: string, advisor: User) => { try { //this sends an email from our email to our advisor: professor Goldstone const mailOptions = { from: USER_EMAIL, - to: ADVISOR_EMAIL, + to: advisor.email, subject, text }; diff --git a/src/backend/src/utils/slack.utils.ts b/src/backend/src/utils/slack.utils.ts index 8a573cd494..9316eb334d 100644 --- a/src/backend/src/utils/slack.utils.ts +++ b/src/backend/src/utils/slack.utils.ts @@ -64,7 +64,7 @@ export const sendSlackRequestedReviewNotification = async ( const btnText = `View CR`; const changeRequestLink = `https://finishlinebyner.com/change-requests/${changeRequest.crId}`; const slackPingMessage = usersToSlackPings(reviewers); - const fullMsg = `${slackPingMessage} Your review has been requested on CR #${changeRequest.crId}!`; + const fullMsg = `${slackPingMessage} Your review has been requested on CR #${changeRequest.identifier}!`; const threads = await prisma.message_Info.findMany({ where: { changeRequestId: changeRequest.crId } }); @@ -173,15 +173,14 @@ export const sendSlackDesignReviewConfirmNotification = async ( * @param team the teams of the cr to notify * @param message the message to send to the teams * @param crId the cr id - * @param budgetImpact the amount of budget requested for the cr + * @param identifier the cr identifier * @returns the channelId and timestamp of the messages sent in slack */ export const sendSlackChangeRequestNotification = async ( team: Team, message: string, crId: string, - identifier: number, - budgetImpact?: number + identifier: number ): Promise<{ channelId: string; ts: string }[]> => { if (process.env.NODE_ENV !== 'production') return []; // don't send msgs unless in prod const msgs: { channelId: string; ts: string }[] = []; @@ -191,16 +190,6 @@ export const sendSlackChangeRequestNotification = async ( const notification = await sendMessage(team.slackId, fullMsg, fullLink, btnText); if (notification) msgs.push(notification); - if (budgetImpact && budgetImpact > 100) { - const importantNotification = await sendMessage( - process.env.SLACK_EBOARD_CHANNEL!, - `${fullMsg} with $${budgetImpact} requested`, - fullLink, - btnText - ); - if (importantNotification) msgs.push(importantNotification); - } - return msgs; }; @@ -374,12 +363,12 @@ export const sendDRScheduledSlackNotif = async ( } }; -export const sendSlackCRReviewedNotification = async (slackId: string, crId: string) => { +export const sendSlackCRReviewedNotification = async (slackId: string, crId: string, identifier: number) => { if (process.env.NODE_ENV !== 'production') return; // don't send msgs unless in prod const msgs = []; const fullMsg = `:tada: Your Change Request was just reviewed! Click the link to view! :tada:`; const fullLink = `https://finishlinebyner.com/cr/${crId}`; - const btnText = `View CR#${crId}`; + const btnText = `View CR#${identifier}`; msgs.push(sendMessage(slackId, fullMsg, fullLink, btnText)); return Promise.all(msgs); @@ -390,6 +379,7 @@ export const sendSlackCRReviewedNotification = async (slackId: string, crId: str * * @param threads the threads of cr slack notifications to reply/react to * @param crId the cr id + * @param identifier the cr identifier * @param approved is the cr approved */ export const sendSlackCRStatusToThread = async ( @@ -400,12 +390,13 @@ export const sendSlackCRStatusToThread = async ( changeRequestId: string | null; }[], crId: string, + identifier: number, approved: boolean ) => { if (process.env.NODE_ENV !== 'production') return; // don't send msgs unless in prod const fullMsg = `This Change Request was ${approved ? 'approved! :tada:' : 'denied.'} Click the link to view.`; const fullLink = `https://finishlinebyner.com/cr/${crId}`; - const btnText = `View CR#${crId}`; + const btnText = `View CR#${identifier}`; try { if (threads && threads.length !== 0) { const msgs = threads.map((thread) =>