From 3a6b1c780da25159b441f160e6a85737683ebd0e Mon Sep 17 00:00:00 2001 From: David Huang Date: Sat, 4 May 2024 21:20:14 +0800 Subject: [PATCH] show upcoming events that are in the progress of application --- pages/api/events/upcoming-current.ts | 18 +++++++++++++++--- pages/api/volunteer-logs/index.ts | 1 + 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/pages/api/events/upcoming-current.ts b/pages/api/events/upcoming-current.ts index a006fd1..003a0f7 100644 --- a/pages/api/events/upcoming-current.ts +++ b/pages/api/events/upcoming-current.ts @@ -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, @@ -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: [ { diff --git a/pages/api/volunteer-logs/index.ts b/pages/api/volunteer-logs/index.ts index bebba63..881a948 100644 --- a/pages/api/volunteer-logs/index.ts +++ b/pages/api/volunteer-logs/index.ts @@ -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({