Feat/controllers - refactor entire backend and new admin interface #75
37
api/controllers/lotteryController.js
Normal file
37
api/controllers/lotteryController.js
Normal file
@@ -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
|
||||||
|
};
|
||||||
35
api/lottery.js
Normal file
35
api/lottery.js
Normal file
@@ -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
|
||||||
|
};
|
||||||
Reference in New Issue
Block a user