Skip to content

Commit

Permalink
added role checker
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobc2700 committed Apr 22, 2024
1 parent c15ab72 commit 1bff567
Showing 1 changed file with 55 additions and 44 deletions.
99 changes: 55 additions & 44 deletions src/services/registration/registration-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,66 +5,77 @@ import {
RegistrationSchema,

Check failure on line 5 in src/services/registration/registration-router.ts

View workflow job for this annotation

GitHub Actions / lint

'RegistrationSchema' is defined but never used

Check failure on line 5 in src/services/registration/registration-router.ts

View workflow job for this annotation

GitHub Actions / build

'RegistrationSchema' is declared but its value is never read.
} 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";

Check failure on line 9 in src/services/registration/registration-router.ts

View workflow job for this annotation

GitHub Actions / lint

'Role' is defined but never used

Check failure on line 9 in src/services/registration/registration-router.ts

View workflow job for this annotation

GitHub Actions / build

'Role' is declared but its value is never read.

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;

0 comments on commit 1bff567

Please sign in to comment.