From 18d8c2c7ca03056a0138e75f18724c31d63e1cb3 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Sun, 24 Jan 2021 10:14:00 +0100 Subject: [PATCH] Lottery, get/delete attendees. Changed so get attendees no longer is done in two endpoints, now we use req.isAuthenticated() to check if we want to return full or minimal json data about each attendee. --- api/controllers/lotteryController.js | 37 ++++++++++++++++++++++++++++ api/lottery.js | 35 ++++++++++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 api/controllers/lotteryController.js create mode 100644 api/lottery.js diff --git a/api/controllers/lotteryController.js b/api/controllers/lotteryController.js new file mode 100644 index 0000000..f64dbec --- /dev/null +++ b/api/controllers/lotteryController.js @@ -0,0 +1,37 @@ +const path = require("path"); +const lotteryRepository = require(path.join(__dirname, "../lottery")); + +const allAttendees = (req, res) => { + const isAdmin = req.isAuthenticated(); + + return lotteryRepository + .allAttendees(isAdmin === "true") + .then(attendees => + res.send({ + attendees: attendees, + success: true + }) + ) + .catch(error => { + const { statusCode, message } = error; + + return res.status(statusCode || 500).send({ + success: false, + message: message || "Unable to fetch lottery attendees." + }); + }); +}; + +const deleteAttendees = (req, res) => { + return lotteryRepository.deleteAttendees().then(success => + res.send({ + message: "Removed all attendees", + success: success + }) + ); +}; + +module.exports = { + allAttendees, + deleteAttendees +}; diff --git a/api/lottery.js b/api/lottery.js new file mode 100644 index 0000000..77547f3 --- /dev/null +++ b/api/lottery.js @@ -0,0 +1,35 @@ +const path = require("path"); +const Attendee = require(path.join(__dirname, "/schemas/Attendee")); + +const redactAttendeeInfoMapper = attendee => { + return { + name: attendee.name, + raffles: attendee.red + attendee.blue + attendee.yellow + attendee.green, + red: attendee.red, + blue: attendee.blue, + green: attendee.green, + yellow: attendee.yellow + }; +}; + +const allAttendees = isAdmin => { + if (!isAdmin) { + return Attendee.find().then(attendees => attendees.map(redactAttendeeInfoMapper)); + } else { + return Attendee.find(); + } +}; + +const deleteAttendees = () => { + const io = req.app.get("socketio"); + + return Attendee.deleteMany().then(_ => { + io.emit("refresh_data", {}); + return true; + }); +}; + +module.exports = { + allAttendees, + deleteAttendees +};