home.js 20 KB

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