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