From b5b61784ccb02270ae460af8b76ceb84eee94ae2 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Sun, 24 Jan 2021 15:34:58 +0100 Subject: [PATCH] Add winners manually by posting /lottery/winners. --- api/controllers/lotteryWinnerController.js | 57 +++++++++++++++++++++- api/lottery.js | 17 ++++++- 2 files changed, 72 insertions(+), 2 deletions(-) diff --git a/api/controllers/lotteryWinnerController.js b/api/controllers/lotteryWinnerController.js index babfc9f..13109e1 100644 --- a/api/controllers/lotteryWinnerController.js +++ b/api/controllers/lotteryWinnerController.js @@ -1,8 +1,62 @@ const path = require("path"); 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 isAdmin = req.isAuthenticated() || true; + const isAdmin = req.isAuthenticated(); return lotteryRepository .allWinners(isAdmin) @@ -94,6 +148,7 @@ const deleteWinners = (req, res) => { }; module.exports = { + addWinners, allWinners, winnerById, deleteWinnerById, diff --git a/api/lottery.js b/api/lottery.js index 08e7f83..f50f0f5 100644 --- a/api/lottery.js +++ b/api/lottery.js @@ -189,7 +189,21 @@ const deleteWines = () => { 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) { return VirtualWinner.find().then(winners => winners.map(redactWinnerInfoMapper)); } else { @@ -360,6 +374,7 @@ module.exports = { updateWineById, deleteWineById, deleteWines, + addWinners, allWinners, winnerById, deleteWinnerById,