home.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  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. addRecipeIngredient: function(req, res){
  217. Merchant.findOne({_id: req.session.user})
  218. .then((merchant)=>{
  219. let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
  220. recipe.ingredients.push(req.body.item)
  221. merchant.save()
  222. .then((newMerchant)=>{
  223. return res.json(newMerchant);
  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. updateRecipeIngredient: 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. for(let i = 0; i < recipe.ingredients.length; i++){
  240. if(recipe.ingredients[i]._id.toString() === req.body.ingredient._id){
  241. recipe.ingredients[i].quantity = req.body.ingredient.quantity;
  242. break;
  243. }
  244. }
  245. merchant.save()
  246. .then(()=>{
  247. return res.json();
  248. })
  249. .catch((err)=>{
  250. console.log(err);
  251. return res.render("error");
  252. });
  253. })
  254. .catch((err)=>{
  255. console.log(err);
  256. return res.render("error");
  257. });
  258. },
  259. removeRecipeIngredient: function(req, res){
  260. Merchant.findOne({_id: req.session.user})
  261. .then((merchant)=>{
  262. let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
  263. for(let i = 0; i < recipe.ingredients.length; i++){
  264. if(req.body.ingredientId === recipe.ingredients[i]._id.toString()){
  265. recipe.ingredients.splice(i, 1);
  266. break;
  267. }
  268. }
  269. merchant.save()
  270. .then((result)=>{
  271. return res.json();
  272. })
  273. .catch((err)=>{
  274. console.log(err);
  275. return res.render("error");
  276. });
  277. })
  278. .catch((err)=>{
  279. console.log(err);
  280. return res.render("error");
  281. });
  282. },
  283. getIngredients: function(req, res){
  284. Ingredient.find()
  285. .then((ingredients)=>{
  286. return res.json(ingredients);
  287. })
  288. .catch((err)=>{
  289. console.log(err);
  290. return res.render("error");
  291. });
  292. },
  293. createNewIngredients: function(req, res){
  294. Ingredient.create(req.body)
  295. .then((ingredients)=>{
  296. return res.json(ingredients);
  297. })
  298. .catch((err)=>{
  299. console.log(err);
  300. return res.render("error");
  301. });
  302. },
  303. createIngredient: function(req, res){
  304. Ingredient.create(req.body.ingredient)
  305. .then((ingredient)=>{
  306. Merchant.findOne({_id: req.session.user})
  307. .then((merchant)=>{
  308. let item = {
  309. ingredient: ingredient,
  310. quantity: req.body.quantity
  311. }
  312. merchant.inventory.push(item);
  313. merchant.save()
  314. .then((merchant)=>{
  315. console.log("something");
  316. return res.json(merchant);
  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. .catch((err)=>{
  329. console.log(err);
  330. return res.render("error");
  331. });
  332. },
  333. getCloverRecipes: function(req, res){
  334. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  335. .then((recipes)=>{
  336. return res.json(recipes);
  337. })
  338. .catch((err)=>{
  339. return res.json(err);
  340. });
  341. }
  342. }