Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added three mentor registration routes #72

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import eventRouter from "../src/services/event/event-router.js";
import profileRouter from "../src/services/profile/profile-router.js";
import newsletterRouter from "../src/services/newsletter/newsletter-router.js";
import staffRouter from "../src/services/staff/staff-router.js";
import registrationRouter from "../src/services/registration/registration-router.js";

const app: Application = express();

Expand All @@ -29,7 +30,7 @@ app.use("/newsletter/", newsletterRouter);
app.use("/event/", eventRouter);
app.use("/profile/", profileRouter);
app.use("/staff/", staffRouter);

app.use("/registration/", registrationRouter);
// Ensure that API is running
app.get("/", (_: Request, res: Response) => {
res.end("API is working!");
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ abstract class Constants {
static readonly PROFILE_DB: string = "profile";
static readonly STAFF_DB: string = "staff";
static readonly USER_DB: string = "user";
static readonly REGISTRATION_DB: string = "registration";
}

export default Constants;
9 changes: 9 additions & 0 deletions src/services/registration/registration-formats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export interface MentorFormat {
id: string,
firstName: string,
lastName: string,
email: string,
shirtSize: string,
github: string,
linkedin: string
}
18 changes: 18 additions & 0 deletions src/services/registration/registration-lib.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { Collection } from "mongodb";
import databaseClient from "../../database.js";
import Constants from "../../constants.js";
import { RegistrationDB, MentorSchema } from "./registration-schemas.js";


export async function getMentor(userID: string):Promise<MentorSchema> {
const collection: Collection = databaseClient.db(Constants.REGISTRATION_DB).collection(RegistrationDB.MENTORS);
try {
const mentor: MentorSchema | null = await collection.findOne({ id: userID }) as MentorSchema | null;
if (mentor) {
return mentor;
}
return Promise.reject("MentorNotFound");
} catch {
return Promise.reject("InternalError");
}
}
80 changes: 80 additions & 0 deletions src/services/registration/registration-router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Router, Request, Response } from "express";

import Constants from "../../constants.js";
import { strongJwtVerification } from "../../middleware/verify-jwt.js";

import { JwtPayload } from "../auth/auth-models.js";
import { hasElevatedPerms } from "../auth/auth-lib.js";
import { getMentor } from "./registration-lib.js";
import { MentorSchema, RegistrationDB } from "./registration-schemas.js";
import { Collection, Document } from "mongodb";
import databaseClient from "../../database.js";
import { MentorFormat } from "./registration-formats.js";

const registrationRouter: Router = Router();

registrationRouter.get("/mentor/:USERID", strongJwtVerification, async (req: Request, res: Response) => {
// If no target user, just GET
if (!req.params.USERID) {
res.redirect("/mentor/");
}

const targetMentor: string = req.params.USERID ?? "";
// Get payload, and check if authorized
const payload: JwtPayload = res.locals.payload as JwtPayload;
if (payload.id == targetMentor || hasElevatedPerms(payload)) {
// Authorized -> return the Mentor
await getMentor(targetMentor).then((mentor: MentorSchema) => {
res.status(Constants.SUCCESS).send(mentor);
}).catch((error:string) => {
res.status(Constants.INTERNAL_ERROR).send(error);
});
} else {
res.status(Constants.FORBIDDEN).send({ error: "No Valid Auth Provided" });
}
});

registrationRouter.get("/mentor/", strongJwtVerification, async (_ : Request, res: Response ):Promise<void> => {
const payload: JwtPayload = res.locals.payload as JwtPayload;
console.log(payload);
try {
const mentor: MentorSchema = await getMentor(payload.id);
res.status(Constants.SUCCESS).send(mentor);
} catch (error) {
if (error == "MentorNotFound") {
res.status(Constants.BAD_REQUEST).send("MentorNotFound");
}
//console.log("Reached");
res.status(Constants.INTERNAL_ERROR).send("InternalError");
}
});

registrationRouter.post("/mentor/", strongJwtVerification, async (req: Request, res: Response):Promise<Response> => {
const token: JwtPayload = res.locals.payload as JwtPayload;

if (!hasElevatedPerms(token)) {
return res.status(Constants.FORBIDDEN).send({ error: "InvalidPermissions" });
}
const mentorData: MentorFormat = req.body as MentorFormat;
if (token && token.id) {
mentorData.id = token.id;
}
if (!mentorData.id || !mentorData.email || !mentorData.firstName || !mentorData.lastName || !mentorData.github || !mentorData.linkedin) {
return res.status(Constants.BAD_REQUEST).send({ error: "InvalidParams" });
}

const collection: Collection<Document> = databaseClient.db(Constants.REGISTRATION_DB).collection(RegistrationDB.MENTORS);

try {
await collection.insertOne(mentorData);
return res.status(Constants.SUCCESS).send({ ...mentorData });
} catch (error) {
console.log(error);
return res.status(Constants.INTERNAL_ERROR).send({ error: "DatabaseError" });
}

});

export default registrationRouter;


19 changes: 19 additions & 0 deletions src/services/registration/registration-schemas.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { Document, ObjectId, WithId } from "mongodb";


export interface MentorSchema extends WithId<Document> {
_id: ObjectId,
id: string,
firstName: string,
lastName: string,
email: string,
shirtSize: string,
github: string,
linkedin: string
}

export enum RegistrationDB {
ATTENDEES = "attendees",
MENTORS = "mentors"
}

Loading
Loading