recipeData.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. const Recipe = require("../models/recipe.js");
  2. const Merchant = require("../models/merchant.js");
  3. const RecipeChange = require("../models/recipeChange.js");
  4. const Validator = require("./validator.js");
  5. const axios = require("axios");
  6. module.exports = {
  7. /*
  8. POST - creates a single new recipe
  9. req.body = {
  10. name: name of recipe,
  11. price: price of the recipe,
  12. ingredients: [{
  13. id: id of ingredient,
  14. quantity: quantity of ingredient in recipe
  15. }]
  16. }
  17. Return = newly created recipe in same form as above, with _id
  18. */
  19. createRecipe: function(req, res){
  20. if(!req.session.user){
  21. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  22. return res.redirect("/");
  23. }
  24. let validation = Validator.recipe(req.body);
  25. if(validation !== true){
  26. return res.json(validation);
  27. }
  28. let recipe = new Recipe({
  29. merchant: req.session.user,
  30. name: req.body.name,
  31. price: Math.round(req.body.price * 100),
  32. ingredients: req.body.ingredients
  33. });
  34. Merchant.findOne({_id: req.session.user})
  35. .then((merchant)=>{
  36. merchant.recipes.push(recipe);
  37. merchant.save()
  38. .catch((err)=>{
  39. return res.json("ERROR: UNABLE TO SAVE RECIPE");
  40. });
  41. })
  42. .catch((err)=>{
  43. return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
  44. });
  45. recipe.save()
  46. .then((newRecipe)=>{
  47. return res.json(newRecipe);
  48. })
  49. .catch((err)=>{
  50. return res.json("ERROR: UNABLE TO SAVE INGREDIENT");
  51. });
  52. },
  53. /*
  54. PUT - Update a single recipe
  55. req.body = {
  56. id: id of recipe,
  57. name: name of recipe,
  58. price: price of recipe,
  59. ingredients: [{
  60. ingredient: id of ingredient,
  61. quantity: quantity of ingredient in recipe
  62. }]
  63. }
  64. */
  65. updateRecipe: function(req, res){
  66. if(!req.session.user){
  67. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  68. return res.redirect("/");
  69. }
  70. let validation = Validator.recipe(req.body);
  71. if(validation !== true){
  72. return res.json(validation);
  73. }
  74. let changes = [];
  75. Recipe.findOne({_id: req.body.id})
  76. .then((recipe)=>{
  77. for(let i = 0; i < req.body.ingredients.length; i++){
  78. let isMatch = false;
  79. for(let j = 0; j < recipe.ingredients.length; j++){
  80. if(req.body.ingredients[i].ingredient === recipe.ingredients[j].ingredient.toString()){
  81. let difference = parseFloat((req.body.ingredients[i].quantity - recipe.ingredients[j].quantity).toFixed(2));
  82. if(difference !== 0){
  83. changes.push({
  84. ingredient: recipe.ingredients[j].ingredient,
  85. change: difference
  86. });
  87. }
  88. isMatch = true;
  89. break;
  90. }
  91. }
  92. if(!isMatch){
  93. changes.push({
  94. ingredient: req.body.ingredients[i].ingredient,
  95. change: req.body.ingredients[i].quantity
  96. });
  97. }
  98. }
  99. for(let i = 0; i < recipe.ingredients.length; i++){
  100. let isMatch = false;
  101. for(let j = 0; j < req.body.ingredients.length; j++){
  102. if(recipe.ingredients[i].ingredient.toString() === req.body.ingredients[i].ingredient){
  103. isMatch = true;
  104. }
  105. }
  106. if(!isMatch){
  107. changes.push({
  108. ingredient: recipe.ingredients[i].ingredient,
  109. change: -recipe.ingredients[i].quantity
  110. });
  111. }
  112. }
  113. recipe.name = req.body.name;
  114. recipe.price = req.body.price;
  115. recipe.ingredients = req.body.ingredients;
  116. return recipe.save()
  117. })
  118. .then((response)=>{
  119. res.json({});
  120. let recipeChange = new RecipeChange({
  121. recipe: response._id,
  122. date: new Date(),
  123. changes: changes
  124. });
  125. return recipeChange.save()
  126. })
  127. .catch((err)=>{
  128. return res.json("ERROR: UNABLE TO UPDATE RECIPE");
  129. });
  130. },
  131. //GET - Checks clover for new or deleted recipes
  132. //Returns:
  133. // merchant: Full merchant (recipe ingredients populated)
  134. // count: Number of new recipes
  135. updateRecipesClover: function(req, res){
  136. if(!req.session.user){
  137. req.session.error = "Must be logged in to do that";
  138. return res.redirect("/");
  139. }
  140. Merchant.find({_id: req.session.user})
  141. .populate("recipes")
  142. .then((response)=>{
  143. merchant = response[0];
  144. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${merchant.posAccessToken}`)
  145. .then((result)=>{
  146. let deletedRecipes = merchant.recipes.slice();
  147. for(let i = 0; i < result.data.elements.length; i++){
  148. for(let j = 0; j < deletedRecipes.length; j++){
  149. if(result.data.elements[i].id === deletedRecipes[j].posId){
  150. result.data.elements.splice(i, 1);
  151. deletedRecipes.splice(j, 1);
  152. i--;
  153. break;
  154. }
  155. }
  156. }
  157. for(let recipe of deletedRecipes){
  158. for(let i = 0; i < merchant.recipes.length; i++){
  159. if(recipe._id === merchant.recipes[i]._id){
  160. merchant.recipes.splice(i, 1);
  161. break;
  162. }
  163. }
  164. }
  165. let newRecipes = []
  166. for(let recipe of result.data.elements){
  167. let newRecipe = new Recipe({
  168. posId: recipe.id,
  169. merchant: merchant._id,
  170. name: recipe.name,
  171. ingredients: [],
  172. price: recipe.price
  173. });
  174. merchant.recipes.push(newRecipe);
  175. newRecipes.push(newRecipe);
  176. }
  177. Recipe.create(newRecipes)
  178. .catch((err)=>{
  179. return res.json("ERROR: UNABLE TO SAVE RECIPES");
  180. });
  181. merchant.save()
  182. .then((newMerchant)=>{
  183. newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
  184. .then((newestMerchant)=>{
  185. merchant.password = undefined;
  186. return res.json({new: newRecipes, removed: deletedRecipes});
  187. })
  188. .catch((err)=>{
  189. return res.json("ERROR: UNABLE TO RETRIEVE DATA");
  190. });
  191. })
  192. .catch((err)=>{
  193. return res.json("ERROR: UNABLE TO SAVE CHANGES FROM CLOVER");
  194. });
  195. })
  196. .catch((err)=>{
  197. return res.json("ERROR: UNABLE TO RETRIEVE DATA FROM CLOVER");
  198. });
  199. })
  200. .catch((err)=>{
  201. return res.json("ERROR: UNABLE TO RETRIEVE MERCHANT DATA");
  202. });
  203. }
  204. }