home.js 17 KB

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