home.js 18 KB

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