From 1bff567be0c872e720b4db4d7a7180054d2a927c Mon Sep 17 00:00:00 2001 From: Jacob Chang Date: Mon, 22 Apr 2024 16:00:00 -0500 Subject: [PATCH] added role checker --- .../registration/registration-router.ts | 99 ++++++++++--------- 1 file changed, 55 insertions(+), 44 deletions(-) diff --git a/src/services/registration/registration-router.ts b/src/services/registration/registration-router.ts index 0c315a5..ea97117 100644 --- a/src/services/registration/registration-router.ts +++ b/src/services/registration/registration-router.ts @@ -5,66 +5,77 @@ import { RegistrationSchema, } from "./registration-schema"; import { Database } from "../../database"; -// import cors from "cors"; -// import RoleChecker from "../../middleware/role-checker"; -// import { Role } from "../auth/auth-models"; +import RoleChecker from "../../middleware/role-checker"; +import { Role } from "../auth/auth-models"; const registrationRouter = Router(); // A database upsert operation to save registration mid-progress -registrationRouter.post("/save", async (req, res, next) => { - try { - const registrationData = RegistrationValidator.parse(req.body); +registrationRouter.post( + "/save", + RoleChecker(["USER"]), + async (req, res, next) => { + try { + const registrationData = RegistrationValidator.parse(req.body); - await Database.REGISTRATION.findOneAndUpdate( - { email: registrationData.email }, // only required one - { - ...registrationData, - complete: false, - }, - { upsert: true, new: true, setDefaultsOnInsert: true } - ); + await Database.REGISTRATION.findOneAndUpdate( + { email: registrationData.email }, // only required one + { + ...registrationData, + complete: false, + }, + { upsert: true, new: true, setDefaultsOnInsert: true } + ); - res.status(StatusCodes.OK).json(registrationData); - } catch (error) { - next(error); + res.status(StatusCodes.OK).json(registrationData); + } catch (error) { + next(error); + } } -}); +); -registrationRouter.post("/submit", async (req, res, next) => { - try { - const registrationData = RegistrationValidator.parse(req.body); +registrationRouter.post( + "/submit", + RoleChecker(["USER"]), + async (req, res, next) => { + try { + const registrationData = RegistrationValidator.parse(req.body); - await Database.REGISTRATION.findOneAndUpdate( - { email: registrationData.email }, // only required one - { - ...registrationData, - complete: true, - }, - { upsert: true, new: true, setDefaultsOnInsert: true } - ); + await Database.REGISTRATION.findOneAndUpdate( + { email: registrationData.email }, // only required one + { + ...registrationData, + complete: true, + }, + { upsert: true, new: true, setDefaultsOnInsert: true } + ); - res.status(StatusCodes.OK).json(registrationData); - } catch (error) { - next(error); + res.status(StatusCodes.OK).json(registrationData); + } catch (error) { + next(error); + } } -}); +); // Retrieve registration fields both to repopulate registration info for a user -registrationRouter.get("/get/:email", async (req, res, next) => { - try { - const email = req.params.email; +registrationRouter.get( + "/get/:email", + RoleChecker(["USER"]), + async (req, res, next) => { + try { + const email = req.params.email; - const registration = await Database.REGISTRATION.findOne({ email }); + const registration = await Database.REGISTRATION.findOne({ email }); - if (!registration) { - return { error: "DoesNotExist" }; - } + if (!registration) { + return { error: "DoesNotExist" }; + } - res.status(StatusCodes.OK).json({ registration }); - } catch (error) { - next(error); + res.status(StatusCodes.OK).json({ registration }); + } catch (error) { + next(error); + } } -}); +); export default registrationRouter;