home.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. merchantSetup: function(req, res){
  61. Ingredient.find()
  62. .then((ingredients)=>{
  63. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  64. .then((recipes)=>{
  65. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
  66. })
  67. .catch((err)=>{
  68. console.log(err);
  69. return res.render("error");
  70. });
  71. })
  72. .catch((err)=>{
  73. console.log(err);
  74. return res.render("error");
  75. });
  76. },
  77. displayRecipes: function(req, res){
  78. if(!req.session.user){
  79. return res.render("error");
  80. }
  81. Merchant.findOne({_id: req.session.user})
  82. .populate({
  83. path: "recipes",
  84. model: "Recipe",
  85. populate: {
  86. path: "ingredients.ingredient",
  87. model: "Ingredient"
  88. }
  89. })
  90. .populate("inventory.ingredient")
  91. .then((merchant)=>{
  92. return res.render("recipesPage/recipes", {merchant: merchant});
  93. })
  94. .catch((err)=>{
  95. console.log(err);
  96. return res.render("error");
  97. });
  98. },
  99. updateRecipes: function(req, res){
  100. if(!req.session.user){
  101. return res.render("error");
  102. }
  103. Merchant.findOne({_id: req.session.user})
  104. .populate("recipes")
  105. .then((merchant)=>{
  106. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  107. .then((result)=>{
  108. let deletedRecipes = merchant.recipes.slice();
  109. for(let i = 0; i < result.data.elements.length; i++){
  110. for(let j = 0; j < deletedRecipes.length; j++){
  111. if(result.data.elements[i].id === deletedRecipes[j].posId){
  112. result.data.elements.splice(i, 1);
  113. deletedRecipes.splice(j, 1);
  114. i--;
  115. break;
  116. }
  117. }
  118. }
  119. for(let recipe of deletedRecipes){
  120. for(let i = 0; i < merchant.recipes.length; i++){
  121. if(recipe._id === merchant.recipes[i]._id){
  122. merchant.recipes.splice(i, 1);
  123. break;
  124. }
  125. }
  126. }
  127. let newRecipes = []
  128. for(let recipe of result.data.elements){
  129. let newRecipe = new Recipe({
  130. posId: recipe.id,
  131. merchant: merchant._id,
  132. name: recipe.name,
  133. ingredients: []
  134. });
  135. merchant.recipes.push(newRecipe);
  136. newRecipes.push(newRecipe);
  137. }
  138. Recipe.create(newRecipes)
  139. .catch((err)=>{
  140. console.log(err);
  141. return res.render("error");
  142. });
  143. merchant.save()
  144. .then((newMerchant)=>{
  145. newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
  146. .then((newestMerchant)=>{
  147. return res.json({merchant: newestMerchant, count: result.data.elements.length});
  148. })
  149. .catch((err)=>{
  150. console.log(err);
  151. return res.render("error");
  152. });
  153. })
  154. .catch((err)=>{
  155. console.log(err);
  156. return res.render("error");
  157. });
  158. })
  159. .catch((err)=>{
  160. console.log(err);
  161. return res.render("error");
  162. });
  163. })
  164. .catch((err)=>{
  165. console.log(err);
  166. return res.render("error");
  167. });
  168. },
  169. createMerchant: function(req, res){
  170. let data = JSON.parse(req.body.data);
  171. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
  172. .then((merchant)=>{
  173. let newMerchant = new Merchant({
  174. name: merchant.data.name,
  175. posId: merchant.data.id,
  176. lastUpdatedTime: Date.now(),
  177. inventory: [],
  178. recipes: []
  179. });
  180. for(let ingredient of data.ingredients){
  181. let newIngredient = {
  182. ingredient: ingredient.id,
  183. quantity: parseInt(ingredient.quantity)
  184. }
  185. newMerchant.inventory.push(newIngredient);
  186. }
  187. let newRecipes = []
  188. for(let recipe of data.recipes){
  189. let newRecipe = {
  190. posId: recipe.posId,
  191. merchant: newMerchant._id,
  192. name: recipe.name,
  193. ingredients: []
  194. };
  195. for(let ingredient of recipe.ingredients){
  196. newRecipe.ingredients.push(ingredient);
  197. }
  198. newRecipes.push(newRecipe);
  199. }
  200. Recipe.create(newRecipes)
  201. .then((recipes)=>{
  202. for(let recipe of recipes){
  203. newMerchant.recipes.push(recipe);
  204. }
  205. newMerchant.save()
  206. .then((merchant)=>{
  207. return res.redirect("/inventory");
  208. })
  209. .catch((err)=>{
  210. console.log(err);
  211. return res.render("error");
  212. });
  213. })
  214. .catch((err)=>{
  215. console.log(err);
  216. return res.render("error");
  217. });
  218. })
  219. .catch((err)=>{
  220. console.log(err);
  221. });
  222. },
  223. addMerchantIngredient: function(req, res){
  224. if(!req.session.user){
  225. return res.render("error");
  226. }
  227. Merchant.findOne({_id: req.session.user})
  228. .then((merchant)=>{
  229. merchant.inventory.push(req.body);
  230. merchant.save()
  231. .then((newMerchant)=>{
  232. return res.json(newMerchant);
  233. })
  234. .catch((err)=>{
  235. console.log(err);
  236. return res.render("error");
  237. });
  238. })
  239. .catch((err)=>{
  240. console.log(err);
  241. return res.render("error");
  242. });
  243. },
  244. removeMerchantIngredient: function(req, res){
  245. if(!req.session.user){
  246. return res.render("error");
  247. }
  248. Merchant.findOne({_id: req.session.user})
  249. .then((merchant)=>{
  250. for(let i = 0; i < merchant.inventory.length; i++){
  251. if(req.body.ingredientId === merchant.inventory[i]._id.toString()){
  252. merchant.inventory.splice(i, 1);
  253. break;
  254. }
  255. }
  256. merchant.save()
  257. .then(()=>{
  258. return res.json();
  259. })
  260. .catch((err)=>{
  261. console.log(err);
  262. return res.render("error");
  263. });
  264. })
  265. .catch((err)=>{
  266. console.log(err);
  267. return res.render("error");
  268. });
  269. },
  270. updateMerchantIngredient: function(req, res){
  271. if(!req.session.user){
  272. return res.render("error");
  273. }
  274. Merchant.findOne({_id: req.session.user})
  275. .then((merchant)=>{
  276. let updateIngredient = merchant.inventory.find(i => i._id.toString() === req.body.ingredientId);
  277. updateIngredient.quantity = req.body.quantity;
  278. merchant.save()
  279. .then((merchant)=>{
  280. return res.json();
  281. })
  282. .catch((err)=>{
  283. console.log(err);
  284. return res.render("error");
  285. })
  286. })
  287. .catch((err)=>{
  288. console.log(err);
  289. return res.render("error");
  290. });
  291. },
  292. addRecipeIngredient: function(req, res){
  293. if(!req.session.user){
  294. return res.render("error");
  295. }
  296. Recipe.findOne({_id: req.body.recipeId})
  297. .then((recipe)=>{
  298. recipe.ingredients.push({
  299. ingredient: req.body.item.ingredient,
  300. quantity: req.body.item.quantity
  301. });
  302. recipe.save()
  303. .then((recipe)=>{
  304. return res.json();
  305. })
  306. .catch((err)=>{
  307. console.log(err);
  308. return res.render("error");
  309. });
  310. })
  311. .catch((err)=>{
  312. console.log(err);
  313. return res.render("error");
  314. });
  315. },
  316. updateRecipeIngredient: function(req, res){
  317. if(!req.session.user){
  318. return res.render("error");
  319. }
  320. Recipe.findOne({_id: req.body.recipeId})
  321. .then((recipe)=>{
  322. for(let ingredient of recipe.ingredients){
  323. if(ingredient._id.toString() === req.body.ingredient._id){
  324. ingredient.quantity = req.body.ingredient.quantity;
  325. recipe.save()
  326. .then((recipe)=>{
  327. return res.json();
  328. })
  329. .catch((err)=>{
  330. console.log(err);
  331. return res.render("error");
  332. })
  333. }
  334. }
  335. })
  336. .catch((err)=>{
  337. console.log(err);
  338. return res.render("error");
  339. });
  340. },
  341. removeRecipeIngredient: function(req, res){
  342. if(!req.session.user){
  343. return res.render("error");
  344. }
  345. Recipe.findOne({_id: req.body.recipeId})
  346. .then((recipe)=>{
  347. for(let i = 0; i < recipe.ingredients.length; i++){
  348. if(recipe.ingredients[i]._id.toString() === req.body.ingredientId){
  349. recipe.ingredients.splice(i, 1);
  350. }
  351. }
  352. recipe.save()
  353. .then((recipe)=>{
  354. return res.json();
  355. })
  356. .catch((err)=>{
  357. console.log(err);
  358. return res.render("error");
  359. });
  360. })
  361. .catch((err)=>{
  362. console.log(err);
  363. return res.render("error");
  364. });
  365. },
  366. getIngredients: function(req, res){
  367. Ingredient.find()
  368. .then((ingredients)=>{
  369. return res.json(ingredients);
  370. })
  371. .catch((err)=>{
  372. console.log(err);
  373. return res.render("error");
  374. });
  375. },
  376. createNewIngredients: function(req, res){
  377. Ingredient.create(req.body)
  378. .then((ingredients)=>{
  379. return res.json(ingredients);
  380. })
  381. .catch((err)=>{
  382. console.log(err);
  383. return res.render("error");
  384. });
  385. },
  386. createIngredient: function(req, res){
  387. Ingredient.create(req.body.ingredient)
  388. .then((ingredient)=>{
  389. Merchant.findOne({_id: req.session.user})
  390. .then((merchant)=>{
  391. let item = {
  392. ingredient: ingredient,
  393. quantity: req.body.quantity
  394. }
  395. merchant.inventory.push(item);
  396. merchant.save()
  397. .then((merchant)=>{
  398. console.log("something");
  399. return res.json(merchant);
  400. })
  401. .catch((err)=>{
  402. console.log(err);
  403. return res.render("error");
  404. });
  405. })
  406. .catch((err)=>{
  407. console.log(err);
  408. return res.render("error");
  409. });
  410. })
  411. .catch((err)=>{
  412. console.log(err);
  413. return res.render("error");
  414. });
  415. },
  416. getCloverRecipes: function(req, res){
  417. if(!req.session.user){
  418. return res.render("error");
  419. }
  420. Merchant.findOne({_id: req.session.user})
  421. .then((merchant)=>{
  422. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${token}`)
  423. .then((recipes)=>{
  424. return res.json(recipes);
  425. })
  426. .catch((err)=>{
  427. return res.json(err);
  428. });
  429. })
  430. .catch((err)=>{
  431. console.log(err);
  432. return res.render("error");
  433. });
  434. },
  435. unregistered: function(req, res){
  436. return res.redirect("/");
  437. }
  438. }