Add winners manually by posting /lottery/winners.
This commit is contained in:
@@ -1,8 +1,62 @@
|
|||||||
const path = require("path");
|
const path = require("path");
|
||||||
const lotteryRepository = require(path.join(__dirname, "../lottery"));
|
const lotteryRepository = require(path.join(__dirname, "../lottery"));
|
||||||
|
|
||||||
|
const addWinners = (req, res) => {
|
||||||
|
const { winners } = req.body;
|
||||||
|
|
||||||
|
if (!(winners instanceof Array)) {
|
||||||
|
return res.status(400).send({
|
||||||
|
message: "Winners must be array.",
|
||||||
|
success: false
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
const requiredAttributes = ["name", "color"];
|
||||||
|
const validColors = ["red", "blue", "green", "yellow"];
|
||||||
|
const validateAllWinners = winners =>
|
||||||
|
winners.map(winner => {
|
||||||
|
return Promise.all(
|
||||||
|
requiredAttributes.map(attr => {
|
||||||
|
if (typeof winner[attr] === "undefined") {
|
||||||
|
return Promise.reject({
|
||||||
|
message: `Incorrect or missing attribute: ${attr}.`,
|
||||||
|
statusCode: 400
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!validColors.includes(winner.color)) {
|
||||||
|
return Promise.reject({
|
||||||
|
message: `Missing or incorrect color value, must have one of values: ${validColors.join(", ")}.`,
|
||||||
|
statusCode: 400
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
return Promise.resolve();
|
||||||
|
})
|
||||||
|
).then(_ => Promise.resolve(winner));
|
||||||
|
});
|
||||||
|
|
||||||
|
return Promise.all(validateAllWinners(winners))
|
||||||
|
.then(winners => lotteryRepository.addWinners(winners))
|
||||||
|
.then(winners =>
|
||||||
|
res.send({
|
||||||
|
winners: winners,
|
||||||
|
message: `Successfully added winners to lottery.`,
|
||||||
|
success: true
|
||||||
|
})
|
||||||
|
)
|
||||||
|
.catch(error => {
|
||||||
|
const { statusCode, message } = error;
|
||||||
|
|
||||||
|
return res.status(statusCode || 500).send({
|
||||||
|
message: message || "Unexpected error occured adding winners.",
|
||||||
|
success: false
|
||||||
|
});
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
const allWinners = (req, res) => {
|
const allWinners = (req, res) => {
|
||||||
const isAdmin = req.isAuthenticated() || true;
|
const isAdmin = req.isAuthenticated();
|
||||||
|
|
||||||
return lotteryRepository
|
return lotteryRepository
|
||||||
.allWinners(isAdmin)
|
.allWinners(isAdmin)
|
||||||
@@ -94,6 +148,7 @@ const deleteWinners = (req, res) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
|
addWinners,
|
||||||
allWinners,
|
allWinners,
|
||||||
winnerById,
|
winnerById,
|
||||||
deleteWinnerById,
|
deleteWinnerById,
|
||||||
|
|||||||
@@ -189,7 +189,21 @@ const deleteWines = () => {
|
|||||||
return PreLotteryWine.deleteMany();
|
return PreLotteryWine.deleteMany();
|
||||||
};
|
};
|
||||||
|
|
||||||
const allWinners = isAdmin => {
|
const addWinners = winners => {
|
||||||
|
return Promise.all(
|
||||||
|
winners.map(winner => {
|
||||||
|
let newWinnerElement = new VirtualWinner({
|
||||||
|
name: winner.name,
|
||||||
|
color: winner.color,
|
||||||
|
timestamp_drawn: new Date().getTime()
|
||||||
|
});
|
||||||
|
|
||||||
|
return newWinnerElement.save();
|
||||||
|
})
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
const allWinners = (isAdmin = false) => {
|
||||||
if (!isAdmin) {
|
if (!isAdmin) {
|
||||||
return VirtualWinner.find().then(winners => winners.map(redactWinnerInfoMapper));
|
return VirtualWinner.find().then(winners => winners.map(redactWinnerInfoMapper));
|
||||||
} else {
|
} else {
|
||||||
@@ -360,6 +374,7 @@ module.exports = {
|
|||||||
updateWineById,
|
updateWineById,
|
||||||
deleteWineById,
|
deleteWineById,
|
||||||
deleteWines,
|
deleteWines,
|
||||||
|
addWinners,
|
||||||
allWinners,
|
allWinners,
|
||||||
winnerById,
|
winnerById,
|
||||||
deleteWinnerById,
|
deleteWinnerById,
|
||||||
|
|||||||
Reference in New Issue
Block a user