|
1 | | -import { DesignReview, WbsNumber, isAdmin, isLeadership } from 'shared'; |
| 1 | +import { Design_Review_Status, User } from '@prisma/client'; |
| 2 | +import { DesignReview, WbsNumber, isAdmin, isLeadership, isNotLeadership } from 'shared'; |
2 | 3 | import prisma from '../prisma/prisma'; |
3 | 4 | import { |
4 | | - AccessDeniedAdminOnlyException, |
5 | | - DeletedException, |
6 | 5 | NotFoundException, |
| 6 | + AccessDeniedMemberException, |
| 7 | + DeletedException, |
7 | 8 | HttpException, |
| 9 | + AccessDeniedAdminOnlyException, |
8 | 10 | AccessDeniedException |
9 | 11 | } from '../utils/errors.utils'; |
10 | | -import { User, Design_Review_Status } from '@prisma/client'; |
11 | | -import designReviewQueryArgs from '../prisma-query-args/design-review.query-args'; |
12 | | -import { designReviewTransformer } from '../transformers/design-review.transformer'; |
| 12 | +import { getUsers, getPrismaQueryUserIds } from '../utils/users.utils'; |
| 13 | +import { validateMeetingTimes } from '../utils/design-reviews.utils'; |
| 14 | +import designReviewQueryArgs from '../prisma-query-args/design-reviews.query-args'; |
| 15 | +import { designReviewTransformer } from '../transformers/design-reviews.transformer'; |
13 | 16 | import { sendSlackDesignReviewNotification } from '../utils/slack.utils'; |
14 | | - |
15 | | -export default class DesignReviewService { |
| 17 | +export default class DesignReviewsService { |
16 | 18 | /** |
17 | 19 | * Gets all design reviews in the database |
18 | 20 | * @returns All of the design reviews |
@@ -207,4 +209,111 @@ export default class DesignReviewService { |
207 | 209 |
|
208 | 210 | return designReviewTransformer(designReview); |
209 | 211 | } |
| 212 | + |
| 213 | + /** |
| 214 | + * Edits a Design_Review in the database |
| 215 | + * @param user the user editing the design review (must be leadership) |
| 216 | + * @param designReviewId the id of the design review to edit |
| 217 | + * @param dateScheduled the date of the design review |
| 218 | + * @param teamTypeId the team that the design_review is for (software, electrical, etc.) |
| 219 | + * @param requiredMembersIds required members Ids for the design review |
| 220 | + * @param optionalMembersIds optional members Ids for the design review |
| 221 | + * @param isOnline is the design review online (IF TRUE: zoom link should be requried)) |
| 222 | + * @param isInPerson is the design review in person (IF TRUE: location should be required) |
| 223 | + * @param zoomLink the zoom link for the design review meeting |
| 224 | + * @param location the location for the design review meeting |
| 225 | + * @param docTemplateLink the document template link for the design review |
| 226 | + * @param status see Design_Review_Status enum |
| 227 | + * @param attendees the attendees for the design review (should they have any relation to the other shit / can't edit this after STATUS: DONE) |
| 228 | + * @param meetingTimes meeting time must be between 0-83 (Monday 12am - Sunday 12am, 1hr minute increments) |
| 229 | + */ |
| 230 | + |
| 231 | + static async editDesignReview( |
| 232 | + user: User, |
| 233 | + designReviewId: string, |
| 234 | + dateScheduled: Date, |
| 235 | + teamTypeId: string, |
| 236 | + requiredMembersIds: number[], |
| 237 | + optionalMembersIds: number[], |
| 238 | + isOnline: boolean, |
| 239 | + isInPerson: boolean, |
| 240 | + zoomLink: string | null, |
| 241 | + location: string | null, |
| 242 | + docTemplateLink: string | null, |
| 243 | + status: Design_Review_Status, |
| 244 | + attendees: number[], |
| 245 | + meetingTimes: number[] |
| 246 | + ): Promise<DesignReview> { |
| 247 | + // verify user is allowed to edit work package |
| 248 | + if (isNotLeadership(user.role)) throw new AccessDeniedMemberException('edit design reviews'); |
| 249 | + |
| 250 | + // make sure the requiredMembersIds are not in the optionalMembers |
| 251 | + if (requiredMembersIds.length > 0 && requiredMembersIds.some((rMemberId) => optionalMembersIds.includes(rMemberId))) { |
| 252 | + throw new HttpException(400, 'required members cannot be in optional members'); |
| 253 | + } |
| 254 | + |
| 255 | + // make sure there is a zoom link if the design review is online |
| 256 | + if (isOnline && zoomLink === null) { |
| 257 | + throw new HttpException(400, 'zoom link is required for online design reviews'); |
| 258 | + } |
| 259 | + // make sure there is a location if the design review is in person |
| 260 | + if (isInPerson && location === null) { |
| 261 | + throw new HttpException(400, 'location is required for in person design reviews'); |
| 262 | + } |
| 263 | + |
| 264 | + // throws if meeting times are not: consecutive and between 0-83 |
| 265 | + meetingTimes = validateMeetingTimes(meetingTimes); |
| 266 | + |
| 267 | + // docTemplateLink is required if the status is scheduled or done |
| 268 | + if (status === Design_Review_Status.SCHEDULED || status === Design_Review_Status.DONE) { |
| 269 | + if (docTemplateLink == null) { |
| 270 | + throw new HttpException(400, 'doc template link is required for scheduled and done design reviews'); |
| 271 | + } |
| 272 | + } |
| 273 | + // validate the design review exists and is not deleted |
| 274 | + const originaldesignReview = await prisma.design_Review.findUnique({ |
| 275 | + where: { designReviewId } |
| 276 | + }); |
| 277 | + if (!originaldesignReview) throw new NotFoundException('Design Review', designReviewId); |
| 278 | + if (originaldesignReview.dateDeleted) throw new DeletedException('Design Review', designReviewId); |
| 279 | + |
| 280 | + // validate the teamTypeId exists |
| 281 | + const teamType = await prisma.teamType.findUnique({ |
| 282 | + where: { teamTypeId } |
| 283 | + }); |
| 284 | + if (!teamType) throw new NotFoundException('Team Type', teamTypeId); |
| 285 | + |
| 286 | + // throw if a user isn't found, then build prisma queries for connecting userIds |
| 287 | + const updatedRequiredMembers = getPrismaQueryUserIds(await getUsers(requiredMembersIds)); |
| 288 | + const updatedOptionalMembers = getPrismaQueryUserIds(await getUsers(optionalMembersIds)); |
| 289 | + const updatedAttendees = getPrismaQueryUserIds(await getUsers(attendees)); |
| 290 | + |
| 291 | + // actually try to update the design review |
| 292 | + const updateDesignReview = await prisma.design_Review.update({ |
| 293 | + where: { designReviewId }, |
| 294 | + ...designReviewQueryArgs, |
| 295 | + data: { |
| 296 | + designReviewId, |
| 297 | + dateScheduled, |
| 298 | + meetingTimes, |
| 299 | + status, |
| 300 | + teamTypeId, |
| 301 | + requiredMembers: { |
| 302 | + set: updatedRequiredMembers |
| 303 | + }, |
| 304 | + optionalMembers: { |
| 305 | + set: updatedOptionalMembers |
| 306 | + }, |
| 307 | + location, |
| 308 | + isOnline, |
| 309 | + isInPerson, |
| 310 | + zoomLink, |
| 311 | + docTemplateLink, |
| 312 | + attendees: { |
| 313 | + set: updatedAttendees |
| 314 | + } |
| 315 | + } |
| 316 | + }); |
| 317 | + return designReviewTransformer(updateDesignReview); |
| 318 | + } |
210 | 319 | } |
0 commit comments