|
| 1 | +import { |
| 2 | + Body, |
| 3 | + Controller, |
| 4 | + Get, |
| 5 | + NotFoundException, |
| 6 | + Param, |
| 7 | + ParseIntPipe, |
| 8 | + Patch, |
| 9 | + UnprocessableEntityException, |
| 10 | +} from "@nestjs/common"; |
| 11 | +import { |
| 12 | + ApiNotFoundResponse, |
| 13 | + ApiTags, |
| 14 | + ApiUnprocessableEntityResponse, |
| 15 | +} from "@nestjs/swagger"; |
| 16 | +import { AuthorizedParties } from "../../auth/authorized-parties.enum"; |
| 17 | +import { AllowAuthorizedParty, UserToken } from "../../auth/decorators"; |
| 18 | +import { ClientTypeBaseRoute } from "../../types"; |
| 19 | +import BaseController from "../BaseController"; |
| 20 | +import { |
| 21 | + ApplicationOfferingChangeRequestStatusAPIOutDTO, |
| 22 | + ApplicationOfferingDetailsAPIOutDTO, |
| 23 | + StudentApplicationOfferingChangeRequestAPIInDTO, |
| 24 | +} from "./models/application-offering-change-request.dto"; |
| 25 | +import { StudentUserToken } from "../../auth"; |
| 26 | +import { ApplicationOfferingChangeRequestControllerService } from "./application-offering-change-request.controller.service"; |
| 27 | +import { ApplicationOfferingChangeRequestService } from "../../services"; |
| 28 | +import { ApplicationOfferingChangeRequestStatus } from "@sims/sims-db"; |
| 29 | + |
| 30 | +/** |
| 31 | + * Application offering change request controller for students client. |
| 32 | + */ |
| 33 | +@AllowAuthorizedParty(AuthorizedParties.student) |
| 34 | +@Controller("application-offering-change-request") |
| 35 | +@ApiTags(`${ClientTypeBaseRoute.Student}-application-offering-change-request`) |
| 36 | +export class ApplicationOfferingChangeRequestStudentsController extends BaseController { |
| 37 | + constructor( |
| 38 | + private readonly applicationOfferingChangeRequestService: ApplicationOfferingChangeRequestService, |
| 39 | + private readonly applicationOfferingChangeRequestControllerService: ApplicationOfferingChangeRequestControllerService, |
| 40 | + ) { |
| 41 | + super(); |
| 42 | + } |
| 43 | + |
| 44 | + /** |
| 45 | + * Gets the Application Offering Change Request details. |
| 46 | + * @param applicationOfferingChangeRequestId the Application Offering Change Request id. |
| 47 | + * @param studentUserToken student user token to authorize the user. |
| 48 | + * @returns Application Offering Change Request details. |
| 49 | + */ |
| 50 | + @Get(":applicationOfferingChangeRequestId") |
| 51 | + @ApiNotFoundResponse({ |
| 52 | + description: "Not able to find an Application Offering Change Request.", |
| 53 | + }) |
| 54 | + async getApplicationOfferingChangeRequest( |
| 55 | + @Param("applicationOfferingChangeRequestId", ParseIntPipe) |
| 56 | + applicationOfferingChangeRequestId: number, |
| 57 | + @UserToken() |
| 58 | + studentUserToken: StudentUserToken, |
| 59 | + ): Promise<ApplicationOfferingDetailsAPIOutDTO> { |
| 60 | + return this.applicationOfferingChangeRequestControllerService.getApplicationOfferingChangeRequest( |
| 61 | + applicationOfferingChangeRequestId, |
| 62 | + { |
| 63 | + studentId: studentUserToken.studentId, |
| 64 | + applicationOfferingDetails: true, |
| 65 | + }, |
| 66 | + ); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Gets the Application Offering Change Request status. |
| 71 | + * @param applicationOfferingChangeRequestId the Application Offering Change Request id. |
| 72 | + * @param studentUserToken student user token to authorize the user. |
| 73 | + * @returns Application Offering Change Request status. |
| 74 | + */ |
| 75 | + @Get( |
| 76 | + ":applicationOfferingChangeRequestId/application-offering-change-request-status", |
| 77 | + ) |
| 78 | + @ApiNotFoundResponse({ |
| 79 | + description: |
| 80 | + "Not able to get the Application Offering Change Request Status.", |
| 81 | + }) |
| 82 | + async getApplicationOfferingChangeRequestStatus( |
| 83 | + @Param("applicationOfferingChangeRequestId", ParseIntPipe) |
| 84 | + applicationOfferingChangeRequestId: number, |
| 85 | + @UserToken() |
| 86 | + studentUserToken: StudentUserToken, |
| 87 | + ): Promise<ApplicationOfferingChangeRequestStatusAPIOutDTO> { |
| 88 | + const applicationOfferingChangeRequestStatus = |
| 89 | + await this.applicationOfferingChangeRequestService.getApplicationOfferingChangeRequestStatusById( |
| 90 | + applicationOfferingChangeRequestId, |
| 91 | + { |
| 92 | + studentId: studentUserToken.studentId, |
| 93 | + }, |
| 94 | + ); |
| 95 | + if (!applicationOfferingChangeRequestStatus) { |
| 96 | + throw new NotFoundException( |
| 97 | + "Application Offering Change Request not found.", |
| 98 | + ); |
| 99 | + } |
| 100 | + return { status: applicationOfferingChangeRequestStatus }; |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * Updates an application offering change request status. |
| 105 | + * @param applicationOfferingChangeRequestId application offering change request id. |
| 106 | + * @param userToken user token to authorize the user. |
| 107 | + * @param payload information to update the application offering change request status. |
| 108 | + */ |
| 109 | + @Patch(":applicationOfferingChangeRequestId") |
| 110 | + @ApiNotFoundResponse({ |
| 111 | + description: |
| 112 | + "Application offering change not found or not in valid status to be updated.", |
| 113 | + }) |
| 114 | + @ApiUnprocessableEntityResponse({ |
| 115 | + description: |
| 116 | + "Invalid application offering change status or student consent not provided", |
| 117 | + }) |
| 118 | + async updateApplicationOfferingChangeRequestStatus( |
| 119 | + @Param("applicationOfferingChangeRequestId", ParseIntPipe) |
| 120 | + applicationOfferingChangeRequestId: number, |
| 121 | + @UserToken() |
| 122 | + userToken: StudentUserToken, |
| 123 | + @Body() |
| 124 | + payload: StudentApplicationOfferingChangeRequestAPIInDTO, |
| 125 | + ): Promise<void> { |
| 126 | + const applicationOfferingChangeRequest = |
| 127 | + await this.applicationOfferingChangeRequestService.applicationOfferingChangeRequestExists( |
| 128 | + applicationOfferingChangeRequestId, |
| 129 | + { |
| 130 | + applicationOfferingChangeRequestStatus: |
| 131 | + ApplicationOfferingChangeRequestStatus.InProgressWithStudent, |
| 132 | + studentId: userToken.studentId, |
| 133 | + }, |
| 134 | + ); |
| 135 | + if (!applicationOfferingChangeRequest) { |
| 136 | + throw new NotFoundException( |
| 137 | + "Application offering change not found or not in valid status to be updated.", |
| 138 | + ); |
| 139 | + } |
| 140 | + if ( |
| 141 | + !( |
| 142 | + payload.applicationOfferingChangeRequestStatus === |
| 143 | + ApplicationOfferingChangeRequestStatus.InProgressWithSABC || |
| 144 | + payload.applicationOfferingChangeRequestStatus === |
| 145 | + ApplicationOfferingChangeRequestStatus.DeclinedByStudent |
| 146 | + ) |
| 147 | + ) { |
| 148 | + throw new UnprocessableEntityException( |
| 149 | + "Invalid application offering change request status.", |
| 150 | + ); |
| 151 | + } |
| 152 | + if ( |
| 153 | + payload.applicationOfferingChangeRequestStatus === |
| 154 | + ApplicationOfferingChangeRequestStatus.InProgressWithSABC && |
| 155 | + !payload.studentConsent |
| 156 | + ) { |
| 157 | + throw new UnprocessableEntityException( |
| 158 | + "Student consent is required to update the application offering change request status.", |
| 159 | + ); |
| 160 | + } |
| 161 | + await this.applicationOfferingChangeRequestService.updateApplicationOfferingChangeRequest( |
| 162 | + applicationOfferingChangeRequestId, |
| 163 | + payload.applicationOfferingChangeRequestStatus, |
| 164 | + payload.studentConsent, |
| 165 | + ); |
| 166 | + } |
| 167 | +} |
0 commit comments