recipeData.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. const axios = require("axios");
  2. const Recipe = require("../models/recipe.js");
  3. const Merchant = require("../models/merchant.js");
  4. const ArchivedRecipe = require("../models/archivedRecipe.js");
  5. const Validator = require("./validator.js");
  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. Recipe.findOne({_id: req.body.id})
  75. .then((recipe)=>{
  76. new ArchivedRecipe({
  77. merchant: req.session.user,
  78. name: recipe.name,
  79. price: recipe.price,
  80. date: new Date(),
  81. ingredients: recipe.ingredients
  82. }).save().catch(()=>{});
  83. recipe.name = req.body.name;
  84. recipe.price = req.body.price;
  85. recipe.ingredients = req.body.ingredients;
  86. return recipe.save();
  87. })
  88. .then((recipe)=>{
  89. res.json(recipe);
  90. })
  91. .catch((err)=>{
  92. return res.json("ERROR: UNABLE TO UPDATE RECIPE");
  93. });
  94. },
  95. //DELETE - removes a single recipe from the merchant and the database
  96. removeRecipe: function(req, res){
  97. if(!req.session.user){
  98. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  99. return res.redirect("/");
  100. }
  101. Merchant.findOne({_id: req.session.user})
  102. .then((merchant)=>{
  103. if(merchant.pos === "clover"){
  104. return res.json("YOU MUST EDIT YOUR RECIPES INSIDE CLOVER");
  105. }
  106. for(let i = 0; i < merchant.recipes.length; i++){
  107. if(merchant.recipes[i].toString() === req.params.id){
  108. merchant.recipes.splice(i, 1);
  109. break;
  110. }
  111. }
  112. merchant.save()
  113. .then((updatedMerchant)=>{
  114. return res.json({});
  115. })
  116. .catch((err)=>{
  117. return res.json("ERROR: UNABLE TO SAVE DATA");
  118. })
  119. return Recipe.deleteOne({_id: req.params.id});
  120. })
  121. .then((response)=>{
  122. return res.json({});
  123. })
  124. .catch((err)=>{
  125. return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
  126. });
  127. },
  128. //GET - Checks clover for new or deleted recipes
  129. //Returns:
  130. // merchant: Full merchant (recipe ingredients populated)
  131. // count: Number of new recipes
  132. updateRecipesClover: function(req, res){
  133. if(!req.session.user){
  134. req.session.error = "Must be logged in to do that";
  135. return res.redirect("/");
  136. }
  137. Merchant.findOne({_id: req.session.user})
  138. .populate("recipes")
  139. .then((merchant)=>{
  140. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${merchant.posAccessToken}`)
  141. .then((result)=>{
  142. let deletedRecipes = merchant.recipes.slice();
  143. for(let i = 0; i < result.data.elements.length; i++){
  144. for(let j = 0; j < deletedRecipes.length; j++){
  145. if(result.data.elements[i].id === deletedRecipes[j].posId){
  146. result.data.elements.splice(i, 1);
  147. deletedRecipes.splice(j, 1);
  148. i--;
  149. break;
  150. }
  151. }
  152. }
  153. for(let i = 0; i < deletedRecipes.length; i++){
  154. for(let j = 0; j < merchant.recipes.length; j++){
  155. if(deletedRecipes[i]._id === merchant.recipes[j]._id){
  156. merchant.recipes.splice(j, 1);
  157. break;
  158. }
  159. }
  160. }
  161. let newRecipes = []
  162. for(let i = 0; i < result.data.elements.length; i++){
  163. let newRecipe = new Recipe({
  164. posId: result.data.elements[i].id,
  165. merchant: merchant._id,
  166. name: result.data.elements[i].name,
  167. ingredients: [],
  168. price: result.data.elements[i].price
  169. });
  170. merchant.recipes.push(newRecipe);
  171. newRecipes.push(newRecipe);
  172. }
  173. Recipe.create(newRecipes)
  174. .catch((err)=>{
  175. return res.json("ERROR: UNABLE TO SAVE RECIPES");
  176. });
  177. merchant.save()
  178. .then((newMerchant)=>{
  179. newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
  180. .then((newestMerchant)=>{
  181. merchant.password = undefined;
  182. return res.json({new: newRecipes, removed: deletedRecipes});
  183. })
  184. .catch((err)=>{
  185. return res.json("ERROR: UNABLE TO RETRIEVE DATA");
  186. });
  187. })
  188. .catch((err)=>{
  189. return res.json("ERROR: UNABLE TO SAVE CHANGES FROM CLOVER");
  190. });
  191. })
  192. .catch((err)=>{
  193. return res.json("ERROR: UNABLE TO RETRIEVE DATA FROM CLOVER");
  194. });
  195. })
  196. .catch((err)=>{
  197. return res.json("ERROR: UNABLE TO RETRIEVE MERCHANT DATA");
  198. });
  199. },
  200. updateRecipesSquare: function(req, res){
  201. if(!req.session.user){
  202. req.session.error = "Must be logged in to do that";
  203. return res.redirect("/");
  204. }
  205. let merchant = {};
  206. let merchantRecipes = [];
  207. let newRecipes = [];
  208. Merchant.findOne({_id: req.session.user})
  209. .populate("recipes")
  210. .then((fetchedMerchant)=>{
  211. merchant = fetchedMerchant;
  212. return axios.post(`${process.env.SQUARE_ADDRESS}/v2/catalog/search`, {
  213. object_types: ["ITEM"]
  214. }, {
  215. headers: {
  216. Authorization: `Bearer ${merchant.posAccessToken}`
  217. }
  218. });
  219. })
  220. .then((response)=>{
  221. merchantRecipes = merchant.recipes.slice();
  222. for(let i = 0; i < response.data.objects.length; i++){
  223. let itemData = response.data.objects[i].item_data;
  224. for(let j = 0; j < itemData.variations.length; j++){
  225. let isFound = false;
  226. for(let k = 0; k < merchantRecipes.length; k++){
  227. if(itemData.variations[j].id === merchantRecipes[k].posId){
  228. merchantRecipes.splice(k, 1);
  229. k--;
  230. isFound = true;
  231. break;
  232. }
  233. }
  234. if(!isFound){
  235. let newRecipe = new Recipe({
  236. posId: itemData.variations[j].id,
  237. merchant: merchant._id,
  238. name: "",
  239. price: itemData.variations[j].item_variation_data.price_money.amount,
  240. ingredients: []
  241. });
  242. if(itemData.variations.length > 1){
  243. newRecipe.name = `${itemData.name} '${itemData.variations[j].item_variation_data.name}'`;
  244. }else{
  245. newRecipe.name = itemData.name;
  246. }
  247. newRecipes.push(newRecipe);
  248. merchant.recipes.push(newRecipe);
  249. }
  250. }
  251. }
  252. let ids = [];
  253. for(let i = 0; i < merchantRecipes.length; i++){
  254. ids.push(merchantRecipes[i]._id);
  255. for(let j = 0; j < merchant.recipes.length; j++){
  256. if(merchantRecipes[i]._id.toString() === merchant.recipes[j]._id.toString()){
  257. merchant.recipes.splice(j, 1);
  258. j--;
  259. break;
  260. }
  261. }
  262. }
  263. if(newRecipes.length > 0){
  264. Recipe.create(newRecipes);
  265. }
  266. if(merchantRecipes.length > 0){
  267. Recipe.deleteMany({_id: {$in: ids}});
  268. }
  269. return merchant.save();
  270. })
  271. .then((merchant)=>{
  272. return res.json({new: newRecipes, removed: merchantRecipes});
  273. })
  274. .catch((err)=>{
  275. return "ERROR: UNABLE TO RETRIEVE RECIPE DATA FROM SQUARE";
  276. });
  277. }
  278. }