Skip to content

Commit

Permalink
Merge branch 'main' into past-activities-EL
Browse files Browse the repository at this point in the history
  • Loading branch information
JiashuHarryHuang committed Nov 12, 2023
2 parents b9be155 + fade0d0 commit 2aa2151
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
9 changes: 0 additions & 9 deletions pages/api/event/[id].ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,11 +92,6 @@ export default async function handler(
// TODO: Speed this up!
event.volunteers.unshift(user._id);
user.events.unshift(event._id);
if (event.program != null) {
if (!user.tags.includes(event.program.tagName)) {
user.tags.unshift(event.program.tagName);
}
}
} else if (userIndex === -1 || eventIndex === -1) {
throw new Error('Inconsistency between collections!');
} else {
Expand All @@ -106,10 +101,6 @@ export default async function handler(
// TODO: Speed this up!
event.volunteers.splice(userIndex, 1);
user.events.splice(eventIndex, 1);
// TODO: Remove the tag for user only if this tag has no relationship
// with other events this user did
// const programIndex = user.tags.indexOf(event.program);
// user.tags.splice(programIndex, 1);
}

// Resave both document
Expand Down
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
7 changes: 5 additions & 2 deletions pages/api/scripts/helper-functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,10 @@ export const generateEvent = (
// get the start and end dates
let startDate, endDate;
if (chosenEvent.isMultipleDays) {
startDate = faker.date.future(1);
startDate = faker.date.between(
'2022-01-01T00:00:00.000Z',
'2025-01-01T00:00:00.000Z'
);
endDate = faker.date.future(1, startDate);
} else {
startDate = new Date();
Expand All @@ -122,7 +125,7 @@ export const generateEvent = (
phone: generatePhone(),
email: faker.internet.email(),
program: programIds[0] || null,
requireApplication: chosenEvent.requireApplication,
requireApplication: false,
tags: tagIds,
volunteers: [],
};
Expand Down

0 comments on commit 2aa2151

Please sign in to comment.