|
1 | | -import { DesignReview, isAdmin } from 'shared'; |
| 1 | +import { DesignReview, WbsNumber, isAdmin, isLeadership } from 'shared'; |
2 | 2 | import prisma from '../prisma/prisma'; |
3 | | -import { AccessDeniedAdminOnlyException, DeletedException, NotFoundException } from '../utils/errors.utils'; |
4 | | -import { User } from '@prisma/client'; |
| 3 | +import { |
| 4 | + AccessDeniedAdminOnlyException, |
| 5 | + DeletedException, |
| 6 | + NotFoundException, |
| 7 | + HttpException, |
| 8 | + AccessDeniedException |
| 9 | +} from '../utils/errors.utils'; |
| 10 | +import { User, Design_Review_Status } from '@prisma/client'; |
5 | 11 | import designReviewQueryArgs from '../prisma-query-args/design-review.query-args'; |
6 | 12 | import { designReviewTransformer } from '../transformers/design-review.transformer'; |
| 13 | +import { sendSlackDesignReviewNotification } from '../utils/slack.utils'; |
7 | 14 |
|
8 | 15 | export default class DesignReviewService { |
9 | 16 | /** |
@@ -45,6 +52,142 @@ export default class DesignReviewService { |
45 | 52 | return designReviewTransformer(deletedDesignReview); |
46 | 53 | } |
47 | 54 |
|
| 55 | + /** |
| 56 | + * Creates a design review |
| 57 | + * @param submitter user who submitted the design review |
| 58 | + * @param dateScheduled when the design review is scheduled for |
| 59 | + * @param teamTypeId team type id of the design review |
| 60 | + * @param requiredMemberIds ids of the required members to attend the design review |
| 61 | + * @param optionalMemberIds ids of the optional members to attend the design reivew |
| 62 | + * @param isOnline if design review is online |
| 63 | + * @param isInPerson if design review is in person |
| 64 | + * @param docTemplateLink link to the doc template |
| 65 | + * @param wbsNum wbs number for the design review |
| 66 | + * @param meetingTimes the meeting times for the design review |
| 67 | + * @param zoomLink link for the zoom if design review is online |
| 68 | + * @param location location of the design review if in person |
| 69 | + * @returns a design review |
| 70 | + */ |
| 71 | + static async createDesignReview( |
| 72 | + submitter: User, |
| 73 | + dateScheduled: Date, |
| 74 | + teamTypeId: string, |
| 75 | + requiredMemberIds: number[], |
| 76 | + optionalMemberIds: number[], |
| 77 | + isOnline: boolean, |
| 78 | + isInPerson: boolean, |
| 79 | + docTemplateLink: string, |
| 80 | + wbsNum: WbsNumber, |
| 81 | + meetingTimes: number[], |
| 82 | + zoomLink?: string, |
| 83 | + location?: string |
| 84 | + ): Promise<DesignReview> { |
| 85 | + if (!isLeadership(submitter.role)) throw new AccessDeniedException('create design review'); |
| 86 | + |
| 87 | + const teamType = await prisma.teamType.findFirst({ |
| 88 | + where: { teamTypeId } |
| 89 | + }); |
| 90 | + |
| 91 | + if (!teamType) { |
| 92 | + throw new NotFoundException('Team Type', teamTypeId); |
| 93 | + } |
| 94 | + |
| 95 | + const wbsElement = await prisma.wBS_Element.findUnique({ |
| 96 | + where: { |
| 97 | + wbsNumber: { |
| 98 | + carNumber: wbsNum.carNumber, |
| 99 | + projectNumber: wbsNum.projectNumber, |
| 100 | + workPackageNumber: wbsNum.workPackageNumber |
| 101 | + } |
| 102 | + } |
| 103 | + }); |
| 104 | + |
| 105 | + if (!wbsElement) { |
| 106 | + throw new NotFoundException('WBS Element', wbsNum.carNumber); |
| 107 | + } |
| 108 | + |
| 109 | + if (meetingTimes.length === 0) { |
| 110 | + throw new HttpException(400, 'There must be at least one meeting time'); |
| 111 | + } |
| 112 | + |
| 113 | + // checks if the meeting times are valid times and are all continous (ie. [1, 2, 3, 4]) |
| 114 | + for (let i = 0; i < meetingTimes.length; i++) { |
| 115 | + if (i === meetingTimes.length - 1) { |
| 116 | + if (meetingTimes[i] < 0 || meetingTimes[i] > 83) { |
| 117 | + throw new HttpException(400, 'Meeting times have to be in range 0-83'); |
| 118 | + } |
| 119 | + continue; |
| 120 | + } |
| 121 | + if (meetingTimes[i + 1] - meetingTimes[i] !== 1 || meetingTimes[i] < 0 || meetingTimes[i] > 83) { |
| 122 | + throw new HttpException(400, 'Meeting times have to be continous and in range 0-83'); |
| 123 | + } |
| 124 | + } |
| 125 | + |
| 126 | + if (isOnline && !zoomLink) { |
| 127 | + throw new HttpException(400, 'If the design review is online then there needs to be a zoom link'); |
| 128 | + } |
| 129 | + |
| 130 | + if (isInPerson && !location) { |
| 131 | + throw new HttpException(400, 'If the design review is in person then there needs to be a location'); |
| 132 | + } |
| 133 | + |
| 134 | + if (dateScheduled.valueOf() < new Date().valueOf()) { |
| 135 | + throw new HttpException(400, 'Design review cannot be scheduled for a past day'); |
| 136 | + } |
| 137 | + |
| 138 | + const designReview = await prisma.design_Review.create({ |
| 139 | + data: { |
| 140 | + dateScheduled, |
| 141 | + dateCreated: new Date(), |
| 142 | + status: Design_Review_Status.UNCONFIRMED, |
| 143 | + location, |
| 144 | + isOnline, |
| 145 | + isInPerson, |
| 146 | + zoomLink, |
| 147 | + docTemplateLink, |
| 148 | + userCreated: { connect: { userId: submitter.userId } }, |
| 149 | + teamType: { connect: { teamTypeId: teamType.teamTypeId } }, |
| 150 | + requiredMembers: { connect: requiredMemberIds.map((memberId) => ({ userId: memberId })) }, |
| 151 | + optionalMembers: { connect: optionalMemberIds.map((memberId) => ({ userId: memberId })) }, |
| 152 | + meetingTimes, |
| 153 | + wbsElement: { connect: { wbsElementId: wbsElement.wbsElementId } } |
| 154 | + }, |
| 155 | + ...designReviewQueryArgs |
| 156 | + }); |
| 157 | + |
| 158 | + const members = await prisma.user.findMany({ |
| 159 | + where: { userId: { in: optionalMemberIds.concat(requiredMemberIds) } } |
| 160 | + }); |
| 161 | + |
| 162 | + if (!members) { |
| 163 | + throw new NotFoundException('User', 'Cannot find members who are invited to the design review'); |
| 164 | + } |
| 165 | + |
| 166 | + // get the user settings for all the members invited, who are leaderingship |
| 167 | + const memberUserSettings = await prisma.user_Settings.findMany({ |
| 168 | + where: { userId: { in: members.map((member) => member.userId) } } |
| 169 | + }); |
| 170 | + |
| 171 | + if (!memberUserSettings) { |
| 172 | + throw new NotFoundException('User Settings', 'Cannot find settings of members'); |
| 173 | + } |
| 174 | + |
| 175 | + // send a slack message to all leadership invited to the design review |
| 176 | + for (const memberUserSetting of memberUserSettings) { |
| 177 | + if (memberUserSetting.slackId) { |
| 178 | + try { |
| 179 | + await sendSlackDesignReviewNotification(memberUserSetting.slackId, designReview.designReviewId); |
| 180 | + } catch (err: unknown) { |
| 181 | + if (err instanceof Error) { |
| 182 | + throw new HttpException(500, `Failed to send slack notification: ${err.message}`); |
| 183 | + } |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | + |
| 188 | + return designReviewTransformer(designReview); |
| 189 | + } |
| 190 | + |
48 | 191 | /** |
49 | 192 | * Retrieves a single design review |
50 | 193 | * |
|
0 commit comments