home.js 19 KB

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