home.js 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  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. if(data.password.length < 15 || data.password !== data.confirmPassword){
  208. return res.render("error");
  209. }
  210. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}?access_token=${token}`)
  211. .then((cloverMerchant)=>{
  212. req.session.posId = undefined;
  213. let salt = bcrypt.genSaltSync(10);
  214. let hash = bcrypt.hashSync(data.password, salt);
  215. let merchant = new Merchant({
  216. name: cloverMerchant.data.name,
  217. email: data.email,
  218. password: hash,
  219. pos: "clover",
  220. posId: cloverMerchant.data.id
  221. });
  222. for(let item of data.inventory){
  223. merchant.inventory.push({
  224. ingredient: item.ingredient.id,
  225. quantity: item.quantity
  226. });
  227. }
  228. for(let recipe of data.recipes){
  229. recipe.merchant = merchant._id;
  230. }
  231. Recipe.create(data.recipes)
  232. .then((recipes)=>{
  233. for(let recipe of recipes){
  234. merchant.recipes.push(recipe._id);
  235. }
  236. merchant.save()
  237. .then((merchant)=>{
  238. req.session.user = merchant._id;
  239. return res.redirect("/inventory");
  240. })
  241. .catch((err)=>{
  242. console.log(err);
  243. return res.render("error");
  244. });
  245. })
  246. .catch((err)=>{
  247. console.log(err);
  248. return res.render("error");
  249. });
  250. Recipe.create(newRecipes)
  251. .then((recipes)=>{
  252. for(let recipe of recipes){
  253. newMerchant.recipes.push(recipe);
  254. }
  255. newMerchant.save()
  256. .then((merchant)=>{
  257. return res.redirect("/inventory");
  258. })
  259. .catch((err)=>{
  260. console.log(err);
  261. return res.render("error");
  262. });
  263. })
  264. .catch((err)=>{
  265. console.log(err);
  266. return res.render("error");
  267. });
  268. })
  269. .catch((err)=>{
  270. console.log(err);
  271. });
  272. },
  273. createMerchantNone: function(req, res){
  274. let data = JSON.parse(req.body.data);
  275. let salt = bcrypt.genSaltSync(10);
  276. let hash = bcrypt.hashSync(data.password, salt);
  277. let merchant = new Merchant({
  278. name: data.name,
  279. email: data.email,
  280. password: hash,
  281. pos: "none"
  282. });
  283. for(let item of data.inventory){
  284. merchant.inventory.push({
  285. ingredient: item.ingredient.id,
  286. quantity: item.quantity
  287. });
  288. }
  289. for(let recipe of data.recipes){
  290. recipe.merchant = merchant._id;
  291. }
  292. Recipe.create(data.recipes)
  293. .then((recipes)=>{
  294. for(let recipe of recipes){
  295. merchant.recipes.push(recipe._id);
  296. }
  297. merchant.save()
  298. .then((merchant)=>{
  299. req.session.user = merchant._id;
  300. return res.redirect("/inventory");
  301. })
  302. .catch((err)=>{
  303. console.log(err);
  304. return res.render("error");
  305. });
  306. })
  307. .catch((err)=>{
  308. console.log(err);
  309. return res.render("error");
  310. });
  311. },
  312. addMerchantIngredient: function(req, res){
  313. if(!req.session.user){
  314. return res.render("error");
  315. }
  316. Merchant.findOne({_id: req.session.user})
  317. .then((merchant)=>{
  318. merchant.inventory.push(req.body);
  319. merchant.save()
  320. .then((newMerchant)=>{
  321. Ingredient.findOne({_id: req.body.ingredient})
  322. .then((ingredient)=>{
  323. return res.json(ingredient);
  324. })
  325. .catch((err)=>{
  326. console.log(err);
  327. return res.render("error");
  328. });
  329. })
  330. .catch((err)=>{
  331. console.log(err);
  332. return res.render("error");
  333. });
  334. })
  335. .catch((err)=>{
  336. console.log(err);
  337. return res.render("error");
  338. });
  339. },
  340. removeMerchantIngredient: function(req, res){
  341. if(!req.session.user){
  342. return res.render("error");
  343. }
  344. Merchant.findOne({_id: req.session.user})
  345. .then((merchant)=>{
  346. for(let i = 0; i < merchant.inventory.length; i++){
  347. if(req.body.ingredientId === merchant.inventory[i]._id.toString()){
  348. merchant.inventory.splice(i, 1);
  349. break;
  350. }
  351. }
  352. merchant.save()
  353. .then(()=>{
  354. return res.json();
  355. })
  356. .catch((err)=>{
  357. console.log(err);
  358. return res.render("error");
  359. });
  360. })
  361. .catch((err)=>{
  362. console.log(err);
  363. return res.render("error");
  364. });
  365. },
  366. updateMerchantIngredient: function(req, res){
  367. if(!req.session.user){
  368. return res.render("error");
  369. }
  370. Merchant.findOne({_id: req.session.user})
  371. .then((merchant)=>{
  372. let updateIngredient = merchant.inventory.find(i => i._id.toString() === req.body.ingredientId);
  373. updateIngredient.quantity += req.body.quantityChange;
  374. merchant.save()
  375. .then((merchant)=>{
  376. res.json();
  377. })
  378. .catch((err)=>{
  379. console.log(err);
  380. return res.render("error");
  381. })
  382. })
  383. .catch((err)=>{
  384. console.log(err);
  385. return res.render("error");
  386. });
  387. let invAdj = new InventoryAdjustment({
  388. date: Date.now(),
  389. merchant: req.session.user,
  390. ingredient: req.body.ingredientId,
  391. quantity: req.body.quantityChange
  392. });
  393. invAdj.save()
  394. .then((newAdjustment)=>{
  395. return;
  396. })
  397. .catch((err)=>{
  398. console.log(err);
  399. return res.render("error");
  400. })
  401. },
  402. addRecipeIngredient: function(req, res){
  403. if(!req.session.user){
  404. return res.render("error");
  405. }
  406. Recipe.findOne({_id: req.body.recipeId})
  407. .then((recipe)=>{
  408. recipe.ingredients.push({
  409. ingredient: req.body.item.ingredient,
  410. quantity: req.body.item.quantity
  411. });
  412. recipe.save()
  413. .then((recipe)=>{
  414. return res.json();
  415. })
  416. .catch((err)=>{
  417. console.log(err);
  418. return res.render("error");
  419. });
  420. })
  421. .catch((err)=>{
  422. console.log(err);
  423. return res.render("error");
  424. });
  425. },
  426. updateRecipeIngredient: 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 ingredient of recipe.ingredients){
  433. if(ingredient._id.toString() === req.body.ingredient._id){
  434. ingredient.quantity = req.body.ingredient.quantity;
  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. }
  445. })
  446. .catch((err)=>{
  447. console.log(err);
  448. return res.render("error");
  449. });
  450. },
  451. removeRecipeIngredient: function(req, res){
  452. if(!req.session.user){
  453. return res.render("error");
  454. }
  455. Recipe.findOne({_id: req.body.recipeId})
  456. .then((recipe)=>{
  457. for(let i = 0; i < recipe.ingredients.length; i++){
  458. if(recipe.ingredients[i]._id.toString() === req.body.ingredientId){
  459. recipe.ingredients.splice(i, 1);
  460. }
  461. }
  462. recipe.save()
  463. .then((recipe)=>{
  464. return res.json();
  465. })
  466. .catch((err)=>{
  467. console.log(err);
  468. return res.render("error");
  469. });
  470. })
  471. .catch((err)=>{
  472. console.log(err);
  473. return res.render("error");
  474. });
  475. },
  476. getIngredients: function(req, res){
  477. Ingredient.find()
  478. .then((ingredients)=>{
  479. return res.json(ingredients);
  480. })
  481. .catch((err)=>{
  482. console.log(err);
  483. return res.render("error");
  484. });
  485. },
  486. createNewIngredients: function(req, res){
  487. Ingredient.create(req.body)
  488. .then((ingredients)=>{
  489. return res.json(ingredients);
  490. })
  491. .catch((err)=>{
  492. console.log(err);
  493. return res.render("error");
  494. });
  495. },
  496. createIngredient: function(req, res){
  497. Ingredient.create(req.body.ingredient)
  498. .then((ingredient)=>{
  499. Merchant.findOne({_id: req.session.user})
  500. .then((merchant)=>{
  501. let item = {
  502. ingredient: ingredient,
  503. quantity: req.body.quantity
  504. }
  505. merchant.inventory.push(item);
  506. merchant.save()
  507. .then((merchant)=>{
  508. console.log("something");
  509. return res.json(item);
  510. })
  511. .catch((err)=>{
  512. console.log(err);
  513. return res.render("error");
  514. });
  515. })
  516. .catch((err)=>{
  517. console.log(err);
  518. return res.render("error");
  519. });
  520. })
  521. .catch((err)=>{
  522. console.log(err);
  523. return res.render("error");
  524. });
  525. },
  526. createTransaction: function(req, res){
  527. let transaction = new nonPosTransaction({
  528. date: Date.now(),
  529. author: "None",
  530. merchant: req.session.user,
  531. recipes: req.body
  532. });
  533. //Calculate all ingredients used, store to list
  534. Merchant.findOne({_id: req.session.user})
  535. .populate("recipes")
  536. .then((merchant)=>{
  537. for(let reqRecipe of req.body){
  538. let merchRecipe = merchant.recipes.find(r => r._id.toString() === reqRecipe.id);
  539. for(let recipeIngredient of merchRecipe.ingredients){
  540. let merchInvIngredient = merchant.inventory.find(i => i.ingredient.toString() === recipeIngredient.ingredient.toString());
  541. merchInvIngredient.quantity -= recipeIngredient.quantity * reqRecipe.quantity;
  542. }
  543. }
  544. merchant.save()
  545. .then((merchant)=>{
  546. res.json(merchant.inventory);
  547. })
  548. .catch((err)=>{
  549. console.log(err);
  550. return res.render("error");
  551. });
  552. })
  553. .catch((err)=>{
  554. console.log(err);
  555. return res.render("error");
  556. });
  557. transaction.save()
  558. .then((transaction)=>{
  559. return;
  560. })
  561. .catch((err)=>{
  562. console.log(err);
  563. return res.render("error");
  564. });
  565. },
  566. getCloverRecipes: function(req, res){
  567. if(!req.session.user){
  568. return res.render("error");
  569. }
  570. Merchant.findOne({_id: req.session.user})
  571. .then((merchant)=>{
  572. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${token}`)
  573. .then((recipes)=>{
  574. return res.json(recipes);
  575. })
  576. .catch((err)=>{
  577. return res.json(err);
  578. });
  579. })
  580. .catch((err)=>{
  581. console.log(err);
  582. return res.render("error");
  583. });
  584. },
  585. unregistered: function(req, res){
  586. return res.redirect("/");
  587. },
  588. login: function(req, res){
  589. Merchant.findOne({email: req.body.email})
  590. .then((merchant)=>{
  591. if(merchant){
  592. bcrypt.compare(req.body.password, merchant.password, (err, result)=>{
  593. if(result){
  594. req.session.user = merchant._id;
  595. return res.redirect("/inventory");
  596. }
  597. });
  598. }else{
  599. req.session.error = {
  600. type: "login",
  601. message: "Invalid email or password"
  602. }
  603. return res.redirect("/");
  604. }
  605. })
  606. .catch((err)=>{
  607. console.log(err);
  608. return res.redirect("/");
  609. });
  610. },
  611. logout: function(req, res){
  612. req.session.user = undefined;
  613. return res.redirect("/");
  614. }
  615. }