diff options
author | Raphael Kabo <raphaelkabo@hey.com> | 2024-03-04 11:25:06 +0000 |
---|---|---|
committer | Raphael Kabo <raphaelkabo@hey.com> | 2024-03-04 11:25:06 +0000 |
commit | 65cf61a8c24c97254c2b87536c6931375848508a (patch) | |
tree | a3c1be3d6136a005e7132ebeef829404d61daa2b /src/routes | |
parent | 724df500a009ec5bebe0911c59892058e74dc4e2 (diff) |
refactor: improve unattend functionality, fix bugs
Diffstat (limited to 'src/routes')
-rw-r--r-- | src/routes/event.ts | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/src/routes/event.ts b/src/routes/event.ts index ad77052..3595e0a 100644 --- a/src/routes/event.ts +++ b/src/routes/event.ts @@ -642,4 +642,74 @@ router.post( }, ); +router.delete( + "/event/attendee/:eventID", + async (req: Request, res: Response) => { + const removalPassword = req.query.p; + if (!removalPassword) { + return res + .status(400) + .json({ error: "Please provide a removal password." }); + } + try { + const response = await Event.findOne({ + id: req.params.eventID, + "attendees.removalPassword": removalPassword, + }); + if (!response) { + return res.status(404).json({ + error: "No attendee found with that removal password.", + }); + } + const attendee = response?.attendees?.find( + (a) => a.removalPassword === removalPassword, + ); + if (!attendee) { + return res.status(404).json({ + error: "No attendee found with that removal password.", + }); + } + const attendeeEmail = attendee.email; + const removalResponse = await Event.updateOne( + { id: req.params.eventID }, + { $pull: { attendees: { removalPassword } } }, + ); + if (removalResponse.nModified === 0) { + return res.status(404).json({ + error: "No attendee found with that removal password.", + }); + } + addToLog( + "unattendEvent", + "success", + `Attendee removed self from event ${req.params.eventID}`, + ); + if (attendeeEmail && req.app.locals.sendEmails) { + await sendEmailFromTemplate( + attendeeEmail, + "You have been removed from an event", + "unattendEvent", + { + eventID: req.params.eventID, + siteName: res.locals.config?.general.site_name, + siteLogo: res.locals.config?.general.email_logo_url, + domain: res.locals.config?.general.domain, + }, + req, + ); + } + res.sendStatus(200); + } catch (e) { + addToLog( + "removeEventAttendee", + "error", + `Attempt to remove attendee from event ${req.params.eventID} failed with error: ${e}`, + ); + return res.status(500).json({ + error: "There has been an unexpected error. Please try again.", + }); + } + }, +); + export default router; |