home.js 19 KB

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