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 path = require("path");
|
||||||
const winnerRepository = require(path.join(__dirname, "../winner"));
|
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 addWinners = (req, res) => {
|
||||||
const { winners } = req.body;
|
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 validColors = ["red", "blue", "green", "yellow"];
|
||||||
const validateAllWinners = winners =>
|
const validateAllWinners = winners =>
|
||||||
winners.map(winner => {
|
winners.map(winner => {
|
||||||
@@ -37,7 +40,11 @@ const addWinners = (req, res) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return Promise.all(validateAllWinners(winners))
|
return Promise.all(validateAllWinners(winners))
|
||||||
.then(winners => winnerRepository.addWinners(winners))
|
.then(winners =>
|
||||||
|
winners.map(winner => {
|
||||||
|
return prizeDistributionRepository.claimPrize(winner, winner.wine);
|
||||||
|
})
|
||||||
|
)
|
||||||
.then(winners =>
|
.then(winners =>
|
||||||
res.send({
|
res.send({
|
||||||
winners: winners,
|
winners: winners,
|
||||||
|
|||||||
@@ -3,6 +3,13 @@ const path = require("path");
|
|||||||
const VirtualWinner = require(path.join(__dirname, "/schemas/VirtualWinner"));
|
const VirtualWinner = require(path.join(__dirname, "/schemas/VirtualWinner"));
|
||||||
const { WinnerNotFound } = require(path.join(__dirname, "/vinlottisErrors"));
|
const { WinnerNotFound } = require(path.join(__dirname, "/vinlottisErrors"));
|
||||||
|
|
||||||
|
const redactWinnerInfoMapper = winner => {
|
||||||
|
return {
|
||||||
|
name: winner.name,
|
||||||
|
color: winner.color
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
const addWinners = winners => {
|
const addWinners = winners => {
|
||||||
return Promise.all(
|
return Promise.all(
|
||||||
winners.map(winner => {
|
winners.map(winner => {
|
||||||
@@ -30,16 +37,15 @@ const allWinners = (isAdmin = false) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const winnerById = (id, isAdmin = false) => {
|
const winnerById = (id, isAdmin = false) => {
|
||||||
return VirtualWinner.findOne({ _id: id }).then(winner => {
|
return VirtualWinner.findOne({ id: id }).then(winner => {
|
||||||
if (winner == null) {
|
if (winner == null) {
|
||||||
throw new WinnerNotFound();
|
throw new WinnerNotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!isAdmin) {
|
if (!isAdmin) {
|
||||||
return redactWinnerInfoMapper(winner);
|
return redactWinnerInfoMapper(winner);
|
||||||
} else {
|
|
||||||
return winner;
|
|
||||||
}
|
}
|
||||||
|
return winner;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -66,12 +72,12 @@ const updateWinnerById = (id, updateModel) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
const deleteWinnerById = id => {
|
const deleteWinnerById = id => {
|
||||||
return VirtualWinner.findOne({ _id: id }).then(winner => {
|
return VirtualWinner.findOne({ id: id }).then(winner => {
|
||||||
if (winner == null) {
|
if (winner == null) {
|
||||||
throw new WinnerNotFound();
|
throw new WinnerNotFound();
|
||||||
}
|
}
|
||||||
|
|
||||||
return VirtualWinner.deleteOne({ _id: id }).then(_ => winner);
|
return VirtualWinner.deleteOne({ id: id }).then(_ => winner);
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user