Prize distribution for selecting wines.

Moved the same functionality out from lottery.js and simplified a bit.
Now the backend also sends with wines to pick from.
When hitting the controller we check that the user is the next user in
line.
This commit is contained in:
2021-01-26 22:38:27 +01:00
parent 939e7e34df
commit 5e018f071d
2 changed files with 199 additions and 0 deletions

View File

@@ -0,0 +1,90 @@
const path = require("path");
const prizeDistribution = require(path.join(__dirname, "../prizeDistribution"));
const winner = require(path.join(__dirname, "../winner"));
const message = require(path.join(__dirname, "../message"));
const start = async (req, res) => {
const allWinners = await winners.allWinners();
if (allWinners.length === 0) {
return res.status(503).send({
message: "No winners left.",
success: false
});
}
const laterWinners = allWinners.slice(1);
return prizeDistribution
.notifyNextWinner()
.then(_ => message.sendInitialMessageToWinners(laterWinners))
.then(_ =>
res.send({
message: `Send link to first winner and notified everyone else.`,
success: true
})
)
.catch(error => {
const { statusCode, message } = error;
return res.status(statusCode || 500).send({
message: message || "Unexpected error occured while starting prize distribution.",
success: false
});
});
};
const getPrizesForWinnerById = (req, res) => {
const { id } = req.params;
return prizeDistribution
.verifyWinnerNextInLine(id)
.then(_ => lottery.allWines())
.then(wines =>
res.send({
wines: wines,
message: "Wines to select from",
success: true
})
)
.catch(error => {
const { statusCode, message } = error;
return res.status(statusCode || 500).send({
message: message || "Unexpected error occured while fetching prizes.",
success: false
});
});
};
const submitPrizeForWinnerById = async (req, res) => {
const { id } = req.params;
const { wine } = req.body;
const winner = await prizeDistribution.verifyWinnerNextInLine(id);
const prelotteryWine = await lottery.wineById(wine.id);
return prizeDistribution
.claimPrize(winner, prelotteryWine)
.then(_ => prizeDistribution.notifyNextWinner())
.then(_ =>
res.send({
message: `${winner.name} successfully claimed prize: ${wine.name}`,
success: true
})
)
.catch(error => {
const { statusCode, message } = error;
return res.status(statusCode || 500).send({
message: message || "Unexpected error occured while claiming prize.",
success: false
});
});
};
module.exports = {
start,
getPrizesForWinnerById,
submitPrizeForWinnerById
};