diff --git a/api/lottery.js b/api/lottery.js index 2a0145f..eaf6815 100644 --- a/api/lottery.js +++ b/api/lottery.js @@ -6,53 +6,61 @@ const Wine = require(path.join(__dirname + '/../schemas/Wine')); // Utils const epochToDateString = date => new Date(parseInt(date)).toDateString(); -const getHighscoreByDates = highscore => { - let groupedLotteries = {} +const sortNewestFirst = (lotteries) => { + return lotteries.sort((a, b) => parseInt(a.date) < parseInt(b.date) ? 1 : -1) +} - highscore.forEach(user => { - user.wins.map(win => { +const groupHighscoreByDate = async (highscore=undefined) => { + if (highscore == undefined) + highscore = await Highscore.find(); + + const highscoreByDate = []; + + highscore.forEach(person => { + person.wins.map(win => { const epochDate = new Date(win.date).setHours(0,0,0,0); - const obj = { - name: user.name, + const winnerObject = { + name: person.name, color: win.color, wine: win.wine, date: epochDate } - groupedLotteries[epochDate] ? - groupedLotteries[epochDate].push(obj) : groupedLotteries[epochDate] = [obj]; + const existingDateIndex = highscoreByDate.findIndex(el => el.date == epochDate) + if (existingDateIndex > -1) + highscoreByDate[existingDateIndex].winners.push(winnerObject); + else + highscoreByDate.push({ + date: epochDate, + winners: [winnerObject] + }) }) }) - return groupedLotteries + return sortNewestFirst(highscoreByDate); } -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 = (highscoreObject, key) => { + const listWithWines = highscoreObject[key] -const resolveWineReferences = listWithWines => { return Promise.all(listWithWines.map(element => - Wine.findById(element.wine) - .then(wine => { - element.wine = wine - return element - }) - )) + Wine.findById(element.wine) + .then(wine => { + element.wine = wine + return element + })) + ) + .then(resolvedListWithWines => { + highscoreObject[key] = resolvedListWithWines; + return highscoreObject + }) } +// end utils // Routes const all = (req, res) => { return Highscore.find() - .then(highscore => getHighscoreByDates(highscore)) - .then(groupedLotteries => groupedHighscoreToSortedList(groupedLotteries)) + .then(highscore => groupHighscoreByDate(highscore)) .then(lotteries => res.send({ message: "Lotteries by date!", lotteries @@ -60,56 +68,60 @@ const all = (req, res) => { } const latest = (req, res) => { - return Highscore.find() - .then(highscore => getHighscoreByDates(highscore)) - .then(groupedLotteries => groupedHighscoreToSortedList(groupedLotteries)) - .then(lotteries => res.send({ - message: "Latest lottery!", - lottery: lotteries.slice(-1).pop() - })) + return groupHighscoreByDate() + .then(lotteries => lotteries.shift()) + .then(latestLottery => resolveWineReferences(latestLottery, "winners")) + .then(lottery => res.send({ + message: "Latest lottery!", + winners: lottery.winners + }) + ) } const byEpochDate = (req, res) => { - const { date } = req.params; + let { date } = req.params; + date = new Date(new Date(parseInt(date)).setHours(0,0,0,0)).getTime() 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) - }) + return groupHighscoreByDate() + .then(lotteries => { + const lottery = lotteries.filter(lottery => lottery.date == date) + if (lottery.length > 0) { + return lottery[0] } else { return res.status(404).send({ - message: `No lottery found for date: ${dateString}` + message: `No lottery found for date: ${ dateString }` }) } }) + .then(lottery => resolveWineReferences(lottery, "winners")) + .then(lottery => res.send({ + message: `Lottery for date: ${ dateString}`, + date, + winners: lottery.winners + })) } const byName = (req, res) => { const { name } = req.params; + const regexName = new RegExp(name, "i"); // lowercase regex of the name 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 - }) + .then(highscore => { + if (highscore.length > 0) { + return highscore[0] } else { return res.status(404).send({ message: `Name: ${ name } not found in leaderboards.` }) } }) + .then(highscore => resolveWineReferences(highscore, "wins")) + .then(highscore => res.send({ + message: `Lottery winnings for name: ${ name }.`, + name: highscore.name, + wins: highscore.wins + })) } module.exports = { diff --git a/api/message.js b/api/message.js index a763a7b..d385019 100644 --- a/api/message.js +++ b/api/message.js @@ -7,7 +7,7 @@ async function sendWineSelectMessage(winnerObject) { winnerObject.timestamp_limit = new Date().getTime() * 600000; await winnerObject.save(); - let url = new URL(`/#/winner/${winnerObject.id}`, "https://lottis.vin"); + let url = new URL(`/winner/${winnerObject.id}`, "https://lottis.vin"); return sendMessageToUser( winnerObject.phoneNumber, diff --git a/api/update.js b/api/update.js index c3fa4d1..9761c40 100644 --- a/api/update.js +++ b/api/update.js @@ -35,7 +35,7 @@ const submitWines = async (req, res) => { const message = JSON.stringify({ message: "Dagens vin er lagt til, se den pƄ lottis.vin/dagens!", title: "Ny vin!", - link: "/#/dagens" + link: "/dagens" }); try { diff --git a/src/api.js b/src/api.js index 05145b9..e747b1f 100644 --- a/src/api.js +++ b/src/api.js @@ -333,6 +333,18 @@ const historyAll = () => { }); } +const historyByDate = (date) => { + const url = new URL(`/api/lottery/by-date/${ date }`, BASE_URL); + + return fetch(url.href).then(resp => { + if (resp.ok) { + return resp.json(); + } else { + return handleErrors(resp); + } + }); +} + export { statistics, colorStatistics, @@ -364,5 +376,6 @@ export { finishedDraw, getAmIWinner, postWineChosen, - historyAll + historyAll, + historyByDate }; diff --git a/src/components/HighscorePage.vue b/src/components/HighscorePage.vue index 574f45c..3b9bf8d 100644 --- a/src/components/HighscorePage.vue +++ b/src/components/HighscorePage.vue @@ -1,56 +1,24 @@ @@ -58,16 +26,15 @@ diff --git a/src/components/HistoryPage.vue b/src/components/HistoryPage.vue index 372e348..2e60cf2 100644 --- a/src/components/HistoryPage.vue +++ b/src/components/HistoryPage.vue @@ -2,14 +2,15 @@

Historie fra tidligere lotteri

-
- +
+
diff --git a/src/components/PersonalHighscorePage.vue b/src/components/PersonalHighscorePage.vue new file mode 100644 index 0000000..69610cd --- /dev/null +++ b/src/components/PersonalHighscorePage.vue @@ -0,0 +1,273 @@ + + + + + \ No newline at end of file diff --git a/src/components/TodaysPage.vue b/src/components/TodaysPage.vue index 0dfdf12..de81fc1 100644 --- a/src/components/TodaysPage.vue +++ b/src/components/TodaysPage.vue @@ -11,6 +11,7 @@ diff --git a/src/components/VinlottisPage.vue b/src/components/VinlottisPage.vue index e683a12..a7b1afd 100644 --- a/src/components/VinlottisPage.vue +++ b/src/components/VinlottisPage.vue @@ -13,7 +13,7 @@ />
- Vil du til lotteriet?Trykk her + Vil du til lotteriet?Trykk her
diff --git a/src/components/WinnerPage.vue b/src/components/WinnerPage.vue index 434f9d7..c60472c 100644 --- a/src/components/WinnerPage.vue +++ b/src/components/WinnerPage.vue @@ -29,7 +29,7 @@