home.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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. updateMerchant: function(req, res){
  73. Merchant.updateOne({_id: req.session.user}, req.body)
  74. .then((merchant)=>{
  75. return res.json(merchant);
  76. })
  77. .catch((err)=>{
  78. console.log(err);
  79. return res.render("error");
  80. });
  81. },
  82. getCloverRecipes: function(req, res){
  83. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  84. .then((recipes)=>{
  85. return res.json(recipes);
  86. })
  87. .catch((err)=>{
  88. return res.json(err);
  89. });
  90. },
  91. createMerchant: function(req, res){
  92. let data = JSON.parse(req.body.data);
  93. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
  94. .then((merchant)=>{
  95. let newMerchant = new Merchant({
  96. name: merchant.data.name,
  97. posId: merchant.data.id,
  98. lastUpdatedTime: Date.now(),
  99. inventory: [],
  100. recipes: []
  101. });
  102. for(let ingredient of data.ingredients){
  103. let newIngredient = {
  104. ingredient: ingredient.id,
  105. quantity: parseInt(ingredient.quantity)
  106. }
  107. newMerchant.inventory.push(newIngredient);
  108. }
  109. for(let recipe of data.recipes){
  110. newMerchant.recipes.push(recipe);
  111. }
  112. newMerchant.save()
  113. .then((newMerchant)=>{
  114. return res.redirect("/");
  115. })
  116. .catch((err)=>{
  117. console.log(err);
  118. return res.render("error");
  119. })
  120. })
  121. .catch((err)=>{
  122. console.log(err);
  123. });
  124. },
  125. createNewIngredients: function(req, res){
  126. Ingredient.create(req.body)
  127. .then((ingredients)=>{
  128. return res.json(ingredients);
  129. })
  130. .catch((err)=>{
  131. console.log(err);
  132. return res.render("error");
  133. });
  134. },
  135. createIngredient: function(req, res){
  136. Ingredient.create(req.body.ingredient)
  137. .then((ingredient)=>{
  138. Merchant.findOne({_id: req.session.user})
  139. .then((merchant)=>{
  140. let item = {
  141. ingredient: ingredient,
  142. quantity: req.body.quantity
  143. }
  144. merchant.inventory.push(item);
  145. merchant.save()
  146. .then((merchant)=>{
  147. console.log("something");
  148. return res.json(merchant);
  149. })
  150. .catch((err)=>{
  151. console.log(err);
  152. return res.render("error");
  153. });
  154. })
  155. .catch((err)=>{
  156. console.log(err);
  157. return res.render("error");
  158. });
  159. })
  160. .catch((err)=>{
  161. console.log(err);
  162. return res.render("error");
  163. });
  164. },
  165. displayRecipes: function(req, res){
  166. Merchant.findOne({_id: req.session.user})
  167. .populate("recipes.ingredients.ingredient")
  168. .populate("inventory.ingredient")
  169. .then((merchant)=>{
  170. return res.render("recipesPage/recipes", {merchant: merchant});
  171. })
  172. .catch((err)=>{
  173. console.log(err);
  174. return res.render("error");
  175. });
  176. },
  177. updateRecipes: function(req, res){
  178. Merchant.findOne({_id: req.session.user})
  179. .then((merchant)=>{
  180. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  181. .then((result)=>{
  182. let deletedRecipes = merchant.recipes.slice();
  183. for(let i = 0; i < result.data.elements.length; i++){
  184. for(let j = 0; j < deletedRecipes.length; j++){
  185. if(result.data.elements[i].id === deletedRecipes[j].posId){
  186. result.data.elements.splice(i, 1);
  187. deletedRecipes.splice(j, 1);
  188. i--;
  189. break;
  190. }
  191. }
  192. }
  193. for(let recipe of deletedRecipes){
  194. for(let i = 0; i < merchant.recipes.length; i++){
  195. if(recipe._id === merchant.recipes[i]._id){
  196. merchant.recipes.splice(i, 1);
  197. break;
  198. }
  199. }
  200. }
  201. for(let recipe of result.data.elements){
  202. merchant.recipes.push({
  203. posId: recipe.id,
  204. name: recipe.name,
  205. ingredients: []
  206. });
  207. }
  208. merchant.save()
  209. .then((newMerchant)=>{
  210. newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
  211. .then((newestMerchant)=>{
  212. return res.json({merchant: newestMerchant, count: result.data.elements.length});
  213. })
  214. .catch((err)=>{
  215. console.log(err);
  216. return res.render("error");
  217. });
  218. })
  219. .catch((err)=>{
  220. console.log(err);
  221. return res.render("error");
  222. });
  223. })
  224. .catch((err)=>{
  225. console.log(err);
  226. return res.render("error");
  227. });
  228. })
  229. .catch((err)=>{
  230. console.log(err);
  231. return res.render("error");
  232. });
  233. },
  234. getIngredients: function(req, res){
  235. Ingredient.find()
  236. .then((ingredients)=>{
  237. return res.json(ingredients);
  238. })
  239. .catch((err)=>{
  240. console.log(err);
  241. return res.render("error");
  242. });
  243. },
  244. addMerchantIngredient: function(req, res){
  245. Merchant.findOne({_id: req.session.user})
  246. .then((merchant)=>{
  247. merchant.inventory.push(req.body);
  248. merchant.save()
  249. .then((newMerchant)=>{
  250. return res.json(newMerchant);
  251. })
  252. .catch((err)=>{
  253. console.log(err);
  254. return res.render("error");
  255. });
  256. })
  257. .catch((err)=>{
  258. console.log(err);
  259. return res.render("error");
  260. });
  261. },
  262. addRecipeIngredient: function(req, res){
  263. Merchant.findOne({_id: req.session.user})
  264. .then((merchant)=>{
  265. let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
  266. recipe.ingredients.push(req.body.item)
  267. merchant.save()
  268. .then((newMerchant)=>{
  269. return res.json(newMerchant);
  270. })
  271. .catch((err)=>{
  272. console.log(err);
  273. return res.render("error");
  274. });
  275. })
  276. .catch((err)=>{
  277. console.log(err);
  278. return res.render("error");
  279. });
  280. },
  281. removeRecipeIngredient: function(req, res){
  282. Merchant.findOne({_id: req.session.user})
  283. .then((merchant)=>{
  284. let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
  285. for(let i = 0; i < recipe.ingredients.length; i++){
  286. if(req.body.ingredientId === recipe.ingredients[i]._id.toString()){
  287. recipe.ingredients.splice(i, 1);
  288. break;
  289. }
  290. }
  291. merchant.save()
  292. .then((result)=>{
  293. return res.json();
  294. })
  295. .catch((err)=>{
  296. console.log(err);
  297. return res.render("error");
  298. });
  299. })
  300. .catch((err)=>{
  301. console.log(err);
  302. return res.render("error");
  303. });
  304. },
  305. removeMerchantIngredient: function(req, res){
  306. Merchant.findOne({_id: req.session.user})
  307. .then((merchant)=>{
  308. for(let i = 0; i < merchant.inventory.length; i++){
  309. if(req.body.ingredientId === merchant.inventory[i]._id.toString()){
  310. merchant.inventory.splice(i, 1);
  311. break;
  312. }
  313. }
  314. merchant.save()
  315. .then(()=>{
  316. return res.json();
  317. })
  318. .catch((err)=>{
  319. console.log(err);
  320. return res.render("error");
  321. });
  322. })
  323. .catch((err)=>{
  324. console.log(err);
  325. return res.render("error");
  326. });
  327. },
  328. updateRecipeIngredient: function(req, res){
  329. Merchant.findOne({_id: req.session.user})
  330. .then((merchant)=>{
  331. let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
  332. for(let i = 0; i < recipe.ingredients.length; i++){
  333. if(recipe.ingredients[i]._id.toString() === req.body.ingredient._id){
  334. recipe.ingredients[i].quantity = req.body.ingredient.quantity;
  335. break;
  336. }
  337. }
  338. merchant.save()
  339. .then(()=>{
  340. return res.json();
  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. }