From 74ec4122ca314df9f0fd3c8171ab6f96f7a22b2c Mon Sep 17 00:00:00 2001 From: David Huang Date: Sat, 11 Nov 2023 13:04:45 -0600 Subject: [PATCH 1/5] change events/upcoming to only show the users' events --- pages/api/events/upcoming.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/pages/api/events/upcoming.ts b/pages/api/events/upcoming.ts index 9b5c2ae1..2e0b9f8c 100644 --- a/pages/api/events/upcoming.ts +++ b/pages/api/events/upcoming.ts @@ -1,6 +1,9 @@ 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]'; export default async function handler( req: NextApiRequest, @@ -8,7 +11,10 @@ export default async function handler( ) { // Get request method const { method } = req; + + const session = await getServerSession(req, res, authOptions); + switch (method) { /** * @route GET /api/events/upcoming @@ -17,11 +23,18 @@ export default async function handler( */ case 'GET': try { + // const session = await getSession({ req }); await dbConnect(); + // Fetch the user by ID to get their events array + const user = await Users.findById(session.user._id); + if (!user) { + return res.status(404).json({ message: 'User not found' }); + } - // Select all events where eventDate > today order by progamDate ascending + // Use the user's events array to filter the VolunteerEvents const events = await VolunteerEvents.find({ - eventDate: { $gt: new Date() }, + _id: { $in: user.events }, + eventDate: { $gt: new Date() } }).sort({ eventDate: 1 }); return res.status(200).json(events); From 378d6f8c9f5ce3a6a2e634d896130a89b4d1e93a Mon Sep 17 00:00:00 2001 From: David Huang Date: Sat, 11 Nov 2023 13:27:13 -0600 Subject: [PATCH 2/5] add comments to events/upcoming --- pages/api/events/upcoming.ts | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/pages/api/events/upcoming.ts b/pages/api/events/upcoming.ts index 2e0b9f8c..dccaf61f 100644 --- a/pages/api/events/upcoming.ts +++ b/pages/api/events/upcoming.ts @@ -12,13 +12,12 @@ export default async function handler( // Get request method const { method } = req; - const session = await getServerSession(req, res, authOptions); switch (method) { /** * @route GET /api/events/upcoming - * @desc Get all events in the future + * @desc Get all events in the future that the user is signed up for * @res QueriedVolunteerEventData[] */ case 'GET': @@ -26,6 +25,8 @@ export default async function handler( // const session = await getSession({ req }); await dbConnect(); // 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); if (!user) { return res.status(404).json({ message: 'User not found' }); From d27ba0957887c011632c6d76a547d8145dd87c2d Mon Sep 17 00:00:00 2001 From: David Huang Date: Sat, 11 Nov 2023 14:19:21 -0600 Subject: [PATCH 3/5] add events/explore API to show all future events let FutureVolunteerEvent use the new API to show all future events for "Explore volunteer opportunities" --- .../Volunteer/FutureVolunteerEvents.tsx | 9 +++- pages/api/events/explore.ts | 42 +++++++++++++++++++ 2 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 pages/api/events/explore.ts diff --git a/components/Volunteer/FutureVolunteerEvents.tsx b/components/Volunteer/FutureVolunteerEvents.tsx index 12c4a262..d82bd377 100644 --- a/components/Volunteer/FutureVolunteerEvents.tsx +++ b/components/Volunteer/FutureVolunteerEvents.tsx @@ -24,6 +24,11 @@ import LongEventCard from '../shared/LongEventCard'; import Link from 'next/link'; import { start } from 'repl'; +// TODO: for now, explore volunteer opportunities show all future events +// in future we can add pagination +// TODO: inconsistency between mobile and desktop for featured events +// featured events are shown on mobile, but not on desktop + const FutureVolunteerEvents = () => { // Holds text in input box const [query, setQuery] = useState(''); @@ -41,9 +46,9 @@ const FutureVolunteerEvents = () => { const [featuredEvents, setFeaturedEvents] = useState(); - // Fetch upcoming events and featured events when rendered + // Fetch explore events and featured events when rendered useEffect(() => { - fetchData('/api/events/upcoming') + fetchData('/api/events/explore') .then(data => setEvents(data)) .catch(err => setError(err)); diff --git a/pages/api/events/explore.ts b/pages/api/events/explore.ts new file mode 100644 index 00000000..fc534e0d --- /dev/null +++ b/pages/api/events/explore.ts @@ -0,0 +1,42 @@ +import dbConnect from '@/lib/dbConnect'; +import VolunteerEvents from 'bookem-shared/src/models/VolunteerEvents'; +import type { NextApiRequest, NextApiResponse } from 'next'; + +export default async function handler( + req: NextApiRequest, + res: NextApiResponse +) { + // Get request method + const { method } = req; + + switch (method) { + /** + * @route GET /api/events/explore + * @desc Get all future events + * @res QueriedVolunteerEventData[] + */ + case 'GET': + try { + await dbConnect(); + + // Select all events where eventDate > today order by progamDate ascending + const events = await VolunteerEvents.find({ + eventDate: { $gt: new Date() }, + }).sort({ eventDate: 1 }); + + return res.status(200).json(events); + } catch (error) { + console.error(error); + res.status(500).json({ message: error }); + } + break; + + // case 'POST': + // case 'PUT': + // case 'DELETE': + default: + // res.setHeader('Allow', ['GET', 'PUT', 'DELETE', 'POST']); + res.status(405).end(`Method ${method} Not Allowed`); + break; + } +} From df2ba2f78d2c4d9144329ef9f218d03fad0773ad Mon Sep 17 00:00:00 2001 From: David Huang Date: Sat, 11 Nov 2023 15:09:41 -0600 Subject: [PATCH 4/5] fix formatting in upcoming --- pages/api/events/upcoming.ts | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/pages/api/events/upcoming.ts b/pages/api/events/upcoming.ts index dccaf61f..fb5dd2f9 100644 --- a/pages/api/events/upcoming.ts +++ b/pages/api/events/upcoming.ts @@ -11,9 +11,9 @@ export default async function handler( ) { // Get request method const { method } = req; - + const session = await getServerSession(req, res, authOptions); - + switch (method) { /** * @route GET /api/events/upcoming @@ -25,7 +25,7 @@ export default async function handler( // const session = await getSession({ req }); await dbConnect(); // Fetch the user by ID to get their events array - // session.user._id shouldn't be null because we have the middleware to + // session.user._id shouldn't be null because we have the middleware to // handle unauthenticated users const user = await Users.findById(session.user._id); if (!user) { @@ -35,7 +35,7 @@ export default async function handler( // Use the user's events array to filter the VolunteerEvents const events = await VolunteerEvents.find({ _id: { $in: user.events }, - eventDate: { $gt: new Date() } + eventDate: { $gt: new Date() }, }).sort({ eventDate: 1 }); return res.status(200).json(events); From 5bd8e86e611e1a0c70801fb53b4f37e2063ab12f Mon Sep 17 00:00:00 2001 From: David Huang Date: Sun, 12 Nov 2023 12:35:26 -0600 Subject: [PATCH 5/5] fix typo - should use startDate not eventDate --- pages/api/events/explore.ts | 6 +++--- pages/api/events/upcoming.ts | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/pages/api/events/explore.ts b/pages/api/events/explore.ts index fc534e0d..a8f4b6eb 100644 --- a/pages/api/events/explore.ts +++ b/pages/api/events/explore.ts @@ -19,10 +19,10 @@ export default async function handler( try { await dbConnect(); - // Select all events where eventDate > today order by progamDate ascending + // Select all events where startDate > today order by progamDate ascending const events = await VolunteerEvents.find({ - eventDate: { $gt: new Date() }, - }).sort({ eventDate: 1 }); + startDate: { $gt: new Date() }, + }).sort({ startDate: 1 }); return res.status(200).json(events); } catch (error) { diff --git a/pages/api/events/upcoming.ts b/pages/api/events/upcoming.ts index fb5dd2f9..b392c6d4 100644 --- a/pages/api/events/upcoming.ts +++ b/pages/api/events/upcoming.ts @@ -35,8 +35,8 @@ export default async function handler( // Use the user's events array to filter the VolunteerEvents const events = await VolunteerEvents.find({ _id: { $in: user.events }, - eventDate: { $gt: new Date() }, - }).sort({ eventDate: 1 }); + startDate: { $gt: new Date() }, + }).sort({ startDate: 1 }); return res.status(200).json(events); } catch (error) {