diff --git a/api/lottery.js b/api/lottery.js new file mode 100644 index 0000000..9d192f3 --- /dev/null +++ b/api/lottery.js @@ -0,0 +1,118 @@ +const express = require('express'); +const path = require('path'); + +const router = express.Router(); +const mongoose = require('mongoose'); +mongoose.connect('mongodb://localhost:27017/vinlottis', { + useNewUrlParser: true +}) + +const mustBeAuthenticated = require(path.join( + __dirname + '/../middleware/mustBeAuthenticated' +)); +const config = require(path.join(__dirname + '/../config/defaults/lottery')); + +const Highscore = require(path.join(__dirname + '/../schemas/Highscore')); +const Wine = require(path.join(__dirname + '/../schemas/Wine')); + + +// Utils +const epochToDateString = date => new Date(parseInt(date)).toDateString(); + +const getHighscoreByDates = highscore => { + let groupedLotteries = {} + + highscore.forEach(user => { + user.wins.map(win => { + const epochDate = new Date(win.date).setHours(0,0,0,0); + const obj = { + name: user.name, + color: win.color, + wine: win.wine, + date: epochDate + } + + groupedLotteries[epochDate] ? + groupedLotteries[epochDate].push(obj) : groupedLotteries[epochDate] = [obj]; + }) + }) + + return groupedLotteries +} + +const groupedHighscoreToSortedList = groupedLotteries => { + return Object.keys(groupedLotteries).map(key => { + const winners = groupedLotteries[key]; + return { + date: parseInt(key), + dateString: epochToDateString(key), + winners + } + }).sort((a,b) => parseInt(a.date) > parseInt(b.date) ? 1 : -1) +} + +const resolveWineReferences = listWithWines => { + return Promise.all(listWithWines.map(element => + Wine.findById(element.wine) + .then(wine => { + element.wine = wine + return element + }) + )) +} + +// Routes +router.route('/all').get((req, res) => { + return Highscore.find() + .then(highscore => getHighscoreByDates(highscore)) + .then(groupedLotteries => groupedHighscoreToSortedList(groupedLotteries)) + .then(lotteries => res.send({ + message: "Lotteries by date!", + lotteries + })) +}) + +router.route('/by-date/:date').get((req, res) => { + const { date } = req.params; + const dateString = epochToDateString(date); + + return Highscore.find() + .then(highscore => getHighscoreByDates(highscore)) + .then(async (lotteries) => { + const lottery = lotteries[date]; + + if (lottery != null) { + return res.send({ + message: `Lottery for date: ${dateString}`, + lottery: await resolveWineReferences(lottery) + }) + } else { + return res.status(404).send({ + message: `No lottery found for date: ${dateString}` + }) + } + }) +}) + +router.route("/by-name").get((req, res) => { + const { name } = req.query; + + return Highscore.find({ name }) + .then(async (highscore) => { + highscore = highscore[0] + if (highscore) { + const highscoreWithResolvedWines = await resolveWineReferences(highscore.wins) + + return res.send({ + message: `Lottery winnings by name: ${name}`, + highscore: highscoreWithResolvedWines + }) + } else { + return res.status(404).send({ + message: `Name: ${ name } not found in leaderboards.` + }) + } + }) +}) + +module.exports = router; diff --git a/server.js b/server.js index ccbf4c5..e0fef6f 100644 --- a/server.js +++ b/server.js @@ -15,6 +15,7 @@ const virtualApi = require(path.join(__dirname + "/api/virtualLottery")); const virtualRegistrationApi = require(path.join( __dirname + "/api/virtualRegistration" )); +const lottery = require(path.join(__dirname + "/api/lottery")); //This is required for the chat to work const chat = require(path.join(__dirname + "/api/chat"))(io); @@ -94,6 +95,7 @@ app.use("/api/", updateApi); app.use("/api/", retrieveApi); app.use("/api/", wineinfoApi); app.use("/api/", chatHistory); +app.use("/api/lottery", lottery); app.use("/api/virtual/", virtualApi(io)); app.use("/api/virtual-registration/", virtualRegistrationApi); app.use("/subscription", subscriptionApi); diff --git a/src/api.js b/src/api.js index aa859fd..eb6d3c2 100644 --- a/src/api.js +++ b/src/api.js @@ -245,6 +245,18 @@ const postWineChosen = (id, wineName) => { }); }; +const historyAll = () => { + const url = new URL(`/api/lottery/all`, BASE_URL); + + return fetch(url.href).then(resp => { + if (resp.ok) { + return resp.json(); + } else { + return handleErrors(resp); + } + }); +} + export { statistics, colorStatistics, @@ -270,5 +282,6 @@ export { getChatHistory, finishedDraw, getAmIWinner, - postWineChosen + postWineChosen, + historyAll }; diff --git a/src/components/HistoryPage.vue b/src/components/HistoryPage.vue new file mode 100644 index 0000000..372e348 --- /dev/null +++ b/src/components/HistoryPage.vue @@ -0,0 +1,34 @@ + + + + + \ No newline at end of file diff --git a/src/routes/vinlottisRouter.js b/src/routes/vinlottisRouter.js index 6c5f517..32ae017 100644 --- a/src/routes/vinlottisRouter.js +++ b/src/routes/vinlottisRouter.js @@ -10,6 +10,7 @@ import AdminPage from "@/components/AdminPage"; import WinnerPage from "@/components/WinnerPage"; import LotteryPage from "@/components/LotteryPage"; +import HistoryPage from "@/components/HistoryPage"; const routes = [ { @@ -47,6 +48,10 @@ const routes = [ { path: "/winner/:id", component: WinnerPage + }, + { + path: "/history", + component: HistoryPage } ]; diff --git a/src/ui/Winners.vue b/src/ui/Winners.vue index 65d2b17..f2d3764 100644 --- a/src/ui/Winners.vue +++ b/src/ui/Winners.vue @@ -1,6 +1,6 @@