home.js 24 KB

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