home.js 19 KB

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