diff --git a/backend/src/events/controllers.ts b/backend/src/events/controllers.ts index 85b085d7..0b567cac 100644 --- a/backend/src/events/controllers.ts +++ b/backend/src/events/controllers.ts @@ -264,6 +264,18 @@ const getEvent = async (eventID: string) => { }); }; +/** + * Returns a boleean indicating whether event is past or not + * @param eventID (String) + * @returns promise with boolean or error + */ +export const isEventPast = async (eventID: string) => { + const currentDateTime = new Date(); + const event = (await getEvent(eventID)) as Event; + const eventDate = new Date(event.startDate); + return eventDate < currentDateTime; +}; + /** * Gets all attendees registered for an event * @param eventID (String) @@ -333,8 +345,9 @@ const addAttendee = async (eventID: string, userID: string) => { var eventDateTimeUnknown = event?.startDate as unknown; var eventDateTimeString = eventDateTimeUnknown as string; var textBody = "Your registration was successful."; + const eventIsInThePast = await isEventPast(eventID); - if (process.env.NODE_ENV != "test") { + if (process.env.NODE_ENV != "test" && !eventIsInThePast) { // creates updated html path with the changed inputs const updatedHtml = replaceEventInputs( stringEventUpdate, @@ -350,6 +363,9 @@ const addAttendee = async (eventID: string, userID: string) => { updatedHtml ); } + if (eventIsInThePast) { + return Promise.reject("Event is past, cannot enroll new user"); + } return await prisma.eventEnrollment.create({ data: { event: { @@ -427,6 +443,10 @@ const deleteAttendee = async ( * @returns promise with event or error */ const updateEventStatus = async (eventID: string, status: string) => { + if (await isEventPast(eventID)) { + return Promise.reject("Event is past, cannot update status"); + } + return await prisma.event.update({ where: { id: eventID,