Skip to content

Commit

Permalink
Merge pull request #64 from ChangePlusPlusVandy/fix-endpoint-for-volu…
Browse files Browse the repository at this point in the history
…nteer-history

update events/user API to return only past events of the user
  • Loading branch information
JiashuHarryHuang authored Nov 12, 2023
2 parents 55b02da + 47901fd commit fade0d0
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pages/api/events/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getServerSession } from 'next-auth';

// import the models and types we need
import VolunteerEvents from 'bookem-shared/src/models/VolunteerEvents';
import Users from 'bookem-shared/src/models/Users';
import { authOptions } from '@/pages/api/auth/[...nextauth]';

/**
Expand Down Expand Up @@ -38,13 +39,19 @@ export default async function handler(

// get all volunteerEvents from collection that match the user's Id
// sorted in descending order
const volunteerEvents = await VolunteerEvents.find({
userId: session.user._id,
startDate: { $lt: new Date() },
}).sort({ startDate: -1 });
const user = await Users.findById(session.user._id);
if (!user) {
return res.status(404).json({ message: 'User not found' });
}

// Use the user's events array to filter the VolunteerEvents
const events = await VolunteerEvents.find({
_id: { $in: user.events },
endDate: { $lt: new Date() },
}).sort({ eventDate: 1 });

// return the result
res.status(200).json(volunteerEvents);
res.status(200).json(events);
} catch (e) {
// if there is an error, print and return the error
console.error('An error has occurred in VolunteerEvents index.ts', e);
Expand Down

0 comments on commit fade0d0

Please sign in to comment.