Skip to content

Commit

Permalink
pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobc2700 committed Jul 1, 2024
1 parent e716b5a commit e00f0e5
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 20 deletions.
36 changes: 17 additions & 19 deletions src/services/attendees/attendee-router.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Router } from "express";
import { StatusCodes } from "http-status-codes";
import { AttendeeValidator } from "./attendee-schema";
import { AttendeeValidator, EventIdValidator } from "./attendee-schema";
import { Database } from "../../database";
import RoleChecker from "../../middleware/role-checker";
import { Role } from "../auth/auth-models";
Expand All @@ -13,14 +13,14 @@ const attendeeRouter = Router();

// Favorite an event for an attendee
attendeeRouter.post(
"/favoriteEvents/:eventId",
"/favorites/:eventId",
RoleChecker([Role.Enum.USER]),
async (req, res, next) => {
try {
const payload = res.locals.payload;
const userId = payload.userId;
const { eventId } = req.params;
const payload = res.locals.payload;
const userId = payload.userId;
const { eventId } = EventIdValidator.parse(req.params);

try {
const attendee = await Database.ATTENDEES.findOne({ userId });

if (!attendee) {
Expand All @@ -43,14 +43,14 @@ attendeeRouter.post(

// Unfavorite an event for an attendee
attendeeRouter.delete(
"/favoriteEvents/:eventId",
"/favorites/:eventId",
RoleChecker([Role.Enum.USER]),
async (req, res, next) => {
try {
const payload = res.locals.payload;
const userId = payload.userId;
const { eventId } = req.params;
const payload = res.locals.payload;
const userId = payload.userId;
const { eventId } = EventIdValidator.parse(req.params);

try {
const attendee = await Database.ATTENDEES.findOne({ userId });

if (!attendee) {
Expand All @@ -73,24 +73,22 @@ attendeeRouter.delete(

// Get favorite events for an attendee
attendeeRouter.get(
"/favoriteEvents",
"/favorites",
RoleChecker([Role.Enum.USER]),
async (req, res, next) => {
try {
const payload = res.locals.payload;
const userId = payload.userId;
const payload = res.locals.payload;
const userId = payload.userId;

const attendee = await Database.ATTENDEES.findOne({
userId,
}).populate("favorites");
try {
const attendee = await Database.ATTENDEES.findOne({ userId });

if (!attendee) {
return res
.status(StatusCodes.NOT_FOUND)
.json({ error: "UserNotFound" });
}

return res.status(StatusCodes.OK).json(attendee.favorites);
return res.status(StatusCodes.OK).json(attendee);
} catch (error) {
next(error);
}
Expand Down
6 changes: 5 additions & 1 deletion src/services/attendees/attendee-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,8 @@ const AttendeeSchema = new mongoose.Schema({
favorites: [{ type: String }],
});

export { AttendeeSchema, AttendeeValidator };
const EventIdValidator = z.object({
eventId: z.string().uuid(),
});

export { AttendeeSchema, AttendeeValidator, EventIdValidator };

0 comments on commit e00f0e5

Please sign in to comment.