home.js 17 KB

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