recipeData.js 4.6 KB

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