Merge pull request #76 from KevinMidboe/feat/latest-lottery
Feat/latest lottery
This commit is contained in:
		| @@ -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) { | ||||
|   return new Promise((resolve, reject) => { | ||||
|     if (raffles == undefined || !raffles instanceof Array) { | ||||
| @@ -188,5 +208,6 @@ module.exports = { | ||||
|   drawWinner, | ||||
|   archiveLottery, | ||||
|   lotteryByDate, | ||||
|   allLotteries | ||||
|   allLotteries, | ||||
|   latestLottery | ||||
| }; | ||||
|   | ||||
| @@ -130,6 +130,10 @@ const allLotteriesIncludingWinners = async (sort = "asc", yearFilter = undefined | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| const latestLottery = async () => { | ||||
|   return Lottery.findOne().sort({ date: -1 }); | ||||
| }; | ||||
|  | ||||
| const drawWinner = async () => { | ||||
|   let allContestants = await Attendee.find({ winner: false }); | ||||
|  | ||||
| @@ -259,5 +263,6 @@ module.exports = { | ||||
|   archive, | ||||
|   lotteryByDate, | ||||
|   allLotteries, | ||||
|   allLotteriesIncludingWinners | ||||
|   allLotteriesIncludingWinners, | ||||
|   latestLottery | ||||
| }; | ||||
|   | ||||
| @@ -71,6 +71,7 @@ router.delete("/lottery/winner/:id", mustBeAuthenticated, winnerController.delet | ||||
|  | ||||
| router.get("/lottery/draw", mustBeAuthenticated, lotteryController.drawWinner); | ||||
| router.post("/lottery/archive", mustBeAuthenticated, lotteryController.archiveLottery); | ||||
| router.get("/lottery/latest", lotteryController.latestLottery); | ||||
| router.get("/lottery/:epoch", lotteryController.lotteryByDate); | ||||
| router.get("/lotteries/", lotteryController.allLotteries); | ||||
|  | ||||
|   | ||||
| @@ -67,7 +67,14 @@ | ||||
|     </div> | ||||
|  | ||||
|     <div v-if="wines.length > 0" class="button-container column"> | ||||
|       <button class="vin-button" @click="archiveLottery">Send inn og arkiver</button> | ||||
|       <p v-if="todaysAlreadySubmitted" class="info-message"> | ||||
|         Lotteriet er arkivert!<br />Du kan nå slette dagens viner, deltakere & vinnere for å tilbakestille til neste | ||||
|         ukes lotteri. | ||||
|       </p> | ||||
|  | ||||
|       <button class="vin-button" @click="archiveLottery" :disabled="todaysAlreadySubmitted"> | ||||
|         {{ todaysAlreadySubmitted == false ? "Send inn og arkiver" : "Dagens lotteri er allerede arkivert" }} | ||||
|       </button> | ||||
|     </div> | ||||
|   </div> | ||||
| </template> | ||||
| @@ -81,6 +88,7 @@ export default { | ||||
|   data() { | ||||
|     return { | ||||
|       payed: undefined, | ||||
|       todaysAlreadySubmitted: false, | ||||
|       wines: [], | ||||
|       winners: [], | ||||
|       attendees: [], | ||||
| @@ -97,6 +105,7 @@ export default { | ||||
|     this.fetchLotteryWines(); | ||||
|     this.fetchLotteryWinners(); | ||||
|     this.fetchLotteryAttendees(); | ||||
|     this.checkIfAlreadySubmittedForToday(); | ||||
|   }, | ||||
|   watch: { | ||||
|     lotteryColors: { | ||||
| @@ -162,6 +171,19 @@ export default { | ||||
|           } | ||||
|         }); | ||||
|     }, | ||||
|     checkIfAlreadySubmittedForToday() { | ||||
|       return fetch("/api/lottery/latest") | ||||
|         .then(resp => resp.json()) | ||||
|         .then(response => { | ||||
|           const getDay = d => new Date(d).getDate(); | ||||
|  | ||||
|           if (response.lottery.date && (getDay(response.lottery.date) == getDay(new Date()))) { | ||||
|             this.todaysAlreadySubmitted = true; | ||||
|           } else { | ||||
|             this.todaysAlreadySubmitted = false; | ||||
|           } | ||||
|         }) | ||||
|     }, | ||||
|     updateLotteryColorsWithAttendees(attendees) { | ||||
|       this.attendees.map(attendee => { | ||||
|         this.lotteryColors.map(color => (color.value += attendee[color.key])); | ||||
| @@ -222,6 +244,7 @@ export default { | ||||
|         .then(resp => resp.json()) | ||||
|         .then(response => { | ||||
|           if (response.success) { | ||||
|             this.todaysAlreadySubmitted = true; | ||||
|             this.$toast.info({ | ||||
|               title: "Lotteriet er sendt inn og arkivert! Du kan nå slette viner, deltakere & vinnere slettes.", | ||||
|               timeout: 10000 | ||||
| @@ -278,6 +301,17 @@ select { | ||||
|   } | ||||
| } | ||||
|  | ||||
| .info-message { | ||||
|   padding: 0.75rem; | ||||
|   text-align: center; | ||||
|   background-color: var(--light-blue); | ||||
|   color: var(--matte-text-color); | ||||
|   border-radius: 4px; | ||||
|  | ||||
|   font-size: 1.1rem; | ||||
|   line-height: 1.5rem; | ||||
| } | ||||
|  | ||||
| .winner-element { | ||||
|   display: flex; | ||||
|   flex-direction: column; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user