Finer control when updating lottery.
Lottery update now has endpoint/functions for also submitting winners, wines and lottery (what's bought) separatly.
This commit is contained in:
		
							
								
								
									
										100
									
								
								api/update.js
									
									
									
									
									
								
							
							
						
						
									
										100
									
								
								api/update.js
									
									
									
									
									
								
							| @@ -10,7 +10,7 @@ const sub = require(path.join(__dirname + "/../api/subscriptions")); | |||||||
| const _wineFunctions = require(path.join(__dirname + "/../api/wine")); | const _wineFunctions = require(path.join(__dirname + "/../api/wine")); | ||||||
| const _personFunctions = require(path.join(__dirname + "/../api/person")); | const _personFunctions = require(path.join(__dirname + "/../api/person")); | ||||||
| const Subscription = require(path.join(__dirname + "/../schemas/Subscription")); | const Subscription = require(path.join(__dirname + "/../schemas/Subscription")); | ||||||
| const Purchase = require(path.join(__dirname + "/../schemas/Purchase")); | const Lottery = require(path.join(__dirname + "/../schemas/Purchase")); | ||||||
| const PreLotteryWine = require(path.join( | const PreLotteryWine = require(path.join( | ||||||
|   __dirname + "/../schemas/PreLotteryWine" |   __dirname + "/../schemas/PreLotteryWine" | ||||||
| )); | )); | ||||||
| @@ -32,17 +32,28 @@ const submitWines = async (req, res) => { | |||||||
|   } |   } | ||||||
|  |  | ||||||
|   let subs = await Subscription.find(); |   let subs = await Subscription.find(); | ||||||
|  |   console.log("Sending new wines w/ push notification to all subscribers.") | ||||||
|   for (let i = 0; i < subs.length; i++) { |   for (let i = 0; i < subs.length; i++) { | ||||||
|     let subscription = subs[i]; //get subscription from your databse here. |     let subscription = subs[i]; //get subscription from your databse here. | ||||||
|  |  | ||||||
|     const message = JSON.stringify({ |     const message = JSON.stringify({ | ||||||
|       message: "Dagens vin er lagt til, se den på lottis.vin/dagens!", |       message: "Dagens vin er lagt til, se den på lottis.vin/dagens!", | ||||||
|       title: "Ny vin!", |       title: "Ny vin!", | ||||||
|       link: "/#/dagens" |       link: "/#/dagens" | ||||||
|     }); |     }); | ||||||
|  |  | ||||||
|  |     try { | ||||||
|       sub.sendNotification(subscription, message); |       sub.sendNotification(subscription, message); | ||||||
|  |     } catch (error) { | ||||||
|  |       console.error("Error when trying to send push notification to subscriber."); | ||||||
|  |       console.error(error); | ||||||
|  |     } | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   return res.send(true); |   return res.send({ | ||||||
|  |     message: "Submitted and notified push subscribers of new wines!", | ||||||
|  |     success: true | ||||||
|  |   }); | ||||||
| }; | }; | ||||||
|  |  | ||||||
| const schema = async (req, res) => { | const schema = async (req, res) => { | ||||||
| @@ -55,44 +66,73 @@ const schema = async (req, res) => { | |||||||
|   return res.send(nulledSchema); |   return res.send(nulledSchema); | ||||||
| } | } | ||||||
|  |  | ||||||
| const submitLottery = async (req, res) => { | // TODO IMPLEMENT WITH FRONTEND (unused) | ||||||
|   await PreLotteryWine.deleteMany(); | const submitWinesToLottery = async (req, res) => { | ||||||
|  |   const { lottery } = req.body; | ||||||
|  |   const { date, wines } = lottery; | ||||||
|  |   const wineObjects = await Promise.all(wines.map(async (wine) => await _wineFunctions.findSaveWine(wine))) | ||||||
|  |  | ||||||
|   const purchaseBody = req.body.purchase; |   return Lottery.findOneAndUpdate({ date: date }, { | ||||||
|   const winnersBody = req.body.winners; |       date: date, | ||||||
|  |       wines: wineObjects | ||||||
|  |     }, { | ||||||
|  |       upsert: true | ||||||
|  |     }).then(_ => res.send(true)) | ||||||
|  |       .catch(err => res.status(500).send({ message: 'Unexpected error while updating/saving wine to lottery.', | ||||||
|  |                                            success: false, | ||||||
|  |                                            exception: err.message })); | ||||||
|  | } | ||||||
|  |  | ||||||
|   const date = purchaseBody.date; |  /** | ||||||
|   const blue = purchaseBody.blue; |   * @apiParam (Request body) {Array} winners List of winners | ||||||
|   const red = purchaseBody.red; |   */ | ||||||
|   const yellow = purchaseBody.yellow; | const submitWinnersToLottery = async (req, res) => { | ||||||
|   const green = purchaseBody.green; |   const { lottery } = req.body; | ||||||
|  |   const { winners, date } = lottery; | ||||||
|  |  | ||||||
|   const bought = purchaseBody.bought; |   for (let i = 0; i < winners.length; i++) { | ||||||
|   const stolen = purchaseBody.stolen; |     let currentWinner = winners[i]; | ||||||
|  |     let wonWine = await _wineFunctions.findSaveWine(currentWinner.wine); // TODO rename to findAndSaveWineToLottery | ||||||
|   const winesThisDate = []; |     await _personFunctions.findSavePerson(currentWinner, wonWine, date); // TODO rename to findAndSaveWineToPerson | ||||||
|  |  | ||||||
|   for (let i = 0; i < winnersBody.length; i++) { |  | ||||||
|     let currentWinner = winnersBody[i]; |  | ||||||
|  |  | ||||||
|     let wonWine = await _wineFunctions.findSaveWine(currentWinner); |  | ||||||
|     winesThisDate.push(wonWine); |  | ||||||
|  |  | ||||||
|     await _personFunctions.findSavePerson(currentWinner, wonWine, date); |  | ||||||
|   } |   } | ||||||
|  |  | ||||||
|   let purchase = new Purchase({ |   return res.json(true); | ||||||
|  | } | ||||||
|  |  | ||||||
|  |  /** | ||||||
|  |   * @apiParam (Request body) {Date} date Date of lottery | ||||||
|  |   * @apiParam (Request body) {Number} blue Number of blue tickets | ||||||
|  |   * @apiParam (Request body) {Number} red Number of red tickets | ||||||
|  |   * @apiParam (Request body) {Number} green Number of green tickets | ||||||
|  |   * @apiParam (Request body) {Number} yellow Number of yellow tickets | ||||||
|  |   * @apiParam (Request body) {Number} bought Number of tickets bought | ||||||
|  |   * @apiParam (Request body) {Number} stolen Number of tickets stolen | ||||||
|  |   */ | ||||||
|  | const submitLottery = async (req, res) => { | ||||||
|  |   const { lottery } = req.body | ||||||
|  |  | ||||||
|  |   const { date, | ||||||
|  |           blue, | ||||||
|  |           red, | ||||||
|  |           yellow, | ||||||
|  |           green, | ||||||
|  |           bought, | ||||||
|  |           stolen } = lottery; | ||||||
|  |  | ||||||
|  |   return Lottery.findOneAndUpdate({ date: date }, { | ||||||
|       date: date, |       date: date, | ||||||
|       blue: blue, |       blue: blue, | ||||||
|       yellow: yellow, |       yellow: yellow, | ||||||
|       red: red, |       red: red, | ||||||
|       green: green, |       green: green, | ||||||
|     wines: winesThisDate, |  | ||||||
|       bought: bought, |       bought: bought, | ||||||
|       stolen: stolen |       stolen: stolen | ||||||
|   }); |     }, { | ||||||
|  |       upsert: true | ||||||
|   await purchase.save(); |     }).then(_ => res.send(true)) | ||||||
|  |       .catch(err => res.status(500).send({ message: 'Unexpected error while updating/saving lottery.', | ||||||
|  |                                            success: false, | ||||||
|  |                                            exception: err.message })); | ||||||
|  |  | ||||||
|   return res.send(true); |   return res.send(true); | ||||||
| }; | }; | ||||||
| @@ -100,5 +140,7 @@ const submitLottery = async (req, res) => { | |||||||
| module.exports = { | module.exports = { | ||||||
|   submitWines, |   submitWines, | ||||||
|   schema, |   schema, | ||||||
|   submitLottery |   submitLottery, | ||||||
|  |   submitWinnersToLottery, | ||||||
|  |   submitWinesToLottery | ||||||
| }; | }; | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user