merchantData.js 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  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 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. let errorMessage = "There was an error and your new recipes could not be saved";
  56. let error = new Error({
  57. code: 547,
  58. displayMessage: errorMessage,
  59. error: err
  60. });
  61. error.save();
  62. return res.json(errorMessage);
  63. });
  64. merchant.save()
  65. .then((newMerchant)=>{
  66. newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
  67. .then((newestMerchant)=>{
  68. merchant.password = undefined;
  69. return res.json({merchant: newestMerchant, count: result.data.elements.length});
  70. })
  71. .catch((err)=>{
  72. let errorMessage = "Unable to retrieve recipe ingredients";
  73. let error = new Error({
  74. code: 626,
  75. displayMessage: errorMessage,
  76. error: err
  77. });
  78. error.save();
  79. return res.json(errorMessage);
  80. });
  81. })
  82. .catch((err)=>{
  83. let errorMessage = "Unable to save changes from Clover";
  84. let error = new Error({
  85. code: 547,
  86. displayMessage: errorMessage,
  87. error: err
  88. });
  89. error.save();
  90. return res.json(errorMessage);
  91. });
  92. })
  93. .catch((err)=>{
  94. let errorMessage = "Unable to retrieve data from Clover";
  95. let error = new Error({
  96. code: 111,
  97. displayMessage: errorMessage,
  98. error: err
  99. });
  100. error.save();
  101. return res.json(errorMessage);
  102. });
  103. })
  104. .catch((err)=>{
  105. let errorMessage = "Unable to retrieve merchant data";
  106. let error = new Error({
  107. code: 626,
  108. displayMessage: errorMessage,
  109. error: err
  110. });
  111. error.save();
  112. return res.json(errorMessage);
  113. });
  114. },
  115. //POST - Creates a Clover merchant from all entered data
  116. //Inputs:
  117. // req.body.data: All data from frontend in form of merchant model
  118. //Redirect to "/inventory"
  119. createMerchantClover: function(req, res){
  120. let data = JSON.parse(req.body.data);
  121. data.email = data.email.toLowerCase();
  122. if(data.password.length < 15 || data.password !== data.confirmPassword){
  123. req.session.error = "Passwords must match and contain at least 15 characters";
  124. return res.redirect("/");
  125. }
  126. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}?access_token=${token}`)
  127. .then((cloverMerchant)=>{
  128. req.session.posId = undefined;
  129. let salt = bcrypt.genSaltSync(10);
  130. let hash = bcrypt.hashSync(data.password, salt);
  131. let merchant = new Merchant({
  132. name: cloverMerchant.data.name,
  133. email: data.email,
  134. password: hash,
  135. pos: "clover",
  136. posId: cloverMerchant.data.id
  137. });
  138. for(let item of data.inventory){
  139. merchant.inventory.push({
  140. ingredient: item.ingredient.id,
  141. quantity: item.quantity
  142. });
  143. }
  144. for(let recipe of data.recipes){
  145. recipe.merchant = merchant._id;
  146. }
  147. Recipe.create(data.recipes)
  148. .then((recipes)=>{
  149. for(let recipe of recipes){
  150. merchant.recipes.push(recipe._id);
  151. }
  152. merchant.save()
  153. .then((merchant)=>{
  154. req.session.user = merchant._id;
  155. return res.redirect("/inventory");
  156. })
  157. .catch((err)=>{
  158. let errorMessage = "There was an error and your account could not be created";
  159. let error = new Error({
  160. code: 547,
  161. displayMessage: errorMessage,
  162. error: err
  163. });
  164. error.save();
  165. return;
  166. });
  167. })
  168. .catch((err)=>{
  169. let errorMessage = "There was an error and your recipes could not be saved";
  170. let error = new Error({
  171. code: 547,
  172. displaymessage: errorMessage,
  173. error: err
  174. });
  175. error.save();
  176. return;
  177. });
  178. })
  179. .catch((err)=>{
  180. let errorMessage = "Unable to retrieve your data from Clover";
  181. let error = new Error({
  182. code: 111,
  183. displayMessage: errorMessage,
  184. error: err
  185. });
  186. error.save();
  187. return;
  188. });
  189. },
  190. //POST - Creates a non-pos merchant from all entered data
  191. //Inputs:
  192. // req.body.data: All data from frontend in form of merchant model
  193. //Redirects to "/inventory"
  194. createMerchantNone: function(req, res){
  195. let data = JSON.parse(req.body.data);
  196. data.email = data.email.toLowerCase();
  197. let salt = bcrypt.genSaltSync(10);
  198. let hash = bcrypt.hashSync(data.password, salt);
  199. let merchant = new Merchant({
  200. name: data.name,
  201. email: data.email,
  202. password: hash,
  203. pos: "none"
  204. });
  205. for(let item of data.inventory){
  206. merchant.inventory.push({
  207. ingredient: item.ingredient.id,
  208. quantity: item.quantity
  209. });
  210. }
  211. for(let recipe of data.recipes){
  212. recipe.merchant = merchant._id;
  213. }
  214. Recipe.create(data.recipes)
  215. .then((recipes)=>{
  216. for(let recipe of recipes){
  217. merchant.recipes.push(recipe._id);
  218. }
  219. merchant.save()
  220. .then((merchant)=>{
  221. req.session.user = merchant._id;
  222. return res.redirect("/inventory");
  223. })
  224. .catch((err)=>{
  225. let errorMessage = "There was an error and your account could not be created";
  226. let error = new Error({
  227. code: 547,
  228. displayMessage: errorMessage,
  229. error: err
  230. });
  231. error.save();
  232. return;
  233. });
  234. })
  235. .catch((err)=>{
  236. let errorMessage = "There was an error while trying to save your recipes";
  237. let error = new Error({
  238. code: 547,
  239. displayMessage: errorMessage,
  240. error: err
  241. });
  242. error.save();
  243. return;
  244. });
  245. },
  246. //POST - Adds an ingredient to merchant's inventory
  247. //Inputs:
  248. // req.body: A merchant inventory item (ingredient id and quantity)
  249. //Returns:
  250. // ingredient: Newly added ingredient
  251. addMerchantIngredient: function(req, res){
  252. if(!req.session.user){
  253. req.session.error = "Must be logged in to do that";
  254. return res.redirect("/");
  255. }
  256. Merchant.findOne({_id: req.session.user})
  257. .then((merchant)=>{
  258. merchant.inventory.push(req.body);
  259. merchant.save()
  260. .then((newMerchant)=>{
  261. newMerchant.populate("inventory.ingredient", (err)=>{
  262. if(err){
  263. let errorMessage = "Ingredient updated, page refresh required to display";
  264. let error = new Error({
  265. code: 626,
  266. displayMessage: errorMessage,
  267. error: err
  268. });
  269. error.save();
  270. return res.json(errorMessage);
  271. }else{
  272. let newIngredient = newMerchant.inventory.find(i => i.ingredient._id.toString() === req.body.ingredient);
  273. return res.json(newIngredient);
  274. }
  275. });
  276. })
  277. .catch((err)=>{
  278. let errorMessage = "Unable to save new ingredient";
  279. let error = new Error({
  280. code: 547,
  281. displayMessage: errorMessage,
  282. error: err
  283. });
  284. error.save();
  285. console.log(err);
  286. return res.json(errorMessage);
  287. });
  288. })
  289. .catch((err)=>{
  290. let errorMessage = "Unable to retrieve merchant data";
  291. let error = new Error({
  292. code: 547,
  293. displayMessage: errorMessage,
  294. error: err
  295. });
  296. error.save();
  297. console.log("error2");
  298. return res.json(errorMessage);
  299. });
  300. },
  301. //POST - Removes an ingredient from the merchant's inventory
  302. //Inputs:
  303. // ingredientId: id of ingredient to remove
  304. //Returns: Nothing
  305. removeMerchantIngredient: function(req, res){
  306. if(!req.session.user){
  307. req.session.error = "Must be logged in to do that";
  308. return res.redirect("/");
  309. }
  310. Merchant.findOne({_id: req.session.user})
  311. .then((merchant)=>{
  312. for(let i = 0; i < merchant.inventory.length; i++){
  313. if(req.body.ingredientId === merchant.inventory[i]._id.toString()){
  314. merchant.inventory.splice(i, 1);
  315. break;
  316. }
  317. }
  318. merchant.save()
  319. .then((merchant)=>{
  320. return res.json(req.body);
  321. })
  322. .catch((err)=>{
  323. let errorMessage = "Unable to update ingredients";
  324. let error = new Error({
  325. code: 547,
  326. displayMessage: errorMessage,
  327. error: err
  328. });
  329. error.save();
  330. return res.json(errorMessage);
  331. });
  332. })
  333. .catch((err)=>{
  334. let errorMessage = "Unable to retrieve merchant data";
  335. let error = new Error({
  336. code: 626,
  337. displayMessage: errorMessage,
  338. error: err
  339. });
  340. error.save();
  341. return res.json(errorMessage);
  342. });
  343. },
  344. //POST - Update the quantity for a merchant inventory item
  345. //Inputs:
  346. // req.body.ingredientId: Id of ingredient to update
  347. // req.body.quantityChange: Amount to change ingredient (not the new value)
  348. //Returns: Nothing
  349. updateMerchantIngredient: function(req, res){
  350. if(!req.session.user){
  351. req.session.error = "Must be logged in to do that";
  352. return res.redirect("/");
  353. }
  354. Merchant.findOne({_id: req.session.user})
  355. .then((merchant)=>{
  356. let updateIngredient = merchant.inventory.find(i => i._id.toString() === req.body.ingredientId);
  357. updateIngredient.quantity += req.body.quantityChange;
  358. merchant.save()
  359. .then((merchant)=>{
  360. res.json(req.body.quantityChange);
  361. })
  362. .catch((err)=>{
  363. let errorMessage = "There was an error and your data could not be saved";
  364. let error = new Error({
  365. code: 547,
  366. displayMessage: errorMessage,
  367. error: err
  368. });
  369. error.save();
  370. return res.json(errorMessage);
  371. })
  372. })
  373. .catch((err)=>{
  374. let errorMessage = "There was an error and we could not retrieve your data";
  375. let error = new Error({
  376. code: 626,
  377. displayMessage: errorMessage,
  378. error: err
  379. });
  380. error.save();
  381. return res.json(errorMessage);
  382. });
  383. let invAdj = new InventoryAdjustment({
  384. date: Date.now(),
  385. merchant: req.session.user,
  386. ingredient: req.body.ingredientId,
  387. quantity: req.body.quantityChange
  388. });
  389. invAdj.save()
  390. .catch((err)=>{
  391. let error = new Error({
  392. code: 547,
  393. displayMessage: "none",
  394. error: err
  395. });
  396. error.save();
  397. });
  398. },
  399. //POST - Adds an ingredient to a recipe
  400. //Inputs:
  401. // req.body.recipeId: Id of recipe to change
  402. // req.body.item: Ingredient to add with a quantity
  403. //Returns: Nothing
  404. addRecipeIngredient: function(req, res){
  405. if(!req.session.user){
  406. req.session.error = "Must be logged in to do that";
  407. return res.redirect("/");
  408. }
  409. Recipe.findOne({_id: req.body.recipeId})
  410. .then((recipe)=>{
  411. recipe.ingredients.push({
  412. ingredient: req.body.item.ingredient,
  413. quantity: req.body.item.quantity
  414. });
  415. recipe.save()
  416. .then((recipe)=>{
  417. return res.json({});
  418. })
  419. .catch((err)=>{
  420. let errorMessage = "There was an error and the recipe could not be updated";
  421. let error = new Error({
  422. code: 547,
  423. displayMessage: errorMessage,
  424. error: err
  425. });
  426. error.save();
  427. return res.json(errorMessage);
  428. });
  429. })
  430. .catch((err)=>{
  431. let errorMessage = "There was an error and the recipe could not be updated"
  432. let error = new Error({
  433. code: 626,
  434. displayMessage: errorMessage,
  435. error: err
  436. });
  437. error.save();
  438. return res.json(errorMessage);
  439. });
  440. },
  441. //POST - Change quantity of a recipe's ingredient
  442. //Inputs:
  443. // req.body.recipeId: Id of recipe containing the ingredient
  444. // req.body.ingredient: The ingredient to update (_id and quantity)
  445. //Returns: Nothing
  446. updateRecipeIngredient: function(req, res){
  447. if(!req.session.user){
  448. req.session.error = "Must be logged in to do that";
  449. return res.redirect("/");
  450. }
  451. Recipe.findOne({_id: req.body.recipeId})
  452. .then((recipe)=>{
  453. for(let ingredient of recipe.ingredients){
  454. if(ingredient._id.toString() === req.body.ingredient._id){
  455. ingredient.quantity = req.body.ingredient.quantity;
  456. recipe.save()
  457. .then((recipe)=>{
  458. return res.json({});
  459. })
  460. .catch((err)=>{
  461. let errorMessage = "There was an error and the recipe could not be updated";
  462. let error = new Error({
  463. code: 547,
  464. displayMessage: errorMessage,
  465. error: err
  466. });
  467. error.save();
  468. return res.json(errorMessage);
  469. });
  470. }
  471. }
  472. })
  473. .catch((err)=>{
  474. let errorMessage = "There was an error and the recipe could not be updated";
  475. let error = new Error({
  476. code: 626,
  477. displayMessage: errorMessage,
  478. error: err
  479. });
  480. error.save();
  481. return res.json(errorMessage);
  482. });
  483. },
  484. //POST - Remove an ingredient from a recipe
  485. //Inputs:
  486. // req.body.ingredientId: Id of ingredient to be removed
  487. // req.body.recipeId: Id of recipe to remove ingredient from
  488. //Returns: Nothing
  489. removeRecipeIngredient: function(req, res){
  490. if(!req.session.user){
  491. req.session.error = "Must be logged in to do that";
  492. return res.redirect("/");
  493. }
  494. Recipe.findOne({_id: req.body.recipeId})
  495. .then((recipe)=>{
  496. for(let i = 0; i < recipe.ingredients.length; i++){
  497. if(recipe.ingredients[i]._id.toString() === req.body.ingredientId){
  498. recipe.ingredients.splice(i, 1);
  499. }
  500. }
  501. recipe.save()
  502. .then((recipe)=>{
  503. return res.json();
  504. })
  505. .catch((err)=>{
  506. let errorMessage = "There was an error and the ingredient could not be remove from the recipe";
  507. let error = new Error({
  508. code: 547,
  509. displayMessage: errorMessage,
  510. error: err
  511. });
  512. error.save();
  513. return res.json(errorMessage);
  514. });
  515. })
  516. .catch((err)=>{
  517. let errorMessage = "There was an error and the ingredient could not be removed from the recipe";
  518. let error = new Error({
  519. code: 626,
  520. displayMessage: errorMessage,
  521. error: err
  522. });
  523. error.save();
  524. return res.json(errorMessage);
  525. });
  526. },
  527. }