home.js 17 KB

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