home.js 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  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. createMerchantClover: 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. createMerchantNone: function(req, res){
  237. let data = JSON.parse(req.body.data);
  238. console.log(data);
  239. },
  240. addMerchantIngredient: function(req, res){
  241. if(!req.session.user){
  242. return res.render("error");
  243. }
  244. Merchant.findOne({_id: req.session.user})
  245. .then((merchant)=>{
  246. merchant.inventory.push(req.body);
  247. merchant.save()
  248. .then((newMerchant)=>{
  249. return res.json(newMerchant);
  250. })
  251. .catch((err)=>{
  252. console.log(err);
  253. return res.render("error");
  254. });
  255. })
  256. .catch((err)=>{
  257. console.log(err);
  258. return res.render("error");
  259. });
  260. },
  261. removeMerchantIngredient: function(req, res){
  262. if(!req.session.user){
  263. return res.render("error");
  264. }
  265. Merchant.findOne({_id: req.session.user})
  266. .then((merchant)=>{
  267. for(let i = 0; i < merchant.inventory.length; i++){
  268. if(req.body.ingredientId === merchant.inventory[i]._id.toString()){
  269. merchant.inventory.splice(i, 1);
  270. break;
  271. }
  272. }
  273. merchant.save()
  274. .then(()=>{
  275. return res.json();
  276. })
  277. .catch((err)=>{
  278. console.log(err);
  279. return res.render("error");
  280. });
  281. })
  282. .catch((err)=>{
  283. console.log(err);
  284. return res.render("error");
  285. });
  286. },
  287. updateMerchantIngredient: function(req, res){
  288. if(!req.session.user){
  289. return res.render("error");
  290. }
  291. Merchant.findOne({_id: req.session.user})
  292. .then((merchant)=>{
  293. let updateIngredient = merchant.inventory.find(i => i._id.toString() === req.body.ingredientId);
  294. updateIngredient.quantity = req.body.quantity;
  295. merchant.save()
  296. .then((merchant)=>{
  297. return res.json();
  298. })
  299. .catch((err)=>{
  300. console.log(err);
  301. return res.render("error");
  302. })
  303. })
  304. .catch((err)=>{
  305. console.log(err);
  306. return res.render("error");
  307. });
  308. },
  309. addRecipeIngredient: function(req, res){
  310. if(!req.session.user){
  311. return res.render("error");
  312. }
  313. Recipe.findOne({_id: req.body.recipeId})
  314. .then((recipe)=>{
  315. recipe.ingredients.push({
  316. ingredient: req.body.item.ingredient,
  317. quantity: req.body.item.quantity
  318. });
  319. recipe.save()
  320. .then((recipe)=>{
  321. return res.json();
  322. })
  323. .catch((err)=>{
  324. console.log(err);
  325. return res.render("error");
  326. });
  327. })
  328. .catch((err)=>{
  329. console.log(err);
  330. return res.render("error");
  331. });
  332. },
  333. updateRecipeIngredient: function(req, res){
  334. if(!req.session.user){
  335. return res.render("error");
  336. }
  337. Recipe.findOne({_id: req.body.recipeId})
  338. .then((recipe)=>{
  339. for(let ingredient of recipe.ingredients){
  340. if(ingredient._id.toString() === req.body.ingredient._id){
  341. ingredient.quantity = req.body.ingredient.quantity;
  342. recipe.save()
  343. .then((recipe)=>{
  344. return res.json();
  345. })
  346. .catch((err)=>{
  347. console.log(err);
  348. return res.render("error");
  349. })
  350. }
  351. }
  352. })
  353. .catch((err)=>{
  354. console.log(err);
  355. return res.render("error");
  356. });
  357. },
  358. removeRecipeIngredient: function(req, res){
  359. if(!req.session.user){
  360. return res.render("error");
  361. }
  362. Recipe.findOne({_id: req.body.recipeId})
  363. .then((recipe)=>{
  364. for(let i = 0; i < recipe.ingredients.length; i++){
  365. if(recipe.ingredients[i]._id.toString() === req.body.ingredientId){
  366. recipe.ingredients.splice(i, 1);
  367. }
  368. }
  369. recipe.save()
  370. .then((recipe)=>{
  371. return res.json();
  372. })
  373. .catch((err)=>{
  374. console.log(err);
  375. return res.render("error");
  376. });
  377. })
  378. .catch((err)=>{
  379. console.log(err);
  380. return res.render("error");
  381. });
  382. },
  383. getIngredients: function(req, res){
  384. Ingredient.find()
  385. .then((ingredients)=>{
  386. return res.json(ingredients);
  387. })
  388. .catch((err)=>{
  389. console.log(err);
  390. return res.render("error");
  391. });
  392. },
  393. createNewIngredients: function(req, res){
  394. Ingredient.create(req.body)
  395. .then((ingredients)=>{
  396. return res.json(ingredients);
  397. })
  398. .catch((err)=>{
  399. console.log(err);
  400. return res.render("error");
  401. });
  402. },
  403. createIngredient: function(req, res){
  404. Ingredient.create(req.body.ingredient)
  405. .then((ingredient)=>{
  406. Merchant.findOne({_id: req.session.user})
  407. .then((merchant)=>{
  408. let item = {
  409. ingredient: ingredient,
  410. quantity: req.body.quantity
  411. }
  412. merchant.inventory.push(item);
  413. merchant.save()
  414. .then((merchant)=>{
  415. console.log("something");
  416. return res.json(merchant);
  417. })
  418. .catch((err)=>{
  419. console.log(err);
  420. return res.render("error");
  421. });
  422. })
  423. .catch((err)=>{
  424. console.log(err);
  425. return res.render("error");
  426. });
  427. })
  428. .catch((err)=>{
  429. console.log(err);
  430. return res.render("error");
  431. });
  432. },
  433. getCloverRecipes: function(req, res){
  434. if(!req.session.user){
  435. return res.render("error");
  436. }
  437. Merchant.findOne({_id: req.session.user})
  438. .then((merchant)=>{
  439. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${token}`)
  440. .then((recipes)=>{
  441. return res.json(recipes);
  442. })
  443. .catch((err)=>{
  444. return res.json(err);
  445. });
  446. })
  447. .catch((err)=>{
  448. console.log(err);
  449. return res.render("error");
  450. });
  451. },
  452. unregistered: function(req, res){
  453. return res.redirect("/");
  454. }
  455. }