Skip to content
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

show upcoming events that are in the progress of application #99

Merged
Merged
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
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
Loading