home.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. const axios = require("axios");
  2. const Merchant = require("../models/merchant");
  3. const Ingredient = require("../models/ingredient");
  4. // const merchantId = "HCVKASXH94531";
  5. // const token = "f1c88a69-e3e4-059a-da06-8858d0636e82";
  6. const merchantId = "YHVPCQMVB1P81";
  7. const token = "b48068eb-411a-918e-ea64-52007147e42c";
  8. module.exports = {
  9. displayInventory: function(req, res){
  10. Merchant.findOne({posId: merchantId})
  11. .populate("inventory.ingredient")
  12. .then((merchant)=>{
  13. if(merchant){
  14. req.session.user = merchant._id;
  15. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${token}`)
  16. .then((result)=>{
  17. for(let order of result.data.elements){
  18. for(let item of order.lineItems.elements){
  19. let recipe = merchant.recipes.find(r => r.posId === item.item.id);
  20. if(recipe){
  21. for(let ingredient of recipe.ingredients){
  22. let inventoryIngredient = {};
  23. for(let invItem of merchant.inventory){
  24. if(invItem.ingredient._id.toString() === ingredient.ingredient.toString()){
  25. inventoryIngredient = invItem;
  26. }
  27. }
  28. inventoryIngredient.quantity -= ingredient.quantity;
  29. }
  30. }
  31. }
  32. }
  33. merchant.lastUpdatedTime = Date.now();
  34. merchant.save()
  35. .then((updatedMerchant)=>{
  36. return res.render("inventory/inventory", {merchant: updatedMerchant});
  37. })
  38. .catch((err)=>{
  39. console.log(err);
  40. return res.render("error");
  41. });
  42. })
  43. .catch((err)=>{
  44. console.log(err);
  45. });
  46. }else{
  47. return res.redirect("/merchant/new");
  48. }
  49. })
  50. .catch((err)=>{
  51. console.log(err);
  52. return res.render("error");
  53. });
  54. },
  55. merchantSetup: function(req, res){
  56. Ingredient.find()
  57. .then((ingredients)=>{
  58. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  59. .then((recipes)=>{
  60. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
  61. })
  62. .catch((err)=>{
  63. console.log(err);
  64. return res.render("error");
  65. });
  66. })
  67. .catch((err)=>{
  68. console.log(err);
  69. return res.render("error");
  70. })
  71. },
  72. displayRecipes: function(req, res){
  73. Merchant.findOne({_id: req.session.user})
  74. .populate("recipes.ingredients.ingredient")
  75. .populate("inventory.ingredient")
  76. .then((merchant)=>{
  77. return res.render("recipesPage/recipes", {merchant: merchant});
  78. })
  79. .catch((err)=>{
  80. console.log(err);
  81. return res.render("error");
  82. });
  83. },
  84. updateRecipes: function(req, res){
  85. Merchant.findOne({_id: req.session.user})
  86. .then((merchant)=>{
  87. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  88. .then((result)=>{
  89. let deletedRecipes = merchant.recipes.slice();
  90. for(let i = 0; i < result.data.elements.length; i++){
  91. for(let j = 0; j < deletedRecipes.length; j++){
  92. if(result.data.elements[i].id === deletedRecipes[j].posId){
  93. result.data.elements.splice(i, 1);
  94. deletedRecipes.splice(j, 1);
  95. i--;
  96. break;
  97. }
  98. }
  99. }
  100. for(let recipe of deletedRecipes){
  101. for(let i = 0; i < merchant.recipes.length; i++){
  102. if(recipe._id === merchant.recipes[i]._id){
  103. merchant.recipes.splice(i, 1);
  104. break;
  105. }
  106. }
  107. }
  108. for(let recipe of result.data.elements){
  109. merchant.recipes.push({
  110. posId: recipe.id,
  111. name: recipe.name,
  112. ingredients: []
  113. });
  114. }
  115. merchant.save()
  116. .then((newMerchant)=>{
  117. newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
  118. .then((newestMerchant)=>{
  119. return res.json({merchant: newestMerchant, count: result.data.elements.length});
  120. })
  121. .catch((err)=>{
  122. console.log(err);
  123. return res.render("error");
  124. });
  125. })
  126. .catch((err)=>{
  127. console.log(err);
  128. return res.render("error");
  129. });
  130. })
  131. .catch((err)=>{
  132. console.log(err);
  133. return res.render("error");
  134. });
  135. })
  136. .catch((err)=>{
  137. console.log(err);
  138. return res.render("error");
  139. });
  140. },
  141. createMerchant: function(req, res){
  142. let data = JSON.parse(req.body.data);
  143. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
  144. .then((merchant)=>{
  145. let newMerchant = new Merchant({
  146. name: merchant.data.name,
  147. posId: merchant.data.id,
  148. lastUpdatedTime: Date.now(),
  149. inventory: [],
  150. recipes: []
  151. });
  152. for(let ingredient of data.ingredients){
  153. let newIngredient = {
  154. ingredient: ingredient.id,
  155. quantity: parseInt(ingredient.quantity)
  156. }
  157. newMerchant.inventory.push(newIngredient);
  158. }
  159. for(let recipe of data.recipes){
  160. newMerchant.recipes.push(recipe);
  161. }
  162. newMerchant.save()
  163. .then((newMerchant)=>{
  164. return res.redirect("/");
  165. })
  166. .catch((err)=>{
  167. console.log(err);
  168. return res.render("error");
  169. })
  170. })
  171. .catch((err)=>{
  172. console.log(err);
  173. });
  174. },
  175. addMerchantIngredient: function(req, res){
  176. Merchant.findOne({_id: req.session.user})
  177. .then((merchant)=>{
  178. merchant.inventory.push(req.body);
  179. merchant.save()
  180. .then((newMerchant)=>{
  181. return res.json(newMerchant);
  182. })
  183. .catch((err)=>{
  184. console.log(err);
  185. return res.render("error");
  186. });
  187. })
  188. .catch((err)=>{
  189. console.log(err);
  190. return res.render("error");
  191. });
  192. },
  193. removeMerchantIngredient: function(req, res){
  194. Merchant.findOne({_id: req.session.user})
  195. .then((merchant)=>{
  196. for(let i = 0; i < merchant.inventory.length; i++){
  197. if(req.body.ingredientId === merchant.inventory[i]._id.toString()){
  198. merchant.inventory.splice(i, 1);
  199. break;
  200. }
  201. }
  202. merchant.save()
  203. .then(()=>{
  204. return res.json();
  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. updateMerchantIngredient: function(req, res){
  217. Merchant.findOne({_id: req.session.user})
  218. .then((merchant)=>{
  219. let updateIngredient = merchant.inventory.find(i => i._id.toString() === req.body.ingredientId);
  220. updateIngredient.quantity = req.body.quantity;
  221. merchant.save()
  222. .then((merchant)=>{
  223. return res.json();
  224. })
  225. .catch((err)=>{
  226. console.log(err);
  227. return res.render("error");
  228. })
  229. })
  230. .catch((err)=>{
  231. console.log(err);
  232. return res.render("error");
  233. })
  234. },
  235. addRecipeIngredient: function(req, res){
  236. Merchant.findOne({_id: req.session.user})
  237. .then((merchant)=>{
  238. let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
  239. recipe.ingredients.push(req.body.item)
  240. merchant.save()
  241. .then((newMerchant)=>{
  242. return res.json(newMerchant);
  243. })
  244. .catch((err)=>{
  245. console.log(err);
  246. return res.render("error");
  247. });
  248. })
  249. .catch((err)=>{
  250. console.log(err);
  251. return res.render("error");
  252. });
  253. },
  254. updateRecipeIngredient: function(req, res){
  255. Merchant.findOne({_id: req.session.user})
  256. .then((merchant)=>{
  257. let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
  258. for(let i = 0; i < recipe.ingredients.length; i++){
  259. if(recipe.ingredients[i]._id.toString() === req.body.ingredient._id){
  260. recipe.ingredients[i].quantity = req.body.ingredient.quantity;
  261. break;
  262. }
  263. }
  264. merchant.save()
  265. .then(()=>{
  266. return res.json();
  267. })
  268. .catch((err)=>{
  269. console.log(err);
  270. return res.render("error");
  271. });
  272. })
  273. .catch((err)=>{
  274. console.log(err);
  275. return res.render("error");
  276. });
  277. },
  278. removeRecipeIngredient: function(req, res){
  279. Merchant.findOne({_id: req.session.user})
  280. .then((merchant)=>{
  281. let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
  282. for(let i = 0; i < recipe.ingredients.length; i++){
  283. if(req.body.ingredientId === recipe.ingredients[i]._id.toString()){
  284. recipe.ingredients.splice(i, 1);
  285. break;
  286. }
  287. }
  288. merchant.save()
  289. .then((result)=>{
  290. return res.json();
  291. })
  292. .catch((err)=>{
  293. console.log(err);
  294. return res.render("error");
  295. });
  296. })
  297. .catch((err)=>{
  298. console.log(err);
  299. return res.render("error");
  300. });
  301. },
  302. getIngredients: function(req, res){
  303. Ingredient.find()
  304. .then((ingredients)=>{
  305. return res.json(ingredients);
  306. })
  307. .catch((err)=>{
  308. console.log(err);
  309. return res.render("error");
  310. });
  311. },
  312. createNewIngredients: function(req, res){
  313. Ingredient.create(req.body)
  314. .then((ingredients)=>{
  315. return res.json(ingredients);
  316. })
  317. .catch((err)=>{
  318. console.log(err);
  319. return res.render("error");
  320. });
  321. },
  322. createIngredient: function(req, res){
  323. Ingredient.create(req.body.ingredient)
  324. .then((ingredient)=>{
  325. Merchant.findOne({_id: req.session.user})
  326. .then((merchant)=>{
  327. let item = {
  328. ingredient: ingredient,
  329. quantity: req.body.quantity
  330. }
  331. merchant.inventory.push(item);
  332. merchant.save()
  333. .then((merchant)=>{
  334. console.log("something");
  335. return res.json(merchant);
  336. })
  337. .catch((err)=>{
  338. console.log(err);
  339. return res.render("error");
  340. });
  341. })
  342. .catch((err)=>{
  343. console.log(err);
  344. return res.render("error");
  345. });
  346. })
  347. .catch((err)=>{
  348. console.log(err);
  349. return res.render("error");
  350. });
  351. },
  352. getCloverRecipes: function(req, res){
  353. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  354. .then((recipes)=>{
  355. return res.json(recipes);
  356. })
  357. .catch((err)=>{
  358. return res.json(err);
  359. });
  360. }
  361. }