Skip to content

Commit

Permalink
Merge pull request #99 from ChangePlusPlusVandy/feature/show-events-i…
Browse files Browse the repository at this point in the history
…n-application-for-upcoming-events

show upcoming events that are in the progress of application
  • Loading branch information
DavidHuang2002 authored May 6, 2024
2 parents b12827f + 3a6b1c7 commit fbfa0bf
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
18 changes: 15 additions & 3 deletions pages/api/events/upcoming-current.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import dbConnect from '@/lib/dbConnect';
import VolunteerEvents from 'bookem-shared/src/models/VolunteerEvents';
import Users from 'bookem-shared/src/models/Users';
import type { NextApiRequest, NextApiResponse } from 'next';
import { getServerSession } from 'next-auth';
import { authOptions } from '@/pages/api/auth/[...nextauth]';
import VolunteerApplications from 'bookem-shared/src/models/VolunteerApplications';
import ApplicationResponse from 'bookem-shared/src/models/ApplicationResponse';
import VolunteerEvents from 'bookem-shared/src/models/VolunteerEvents';
import { UserData } from 'bookem-shared/src/types/database';

export default async function handler(
req: NextApiRequest,
Expand All @@ -27,16 +30,25 @@ export default async function handler(
// Fetch the user by ID to get their events array
// session.user._id shouldn't be null because we have the middleware to
// handle unauthenticated users
const user = await Users.findById(session.user._id);
const user: UserData | null = await Users.findById(session.user._id);
if (!user) {
return res.status(404).json({ message: 'User not found' });
}

// select the events that user signed up for but are still in the application process
const inProgressApplications = await ApplicationResponse.find({
volunteer: session.user._id,
});

const inProgressApplicationEventIds = inProgressApplications.map(
application => application.event
);

// Use the user's events array to filter the VolunteerEvents
// select * from events where id in user.events and (startDate > today or (startDate < today and endDate > today))
const currentDate = new Date();
const events = await VolunteerEvents.find({
_id: { $in: user.events },
_id: { $in: user.events.concat(inProgressApplicationEventIds) },
published: true,
$or: [
{
Expand Down
1 change: 1 addition & 0 deletions pages/api/volunteer-logs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ export default async function handler(

const usersId = user._id;

// "dark magic" to register the volunteerEvent model so that the populate method works
await VolunteerEvents.findOne();
// get all volunteerEvents from collection that match the user's Id
const volunteerLogs = await VolunteerLogs.find({
Expand Down

0 comments on commit fbfa0bf

Please sign in to comment.