recipeData.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. const Recipe = require("../models/recipe.js");
  2. module.exports = {
  3. /*
  4. POST - creates a single new recipe
  5. req.body = {
  6. name: name of recipe,
  7. price: price of the recipe,
  8. category: String
  9. ingredients: [{
  10. id: id of ingredient,
  11. quantity: quantity of ingredient in recipe,
  12. unit: String
  13. }]
  14. }
  15. Return = newly created recipe in same form as above, with _id
  16. */
  17. createRecipe: function(req, res){
  18. let recipe = new Recipe({
  19. merchant: res.locals.merchant._id,
  20. name: req.body.name,
  21. price: req.body.price,
  22. category: req.body.category,
  23. ingredients: req.body.ingredients
  24. });
  25. recipe.save()
  26. .then((newRecipe)=>{
  27. res.locals.merchant.recipes.push(recipe);
  28. res.locals.merchant.save().catch((err)=>{throw err});
  29. return res.json(newRecipe);
  30. })
  31. .catch((err)=>{
  32. if(typeof(err) === "string"){
  33. return res.json(err);
  34. }
  35. if(err.name === "ValidationError"){
  36. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  37. }
  38. return res.json("ERROR: UNABLE TO SAVE INGREDIENT");
  39. });
  40. },
  41. /*
  42. PUT - Update a single recipe
  43. req.body = {
  44. id: id of recipe,
  45. name: name of recipe,
  46. price: price of recipe,
  47. category: String
  48. ingredients: [{
  49. ingredient: id of ingredient,
  50. quantity: quantity of ingredient in recipe
  51. unit: String
  52. }]
  53. }
  54. */
  55. updateRecipe: function(req, res){
  56. Recipe.findOne({_id: req.body.id})
  57. .then((recipe)=>{
  58. if(res.locals.merchant.pos === "none"){
  59. recipe.name = req.body.name;
  60. recipe.price = req.body.price;
  61. recipe.category = req.body.category;
  62. }
  63. recipe.ingredients = req.body.ingredients;
  64. return recipe.save();
  65. })
  66. .then((recipe)=>{
  67. res.json(recipe);
  68. })
  69. .catch((err)=>{
  70. if(typeof(err) === "string") return res.json(err);
  71. if(err.name === "ValidationError"){
  72. return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
  73. }
  74. return res.json("ERROR: UNABLE TO UPDATE DATA");
  75. });
  76. },
  77. /*
  78. DELETE: removes a single recipe from the merchant and the database
  79. req.params.id = String (recipe id)
  80. response = {}
  81. */
  82. removeRecipe: function(req, res){
  83. if(res.locals.merchant.pos === "square") return res.json("YOU MUST EDIT YOUR RECIPES INSIDE SQUARE");
  84. for(let i = 0; i < res.locals.merchant.recipes.length; i++){
  85. if(res.locals.merchant.recipes[i].toString() === req.params.id){
  86. res.locals.merchant.recipes.splice(i, 1);
  87. break;
  88. }
  89. }
  90. res.locals.merchant.save()
  91. .then(()=>{
  92. return res.json({});
  93. })
  94. .catch((err)=>{
  95. return res.json("ERROR: UNABLE TO DELETE RECIPE");
  96. });
  97. },
  98. /*
  99. GET: toggles the hidden property of a recipe
  100. req.params.id = String (id of recipe)
  101. response = {}
  102. */
  103. hideRecipe: function(req, res){
  104. Recipe.findOne({_id: req.params.id})
  105. .then((recipe)=>{
  106. if(recipe.merchant.toString() !== res.locals.merchant._id.toString()) throw "unauthorized";
  107. recipe.hidden = (recipe.hidden === true) ? false : true;
  108. return recipe.save();
  109. })
  110. .then((recipe)=>{
  111. return res.json({});
  112. })
  113. .catch((err)=>{
  114. if(err === "unauthorized") return res.json("YOU DO NOT HAVE PERMISSION TO EDIT THAT RECIPE");
  115. return res.json("ERROR: UNABLE TO HIDE/UNHIDE THE RECIPE");
  116. });
  117. },
  118. /*
  119. POST: gets a list of individual recipes
  120. req.body = [String] (ids)
  121. response = [Recipe]
  122. */
  123. findRecipes: function(req, res){
  124. Recipe.find(req.body)
  125. .then((recipes)=>{
  126. return res.json(recipes);
  127. })
  128. .catch((err)=>{
  129. return res.json("ERROR: UNABLE TO FIND DELETED RECIPES TO MATCH WITH TRANSACTIONS");
  130. });
  131. }
  132. }