Skip to content

Fetch User's Upcoming Interested/Attending Events #60

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

Open
wants to merge 4 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
25 changes: 25 additions & 0 deletions packages/server/controllers/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,30 @@ async function addCheckInPoints(eventId, userId) {
});
}

async function getUserEvents(currentDate, userId) {
const userEvents = await prisma.events.findMany({
where: {
eventAttendees: {
some: {
userId: userId,
status: { in: ["Interested", "Attending"] },
},
},
startDate: { gte: currentDate },
},
select: {
eventId: true,
startDate: true,
eventAttendees: {
select: { status: true },
where: { userId: userId },
},
},
});
Comment on lines +210 to +228
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These will be displayed as a list of event info cards for the user, let's sort them by ascending start date for convenience.


return userEvents;
}

module.exports = {
getEvent,
getEvents,
Expand All @@ -217,4 +241,5 @@ module.exports = {
getUpcomingEvents,
getEventAttendees,
updateAttendeeStatus,
getUserEvents,
};
78 changes: 59 additions & 19 deletions packages/server/routes/api/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ const {
attendeeStatusValidator,
} = require("../../validators/events");
const { guildIdValidator } = require("../../validators/guilds");
const { userIdBodyValidator } = require("../../validators/users");
const {
userIdValidator,
userIdBodyValidator,
} = require("../../validators/users");
const { validationResult, matchedData } = require("express-validator");
const DEFAULT_EVENT_LIMIT = 10;
/**
Expand Down Expand Up @@ -43,7 +46,7 @@ router.get(
const events = await EventController.getEvents(
eventLimit,
action,
eventId
eventId,
);

if (events.length === 0) {
Expand All @@ -56,10 +59,10 @@ router.get(
const firstEventId = events[0].eventId;
const lastEventId = events[events.length - 1].eventId;
const prevCursor = Buffer.from(
`${currentPage - 1}___prev___${firstEventId}`
`${currentPage - 1}___prev___${firstEventId}`,
).toString("base64");
const nextCursor = Buffer.from(
`${currentPage + 1}___next___${lastEventId}`
`${currentPage + 1}___next___${lastEventId}`,
).toString("base64");

// follow-up request, not first request to api route
Expand Down Expand Up @@ -97,7 +100,7 @@ router.get(
message: error.message,
});
}
}
},
);

router.post(
Expand Down Expand Up @@ -135,7 +138,7 @@ router.post(
message: error.message,
});
}
}
},
);

router.get(
Expand Down Expand Up @@ -168,7 +171,7 @@ router.get(
message: error.message,
});
}
}
},
);

router.delete(
Expand Down Expand Up @@ -203,7 +206,7 @@ router.delete(
message: error.message,
});
}
}
},
);

router.put(
Expand Down Expand Up @@ -231,7 +234,7 @@ router.put(

const updatedEvent = await EventController.updateEvent(
eventId,
validatedData
validatedData,
);

response.status(200).json({
Expand All @@ -249,7 +252,7 @@ router.put(
});
return;
}
}
},
);

router.get(
Expand All @@ -266,9 +269,8 @@ router.get(
}
try {
const { eventId } = matchedData(request);
const eventAttendeesData = await EventController.getEventAttendees(
eventId
);
const eventAttendeesData =
await EventController.getEventAttendees(eventId);
response.status(200).json({
status: "success",
data: {
Expand All @@ -281,7 +283,7 @@ router.get(
message: error.message,
});
}
}
},
);

router.get(
Expand Down Expand Up @@ -309,7 +311,7 @@ router.get(
//Should return a list of events from the guild that are upcoming and in ascending order
const upcoming = await EventController.getUpcomingEvents(
currDate,
guildId
guildId,
);

response.status(200).json({
Expand All @@ -324,7 +326,7 @@ router.get(
message: error.message,
});
}
}
},
);

router.post(
Expand All @@ -348,7 +350,7 @@ router.post(
const eventAttendeeData = await EventController.updateAttendeeStatus(
eventId,
userId,
"CheckedIn"
"CheckedIn",
);

response.status(200).json({
Expand All @@ -363,7 +365,7 @@ router.post(
message: error.message,
});
}
}
},
);

router.put(
Expand Down Expand Up @@ -403,7 +405,45 @@ router.put(
message: error.message,
});
}
}
},
);

router.get(
"/:userId/events",
Comment on lines +411 to +412
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After looking into it a bit more, I think it makes more sense to move this to the user router instead so it becomes GET /users/:userId/events. This follows REST API convention more since we're interested in the events (a subcollection) of a particular user with userId in the users collection.

AuthController.authenticate,
userIdValidator,
async (request, response) => {
const result = validationResult(request);

// if validation result is not empty, errors occurred
if (!result.isEmpty()) {
response.status(400).json({
status: "fail",
data: result.array(),
});
return;
}
const currDate = new Date();

const data = matchedData(request);
const userId = data.userId;

try {
const userEvents = await EventController.getUserEvents(currDate, userId);

response.status(200).json({
status: "success",
data: {
upcomingEvents: userEvents,
},
});
} catch (error) {
response.status(500).json({
status: "error",
message: error.message,
});
}
},
);

module.exports = router;