From 1b12453df012192086a7bb10717c7a1749a0ef12 Mon Sep 17 00:00:00 2001 From: KevinMidboe Date: Thu, 18 Feb 2021 23:38:00 +0100 Subject: [PATCH] LotteryWinner functionality not yet used. Can post winners directly with parsing of parameters and error handling. --- api/controllers/lotteryWinnerController.js | 11 +++++++++-- api/winner.js | 16 +++++++++++----- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/api/controllers/lotteryWinnerController.js b/api/controllers/lotteryWinnerController.js index baa7c22..1923d2e 100644 --- a/api/controllers/lotteryWinnerController.js +++ b/api/controllers/lotteryWinnerController.js @@ -1,6 +1,9 @@ const path = require("path"); const winnerRepository = require(path.join(__dirname, "../winner")); +const { WinnerNotFound } = require(path.join(__dirname, "../vinlottisErrors")); +const prizeDistributionRepository = require(path.join(__dirname, "../prizeDistribution")); +// should not be used, is done through POST /lottery/prize-distribution/prize/:id - claimPrize. const addWinners = (req, res) => { const { winners } = req.body; @@ -11,7 +14,7 @@ const addWinners = (req, res) => { }); } - const requiredAttributes = ["name", "color"]; + const requiredAttributes = ["name", "color", "wine"]; const validColors = ["red", "blue", "green", "yellow"]; const validateAllWinners = winners => winners.map(winner => { @@ -37,7 +40,11 @@ const addWinners = (req, res) => { }); return Promise.all(validateAllWinners(winners)) - .then(winners => winnerRepository.addWinners(winners)) + .then(winners => + winners.map(winner => { + return prizeDistributionRepository.claimPrize(winner, winner.wine); + }) + ) .then(winners => res.send({ winners: winners, diff --git a/api/winner.js b/api/winner.js index d53a40d..85514ed 100644 --- a/api/winner.js +++ b/api/winner.js @@ -3,6 +3,13 @@ const path = require("path"); const VirtualWinner = require(path.join(__dirname, "/schemas/VirtualWinner")); const { WinnerNotFound } = require(path.join(__dirname, "/vinlottisErrors")); +const redactWinnerInfoMapper = winner => { + return { + name: winner.name, + color: winner.color + }; +}; + const addWinners = winners => { return Promise.all( winners.map(winner => { @@ -30,16 +37,15 @@ const allWinners = (isAdmin = false) => { }; const winnerById = (id, isAdmin = false) => { - return VirtualWinner.findOne({ _id: id }).then(winner => { + return VirtualWinner.findOne({ id: id }).then(winner => { if (winner == null) { throw new WinnerNotFound(); } if (!isAdmin) { return redactWinnerInfoMapper(winner); - } else { - return winner; } + return winner; }); }; @@ -66,12 +72,12 @@ const updateWinnerById = (id, updateModel) => { }; const deleteWinnerById = id => { - return VirtualWinner.findOne({ _id: id }).then(winner => { + return VirtualWinner.findOne({ id: id }).then(winner => { if (winner == null) { throw new WinnerNotFound(); } - return VirtualWinner.deleteOne({ _id: id }).then(_ => winner); + return VirtualWinner.deleteOne({ id: id }).then(_ => winner); }); };