home.js 21 KB

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