API endpoint for getting latest lottery.

This commit is contained in:
2021-02-20 13:29:41 +01:00
parent 7e2b5a5bb0
commit 80e6c91045
3 changed files with 29 additions and 2 deletions

View File

@@ -139,6 +139,26 @@ const allLotteries = (req, res) => {
}); });
}; };
const latestLottery = (req, res) => {
return lotteryRepository
.latestLottery()
.then(lottery =>
res.send({
lottery,
message: "Latest lottery.",
success: true
})
)
.catch(error => {
const { statusCode, message } = error;
return res.status(statusCode || 500).send({
message: message || "Unexpected error occured while fetching all lotteries.",
success: false
});
});
};
function verifyLotteryPayload(raffles, stolen, wines) { function verifyLotteryPayload(raffles, stolen, wines) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
if (raffles == undefined || !raffles instanceof Array) { if (raffles == undefined || !raffles instanceof Array) {
@@ -188,5 +208,6 @@ module.exports = {
drawWinner, drawWinner,
archiveLottery, archiveLottery,
lotteryByDate, lotteryByDate,
allLotteries allLotteries,
latestLottery
}; };

View File

@@ -130,6 +130,10 @@ const allLotteriesIncludingWinners = async (sort = "asc", yearFilter = undefined
}); });
}; };
const latestLottery = async () => {
return Lottery.findOne().sort({ date: -1 });
};
const drawWinner = async () => { const drawWinner = async () => {
let allContestants = await Attendee.find({ winner: false }); let allContestants = await Attendee.find({ winner: false });
@@ -259,5 +263,6 @@ module.exports = {
archive, archive,
lotteryByDate, lotteryByDate,
allLotteries, allLotteries,
allLotteriesIncludingWinners allLotteriesIncludingWinners,
latestLottery
}; };

View File

@@ -71,6 +71,7 @@ router.delete("/lottery/winner/:id", mustBeAuthenticated, winnerController.delet
router.get("/lottery/draw", mustBeAuthenticated, lotteryController.drawWinner); router.get("/lottery/draw", mustBeAuthenticated, lotteryController.drawWinner);
router.post("/lottery/archive", mustBeAuthenticated, lotteryController.archiveLottery); router.post("/lottery/archive", mustBeAuthenticated, lotteryController.archiveLottery);
router.get("/lottery/latest", lotteryController.latestLottery);
router.get("/lottery/:epoch", lotteryController.lotteryByDate); router.get("/lottery/:epoch", lotteryController.lotteryByDate);
router.get("/lotteries/", lotteryController.allLotteries); router.get("/lotteries/", lotteryController.allLotteries);