|
| 1 | +import { Router, Request, Response } from "express"; |
| 2 | +import { strongJwtVerification } from "../../middleware/verify-jwt.js"; |
| 3 | + |
| 4 | +import { JwtPayload } from "../auth/auth-models.js"; |
| 5 | +import { DecisionInfo } from "../../database/decision-db.js"; |
| 6 | +import Models from "../../database/models.js"; |
| 7 | +import { hasElevatedPerms } from "../auth/auth-lib.js"; |
| 8 | +import { ApplicantDecisionFormat } from "./admission-formats.js"; |
| 9 | +import { StatusCode } from "status-code-enum"; |
| 10 | + |
| 11 | +const admissionRouter: Router = Router(); |
| 12 | + |
| 13 | +/** |
| 14 | + * @api {get} /admission/ GET /admission/ |
| 15 | + * @apiGroup Admission |
| 16 | + * @apiDescription Gets all applicants who don't have an email sent |
| 17 | + * |
| 18 | + * @apiSuccess (200: Success) {Json} entries The list of applicants without email sent |
| 19 | + * @apiSuccessExample Example Success Response (Staff POV) |
| 20 | + * HTTP/1.1 200 OK |
| 21 | + * [ |
| 22 | + * { |
| 23 | + * "userId": "user1", |
| 24 | + * "status": "ACCEPTED", |
| 25 | + * "response": "ACCEPTED", |
| 26 | + * "reviewer": "reviewer1", |
| 27 | + * "emailSent": false |
| 28 | + * }, |
| 29 | + * { |
| 30 | + * "userId": "user3", |
| 31 | + * "status": "WAITLISTED", |
| 32 | + * "response": "PENDING", |
| 33 | + * "reviewer": "reviewer1", |
| 34 | + * "emailSent": false |
| 35 | + * }, |
| 36 | + * { |
| 37 | + * "userId": "user4", |
| 38 | + * "status": "WAITLISTED", |
| 39 | + * "response": "PENDING", |
| 40 | + * "reviewer": "reviewer1", |
| 41 | + * "emailSent": false |
| 42 | + * } |
| 43 | + * ] |
| 44 | + * @apiUser strongVerifyErrors |
| 45 | + * @apiError (500: Internal Server Error) {String} InternalError occurred on the server. |
| 46 | + * @apiError (403: Forbidden) {String} Forbidden API accessed by user without valid perms. |
| 47 | + * */ |
| 48 | +admissionRouter.get("/", strongJwtVerification, async (_: Request, res: Response) => { |
| 49 | + const token: JwtPayload = res.locals.payload as JwtPayload; |
| 50 | + if (!hasElevatedPerms(token)) { |
| 51 | + return res.status(StatusCode.ClientErrorForbidden).send({ error: "Forbidden" }); |
| 52 | + } |
| 53 | + try { |
| 54 | + const filteredEntries: DecisionInfo[] = await Models.DecisionInfo.find({ emailSent: false }); |
| 55 | + return res.status(StatusCode.SuccessOK).send(filteredEntries); |
| 56 | + } catch (error) { |
| 57 | + console.error(error); |
| 58 | + } |
| 59 | + return res.status(StatusCode.ClientErrorBadRequest).send({ error: "InternalError" }); |
| 60 | +}); |
| 61 | +/** |
| 62 | + * @api {put} /admission/ PUT /admission/ |
| 63 | + * @apiGroup Admission |
| 64 | + * @apiDescription Updates the admission status of all applicants |
| 65 | + * |
| 66 | + * @apiHeader {String} Authorization Admin or Staff JWT Token |
| 67 | + * |
| 68 | + * @apiBody {Json} entries List of Applicants whose status needs to be updated |
| 69 | + * |
| 70 | + * @apiParamExample Example Request (Staff): |
| 71 | + * HTTP/1.1 PUT /admission/ |
| 72 | + * [ |
| 73 | + * { |
| 74 | + * "userId": "user1", |
| 75 | + * "status": "ACCEPTED" |
| 76 | + * }, |
| 77 | + * { |
| 78 | + * "userId": "user2", |
| 79 | + * "status": "REJECTED" |
| 80 | + * }, |
| 81 | + * { |
| 82 | + * "userId": "user3", |
| 83 | + * "status": "WAITLISTED" |
| 84 | + * } |
| 85 | + * ] |
| 86 | + * |
| 87 | + * @apiSuccess (200: Success) {String} StatusSuccess |
| 88 | + * |
| 89 | + * @apiUse strongVerifyErrors |
| 90 | + * @apiError (500: Internal Server Error) {String} InternalError occurred on the server. |
| 91 | + * @apiError (403: Forbidden) {String} Forbidden API accessed by user without valid perms. |
| 92 | + * */ |
| 93 | +admissionRouter.put("/", strongJwtVerification, async (req: Request, res: Response) => { |
| 94 | + const token: JwtPayload = res.locals.payload as JwtPayload; |
| 95 | + if (!hasElevatedPerms(token)) { |
| 96 | + return res.status(StatusCode.ClientErrorForbidden).send({ error: "Forbidden" }); |
| 97 | + } |
| 98 | + const updateEntries: ApplicantDecisionFormat[] = req.body as ApplicantDecisionFormat[]; |
| 99 | + const ops = updateEntries.map((entry) => { |
| 100 | + return Models.DecisionInfo.findOneAndUpdate({ userId: entry.userId }, { $set: { status: entry.status } }); |
| 101 | + }); |
| 102 | + try { |
| 103 | + await Promise.all(ops); |
| 104 | + return res.status(StatusCode.SuccessOK).send({ message: "StatusSuccess" }); |
| 105 | + } catch (error) { |
| 106 | + console.log(error); |
| 107 | + } |
| 108 | + return res.status(StatusCode.ClientErrorBadRequest).send("InternalError"); |
| 109 | +}); |
| 110 | +export default admissionRouter; |
0 commit comments