merchantData.js 17 KB

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