Skip to content

Commit

Permalink
Merge pull request #61 from ChangePlusPlusVandy/update-upcoming-event…
Browse files Browse the repository at this point in the history
…s-API

update events/upcoming API and add new API events/explore
  • Loading branch information
JiashuHarryHuang authored Nov 25, 2023
2 parents 7ac598c + 1f7d7f0 commit b0e0deb
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 6 deletions.
9 changes: 7 additions & 2 deletions components/Volunteer/FutureVolunteerEvents.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
Expand All @@ -41,9 +46,9 @@ const FutureVolunteerEvents = () => {
const [featuredEvents, setFeaturedEvents] =
useState<QueriedVolunteerEventData[]>();

// 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));

Expand Down
42 changes: 42 additions & 0 deletions pages/api/events/explore.ts
Original file line number Diff line number Diff line change
@@ -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 startDate > today order by progamDate ascending
const events = await VolunteerEvents.find({
startDate: { $gt: new Date() },
}).sort({ startDate: 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;
}
}
22 changes: 18 additions & 4 deletions pages/api/events/upcoming.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -9,20 +12,31 @@ 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':
try {
// 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' });
}

// 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() },
}).sort({ eventDate: 1 });
_id: { $in: user.events },
startDate: { $gt: new Date() },
}).sort({ startDate: 1 });

return res.status(200).json(events);
} catch (error) {
Expand Down

0 comments on commit b0e0deb

Please sign in to comment.