Skip to content

Commit

Permalink
Remove Need for Unnecessary Env Variables and Fix More CrId Slack Bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
Peyton-McKee committed Jul 6, 2024
1 parent b4ebcfe commit 2a5f742
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/backend/src/services/change-requests.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
10 changes: 9 additions & 1 deletion src/backend/src/services/reimbursement-requests.services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
2 changes: 1 addition & 1 deletion src/backend/src/utils/change-requests.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
8 changes: 4 additions & 4 deletions src/backend/src/utils/google-integration.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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');
Expand Down Expand Up @@ -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
};
Expand Down
25 changes: 8 additions & 17 deletions src/backend/src/utils/slack.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 } });

Expand Down Expand Up @@ -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 }[] = [];
Expand All @@ -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;
};

Expand Down Expand Up @@ -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);
Expand All @@ -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 (
Expand All @@ -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) =>
Expand Down

0 comments on commit 2a5f742

Please sign in to comment.