|
1 | 1 | import { ChangeRequest, daysBetween, Task, User, wbsPipe, WorkPackage } from 'shared'; |
2 | | -import { sendMessage } from '../integrations/slack'; |
| 2 | +import { reactToMessage, replyToMessageInThread, sendMessage } from '../integrations/slack'; |
3 | 3 | import { getUserSlackId } from './users.utils'; |
4 | 4 | import prisma from '../prisma/prisma'; |
5 | 5 | import { HttpException } from './errors.utils'; |
| 6 | +import { Change_Request, Team, WBS_Element } from '@prisma/client'; |
6 | 7 |
|
7 | 8 | // build the "due" string for the upcoming deadlines slack message |
8 | 9 | const buildDueString = (daysUntilDeadline: number): string => { |
@@ -89,3 +90,158 @@ export const sendReimbursementRequestDeniedNotification = async (slackId: string |
89 | 90 | } |
90 | 91 | } |
91 | 92 | }; |
| 93 | + |
| 94 | +export const sendSlackDesignReviewNotification = async (slackId: string, designReviewId: string) => { |
| 95 | + if (process.env.NODE_ENV !== 'production') return; // don't send msgs unless in prod |
| 96 | + const msg = `You have been invited to a Design Review!`; |
| 97 | + const fullLink = `https://finishlinebyner.com/design-reviews/${designReviewId}`; |
| 98 | + const linkButtonText = 'RSVP for the Design Review'; |
| 99 | + |
| 100 | + try { |
| 101 | + await sendMessage(slackId, msg, fullLink, linkButtonText); |
| 102 | + } catch (error: unknown) { |
| 103 | + if (error instanceof Error) { |
| 104 | + throw new HttpException(500, `Failed to send slack notification: ${error.message}`); |
| 105 | + } |
| 106 | + } |
| 107 | +}; |
| 108 | + |
| 109 | +/** |
| 110 | + * Sends slack notifications to teams for new CRs and returns the messages sent in slack |
| 111 | + * |
| 112 | + * @param team the teams of the cr to notify |
| 113 | + * @param message the message to send to the teams |
| 114 | + * @param crId the cr id |
| 115 | + * @param budgetImpact the amount of budget requested for the cr |
| 116 | + * @returns the channelId and timestamp of the messages sent in slack |
| 117 | + */ |
| 118 | +export const sendSlackChangeRequestNotification = async ( |
| 119 | + team: Team, |
| 120 | + message: string, |
| 121 | + crId: number, |
| 122 | + budgetImpact?: number |
| 123 | +): Promise<{ channelId: string; ts: string }[]> => { |
| 124 | + if (process.env.NODE_ENV !== 'production') return []; // don't send msgs unless in prod |
| 125 | + const msgs: { channelId: string; ts: string }[] = []; |
| 126 | + const fullMsg = `:tada: New Change Request! :tada: ${message}`; |
| 127 | + const fullLink = `https://finishlinebyner.com/cr/${crId}`; |
| 128 | + const btnText = `View CR #${crId}`; |
| 129 | + const notification = await sendMessage(team.slackId, fullMsg, fullLink, btnText); |
| 130 | + if (notification) msgs.push(notification); |
| 131 | + |
| 132 | + if (budgetImpact && budgetImpact > 100) { |
| 133 | + const importantNotification = await sendMessage( |
| 134 | + process.env.SLACK_EBOARD_CHANNEL!, |
| 135 | + `${fullMsg} with $${budgetImpact} requested`, |
| 136 | + fullLink, |
| 137 | + btnText |
| 138 | + ); |
| 139 | + if (importantNotification) msgs.push(importantNotification); |
| 140 | + } |
| 141 | + |
| 142 | + return msgs; |
| 143 | +}; |
| 144 | + |
| 145 | +export const sendAndGetSlackCRNotifications = async ( |
| 146 | + teams: Team[], |
| 147 | + changeRequest: Change_Request, |
| 148 | + submitter: User, |
| 149 | + wbsElement: WBS_Element, |
| 150 | + projectWbsName: string |
| 151 | +) => { |
| 152 | + const notifications: { channelId: string; ts: string }[] = []; |
| 153 | + let message = ''; |
| 154 | + switch (changeRequest.type) { |
| 155 | + case 'ACTIVATION': |
| 156 | + message = `${submitter.firstName} ${submitter.lastName} wants to activate ${wbsElement.name} in ${projectWbsName}`; |
| 157 | + break; |
| 158 | + case 'STAGE_GATE': |
| 159 | + message = `${submitter.firstName} ${submitter.lastName} wants to stage gate ${wbsElement.name} in ${projectWbsName}`; |
| 160 | + break; |
| 161 | + default: |
| 162 | + message = `${changeRequest.type} CR submitted by ${submitter.firstName} ${submitter.lastName} for the ${projectWbsName} project`; |
| 163 | + } |
| 164 | + |
| 165 | + const completion: Promise<void>[] = teams.map(async (team) => { |
| 166 | + const sentNotifications: { channelId: string; ts: string }[] = await sendSlackChangeRequestNotification( |
| 167 | + team, |
| 168 | + message, |
| 169 | + changeRequest.crId |
| 170 | + ); |
| 171 | + if (sentNotifications) notifications.push(...sentNotifications); |
| 172 | + }); |
| 173 | + await Promise.all(completion); |
| 174 | + |
| 175 | + return notifications; |
| 176 | +}; |
| 177 | + |
| 178 | +export const sendSlackCRReviewedNotification = async (slackId: string, crId: number) => { |
| 179 | + if (process.env.NODE_ENV !== 'production') return; // don't send msgs unless in prod |
| 180 | + const msgs = []; |
| 181 | + const fullMsg = `:tada: Your Change Request was just reviewed! Click the link to view! :tada:`; |
| 182 | + const fullLink = `https://finishlinebyner.com/cr/${crId}`; |
| 183 | + const btnText = `View CR#${crId}`; |
| 184 | + msgs.push(sendMessage(slackId, fullMsg, fullLink, btnText)); |
| 185 | + |
| 186 | + return Promise.all(msgs); |
| 187 | +}; |
| 188 | + |
| 189 | +/** |
| 190 | + * Replies and reacts to slack messages with the new change request status |
| 191 | + * |
| 192 | + * @param threads the threads of cr slack notifications to reply/react to |
| 193 | + * @param crId the cr id |
| 194 | + * @param approved is the cr approved |
| 195 | + */ |
| 196 | +export const sendSlackCRStatusToThread = async ( |
| 197 | + threads: { |
| 198 | + messageInfoId: string; |
| 199 | + channelId: string; |
| 200 | + timestamp: string; |
| 201 | + changeRequestId: number; |
| 202 | + }[], |
| 203 | + crId: number, |
| 204 | + approved: boolean |
| 205 | +) => { |
| 206 | + if (process.env.NODE_ENV !== 'production') return; // don't send msgs unless in prod |
| 207 | + const fullMsg = `This Change Request was ${approved ? 'approved! :tada:' : 'denied.'} Click the link to view.`; |
| 208 | + const fullLink = `https://finishlinebyner.com/cr/${crId}`; |
| 209 | + const btnText = `View CR#${crId}`; |
| 210 | + try { |
| 211 | + if (threads && threads.length !== 0) { |
| 212 | + const msgs = threads.map((thread) => |
| 213 | + replyToMessageInThread(thread.channelId, thread.timestamp, fullMsg, fullLink, btnText) |
| 214 | + ); |
| 215 | + const reactions = threads.map((thread) => |
| 216 | + reactToMessage(thread.channelId, thread.timestamp, approved ? 'white_check_mark' : 'x') |
| 217 | + ); |
| 218 | + await Promise.all([...msgs, ...reactions]); |
| 219 | + } |
| 220 | + } catch (err: unknown) { |
| 221 | + if (err instanceof Error) { |
| 222 | + throw new HttpException(500, `Failed to send slack notification: ${err.message}`); |
| 223 | + } |
| 224 | + } |
| 225 | +}; |
| 226 | + |
| 227 | +/** |
| 228 | + * Adds the relevant slack notifications for a change request to the change request |
| 229 | + * |
| 230 | + * @param crId the change request to add the slack threads to |
| 231 | + * @param notifications the slack threads to add to the change request |
| 232 | + */ |
| 233 | +export const addSlackThreadsToChangeRequest = async (crId: number, threads: { channelId: string; ts: string }[]) => { |
| 234 | + const promises = threads.map((notification) => |
| 235 | + prisma.message_Info.create({ |
| 236 | + data: { |
| 237 | + changeRequestId: crId, |
| 238 | + channelId: notification.channelId, |
| 239 | + timestamp: notification.ts |
| 240 | + }, |
| 241 | + include: { |
| 242 | + changeRequest: true |
| 243 | + } |
| 244 | + }) |
| 245 | + ); |
| 246 | + await Promise.all(promises); |
| 247 | +}; |
0 commit comments