From f28d172a2872fdadba8cd91c8bb02f69dc8169f6 Mon Sep 17 00:00:00 2001 From: Jacob Chang Date: Sun, 14 Jul 2024 18:10:00 -0700 Subject: [PATCH] endpoint --- src/services/attendee/attendee-router.ts | 412 +++++++++++------------ 1 file changed, 206 insertions(+), 206 deletions(-) diff --git a/src/services/attendee/attendee-router.ts b/src/services/attendee/attendee-router.ts index c3323bd..7a200b7 100644 --- a/src/services/attendee/attendee-router.ts +++ b/src/services/attendee/attendee-router.ts @@ -1,206 +1,206 @@ -import { Router } from "express"; -import { StatusCodes } from "http-status-codes"; -import { - AttendeeValidator, - EventIdValidator, - PartialAttendeeValidator, -} from "./attendee-schema"; -import { Database } from "../../database"; -import RoleChecker from "../../middleware/role-checker"; -import { Role } from "../auth/auth-models"; -import dotenv from "dotenv"; -import { generateQrHash } from "./attendee-utils"; - -dotenv.config(); - -const attendeeRouter = Router(); - -// Favorite an event for an attendee -attendeeRouter.post( - "/favorites/:eventId", - RoleChecker([Role.Enum.USER]), - async (req, res, next) => { - const payload = res.locals.payload; - const userId = payload.userId; - const { eventId } = EventIdValidator.parse(req.params); - - try { - const attendee = await Database.ATTENDEE.findOne({ userId }); - - if (!attendee) { - return res - .status(StatusCodes.NOT_FOUND) - .json({ error: "UserNotFound" }); - } - - await Database.ATTENDEE.updateOne( - { userId: userId }, - { $addToSet: { favorites: eventId } } - ); - - return res.status(StatusCodes.OK).json(attendee); - } catch (error) { - next(error); - } - } -); - -// Unfavorite an event for an attendee -attendeeRouter.delete( - "/favorites/:eventId", - RoleChecker([Role.Enum.USER]), - async (req, res, next) => { - const payload = res.locals.payload; - const userId = payload.userId; - const { eventId } = EventIdValidator.parse(req.params); - - try { - const attendee = await Database.ATTENDEE.findOne({ userId }); - - if (!attendee) { - return res - .status(StatusCodes.NOT_FOUND) - .json({ error: "UserNotFound" }); - } - - await Database.ATTENDEE.updateOne( - { userId: userId }, - { $pull: { favorites: eventId } } - ); - - return res.status(StatusCodes.OK).json(attendee); - } catch (error) { - next(error); - } - } -); - -// Get favorite events for an attendee -attendeeRouter.get( - "/favorites", - RoleChecker([Role.Enum.USER]), - async (req, res, next) => { - const payload = res.locals.payload; - const userId = payload.userId; - - try { - const attendee = await Database.ATTENDEE.findOne({ userId }); - - if (!attendee) { - return res - .status(StatusCodes.NOT_FOUND) - .json({ error: "UserNotFound" }); - } - - return res.status(StatusCodes.OK).json(attendee); - } catch (error) { - next(error); - } - } -); - -// Create a new attendee -attendeeRouter.post("/", async (req, res, next) => { - try { - const attendeeData = AttendeeValidator.parse(req.body); - const attendee = new Database.ATTENDEE(attendeeData); - await attendee.save(); - - return res.status(StatusCodes.CREATED).json(attendeeData); - } catch (error) { - next(error); - } -}); - -// generates a unique QR code for each attendee -attendeeRouter.get( - "/qr/", - RoleChecker([Role.Enum.USER]), - async (req, res, next) => { - const payload = res.locals.payload; - - try { - const userId = payload.userId; - const expTime = Math.floor(Date.now() / 1000) + 20; // Current epoch time in seconds + 20 seconds - const qrCodeString = generateQrHash(userId, expTime); - return res.status(StatusCodes.OK).json({ qrCode: qrCodeString }); - } catch (error) { - next(error); - } - } -); - -attendeeRouter.get( - "/", - RoleChecker([Role.Enum.USER]), - async (req, res, next) => { - try { - const payload = res.locals.payload; - const userId = payload.userId; - - // Check if the user exists in the database - const user = await Database.ATTENDEE.findOne({ userId }); - - if (!user) { - return res - .status(StatusCodes.NOT_FOUND) - .json({ error: "UserNotFound" }); - } - - return res.status(StatusCodes.OK).json(user); - } catch (error) { - next(error); - } - } -); - -// Get attendees based on a partial filter in body -attendeeRouter.get( - "/filter", - RoleChecker([Role.Enum.STAFF, Role.Enum.CORPORATE]), - async (req, res, next) => { - try { - const attendeeData = PartialAttendeeValidator.parse(req.body); - const attendees = await Database.ATTENDEE.find( - attendeeData, - "userId" - ); - - return res.status(StatusCodes.OK).json(attendees); - } catch (error) { - next(error); - } - } -); - -// Update an attendee with partial data -attendeeRouter.put( - "/update", - RoleChecker([Role.Enum.USER]), - async (req, res, next) => { - const payload = res.locals.payload; - const userId = payload.userId; - - try { - const updateData = PartialAttendeeValidator.parse(req.body); - - const attendee = await Database.ATTENDEE.findOneAndUpdate( - { userId }, - { $set: updateData }, - { new: true, runValidators: true } - ); - - if (!attendee) { - return res - .status(StatusCodes.NOT_FOUND) - .json({ error: "UserNotFound" }); - } - - return res.status(StatusCodes.OK).json(attendee); - } catch (error) { - next(error); - } - } -); - -export default attendeeRouter; +import { Router } from "express"; +import { StatusCodes } from "http-status-codes"; +import { + AttendeeValidator, + EventIdValidator, + PartialAttendeeValidator, +} from "./attendee-schema"; +import { Database } from "../../database"; +import RoleChecker from "../../middleware/role-checker"; +import { Role } from "../auth/auth-models"; +import dotenv from "dotenv"; +import { generateQrHash } from "./attendee-utils"; + +dotenv.config(); + +const attendeeRouter = Router(); + +// Favorite an event for an attendee +attendeeRouter.post( + "/favorites/:eventId", + RoleChecker([Role.Enum.USER]), + async (req, res, next) => { + const payload = res.locals.payload; + const userId = payload.userId; + const { eventId } = EventIdValidator.parse(req.params); + + try { + const attendee = await Database.ATTENDEE.findOne({ userId }); + + if (!attendee) { + return res + .status(StatusCodes.NOT_FOUND) + .json({ error: "UserNotFound" }); + } + + await Database.ATTENDEE.updateOne( + { userId: userId }, + { $addToSet: { favorites: eventId } } + ); + + return res.status(StatusCodes.OK).json(attendee); + } catch (error) { + next(error); + } + } +); + +// Unfavorite an event for an attendee +attendeeRouter.delete( + "/favorites/:eventId", + RoleChecker([Role.Enum.USER]), + async (req, res, next) => { + const payload = res.locals.payload; + const userId = payload.userId; + const { eventId } = EventIdValidator.parse(req.params); + + try { + const attendee = await Database.ATTENDEE.findOne({ userId }); + + if (!attendee) { + return res + .status(StatusCodes.NOT_FOUND) + .json({ error: "UserNotFound" }); + } + + await Database.ATTENDEE.updateOne( + { userId: userId }, + { $pull: { favorites: eventId } } + ); + + return res.status(StatusCodes.OK).json(attendee); + } catch (error) { + next(error); + } + } +); + +// Get favorite events for an attendee +attendeeRouter.get( + "/favorites", + RoleChecker([Role.Enum.USER]), + async (req, res, next) => { + const payload = res.locals.payload; + const userId = payload.userId; + + try { + const attendee = await Database.ATTENDEE.findOne({ userId }); + + if (!attendee) { + return res + .status(StatusCodes.NOT_FOUND) + .json({ error: "UserNotFound" }); + } + + return res.status(StatusCodes.OK).json(attendee); + } catch (error) { + next(error); + } + } +); + +// Create a new attendee +attendeeRouter.post("/", async (req, res, next) => { + try { + const attendeeData = AttendeeValidator.parse(req.body); + const attendee = new Database.ATTENDEE(attendeeData); + await attendee.save(); + + return res.status(StatusCodes.CREATED).json(attendeeData); + } catch (error) { + next(error); + } +}); + +// generates a unique QR code for each attendee +attendeeRouter.get( + "/qr/", + RoleChecker([Role.Enum.USER]), + async (req, res, next) => { + const payload = res.locals.payload; + + try { + const userId = payload.userId; + const expTime = Math.floor(Date.now() / 1000) + 20; // Current epoch time in seconds + 20 seconds + const qrCodeString = generateQrHash(userId, expTime); + return res.status(StatusCodes.OK).json({ qrCode: qrCodeString }); + } catch (error) { + next(error); + } + } +); + +attendeeRouter.get( + "/", + RoleChecker([Role.Enum.USER]), + async (req, res, next) => { + try { + const payload = res.locals.payload; + const userId = payload.userId; + + // Check if the user exists in the database + const user = await Database.ATTENDEE.findOne({ userId }); + + if (!user) { + return res + .status(StatusCodes.NOT_FOUND) + .json({ error: "UserNotFound" }); + } + + return res.status(StatusCodes.OK).json(user); + } catch (error) { + next(error); + } + } +); + +// Get attendees based on a partial filter in body +attendeeRouter.get( + "/filter", + RoleChecker([Role.Enum.STAFF, Role.Enum.CORPORATE]), + async (req, res, next) => { + try { + const attendeeData = PartialAttendeeValidator.parse(req.body); + const attendees = await Database.ATTENDEE.find( + attendeeData, + "userId" + ); + + return res.status(StatusCodes.OK).json(attendees); + } catch (error) { + next(error); + } + } +); + +// Update an attendee with partial data +attendeeRouter.put( + "/update", + RoleChecker([Role.Enum.USER]), + async (req, res, next) => { + const payload = res.locals.payload; + const userId = payload.userId; + + try { + const updateData = PartialAttendeeValidator.parse(req.body); + + const attendee = await Database.ATTENDEE.findOneAndUpdate( + { userId }, + { $set: updateData }, + { new: true, runValidators: true } + ); + + if (!attendee) { + return res + .status(StatusCodes.NOT_FOUND) + .json({ error: "UserNotFound" }); + } + + return res.status(StatusCodes.OK).json(attendee); + } catch (error) { + next(error); + } + } +); + +export default attendeeRouter;