LotteryWinner functionality not yet used.
Can post winners directly with parsing of parameters and error handling.
This commit is contained in:
		| @@ -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, | ||||
|   | ||||
| @@ -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); | ||||
|   }); | ||||
| }; | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user