merchantData.js 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  1. const axios = require("axios");
  2. const bcrypt = require("bcryptjs");
  3. const Error = require("../models/error");
  4. const Merchant = require("../models/merchant");
  5. const Recipe = require("../models/recipe");
  6. const InventoryAdjustment = require("../models/inventoryAdjustment");
  7. const RecipeChange = require("../models/recipeChange");
  8. const token = "b48068eb-411a-918e-ea64-52007147e42c";
  9. module.exports = {
  10. //GET - Checks clover for new or deleted recipes
  11. //Returns:
  12. // merchant: Full merchant (recipe ingredients populated)
  13. // count: Number of new recipes
  14. updateRecipes: function(req, res){
  15. if(!req.session.user){
  16. req.session.error = "Must be logged in to do that";
  17. return res.redirect("/");
  18. }
  19. Merchant.findOne({_id: req.session.user})
  20. .populate("recipes")
  21. .then((merchant)=>{
  22. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${token}`)
  23. .then((result)=>{
  24. let deletedRecipes = merchant.recipes.slice();
  25. for(let i = 0; i < result.data.elements.length; i++){
  26. for(let j = 0; j < deletedRecipes.length; j++){
  27. if(result.data.elements[i].id === deletedRecipes[j].posId){
  28. result.data.elements.splice(i, 1);
  29. deletedRecipes.splice(j, 1);
  30. i--;
  31. break;
  32. }
  33. }
  34. }
  35. for(let recipe of deletedRecipes){
  36. for(let i = 0; i < merchant.recipes.length; i++){
  37. if(recipe._id === merchant.recipes[i]._id){
  38. merchant.recipes.splice(i, 1);
  39. break;
  40. }
  41. }
  42. }
  43. let newRecipes = []
  44. for(let recipe of result.data.elements){
  45. let newRecipe = new Recipe({
  46. posId: recipe.id,
  47. merchant: merchant._id,
  48. name: recipe.name,
  49. ingredients: []
  50. });
  51. merchant.recipes.push(newRecipe);
  52. newRecipes.push(newRecipe);
  53. }
  54. Recipe.create(newRecipes)
  55. .catch((err)=>{
  56. let errorMessage = "There was an error and your new recipes could not be saved";
  57. let error = new Error({
  58. code: 547,
  59. displayMessage: errorMessage,
  60. error: err
  61. });
  62. error.save();
  63. return res.json(errorMessage);
  64. });
  65. merchant.save()
  66. .then((newMerchant)=>{
  67. newMerchant.populate(["recipes.ingredients.ingredient", "inventory.ingredient"]).execPopulate()
  68. .then((newestMerchant)=>{
  69. merchant.password = undefined;
  70. return res.json(newestMerchant);
  71. })
  72. .catch((err)=>{
  73. let errorMessage = "Unable to retrieve recipe ingredients";
  74. let error = new Error({
  75. code: 626,
  76. displayMessage: errorMessage,
  77. error: err
  78. });
  79. error.save();
  80. return res.json(errorMessage);
  81. });
  82. })
  83. .catch((err)=>{
  84. let errorMessage = "Unable to save changes from Clover";
  85. let error = new Error({
  86. code: 547,
  87. displayMessage: errorMessage,
  88. error: err
  89. });
  90. error.save();
  91. return res.json(errorMessage);
  92. });
  93. })
  94. .catch((err)=>{
  95. let errorMessage = "Unable to retrieve data from Clover";
  96. let error = new Error({
  97. code: 111,
  98. displayMessage: errorMessage,
  99. error: err
  100. });
  101. error.save();
  102. return res.json(errorMessage);
  103. });
  104. })
  105. .catch((err)=>{
  106. let errorMessage = "Unable to retrieve merchant data";
  107. let error = new Error({
  108. code: 626,
  109. displayMessage: errorMessage,
  110. error: err
  111. });
  112. error.save();
  113. return res.json(errorMessage);
  114. });
  115. },
  116. //POST - Creates a Clover merchant from all entered data
  117. //Inputs:
  118. // req.body.data: All data from frontend in form of merchant model
  119. //Redirect to "/inventory"
  120. createMerchantClover: async function(req, res){
  121. let data = JSON.parse(req.body.data);
  122. data.email = data.email.toLowerCase();
  123. let merchant = await Merchant.findOne({email: data.email});
  124. if(merchant){
  125. req.session.error = "Email already in use";
  126. return res.redirect("/merchant/new/clover");
  127. }
  128. if(data.password.length < 15 || data.password !== data.confirmPassword){
  129. req.session.error = "Passwords must match and contain at least 15 characters";
  130. return res.redirect("/");
  131. }
  132. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}?access_token=${token}`)
  133. .then((cloverMerchant)=>{
  134. req.session.posId = undefined;
  135. let salt = bcrypt.genSaltSync(10);
  136. let hash = bcrypt.hashSync(data.password, salt);
  137. let merchant = new Merchant({
  138. name: cloverMerchant.data.name,
  139. email: data.email,
  140. password: hash,
  141. pos: "clover",
  142. posId: cloverMerchant.data.id
  143. });
  144. for(let item of data.inventory){
  145. merchant.inventory.push({
  146. ingredient: item.ingredient.id,
  147. quantity: item.quantity
  148. });
  149. }
  150. for(let recipe of data.recipes){
  151. recipe.merchant = merchant._id;
  152. }
  153. Recipe.create(data.recipes)
  154. .then((recipes)=>{
  155. for(let recipe of recipes){
  156. merchant.recipes.push(recipe._id);
  157. }
  158. merchant.save()
  159. .then((merchant)=>{
  160. req.session.user = merchant._id;
  161. return res.redirect("/inventory");
  162. })
  163. .catch((err)=>{
  164. let errorMessage = "There was an error and your account could not be created";
  165. let error = new Error({
  166. code: 547,
  167. displayMessage: errorMessage,
  168. error: err
  169. });
  170. error.save();
  171. return;
  172. });
  173. })
  174. .catch((err)=>{
  175. let errorMessage = "There was an error and your recipes could not be saved";
  176. let error = new Error({
  177. code: 547,
  178. displaymessage: errorMessage,
  179. error: err
  180. });
  181. error.save();
  182. return;
  183. });
  184. })
  185. .catch((err)=>{
  186. let errorMessage = "Unable to retrieve your data from Clover";
  187. let error = new Error({
  188. code: 111,
  189. displayMessage: errorMessage,
  190. error: err
  191. });
  192. error.save()
  193. return;
  194. });
  195. },
  196. //POST - Creates a non-pos merchant from all entered data
  197. //Inputs:
  198. // req.body.data: All data from frontend in form of merchant model
  199. //Redirects to "/inventory"
  200. createMerchantNone: async function(req, res){
  201. let data = JSON.parse(req.body.data);
  202. data.email = data.email.toLowerCase();
  203. let merchantExists = await Merchant.findOne({email: data.email});
  204. if(merchantExists){
  205. req.session.error = "Email already in use";
  206. return res.redirect("/merchant/new/none");
  207. }
  208. if(data.password.length < 15 || data.password !== data.confirmPassword){
  209. req.session.error = "Passwords must match and contain at least 15 characters";
  210. return res.redirect("/");
  211. }
  212. let salt = bcrypt.genSaltSync(10);
  213. let hash = bcrypt.hashSync(data.password, salt);
  214. let merchant = new Merchant({
  215. name: data.name,
  216. email: data.email,
  217. password: hash,
  218. pos: "none"
  219. });
  220. for(let item of data.inventory){
  221. merchant.inventory.push({
  222. ingredient: item.ingredient.id,
  223. quantity: item.quantity
  224. });
  225. }
  226. for(let recipe of data.recipes){
  227. recipe.merchant = merchant._id;
  228. }
  229. Recipe.create(data.recipes)
  230. .then((recipes)=>{
  231. for(let recipe of recipes){
  232. merchant.recipes.push(recipe._id);
  233. }
  234. merchant.save()
  235. .then((merchant)=>{
  236. req.session.user = merchant._id;
  237. return res.redirect("/inventory");
  238. })
  239. .catch((err)=>{
  240. let errorMessage = "There was an error and your account could not be created";
  241. let error = new Error({
  242. code: 547,
  243. displayMessage: errorMessage,
  244. error: err
  245. });
  246. error.save();
  247. return;
  248. });
  249. })
  250. .catch((err)=>{
  251. let errorMessage = "There was an error while trying to save your recipes";
  252. let error = new Error({
  253. code: 547,
  254. displayMessage: errorMessage,
  255. error: err
  256. });
  257. error.save();
  258. return;
  259. });
  260. },
  261. //POST - Adds an ingredient to merchant's inventory
  262. //Inputs:
  263. // req.body.ingredient: ingredient id
  264. // req.body.quantity: quantity for the ingredient
  265. //Returns:
  266. // ingredient: Newly added ingredient
  267. addMerchantIngredient: function(req, res){
  268. if(!req.session.user){
  269. req.session.error = "Must be logged in to do that";
  270. return res.redirect("/");
  271. }
  272. Merchant.findOne({_id: req.session.user})
  273. .then((merchant)=>{
  274. for(let item of merchant.inventory){
  275. if(item.ingredient.toString() === req.body.ingredient){
  276. return res.json("Ingredient is already in your inventory");
  277. }
  278. }
  279. merchant.inventory.push(req.body);
  280. merchant.save()
  281. .then((newMerchant)=>{
  282. newMerchant.populate("inventory.ingredient", (err)=>{
  283. if(err){
  284. let errorMessage = "Ingredient updated, page refresh required to display";
  285. let error = new Error({
  286. code: 626,
  287. displayMessage: errorMessage,
  288. error: err
  289. });
  290. error.save();
  291. return res.json(errorMessage);
  292. }else{
  293. let newIngredient = newMerchant.inventory.find(i => i.ingredient._id.toString() === req.body.ingredient);
  294. return res.json(newIngredient);
  295. }
  296. });
  297. })
  298. .catch((err)=>{
  299. let errorMessage = "Unable to save new ingredient";
  300. let error = new Error({
  301. code: 547,
  302. displayMessage: errorMessage,
  303. error: err
  304. });
  305. error.save();
  306. console.log(err);
  307. return res.json(errorMessage);
  308. });
  309. })
  310. .catch((err)=>{
  311. let errorMessage = "Unable to retrieve merchant data";
  312. let error = new Error({
  313. code: 547,
  314. displayMessage: errorMessage,
  315. error: err
  316. });
  317. error.save();
  318. console.log("error2");
  319. return res.json(errorMessage);
  320. });
  321. },
  322. //POST - Removes an ingredient from the merchant's inventory
  323. //Inputs:
  324. // ingredientId: id of ingredient to remove
  325. //Returns: Nothing
  326. removeMerchantIngredient: function(req, res){
  327. if(!req.session.user){
  328. req.session.error = "Must be logged in to do that";
  329. return res.redirect("/");
  330. }
  331. Merchant.findOne({_id: req.session.user})
  332. .then((merchant)=>{
  333. for(let i = 0; i < merchant.inventory.length; i++){
  334. if(req.body.ingredientId === merchant.inventory[i].ingredient._id.toString()){
  335. merchant.inventory.splice(i, 1);
  336. break;
  337. }
  338. }
  339. merchant.save()
  340. .then((merchant)=>{
  341. return res.json(req.body);
  342. })
  343. .catch((err)=>{
  344. let errorMessage = "Unable to update ingredients";
  345. let error = new Error({
  346. code: 547,
  347. displayMessage: errorMessage,
  348. error: err
  349. });
  350. error.save();
  351. return res.json(errorMessage);
  352. });
  353. })
  354. .catch((err)=>{
  355. let errorMessage = "Unable to retrieve merchant data";
  356. let error = new Error({
  357. code: 626,
  358. displayMessage: errorMessage,
  359. error: err
  360. });
  361. error.save();
  362. return res.json(errorMessage);
  363. });
  364. },
  365. //POST - Update the quantity for a merchant inventory item
  366. //Inputs:
  367. // req.body.ingredientId: Id of ingredient to update
  368. // req.body.quantityChange: Amount to change ingredient (not the new value)
  369. //Returns: Nothing
  370. updateMerchantIngredient: function(req, res){
  371. if(!req.session.user){
  372. req.session.error = "Must be logged in to do that";
  373. return res.redirect("/");
  374. }
  375. Merchant.findOne({_id: req.session.user})
  376. .then((merchant)=>{
  377. let updateIngredient = merchant.inventory.find(i => i.ingredient.toString() === req.body.ingredientId);
  378. updateIngredient.quantity = (updateIngredient.quantity + req.body.quantityChange).toFixed(2);
  379. merchant.save()
  380. .then((merchant)=>{
  381. res.json(req.body.quantityChange);
  382. })
  383. .catch((err)=>{
  384. let errorMessage = "Error: your data could not be saved";
  385. let error = new Error({
  386. code: 547,
  387. displayMessage: errorMessage,
  388. error: err
  389. });
  390. error.save();
  391. return res.json(errorMessage);
  392. })
  393. })
  394. .catch((err)=>{
  395. let errorMessage = "Error: your data could not be retrieved";
  396. let error = new Error({
  397. code: 626,
  398. displayMessage: errorMessage,
  399. error: err
  400. });
  401. error.save();
  402. return res.json(errorMessage);
  403. });
  404. let invAdj = new InventoryAdjustment({
  405. date: Date.now(),
  406. merchant: req.session.user,
  407. ingredient: req.body.ingredientId,
  408. quantity: req.body.quantityChange
  409. });
  410. invAdj.save()
  411. .catch((err)=>{
  412. let error = new Error({
  413. code: 547,
  414. displayMessage: "none",
  415. error: err
  416. });
  417. error.save();
  418. });
  419. },
  420. //POST - Adds an ingredient to a recipe
  421. //Inputs:
  422. // req.body.recipeId: Id of recipe to change
  423. // req.body.item: Ingredient to add with a quantity
  424. //Returns:
  425. // recipe: Updated recipe with populated ingredients
  426. addRecipeIngredient: function(req, res){
  427. if(!req.session.user){
  428. req.session.error = "Must be logged in to do that";
  429. return res.redirect("/");
  430. }
  431. Recipe.findOne({_id: req.body.recipeId})
  432. .then((recipe)=>{
  433. recipe.ingredients.push({
  434. ingredient: req.body.item.ingredient,
  435. quantity: req.body.item.quantity
  436. });
  437. recipe.save()
  438. .then((recipe)=>{
  439. recipe.populate("ingredients.ingredient", (err)=>{
  440. if(err){
  441. let errorMessage = "Error: could not retrieve ingredients. Please refresh page to see changes";
  442. let error = new Error({
  443. code: 626,
  444. displayMessage: errorMessage,
  445. error: err
  446. });
  447. error.save();
  448. return res.json(errorMessage);
  449. }
  450. res.json(recipe);
  451. let rc = new RecipeChange({
  452. recipe: recipe,
  453. ingredient: req.body.item.ingredient,
  454. change: req.body.item.quantity
  455. });
  456. rc.save()
  457. .catch((err)=>{
  458. let error = new Error({
  459. code: 120,
  460. displayMessage: "none",
  461. error: err
  462. });
  463. error.save();
  464. });
  465. return;
  466. })
  467. })
  468. .catch((err)=>{
  469. let errorMessage = "There was an error and the recipe could not be updated";
  470. let error = new Error({
  471. code: 547,
  472. displayMessage: errorMessage,
  473. error: err
  474. });
  475. error.save();
  476. return res.json(errorMessage);
  477. });
  478. })
  479. .catch((err)=>{
  480. let errorMessage = "There was an error and the recipe could not be updated"
  481. let error = new Error({
  482. code: 626,
  483. displayMessage: errorMessage,
  484. error: err
  485. });
  486. error.save();
  487. return res.json(errorMessage);
  488. });
  489. },
  490. //POST - Change quantity of a recipe's ingredient
  491. //Inputs:
  492. // req.body.recipeId: Id of recipe containing the ingredient
  493. // req.body.ingredient: The ingredient to update (_id and quantity)
  494. //Returns: Nothing
  495. updateRecipeIngredient: function(req, res){
  496. if(!req.session.user){
  497. req.session.error = "Must be logged in to do that";
  498. return res.redirect("/");
  499. }
  500. Recipe.findOne({_id: req.body.recipeId})
  501. .then((recipe)=>{
  502. for(let ingredient of recipe.ingredients){
  503. if(ingredient._id.toString() === req.body.ingredient._id){
  504. let change = Number(req.body.ingredient.quantity) - ingredient.quantity;
  505. ingredient.quantity = req.body.ingredient.quantity;
  506. recipe.save()
  507. .then((recipe)=>{
  508. res.json({});
  509. let rc = new RecipeChange({
  510. recipe: recipe,
  511. ingredient: ingredient.ingredient,
  512. change: change
  513. });
  514. rc.save()
  515. .catch((err)=>{
  516. let error = new Error({
  517. code: 120,
  518. errorMessage: "none",
  519. error: err
  520. });
  521. error.save();
  522. });
  523. return;
  524. })
  525. .catch((err)=>{
  526. let errorMessage = "There was an error and the recipe could not be updated";
  527. let error = new Error({
  528. code: 547,
  529. displayMessage: errorMessage,
  530. error: err
  531. });
  532. error.save();
  533. return res.json(errorMessage);
  534. });
  535. }
  536. }
  537. })
  538. .catch((err)=>{
  539. let errorMessage = "There was an error and the recipe could not be updated";
  540. let error = new Error({
  541. code: 626,
  542. displayMessage: errorMessage,
  543. error: err
  544. });
  545. error.save();
  546. return res.json(errorMessage);
  547. });
  548. },
  549. //POST - Remove an ingredient from a recipe
  550. //Inputs:
  551. // req.body.ingredientId: Id of ingredient to be removed
  552. // req.body.recipeId: Id of recipe to remove ingredient from
  553. // req.body.quantity: quantity of recipe ingredient for storing
  554. //Returns: Nothing
  555. removeRecipeIngredient: function(req, res){
  556. if(!req.session.user){
  557. req.session.error = "Must be logged in to do that";
  558. return res.redirect("/");
  559. }
  560. Recipe.findOne({_id: req.body.recipeId})
  561. .then((recipe)=>{
  562. for(let i = 0; i < recipe.ingredients.length; i++){
  563. if(recipe.ingredients[i].ingredient._id.toString() === req.body.ingredientId){
  564. recipe.ingredients.splice(i, 1);
  565. break;
  566. }
  567. }
  568. recipe.save()
  569. .then((recipe)=>{
  570. res.json({});
  571. let rc = new RecipeChange({
  572. recipe: req.body.recipeId,
  573. ingredient: req.body.ingredientId,
  574. change: -req.body.quantity
  575. });
  576. rc.save()
  577. .catch((err)=>{
  578. let error = new Error({
  579. code: 120,
  580. displayMessage: "none",
  581. error: err
  582. });
  583. error.save();
  584. });
  585. return;
  586. })
  587. .catch((err)=>{
  588. let errorMessage = "There was an error and the ingredient could not be remove from the recipe";
  589. let error = new Error({
  590. code: 547,
  591. displayMessage: errorMessage,
  592. error: err
  593. });
  594. error.save();
  595. return res.json(errorMessage);
  596. });
  597. })
  598. .catch((err)=>{
  599. let errorMessage = "There was an error and the ingredient could not be removed from the recipe";
  600. let error = new Error({
  601. code: 626,
  602. displayMessage: errorMessage,
  603. error: err
  604. });
  605. error.save();
  606. return res.json(errorMessage);
  607. });
  608. }
  609. }