home.js 21 KB

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