merchantData.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394
  1. const axios = require("axios");
  2. const bcrypt = require("bcryptjs");
  3. const Merchant = require("../models/merchant");
  4. const Recipe = require("../models/recipe");
  5. const InventoryAdjustment = require("../models/inventoryAdjustment");
  6. const RecipeChange = require("../models/recipeChange");
  7. const token = "b48068eb-411a-918e-ea64-52007147e42c";
  8. module.exports = {
  9. //GET - Checks clover for new or deleted recipes
  10. //Returns:
  11. // merchant: Full merchant (recipe ingredients populated)
  12. // count: Number of new recipes
  13. updateRecipes: function(req, res){
  14. if(!req.session.user){
  15. req.session.error = "Must be logged in to do that";
  16. return res.redirect("/");
  17. }
  18. Merchant.findOne({_id: req.session.user})
  19. .populate("recipes")
  20. .then((merchant)=>{
  21. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${token}`)
  22. .then((result)=>{
  23. let deletedRecipes = merchant.recipes.slice();
  24. for(let i = 0; i < result.data.elements.length; i++){
  25. for(let j = 0; j < deletedRecipes.length; j++){
  26. if(result.data.elements[i].id === deletedRecipes[j].posId){
  27. result.data.elements.splice(i, 1);
  28. deletedRecipes.splice(j, 1);
  29. i--;
  30. break;
  31. }
  32. }
  33. }
  34. for(let recipe of deletedRecipes){
  35. for(let i = 0; i < merchant.recipes.length; i++){
  36. if(recipe._id === merchant.recipes[i]._id){
  37. merchant.recipes.splice(i, 1);
  38. break;
  39. }
  40. }
  41. }
  42. let newRecipes = []
  43. for(let recipe of result.data.elements){
  44. let newRecipe = new Recipe({
  45. posId: recipe.id,
  46. merchant: merchant._id,
  47. name: recipe.name,
  48. ingredients: []
  49. });
  50. merchant.recipes.push(newRecipe);
  51. newRecipes.push(newRecipe);
  52. }
  53. Recipe.create(newRecipes)
  54. .catch((err)=>{
  55. return res.json("Error: unable to create recipes");
  56. });
  57. merchant.save()
  58. .then((newMerchant)=>{
  59. newMerchant.populate(["recipes.ingredients.ingredient", "inventory.ingredient"]).execPopulate()
  60. .then((newestMerchant)=>{
  61. merchant.password = undefined;
  62. return res.json(newestMerchant);
  63. })
  64. .catch((err)=>{
  65. return res.json("Error: unable to retrieve user data");
  66. });
  67. })
  68. .catch((err)=>{
  69. return res.json("Error: unable to retrieve user data");
  70. });
  71. })
  72. .catch((err)=>{
  73. return res.json("Error: unable to retrieve data from Clover");
  74. });
  75. })
  76. .catch((err)=>{
  77. return res.json("Error: unable to retrieve user data");
  78. });
  79. },
  80. //POST - Adds an ingredient to merchant's inventory
  81. //Inputs:
  82. // req.body.ingredient: ingredient id
  83. // req.body.quantity: quantity for the ingredient
  84. //Returns:
  85. // ingredient: Newly added ingredient
  86. addMerchantIngredient: function(req, res){
  87. if(!req.session.user){
  88. req.session.error = "Must be logged in to do that";
  89. return res.redirect("/");
  90. }
  91. Merchant.findOne({_id: req.session.user})
  92. .then((merchant)=>{
  93. for(let item of merchant.inventory){
  94. if(item.ingredient.toString() === req.body.ingredient){
  95. return res.json("Ingredient is already in your inventory");
  96. }
  97. }
  98. merchant.inventory.push(req.body);
  99. merchant.save()
  100. .then((newMerchant)=>{
  101. newMerchant.populate("inventory.ingredient", (err)=>{
  102. if(err){
  103. return res.json("Warning: refresh page to view updates");
  104. }else{
  105. let newIngredient = newMerchant.inventory.find(i => i.ingredient._id.toString() === req.body.ingredient);
  106. return res.json(newIngredient);
  107. }
  108. });
  109. })
  110. .catch((err)=>{
  111. return res.json("Error: unable to save new ingredient");
  112. });
  113. })
  114. .catch((err)=>{
  115. return res.json("Error: unable to retrieve user data");
  116. });
  117. },
  118. //POST - Removes an ingredient from the merchant's inventory
  119. //Inputs:
  120. // ingredientId: id of ingredient to remove
  121. //Returns: Nothing
  122. removeMerchantIngredient: function(req, res){
  123. if(!req.session.user){
  124. req.session.error = "Must be logged in to do that";
  125. return res.redirect("/");
  126. }
  127. Merchant.findOne({_id: req.session.user})
  128. .then((merchant)=>{
  129. for(let i = 0; i < merchant.inventory.length; i++){
  130. if(req.body.ingredientId === merchant.inventory[i].ingredient._id.toString()){
  131. merchant.inventory.splice(i, 1);
  132. break;
  133. }
  134. }
  135. merchant.save()
  136. .then((merchant)=>{
  137. return res.json(req.body);
  138. })
  139. .catch((err)=>{
  140. return res.json("Error: unable to save user data");
  141. });
  142. })
  143. .catch((err)=>{
  144. return res.json("Error: unable to retrieve user data");
  145. });
  146. },
  147. //POST - Update the quantity for a merchant inventory item
  148. //Inputs:
  149. // req.body.ingredientId: Id of ingredient to update
  150. // req.body.quantityChange: Amount to change ingredient (not the new value)
  151. //Returns: Nothing
  152. updateMerchantIngredient: function(req, res){
  153. if(!req.session.user){
  154. req.session.error = "Must be logged in to do that";
  155. return res.redirect("/");
  156. }
  157. Merchant.findOne({_id: req.session.user})
  158. .then((merchant)=>{
  159. let updateIngredient = merchant.inventory.find(i => i.ingredient.toString() === req.body.ingredientId);
  160. updateIngredient.quantity = (updateIngredient.quantity + req.body.quantityChange).toFixed(2);
  161. merchant.save()
  162. .then((newMerchant)=>{
  163. res.json({});
  164. })
  165. .catch((err)=>{
  166. return res.json("Error: your data could not be saved");
  167. })
  168. })
  169. .catch((err)=>{
  170. return res.json("Error: your data could not be retrieved");
  171. });
  172. let invAdj = new InventoryAdjustment({
  173. date: Date.now(),
  174. merchant: req.session.user,
  175. ingredient: req.body.ingredientId,
  176. quantity: req.body.quantityChange
  177. });
  178. invAdj.save().catch((err)=>{});
  179. },
  180. //POST - Adds an ingredient to a recipe
  181. //Inputs:
  182. // req.body.recipeId: Id of recipe to change
  183. // req.body.item: Ingredient to add with a quantity
  184. //Returns:
  185. // recipe: Updated recipe with populated ingredients
  186. addRecipeIngredient: function(req, res){
  187. if(!req.session.user){
  188. req.session.error = "Must be logged in to do that";
  189. return res.redirect("/");
  190. }
  191. Recipe.findOne({_id: req.body.recipeId})
  192. .then((recipe)=>{
  193. recipe.ingredients.push({
  194. ingredient: req.body.item.ingredient,
  195. quantity: req.body.item.quantity
  196. });
  197. recipe.save()
  198. .then((recipe)=>{
  199. recipe.populate("ingredients.ingredient", (err)=>{
  200. if(err){
  201. return res.json("Error: could not retrieve ingredients. Please refresh page to see changes");
  202. }
  203. res.json(recipe);
  204. let rc = new RecipeChange({
  205. recipe: recipe,
  206. ingredient: req.body.item.ingredient,
  207. change: req.body.item.quantity
  208. });
  209. rc.save().catch((err)=>{});
  210. return;
  211. })
  212. })
  213. .catch((err)=>{
  214. return res.json("Error: unable to save recipe data");
  215. });
  216. })
  217. .catch((err)=>{
  218. return res.json("Error: unable to retrieve recipe data");
  219. });
  220. },
  221. //POST - Change quantity of a recipe's ingredient
  222. //Inputs:
  223. // req.body.recipeId: Id of recipe containing the ingredient
  224. // req.body.ingredient: The ingredient to update (_id and quantity)
  225. //Returns: Nothing
  226. updateRecipeIngredient: function(req, res){
  227. if(!req.session.user){
  228. req.session.error = "Must be logged in to do that";
  229. return res.redirect("/");
  230. }
  231. Recipe.findOne({_id: req.body.recipeId})
  232. .then((recipe)=>{
  233. for(let ingredient of recipe.ingredients){
  234. if(ingredient._id.toString() === req.body.ingredient._id){
  235. let change = Number(req.body.ingredient.quantity) - ingredient.quantity;
  236. ingredient.quantity = req.body.ingredient.quantity;
  237. recipe.save()
  238. .then((recipe)=>{
  239. res.json({});
  240. let rc = new RecipeChange({
  241. recipe: recipe,
  242. ingredient: ingredient.ingredient,
  243. change: change
  244. });
  245. rc.save().catch((err)=>{});
  246. return;
  247. })
  248. .catch((err)=>{
  249. return res.json("Error: Could not save recipe data");
  250. });
  251. }
  252. }
  253. })
  254. .catch((err)=>{
  255. return res.json("Error: unable to retrieve recipe data");
  256. });
  257. },
  258. //POST - Remove an ingredient from a recipe
  259. //Inputs:
  260. // req.body.ingredientId: Id of ingredient to be removed
  261. // req.body.recipeId: Id of recipe to remove ingredient from
  262. // req.body.quantity: quantity of recipe ingredient for storing
  263. //Returns: Nothing
  264. removeRecipeIngredient: function(req, res){
  265. if(!req.session.user){
  266. req.session.error = "Must be logged in to do that";
  267. return res.redirect("/");
  268. }
  269. Recipe.findOne({_id: req.body.recipeId})
  270. .then((recipe)=>{
  271. for(let i = 0; i < recipe.ingredients.length; i++){
  272. if(recipe.ingredients[i].ingredient._id.toString() === req.body.ingredientId){
  273. recipe.ingredients.splice(i, 1);
  274. break;
  275. }
  276. }
  277. recipe.save()
  278. .then((recipe)=>{
  279. res.json({});
  280. let rc = new RecipeChange({
  281. recipe: req.body.recipeId,
  282. ingredient: req.body.ingredientId,
  283. change: -req.body.quantity
  284. });
  285. rc.save().catch((err)=>{});
  286. return;
  287. })
  288. .catch((err)=>{
  289. return res.json("Error: unable to save recipe data");
  290. });
  291. })
  292. .catch((err)=>{
  293. return res.json("Error: unable to retrieve recipe data");
  294. });
  295. },
  296. //POST - Update merchant information
  297. //Inputs:
  298. // req.body.name: name update
  299. // req.body.email: email update
  300. //Returns: Nothing
  301. updateMerchant: function(req, res){
  302. if(!req.session.user){
  303. req.session.error = "Must be logged in to do that";
  304. return res.redirect("/");
  305. }
  306. Merchant.findOne({_id: req.session.user})
  307. .then((merchant)=>{
  308. merchant.name = req.body.name;
  309. merchant.email = req.body.email;
  310. merchant.save()
  311. .then((updatedMerchant)=>{
  312. return res.json({});
  313. })
  314. .catch((err)=>{
  315. return res.json("Error: unable to save merchant data");
  316. });
  317. })
  318. .catch((err)=>{
  319. return res.json("Error: unable to retrieve merchant data");
  320. });
  321. },
  322. //POST - Update merchant password
  323. //Inputs:
  324. // req.body.oldPass: current merchant password (supposedly)
  325. // req.body.newPass: replacement password
  326. //Returns: Nothing
  327. updatePassword: function(req, res){
  328. if(!req.session.user){
  329. req.session.error = "Must be logged in to do that";
  330. return res.redirect("/");
  331. }
  332. Merchant.findOne({_id: req.session.user})
  333. .then((merchant)=>{
  334. bcrypt.compare(req.body.oldPass, merchant.password, (err, result)=>{
  335. if(result){
  336. let salt = bcrypt.genSaltSync(10);
  337. let hash = bcrypt.hashSync(req.body.newPass, salt);
  338. merchant.password = hash;
  339. merchant.save()
  340. .then((updatedMerchant)=>{
  341. return res.json({});
  342. })
  343. .catch((err)=>{
  344. return res.json("Error: Unable to save new password");
  345. });
  346. }else{
  347. return res.json("Error: old password does not match current password");
  348. }
  349. });
  350. })
  351. .catch((err)=>{
  352. return res.json("Error: Unable to retrieve merchant data");
  353. });
  354. }
  355. }