home.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  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 homeHelper = require("./homeHelper");
  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. displayInventory: function(req, res){
  12. Merchant.findOne({posId: merchantId})
  13. .populate("inventory.ingredient")
  14. .populate("recipes")
  15. .then((merchant)=>{
  16. if(merchant){
  17. req.session.user = merchant._id;
  18. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${token}`)
  19. .then((result)=>{
  20. for(let order of result.data.elements){
  21. for(let item of order.lineItems.elements){
  22. let recipe = merchant.recipes.find(r => r.posId === item.item.id);
  23. if(recipe){
  24. for(let ingredient of recipe.ingredients){
  25. let inventoryIngredient = {};
  26. for(let invItem of merchant.inventory){
  27. if(invItem.ingredient._id.toString() === ingredient.ingredient.toString()){
  28. inventoryIngredient = invItem;
  29. }
  30. }
  31. inventoryIngredient.quantity -= ingredient.quantity;
  32. }
  33. }
  34. }
  35. }
  36. merchant.lastUpdatedTime = Date.now();
  37. merchant.save()
  38. .then((updatedMerchant)=>{
  39. return res.render("inventory/inventory", {merchant: updatedMerchant});
  40. })
  41. .catch((err)=>{
  42. console.log(err);
  43. return res.render("error");
  44. });
  45. })
  46. .catch((err)=>{
  47. console.log(err);
  48. });
  49. }else{
  50. return res.redirect("/merchant/new");
  51. }
  52. })
  53. .catch((err)=>{
  54. console.log(err);
  55. return res.render("error");
  56. });
  57. },
  58. merchantSetup: function(req, res){
  59. Ingredient.find()
  60. .then((ingredients)=>{
  61. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  62. .then((recipes)=>{
  63. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
  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. displayRecipes: function(req, res){
  76. if(!req.session.user){
  77. return res.render("error");
  78. }
  79. Merchant.findOne({_id: req.session.user})
  80. .populate({
  81. path: "recipes",
  82. model: "Recipe",
  83. populate: {
  84. path: "ingredients.ingredient",
  85. model: "Ingredient"
  86. }
  87. })
  88. .populate("inventory.ingredient")
  89. .then((merchant)=>{
  90. return res.render("recipesPage/recipes", {merchant: merchant});
  91. })
  92. .catch((err)=>{
  93. console.log(err);
  94. return res.render("error");
  95. });
  96. },
  97. updateRecipes: function(req, res){
  98. if(!req.session.user){
  99. return res.render("error");
  100. }
  101. Merchant.findOne({_id: req.session.user})
  102. .then((merchant)=>{
  103. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  104. .then((result)=>{
  105. let deletedRecipes = merchant.recipes.slice();
  106. for(let i = 0; i < result.data.elements.length; i++){
  107. for(let j = 0; j < deletedRecipes.length; j++){
  108. if(result.data.elements[i].id === deletedRecipes[j].posId){
  109. result.data.elements.splice(i, 1);
  110. deletedRecipes.splice(j, 1);
  111. i--;
  112. break;
  113. }
  114. }
  115. }
  116. for(let recipe of deletedRecipes){
  117. for(let i = 0; i < merchant.recipes.length; i++){
  118. if(recipe._id === merchant.recipes[i]._id){
  119. merchant.recipes.splice(i, 1);
  120. break;
  121. }
  122. }
  123. }
  124. for(let recipe of result.data.elements){
  125. merchant.recipes.push({
  126. posId: recipe.id,
  127. name: recipe.name,
  128. ingredients: []
  129. });
  130. }
  131. merchant.save()
  132. .then((newMerchant)=>{
  133. newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
  134. .then((newestMerchant)=>{
  135. return res.json({merchant: newestMerchant, count: result.data.elements.length});
  136. })
  137. .catch((err)=>{
  138. console.log(err);
  139. return res.render("error");
  140. });
  141. })
  142. .catch((err)=>{
  143. console.log(err);
  144. return res.render("error");
  145. });
  146. })
  147. .catch((err)=>{
  148. console.log(err);
  149. return res.render("error");
  150. });
  151. })
  152. .catch((err)=>{
  153. console.log(err);
  154. return res.render("error");
  155. });
  156. },
  157. createMerchant: function(req, res){
  158. let data = JSON.parse(req.body.data);
  159. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
  160. .then((merchant)=>{
  161. let newMerchant = new Merchant({
  162. name: merchant.data.name,
  163. posId: merchant.data.id,
  164. lastUpdatedTime: Date.now(),
  165. inventory: [],
  166. recipes: []
  167. });
  168. for(let ingredient of data.ingredients){
  169. let newIngredient = {
  170. ingredient: ingredient.id,
  171. quantity: parseInt(ingredient.quantity)
  172. }
  173. newMerchant.inventory.push(newIngredient);
  174. }
  175. let newRecipes = []
  176. for(let recipe of data.recipes){
  177. let newRecipe = {
  178. posId: recipe.posId,
  179. name: recipe.name,
  180. ingredients: []
  181. };
  182. for(let ingredient of recipe.ingredients){
  183. newRecipe.ingredients.push(ingredient);
  184. }
  185. newRecipes.push(newRecipe);
  186. }
  187. Recipe.create(newRecipes)
  188. .then((recipes)=>{
  189. for(let recipe of recipes){
  190. newMerchant.recipes.push(recipe);
  191. }
  192. newMerchant.save()
  193. .then((merchant)=>{
  194. return res.redirect("/");
  195. })
  196. .catch((err)=>{
  197. console.log(err);
  198. return res.render("error");
  199. });
  200. })
  201. .catch((err)=>{
  202. console.log(err);
  203. return res.render("error");
  204. });
  205. })
  206. .catch((err)=>{
  207. console.log(err);
  208. });
  209. },
  210. addMerchantIngredient: function(req, res){
  211. if(!req.session.user){
  212. return res.render("error");
  213. }
  214. Merchant.findOne({_id: req.session.user})
  215. .then((merchant)=>{
  216. merchant.inventory.push(req.body);
  217. merchant.save()
  218. .then((newMerchant)=>{
  219. return res.json(newMerchant);
  220. })
  221. .catch((err)=>{
  222. console.log(err);
  223. return res.render("error");
  224. });
  225. })
  226. .catch((err)=>{
  227. console.log(err);
  228. return res.render("error");
  229. });
  230. },
  231. removeMerchantIngredient: function(req, res){
  232. if(!req.session.user){
  233. return res.render("error");
  234. }
  235. Merchant.findOne({_id: req.session.user})
  236. .then((merchant)=>{
  237. for(let i = 0; i < merchant.inventory.length; i++){
  238. if(req.body.ingredientId === merchant.inventory[i]._id.toString()){
  239. merchant.inventory.splice(i, 1);
  240. break;
  241. }
  242. }
  243. merchant.save()
  244. .then(()=>{
  245. return res.json();
  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. updateMerchantIngredient: 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. let updateIngredient = merchant.inventory.find(i => i._id.toString() === req.body.ingredientId);
  264. updateIngredient.quantity = req.body.quantity;
  265. merchant.save()
  266. .then((merchant)=>{
  267. return res.json();
  268. })
  269. .catch((err)=>{
  270. console.log(err);
  271. return res.render("error");
  272. })
  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. Merchant.findOne({_id: req.session.user})
  284. .then((merchant)=>{
  285. let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
  286. recipe.ingredients.push(req.body.item)
  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. updateRecipeIngredient: function(req, res){
  302. if(!req.session.user){
  303. return res.render("error");
  304. }
  305. Recipe.findOne({_id: req.body.recipeId})
  306. .then((recipe)=>{
  307. for(let ingredient of recipe.ingredients){
  308. if(ingredient._id.toString() === req.body.ingredient._id){
  309. ingredient.quantity = req.body.ingredient.quantity;
  310. recipe.save()
  311. .then((recipe)=>{
  312. return res.json();
  313. })
  314. .catch((err)=>{
  315. console.log(err);
  316. return res.render("error");
  317. })
  318. }
  319. }
  320. })
  321. .catch((err)=>{
  322. console.log(err);
  323. return res.render("error");
  324. });
  325. },
  326. removeRecipeIngredient: function(req, res){
  327. if(!req.session.user){
  328. return res.render("error");
  329. }
  330. Merchant.findOne({_id: req.session.user})
  331. .then((merchant)=>{
  332. let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
  333. for(let i = 0; i < recipe.ingredients.length; i++){
  334. if(req.body.ingredientId === recipe.ingredients[i]._id.toString()){
  335. recipe.ingredients.splice(i, 1);
  336. break;
  337. }
  338. }
  339. merchant.save()
  340. .then((result)=>{
  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(merchant);
  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. getCloverRecipes: function(req, res){
  404. if(!req.session.user){
  405. return res.render("error");
  406. }
  407. Merchant.findOne({_id: req.session.user})
  408. .then((merchant)=>{
  409. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${token}`)
  410. .then((recipes)=>{
  411. return res.json(recipes);
  412. })
  413. .catch((err)=>{
  414. return res.json(err);
  415. });
  416. })
  417. .catch((err)=>{
  418. console.log(err);
  419. return res.render("error");
  420. });
  421. },
  422. unregistered: function(req, res){
  423. return res.redirect("/");
  424. }
  425. }