home.js 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  1. const axios = require("axios");
  2. const bcrypt = require("bcryptjs");
  3. const Merchant = require("../models/merchant");
  4. const Ingredient = require("../models/ingredient");
  5. const Recipe = require("../models/recipe");
  6. const token = "b48068eb-411a-918e-ea64-52007147e42c";
  7. module.exports = {
  8. landingPage: function(req, res){
  9. return res.render("landingPage/landing");
  10. },
  11. displayInventory: function(req, res){
  12. if(!req.session.user){
  13. return res.redirect("/");
  14. }
  15. Merchant.findOne({_id: req.session.user})
  16. .populate("inventory.ingredient")
  17. .populate("recipes")
  18. .then((merchant)=>{
  19. if(merchant.pos === "clover"){
  20. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${token}`)
  21. .then((result)=>{
  22. for(let order of result.data.elements){
  23. console.log(order);
  24. for(let item of order.lineItems.elements){
  25. console.log(item);
  26. let recipe = merchant.recipes.find(r => r.posId === item.item.id);
  27. if(recipe){
  28. for(let ingredient of recipe.ingredients){
  29. let inventoryIngredient = {};
  30. for(let invItem of merchant.inventory){
  31. if(invItem.ingredient._id.toString() === ingredient.ingredient.toString()){
  32. inventoryIngredient = invItem;
  33. }
  34. }
  35. inventoryIngredient.quantity -= ingredient.quantity;
  36. }
  37. }
  38. }
  39. }
  40. merchant.lastUpdatedTime = Date.now();
  41. merchant.save()
  42. .then((updatedMerchant)=>{
  43. res.render("inventoryPage/inventory", {merchant: updatedMerchant});
  44. // this.storeTransactions(result);
  45. return;
  46. })
  47. .catch((err)=>{
  48. console.log(err);
  49. return res.render("error");
  50. });
  51. })
  52. .catch((err)=>{
  53. console.log(err);
  54. });
  55. }else if(merchant.pos === "none"){
  56. return res.render("inventoryPage/inventory", {merchant: merchant})
  57. }else{
  58. return res.redirect("/");
  59. }
  60. })
  61. .catch((err)=>{
  62. console.log(err);
  63. return res.render("error");
  64. });
  65. },
  66. //Display page to set up new merchant with Clover POS
  67. //TODO: This is for development, needs updating for production
  68. merchantSetupClover: function(req, res){
  69. req.session.posId = "YHVPCQMVB1P81";
  70. Ingredient.find()
  71. .then((ingredients)=>{
  72. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}/items?access_token=${token}`)
  73. .then((recipes)=>{
  74. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
  75. })
  76. .catch((err)=>{
  77. console.log(err);
  78. return res.render("error");
  79. });
  80. })
  81. .catch((err)=>{
  82. console.log(err);
  83. return res.render("error");
  84. });
  85. },
  86. //Display page to set up merchant with no POS system
  87. merchantSetupNone: function(req, res){
  88. Ingredient.find()
  89. .then((ingredients)=>{
  90. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: null});
  91. })
  92. .catch((err)=>{
  93. console.log(err);
  94. return res.render("error");
  95. });
  96. },
  97. displayRecipes: function(req, res){
  98. if(!req.session.user){
  99. return res.render("error");
  100. }
  101. Merchant.findOne({_id: req.session.user})
  102. .populate({
  103. path: "recipes",
  104. model: "Recipe",
  105. populate: {
  106. path: "ingredients.ingredient",
  107. model: "Ingredient"
  108. }
  109. })
  110. .populate("inventory.ingredient")
  111. .then((merchant)=>{
  112. return res.render("recipesPage/recipes", {merchant: merchant});
  113. })
  114. .catch((err)=>{
  115. console.log(err);
  116. return res.render("error");
  117. });
  118. },
  119. updateRecipes: function(req, res){
  120. if(!req.session.user){
  121. return res.render("error");
  122. }
  123. Merchant.findOne({_id: req.session.user})
  124. .populate("recipes")
  125. .then((merchant)=>{
  126. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  127. .then((result)=>{
  128. let deletedRecipes = merchant.recipes.slice();
  129. for(let i = 0; i < result.data.elements.length; i++){
  130. for(let j = 0; j < deletedRecipes.length; j++){
  131. if(result.data.elements[i].id === deletedRecipes[j].posId){
  132. result.data.elements.splice(i, 1);
  133. deletedRecipes.splice(j, 1);
  134. i--;
  135. break;
  136. }
  137. }
  138. }
  139. for(let recipe of deletedRecipes){
  140. for(let i = 0; i < merchant.recipes.length; i++){
  141. if(recipe._id === merchant.recipes[i]._id){
  142. merchant.recipes.splice(i, 1);
  143. break;
  144. }
  145. }
  146. }
  147. let newRecipes = []
  148. for(let recipe of result.data.elements){
  149. let newRecipe = new Recipe({
  150. posId: recipe.id,
  151. merchant: merchant._id,
  152. name: recipe.name,
  153. ingredients: []
  154. });
  155. merchant.recipes.push(newRecipe);
  156. newRecipes.push(newRecipe);
  157. }
  158. Recipe.create(newRecipes)
  159. .catch((err)=>{
  160. console.log(err);
  161. return res.render("error");
  162. });
  163. merchant.save()
  164. .then((newMerchant)=>{
  165. newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
  166. .then((newestMerchant)=>{
  167. return res.json({merchant: newestMerchant, count: result.data.elements.length});
  168. })
  169. .catch((err)=>{
  170. console.log(err);
  171. return res.render("error");
  172. });
  173. })
  174. .catch((err)=>{
  175. console.log(err);
  176. return res.render("error");
  177. });
  178. })
  179. .catch((err)=>{
  180. console.log(err);
  181. return res.render("error");
  182. });
  183. })
  184. .catch((err)=>{
  185. console.log(err);
  186. return res.render("error");
  187. });
  188. },
  189. createMerchantClover: function(req, res){
  190. let data = JSON.parse(req.body.data);
  191. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}?access_token=${token}`)
  192. .then((cloverMerchant)=>{
  193. req.session.posId = undefined;
  194. let salt = bcrypt.genSaltSync(10);
  195. let hash = bcrypt.hashSync(data.password, salt);
  196. let merchant = new Merchant({
  197. name: cloverMerchant.data.name,
  198. email: data.email,
  199. password: hash,
  200. pos: "clover",
  201. posId: cloverMerchant.data.id
  202. });
  203. for(let item of data.inventory){
  204. merchant.inventory.push({
  205. ingredient: item.ingredient.id,
  206. quantity: item.quantity
  207. });
  208. }
  209. for(let recipe of data.recipes){
  210. recipe.merchant = merchant._id;
  211. }
  212. Recipe.create(data.recipes)
  213. .then((recipes)=>{
  214. for(let recipe of recipes){
  215. merchant.recipes.push(recipe._id);
  216. }
  217. merchant.save()
  218. .then((merchant)=>{
  219. req.session.user = merchant._id;
  220. return res.redirect("/inventory");
  221. })
  222. .catch((err)=>{
  223. console.log(err);
  224. return res.render("error");
  225. });
  226. })
  227. .catch((err)=>{
  228. console.log(err);
  229. return res.render("error");
  230. });
  231. Recipe.create(newRecipes)
  232. .then((recipes)=>{
  233. for(let recipe of recipes){
  234. newMerchant.recipes.push(recipe);
  235. }
  236. newMerchant.save()
  237. .then((merchant)=>{
  238. return res.redirect("/inventory");
  239. })
  240. .catch((err)=>{
  241. console.log(err);
  242. return res.render("error");
  243. });
  244. })
  245. .catch((err)=>{
  246. console.log(err);
  247. return res.render("error");
  248. });
  249. })
  250. .catch((err)=>{
  251. console.log(err);
  252. });
  253. },
  254. createMerchantNone: function(req, res){
  255. let data = JSON.parse(req.body.data);
  256. let salt = bcrypt.genSaltSync(10);
  257. let hash = bcrypt.hashSync(data.password, salt);
  258. let merchant = new Merchant({
  259. name: data.name,
  260. email: data.email,
  261. password: hash,
  262. pos: "none"
  263. });
  264. for(let item of data.inventory){
  265. merchant.inventory.push({
  266. ingredient: item.ingredient.id,
  267. quantity: item.quantity
  268. });
  269. }
  270. for(let recipe of data.recipes){
  271. recipe.merchant = merchant._id;
  272. }
  273. Recipe.create(data.recipes)
  274. .then((recipes)=>{
  275. for(let recipe of recipes){
  276. merchant.recipes.push(recipe._id);
  277. }
  278. merchant.save()
  279. .then((merchant)=>{
  280. req.session.user = merchant._id;
  281. return res.redirect("/inventory");
  282. })
  283. .catch((err)=>{
  284. console.log(err);
  285. return res.render("error");
  286. });
  287. })
  288. .catch((err)=>{
  289. console.log(err);
  290. return res.render("error");
  291. });
  292. },
  293. addMerchantIngredient: function(req, res){
  294. if(!req.session.user){
  295. return res.render("error");
  296. }
  297. Merchant.findOne({_id: req.session.user})
  298. .then((merchant)=>{
  299. merchant.inventory.push(req.body);
  300. merchant.save()
  301. .then((newMerchant)=>{
  302. Ingredient.findOne({_id: req.body.ingredient})
  303. .then((ingredient)=>{
  304. return res.json(ingredient);
  305. })
  306. .catch((err)=>{
  307. console.log(err);
  308. return res.render("error");
  309. });
  310. })
  311. .catch((err)=>{
  312. console.log(err);
  313. return res.render("error");
  314. });
  315. })
  316. .catch((err)=>{
  317. console.log(err);
  318. return res.render("error");
  319. });
  320. },
  321. removeMerchantIngredient: function(req, res){
  322. if(!req.session.user){
  323. return res.render("error");
  324. }
  325. Merchant.findOne({_id: req.session.user})
  326. .then((merchant)=>{
  327. for(let i = 0; i < merchant.inventory.length; i++){
  328. if(req.body.ingredientId === merchant.inventory[i]._id.toString()){
  329. merchant.inventory.splice(i, 1);
  330. break;
  331. }
  332. }
  333. merchant.save()
  334. .then(()=>{
  335. return res.json();
  336. })
  337. .catch((err)=>{
  338. console.log(err);
  339. return res.render("error");
  340. });
  341. })
  342. .catch((err)=>{
  343. console.log(err);
  344. return res.render("error");
  345. });
  346. },
  347. updateMerchantIngredient: function(req, res){
  348. if(!req.session.user){
  349. return res.render("error");
  350. }
  351. Merchant.findOne({_id: req.session.user})
  352. .then((merchant)=>{
  353. let updateIngredient = merchant.inventory.find(i => i._id.toString() === req.body.ingredientId);
  354. updateIngredient.quantity = req.body.quantity;
  355. merchant.save()
  356. .then((merchant)=>{
  357. return res.json();
  358. })
  359. .catch((err)=>{
  360. console.log(err);
  361. return res.render("error");
  362. })
  363. })
  364. .catch((err)=>{
  365. console.log(err);
  366. return res.render("error");
  367. });
  368. },
  369. addRecipeIngredient: function(req, res){
  370. if(!req.session.user){
  371. return res.render("error");
  372. }
  373. Recipe.findOne({_id: req.body.recipeId})
  374. .then((recipe)=>{
  375. recipe.ingredients.push({
  376. ingredient: req.body.item.ingredient,
  377. quantity: req.body.item.quantity
  378. });
  379. recipe.save()
  380. .then((recipe)=>{
  381. return res.json();
  382. })
  383. .catch((err)=>{
  384. console.log(err);
  385. return res.render("error");
  386. });
  387. })
  388. .catch((err)=>{
  389. console.log(err);
  390. return res.render("error");
  391. });
  392. },
  393. updateRecipeIngredient: function(req, res){
  394. if(!req.session.user){
  395. return res.render("error");
  396. }
  397. Recipe.findOne({_id: req.body.recipeId})
  398. .then((recipe)=>{
  399. for(let ingredient of recipe.ingredients){
  400. if(ingredient._id.toString() === req.body.ingredient._id){
  401. ingredient.quantity = req.body.ingredient.quantity;
  402. recipe.save()
  403. .then((recipe)=>{
  404. return res.json();
  405. })
  406. .catch((err)=>{
  407. console.log(err);
  408. return res.render("error");
  409. })
  410. }
  411. }
  412. })
  413. .catch((err)=>{
  414. console.log(err);
  415. return res.render("error");
  416. });
  417. },
  418. removeRecipeIngredient: function(req, res){
  419. if(!req.session.user){
  420. return res.render("error");
  421. }
  422. Recipe.findOne({_id: req.body.recipeId})
  423. .then((recipe)=>{
  424. for(let i = 0; i < recipe.ingredients.length; i++){
  425. if(recipe.ingredients[i]._id.toString() === req.body.ingredientId){
  426. recipe.ingredients.splice(i, 1);
  427. }
  428. }
  429. recipe.save()
  430. .then((recipe)=>{
  431. return res.json();
  432. })
  433. .catch((err)=>{
  434. console.log(err);
  435. return res.render("error");
  436. });
  437. })
  438. .catch((err)=>{
  439. console.log(err);
  440. return res.render("error");
  441. });
  442. },
  443. getIngredients: function(req, res){
  444. Ingredient.find()
  445. .then((ingredients)=>{
  446. return res.json(ingredients);
  447. })
  448. .catch((err)=>{
  449. console.log(err);
  450. return res.render("error");
  451. });
  452. },
  453. createNewIngredients: function(req, res){
  454. Ingredient.create(req.body)
  455. .then((ingredients)=>{
  456. return res.json(ingredients);
  457. })
  458. .catch((err)=>{
  459. console.log(err);
  460. return res.render("error");
  461. });
  462. },
  463. createIngredient: function(req, res){
  464. Ingredient.create(req.body.ingredient)
  465. .then((ingredient)=>{
  466. Merchant.findOne({_id: req.session.user})
  467. .then((merchant)=>{
  468. let item = {
  469. ingredient: ingredient,
  470. quantity: req.body.quantity
  471. }
  472. merchant.inventory.push(item);
  473. merchant.save()
  474. .then((merchant)=>{
  475. console.log("something");
  476. return res.json(item);
  477. })
  478. .catch((err)=>{
  479. console.log(err);
  480. return res.render("error");
  481. });
  482. })
  483. .catch((err)=>{
  484. console.log(err);
  485. return res.render("error");
  486. });
  487. })
  488. .catch((err)=>{
  489. console.log(err);
  490. return res.render("error");
  491. });
  492. },
  493. getCloverRecipes: function(req, res){
  494. if(!req.session.user){
  495. return res.render("error");
  496. }
  497. Merchant.findOne({_id: req.session.user})
  498. .then((merchant)=>{
  499. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${token}`)
  500. .then((recipes)=>{
  501. return res.json(recipes);
  502. })
  503. .catch((err)=>{
  504. return res.json(err);
  505. });
  506. })
  507. .catch((err)=>{
  508. console.log(err);
  509. return res.render("error");
  510. });
  511. },
  512. unregistered: function(req, res){
  513. return res.redirect("/");
  514. },
  515. login: function(req, res){
  516. Merchant.findOne({email: req.body.email})
  517. .then((merchant)=>{
  518. bcrypt.compare(req.body.password, merchant.password, (err, result)=>{
  519. if(result){
  520. req.session.user = merchant._id;
  521. return res.redirect("/inventory");
  522. }
  523. console.log(err);
  524. return res.redirect("/");
  525. });
  526. })
  527. .catch((err)=>{
  528. console.log(err);
  529. return res.redirect("/");
  530. });
  531. },
  532. //Helper functions
  533. storeTransactions: function(data){
  534. console.log(data);
  535. }
  536. }