home.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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. deleteRecipeIngredient: function(req, res){
  178. Recipe.findOne({_id: req.body.recipeId})
  179. .populate("ingredients.id")
  180. .then((recipe)=>{
  181. for(let i = 0; i < recipe.ingredients.length; i++){
  182. if(recipe.ingredients[i]._id.toString() === req.body.ingredientId){
  183. recipe.ingredients.splice(i, 1);
  184. break;
  185. }
  186. }
  187. recipe.save()
  188. .then((recipe)=>{
  189. return res.json(recipe);
  190. })
  191. .catch((err)=>{
  192. console.log(err);
  193. return res.render("error");
  194. });
  195. })
  196. .catch((err)=>{
  197. console.log(err);
  198. return res.render("error");
  199. });
  200. },
  201. updateRecipes: function(req, res){
  202. Merchant.findOne({_id: req.session.user})
  203. .then((merchant)=>{
  204. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  205. .then((result)=>{
  206. let deletedRecipes = merchant.recipes.slice();
  207. for(let i = 0; i < result.data.elements.length; i++){
  208. for(let j = 0; j < deletedRecipes.length; j++){
  209. if(result.data.elements[i].id === deletedRecipes[j].posId){
  210. result.data.elements.splice(i, 1);
  211. deletedRecipes.splice(j, 1);
  212. i--;
  213. break;
  214. }
  215. }
  216. }
  217. for(let recipe of deletedRecipes){
  218. for(let i = 0; i < merchant.recipes.length; i++){
  219. if(recipe._id === merchant.recipes[i]._id){
  220. merchant.recipes.splice(i, 1);
  221. break;
  222. }
  223. }
  224. }
  225. for(let recipe of result.data.elements){
  226. merchant.recipes.push({
  227. posId: recipe.id,
  228. name: recipe.name,
  229. ingredients: []
  230. });
  231. }
  232. merchant.save()
  233. .then((newMerchant)=>{
  234. newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
  235. .then((newestMerchant)=>{
  236. return res.json({merchant: newestMerchant, count: result.data.elements.length});
  237. })
  238. .catch((err)=>{
  239. console.log(err);
  240. return res.render("error");
  241. });
  242. })
  243. .catch((err)=>{
  244. console.log(err);
  245. return res.render("error");
  246. });
  247. })
  248. .catch((err)=>{
  249. console.log(err);
  250. return res.render("error");
  251. });
  252. })
  253. .catch((err)=>{
  254. console.log(err);
  255. return res.render("error");
  256. });
  257. },
  258. getIngredients: function(req, res){
  259. Ingredient.find()
  260. .then((ingredients)=>{
  261. return res.json(ingredients);
  262. })
  263. .catch((err)=>{
  264. console.log(err);
  265. return res.render("error");
  266. });
  267. },
  268. addMerchantIngredient: function(req, res){
  269. Merchant.findOne({_id: req.session.user})
  270. .then((merchant)=>{
  271. merchant.inventory.push(req.body);
  272. merchant.save()
  273. .then((newMerchant)=>{
  274. return res.json(newMerchant);
  275. })
  276. .catch((err)=>{
  277. console.log(err);
  278. return res.render("error");
  279. });
  280. })
  281. .catch((err)=>{
  282. console.log(err);
  283. return res.render("error");
  284. });
  285. },
  286. addRecipeIngredient: function(req, res){
  287. Merchant.findOne({_id: req.session.user})
  288. .then((merchant)=>{
  289. let recipe = merchant.recipes.find(r => r._id.toString() === req.body.recipeId);
  290. recipe.ingredients.push(req.body.item)
  291. merchant.save()
  292. .then((newMerchant)=>{
  293. return res.json(newMerchant);
  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. }