Rm from lottery.js attendee has own repo and ctrl.

This commit is contained in:
2021-01-26 22:28:27 +01:00
parent afab4387cc
commit 03c0513da3
2 changed files with 92 additions and 10 deletions

81
api/attendee.js Normal file
View File

@@ -0,0 +1,81 @@
const path = require("path");
const Attendee = require(path.join(__dirname, "/schemas/Attendee"));
const { UserNotFound } = require(path.join(__dirname, "/vinlottisErrors"));
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 = false) => {
if (!isAdmin) {
return Attendee.find().then(attendees => attendees.map(redactAttendeeInfoMapper));
} else {
return Attendee.find();
}
};
const addAttendee = attendee => {
const { name, red, blue, green, yellow, phoneNumber } = attendee;
let newAttendee = new Attendee({
name,
red,
blue,
green,
yellow,
phoneNumber,
winner: false
});
return newAttendee.save().then(_ => newAttendee);
};
const updateAttendeeById = (id, updateModel) => {
return Attendee.findOne({ _id: id }).then(attendee => {
if (attendee == null) {
throw new UserNotFound();
}
const updatedAttendee = {
name: updateModel.name != null ? updateModel.name : attendee.name,
green: updateModel.green != null ? updateModel.green : attendee.green,
red: updateModel.red != null ? updateModel.red : attendee.red,
blue: updateModel.blue != null ? updateModel.blue : attendee.blue,
yellow: updateModel.yellow != null ? updateModel.yellow : attendee.yellow,
phoneNumber: updateModel.phoneNumber != null ? updateModel.phoneNumber : attendee.phoneNumber,
winner: updateModel.winner != null ? updateModel.winner : attendee.winner
};
return Attendee.updateOne({ _id: id }, updatedAttendee).then(_ => updatedAttendee);
});
};
const deleteAttendeeById = id => {
return Attendee.findOne({ _id: id }).then(attendee => {
if (attendee == null) {
throw new UserNotFound();
}
return Attendee.deleteOne({ _id: id }).then(_ => attendee);
});
};
const deleteAttendees = () => {
return Attendee.deleteMany();
};
module.exports = {
allAttendees,
addAttendee,
updateAttendeeById,
deleteAttendeeById,
deleteAttendees
};

View File

@@ -1,10 +1,10 @@
const path = require("path"); const path = require("path");
const lotteryRepository = require(path.join(__dirname, "../lottery")); const attendeeRepository = require(path.join(__dirname, "../attendee"));
const allAttendees = (req, res) => { const allAttendees = (req, res) => {
const isAdmin = req.isAuthenticated(); const isAdmin = req.isAuthenticated() || true;
return lotteryRepository return attendeeRepository
.allAttendees(isAdmin) .allAttendees(isAdmin)
.then(attendees => .then(attendees =>
res.send({ res.send({
@@ -41,17 +41,18 @@ const addAttendee = (req, res) => {
}); });
} }
return lotteryRepository return attendeeRepository
.addAttendee(attendee) .addAttendee(attendee)
.then(savedAttendee => { .then(savedAttendee => {
var io = req.app.get("socketio"); var io = req.app.get("socketio");
io.emit("new_attendee", {}); io.emit("new_attendee", {});
return true; return savedAttendee;
}) })
.then(success => .then(savedAttendee =>
res.send({ res.send({
attendee: savedAttendee,
message: `Successfully added attendee ${attendee.name} to lottery.`, message: `Successfully added attendee ${attendee.name} to lottery.`,
success: success success: true
}) })
); );
}; };
@@ -60,7 +61,7 @@ const updateAttendeeById = (req, res) => {
const { id } = req.params; const { id } = req.params;
const { attendee } = req.body; const { attendee } = req.body;
return lotteryRepository return attendeeRepository
.updateAttendeeById(id, attendee) .updateAttendeeById(id, attendee)
.then(updatedAttendee => { .then(updatedAttendee => {
var io = req.app.get("socketio"); var io = req.app.get("socketio");
@@ -87,7 +88,7 @@ const updateAttendeeById = (req, res) => {
const deleteAttendeeById = (req, res) => { const deleteAttendeeById = (req, res) => {
const { id } = req.params; const { id } = req.params;
return lotteryRepository return attendeeRepository
.deleteAttendeeById(id) .deleteAttendeeById(id)
.then(removedAttendee => { .then(removedAttendee => {
var io = req.app.get("socketio"); var io = req.app.get("socketio");
@@ -111,7 +112,7 @@ const deleteAttendeeById = (req, res) => {
}; };
const deleteAttendees = (req, res) => { const deleteAttendees = (req, res) => {
return lotteryRepository return attendeeRepository
.deleteAttendees() .deleteAttendees()
.then(removedAttendee => { .then(removedAttendee => {
var io = req.app.get("socketio"); var io = req.app.get("socketio");