Add, update and delete attendees to lottery.
This commit is contained in:
		| @@ -5,7 +5,7 @@ const allAttendees = (req, res) => { | ||||
|   const isAdmin = req.isAuthenticated(); | ||||
|  | ||||
|   return lotteryRepository | ||||
|     .allAttendees(isAdmin === "true") | ||||
|     .allAttendees(isAdmin) | ||||
|     .then(attendees => | ||||
|       res.send({ | ||||
|         attendees: attendees, | ||||
| @@ -22,16 +22,113 @@ const allAttendees = (req, res) => { | ||||
|     }); | ||||
| }; | ||||
|  | ||||
| const deleteAttendees = (req, res) => { | ||||
|   return lotteryRepository.deleteAttendees().then(success => | ||||
|     res.send({ | ||||
|       message: "Removed all attendees", | ||||
|       success: success | ||||
| const addAttendee = (req, res) => { | ||||
|   const { attendee } = req.body; | ||||
|  | ||||
|   const requiredColors = [attendee["red"], attendee["blue"], attendee["green"], attendee["yellow"]]; | ||||
|   const correctColorsTypes = requiredColors.filter(color => typeof color === "number"); | ||||
|   if (requiredColors.length !== correctColorsTypes.length) { | ||||
|     return res.status(400).send({ | ||||
|       message: "Incorrect or missing color, required type Number for keys: 'blue', 'red', 'green' & 'yellow'.", | ||||
|       success: false | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   if (typeof attendee["name"] !== "string" && typeof attendee["phoneNumber"] !== "number") { | ||||
|     return res.status(400).send({ | ||||
|       message: "Incorrect or missing attendee keys 'name' or 'phoneNumber'.", | ||||
|       success: false | ||||
|     }); | ||||
|   } | ||||
|  | ||||
|   return lotteryRepository | ||||
|     .addAttendee(attendee) | ||||
|     .then(savedAttendee => { | ||||
|       var io = req.app.get("socketio"); | ||||
|       io.emit("new_attendee", {}); | ||||
|       return true; | ||||
|     }) | ||||
|   ); | ||||
|     .then(success => | ||||
|       res.send({ | ||||
|         message: `Successfully added attendee ${attendee.name} to lottery.`, | ||||
|         success: success | ||||
|       }) | ||||
|     ); | ||||
| }; | ||||
|  | ||||
| const updateAttendeeById = (req, res) => { | ||||
|   const { id } = req.params; | ||||
|   const { attendee } = req.body; | ||||
|  | ||||
|   return lotteryRepository | ||||
|     .updateAttendeeById(id, attendee) | ||||
|     .then(updatedAttendee => { | ||||
|       var io = req.app.get("socketio"); | ||||
|       io.emit("refresh_data", {}); | ||||
|       return updatedAttendee; | ||||
|     }) | ||||
|     .then(attendee => | ||||
|       res.send({ | ||||
|         attendee, | ||||
|         message: `Updated attendee: ${attendee.name}`, | ||||
|         success: true | ||||
|       }) | ||||
|     ) | ||||
|     .catch(error => { | ||||
|       const { statusCode, message } = error; | ||||
|  | ||||
|       return res.status(statusCode || 500).send({ | ||||
|         message: message || "Unexpected error occured while deleteing attendee by id.", | ||||
|         success: false | ||||
|       }); | ||||
|     }); | ||||
| }; | ||||
|  | ||||
| const deleteAttendeeById = (req, res) => { | ||||
|   const { id } = req.params; | ||||
|  | ||||
|   return lotteryRepository | ||||
|     .deleteAttendeeById(id) | ||||
|     .then(removedAttendee => { | ||||
|       var io = req.app.get("socketio"); | ||||
|       io.emit("refresh_data", {}); | ||||
|       return removedAttendee; | ||||
|     }) | ||||
|     .then(attendee => | ||||
|       res.send({ | ||||
|         message: `Removed attendee: ${attendee.name}`, | ||||
|         success: true | ||||
|       }) | ||||
|     ) | ||||
|     .catch(error => { | ||||
|       const { statusCode, message } = error; | ||||
|  | ||||
|       return res.status(statusCode || 500).send({ | ||||
|         message: message || "Unexpected error occured while deleteing attendee by id.", | ||||
|         success: false | ||||
|       }); | ||||
|     }); | ||||
| }; | ||||
|  | ||||
| const deleteAttendees = (req, res) => { | ||||
|   return lotteryRepository | ||||
|     .deleteAttendees() | ||||
|     .then(removedAttendee => { | ||||
|       var io = req.app.get("socketio"); | ||||
|       io.emit("refresh_data", {}); | ||||
|     }) | ||||
|     .then(_ => | ||||
|       res.send({ | ||||
|         message: "Removed all attendees", | ||||
|         success: true | ||||
|       }) | ||||
|     ); | ||||
| }; | ||||
|  | ||||
| module.exports = { | ||||
|   allAttendees, | ||||
|   addAttendee, | ||||
|   updateAttendeeById, | ||||
|   deleteAttendeeById, | ||||
|   deleteAttendees | ||||
| }; | ||||
|   | ||||
| @@ -1,6 +1,16 @@ | ||||
| const path = require("path"); | ||||
| const Attendee = require(path.join(__dirname, "/schemas/Attendee")); | ||||
|  | ||||
| class UserNotFound extends Error { | ||||
|   constructor(message = "User not found.") { | ||||
|     super(message); | ||||
|     this.name = "UserNotFound"; | ||||
|     this.statusCode = 404; | ||||
|   } | ||||
|  | ||||
|   // TODO log missing user | ||||
| } | ||||
|  | ||||
| const redactAttendeeInfoMapper = attendee => { | ||||
|   return { | ||||
|     name: attendee.name, | ||||
| @@ -20,16 +30,59 @@ const allAttendees = isAdmin => { | ||||
|   } | ||||
| }; | ||||
|  | ||||
| const deleteAttendees = () => { | ||||
|   const io = req.app.get("socketio"); | ||||
| const addAttendee = attendee => { | ||||
|   const { name, red, blue, green, yellow, phoneNumber } = attendee; | ||||
|  | ||||
|   return Attendee.deleteMany().then(_ => { | ||||
|     io.emit("refresh_data", {}); | ||||
|     return true; | ||||
|   let newAttendee = new Attendee({ | ||||
|     name, | ||||
|     red, | ||||
|     blue, | ||||
|     green, | ||||
|     yellow, | ||||
|     phoneNumber, | ||||
|     winner: false | ||||
|   }); | ||||
|  | ||||
|   return newAttendee.save(); | ||||
| }; | ||||
|  | ||||
| const updateAttendeeById = (id, updateModel) => { | ||||
|   return Attendee.findOne({ _id: id }).then(attendee => { | ||||
|     if (attendee == null) { | ||||
|       throw new UserNotFound(); | ||||
|     } | ||||
|  | ||||
|     const updatedAttendee = { | ||||
|       name: updateModel.name || attendee.name, | ||||
|       green: updateModel.green || attendee.green, | ||||
|       red: updateModel.red || attendee.red, | ||||
|       blue: updateModel.blue || attendee.blue, | ||||
|       yellow: updateModel.yellow || attendee.yellow, | ||||
|       phoneNumber: updateModel.phoneNumber || attendee.phoneNumber | ||||
|     }; | ||||
|  | ||||
|     return Attendee.updateOne({ _id: id }, updatedAttendee).then(_ => updatedAttendee); | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| const deleteAttendeeById = id => { | ||||
|   return Attendee.findOne({ _id: id }).then(attendee => { | ||||
|     if (attendee == null) { | ||||
|       throw new UserNotFound(); | ||||
|     } | ||||
|  | ||||
|     return Attendee.deleteOne({ _id: id }).then(_ => attendee); | ||||
|   }); | ||||
| }; | ||||
|  | ||||
| const deleteAttendees = () => { | ||||
|   return Attendee.deleteMany(); | ||||
| }; | ||||
|  | ||||
| module.exports = { | ||||
|   allAttendees, | ||||
|   addAttendee, | ||||
|   updateAttendeeById, | ||||
|   deleteAttendeeById, | ||||
|   deleteAttendees | ||||
| }; | ||||
|   | ||||
		Reference in New Issue
	
	Block a user