|
| 1 | +import { Request, Response, Router } from "express"; |
| 2 | +import { StatusCode } from "status-code-enum"; |
| 3 | +import { strongJwtVerification } from "../../middleware/verify-jwt.js"; |
| 4 | +import { JwtPayload } from "../auth/auth-models.js"; |
| 5 | +import { hasElevatedPerms } from "../auth/auth-lib.js"; |
| 6 | +import { DecisionStatus, DecisionResponse, DecisionInfo } from "../../database/decision-db.js"; |
| 7 | +import Models from "../../database/models.js"; |
| 8 | + |
| 9 | +const rsvpRouter: Router = Router(); |
| 10 | + |
| 11 | +/** |
| 12 | + * @api {get} /rsvp/:USERID/ GET /rsvp/:USERID/ |
| 13 | + * @apiGroup rsvp |
| 14 | + * @apiDescription Check RSVP decision for a given userId, provided that the current user has elevated perms |
| 15 | + * |
| 16 | + * |
| 17 | + * @apiSuccess (200: Success) {string} userId |
| 18 | + * @apiSuccess (200: Success) {string} User's applicatoin status |
| 19 | + * @apiSuccess (200: Success) {string} User's Response (whether or whether not they're attending) |
| 20 | + * @apiSuccess (200: Success) {string} Reviwer |
| 21 | + * @apiSuccess (200: Success) {boolean} Whether email has been sent |
| 22 | + * @apiSuccessExample Example Success Response: |
| 23 | + * HTTP/1.1 200 OK |
| 24 | + * { |
| 25 | + * "userId": "github0000001", |
| 26 | + * "status": "ACCEPTED", |
| 27 | + * "response": "PENDING", |
| 28 | + * "reviewer": "reviewer1", |
| 29 | + * "emailSent": true |
| 30 | + * } |
| 31 | + * |
| 32 | + * @apiUse strongVerifyErrors |
| 33 | + */ |
| 34 | +rsvpRouter.get("/:USERID", strongJwtVerification, async (req: Request, res: Response) => { |
| 35 | + const userId: string | undefined = req.params.USERID; |
| 36 | + |
| 37 | + const payload: JwtPayload = res.locals.payload as JwtPayload; |
| 38 | + |
| 39 | + //Sends error if caller doesn't have elevated perms |
| 40 | + if (!hasElevatedPerms(payload)) { |
| 41 | + return res.status(StatusCode.ClientErrorForbidden).send({ error: "Forbidden" }); |
| 42 | + } |
| 43 | + |
| 44 | + const queryResult: DecisionInfo | null = await Models.DecisionInfo.findOne({ userId: userId }); |
| 45 | + |
| 46 | + //Returns error if query is empty |
| 47 | + if (!queryResult) { |
| 48 | + return res.status(StatusCode.ClientErrorBadRequest).send({ error: "UserNotFound" }); |
| 49 | + } |
| 50 | + |
| 51 | + return res.status(StatusCode.SuccessOK).send(queryResult); |
| 52 | +}); |
| 53 | + |
| 54 | +/** |
| 55 | + * @api {get} /rsvp/ GET /rsvp/ |
| 56 | + * @apiGroup rsvp |
| 57 | + * @apiDescription Check RSVP decision for current user, returns filtered info for attendees and unfiltered info for staff/admin |
| 58 | + * |
| 59 | + * |
| 60 | + * @apiSuccess (200: Success) {string} userId |
| 61 | + * @apiSuccess (200: Success) {string} User's applicatoin status |
| 62 | + * @apiSuccess (200: Success) {string} User's Response (whether or whether not they're attending) |
| 63 | + * @apiSuccessExample Example Success Response (caller is a user): |
| 64 | + * HTTP/1.1 200 OK |
| 65 | + * { |
| 66 | + * "userId": "github0000001", |
| 67 | + * "status": "ACCEPTED", |
| 68 | + * "response": "ACCEPTED", |
| 69 | + * } |
| 70 | + * |
| 71 | + * @apiSuccessExample Example Success Response (caller is a staff/admin): |
| 72 | + * HTTP/1.1 200 OK |
| 73 | + * { |
| 74 | + * "userId": "github0000001", |
| 75 | + * "status": "ACCEPTED", |
| 76 | + * "response": "ACCEPTED", |
| 77 | + * "reviewer": "reviewer1", |
| 78 | + * "emailSent": true, |
| 79 | + * } |
| 80 | + * |
| 81 | + * @apiUse strongVerifyErrors |
| 82 | + */ |
| 83 | +rsvpRouter.get("/", strongJwtVerification, async (_: Request, res: Response) => { |
| 84 | + const payload: JwtPayload = res.locals.payload as JwtPayload; |
| 85 | + |
| 86 | + const userId: string = payload.id; |
| 87 | + |
| 88 | + const queryResult: DecisionInfo | null = await Models.DecisionInfo.findOne({ userId: userId }); |
| 89 | + |
| 90 | + //Returns error if query is empty |
| 91 | + if (!queryResult) { |
| 92 | + return res.status(StatusCode.ClientErrorBadRequest).send({ error: "UserNotFound" }); |
| 93 | + } |
| 94 | + |
| 95 | + //Filters data if caller doesn't have elevated perms |
| 96 | + if (!hasElevatedPerms(payload)) { |
| 97 | + return res |
| 98 | + .status(StatusCode.SuccessOK) |
| 99 | + .send({ userId: queryResult.userId, status: queryResult.status, response: queryResult.response }); |
| 100 | + } |
| 101 | + |
| 102 | + return res.status(StatusCode.SuccessOK).send(queryResult); |
| 103 | +}); |
| 104 | + |
| 105 | +/** |
| 106 | + * @api {put} /rsvp/ Put /rsvp/ |
| 107 | + * @apiGroup rsvp |
| 108 | + * @apiDescription Updates an rsvp for the currently authenticated user (determined by the JWT in the Authorization header). |
| 109 | + * |
| 110 | + * @apiBody {boolean} isAttending Whether or whether not the currently authenticated user is attending |
| 111 | + * @apiParamExample {json} Example Request: |
| 112 | + * { |
| 113 | + * "isAttending": false |
| 114 | + * } |
| 115 | + * |
| 116 | + * @apiSuccess (200: Success) {string} userId |
| 117 | + * @apiSuccess (200: Success) {string} User's applicatoin status |
| 118 | + * @apiSuccess (200: Success) {string} User's Response (whether or whether not they're attending) |
| 119 | + * @apiSuccess (200: Success) {string} Reviwer |
| 120 | + * @apiSuccess (200: Success) {boolean} Whether email has been sent |
| 121 | + * @apiSuccessExample Example Success Response: |
| 122 | + * HTTP/1.1 200 OK |
| 123 | + * { |
| 124 | + * "userId": "github0000001", |
| 125 | + * "status": "ACCEPTED", |
| 126 | + * "response": "DECLINED", |
| 127 | + * "reviewer": "reviewer1", |
| 128 | + * "emailSent": true |
| 129 | + * } |
| 130 | + * |
| 131 | + * @apiUse strongVerifyErrors |
| 132 | + */ |
| 133 | +rsvpRouter.put("/", strongJwtVerification, async (req: Request, res: Response) => { |
| 134 | + const rsvp: boolean | undefined = req.body.isAttending; |
| 135 | + |
| 136 | + //Returns error if request body has no isAttending parameter |
| 137 | + if (rsvp === undefined) { |
| 138 | + return res.status(StatusCode.ClientErrorBadRequest).send({ error: "InvalidParams" }); |
| 139 | + } |
| 140 | + |
| 141 | + const payload: JwtPayload = res.locals.payload as JwtPayload; |
| 142 | + |
| 143 | + const userid: string = payload.id; |
| 144 | + |
| 145 | + const queryResult: DecisionInfo | null = await Models.DecisionInfo.findOne({ userId: userid }); |
| 146 | + |
| 147 | + //Returns error if query is empty |
| 148 | + if (!queryResult) { |
| 149 | + return res.status(StatusCode.ClientErrorBadRequest).send({ error: "UserNotFound" }); |
| 150 | + } |
| 151 | + |
| 152 | + //If the current user has not been accepted, send an error |
| 153 | + if (queryResult.status != DecisionStatus.ACCEPTED) { |
| 154 | + return res.status(StatusCode.ClientErrorForbidden).send({ error: "NotAccepted" }); |
| 155 | + } |
| 156 | + |
| 157 | + //If current user has been accepted, update their RSVP decision to "ACCEPTED"/"DECLINED" acoordingly |
| 158 | + const updatedDecision: DecisionInfo | null = await Models.DecisionInfo.findOneAndUpdate( |
| 159 | + { userId: queryResult.userId }, |
| 160 | + { |
| 161 | + status: queryResult.status, |
| 162 | + response: rsvp ? DecisionResponse.ACCEPTED : DecisionResponse.DECLINED, |
| 163 | + }, |
| 164 | + { new: true }, |
| 165 | + ); |
| 166 | + |
| 167 | + if (updatedDecision) { |
| 168 | + //return res.status(StatusCode.SuccessOK).send(updatedDecision.toObject()); |
| 169 | + return res.status(StatusCode.SuccessOK).send(updatedDecision); |
| 170 | + } else { |
| 171 | + return res.status(StatusCode.ServerErrorInternal).send({ error: "InternalError" }); |
| 172 | + } |
| 173 | +}); |
| 174 | + |
| 175 | +export default rsvpRouter; |
0 commit comments