home.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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: (req, res)=>{
  10. Merchant.findOne({posId: merchantId})
  11. .populate("inventory.ingredient")
  12. .then((merchant)=>{
  13. if(merchant){
  14. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${token}`)
  15. .then((result)=>{
  16. for(let order of result.data.elements){
  17. for(let item of order.lineItems.elements){
  18. let recipe = merchant.recipes.find(r => r.posId === item.item.id);
  19. if(recipe){
  20. for(let ingredient of recipe.ingredients){
  21. let inventoryIngredient = {};
  22. for(let invItem of merchant.inventory){
  23. if(invItem.ingredient._id.toString() === ingredient.ingredient.toString()){
  24. inventoryIngredient = invItem;
  25. }
  26. }
  27. inventoryIngredient.quantity -= ingredient.quantity;
  28. }
  29. }
  30. }
  31. }
  32. merchant.lastUpdatedTime = Date.now();
  33. merchant.save()
  34. .then((updatedMerchant)=>{
  35. return res.render("inventory/inventory", {merchant: updatedMerchant});
  36. })
  37. .catch((err)=>{
  38. console.log(err);
  39. return res.render("error");
  40. });
  41. })
  42. .catch((err)=>{
  43. console.log(err);
  44. });
  45. }else{
  46. return res.redirect("/merchant/new");
  47. }
  48. })
  49. .catch((err)=>{
  50. console.log(err);
  51. return res.render("error");
  52. });
  53. },
  54. merchantSetup: (req, res)=>{
  55. Ingredient.find()
  56. .then((ingredients)=>{
  57. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  58. .then((recipes)=>{
  59. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
  60. })
  61. .catch((err)=>{
  62. console.log(err);
  63. return res.render("error");
  64. });
  65. })
  66. .catch((err)=>{
  67. console.log(err);
  68. return res.render("error");
  69. })
  70. },
  71. getRecipes: (req, res)=>{
  72. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  73. .then((recipes)=>{
  74. return res.json(recipes);
  75. })
  76. .catch((err)=>{
  77. return res.json(err);
  78. });
  79. },
  80. createMerchant: (req, res)=>{
  81. let data = JSON.parse(req.body.data);
  82. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
  83. .then((merchant)=>{
  84. let newMerchant = new Merchant({
  85. name: merchant.data.name,
  86. posId: merchant.data.id,
  87. lastUpdatedTime: Date.now(),
  88. inventory: [],
  89. recipes: []
  90. });
  91. for(let ingredient of data.ingredients){
  92. let newIngredient = {
  93. ingredient: ingredient.id,
  94. quantity: parseInt(ingredient.quantity)
  95. }
  96. newMerchant.inventory.push(newIngredient);
  97. }
  98. for(let recipe of data.recipes){
  99. newMerchant.recipes.push(recipe);
  100. }
  101. newMerchant.save()
  102. .then((newMerchant)=>{
  103. return res.redirect("/");
  104. })
  105. .catch((err)=>{
  106. console.log(err);
  107. return res.render("error");
  108. })
  109. })
  110. .catch((err)=>{
  111. console.log(err);
  112. });
  113. },
  114. createNewIngredients: (req, res)=>{
  115. Ingredient.create(req.body)
  116. .then((ingredients)=>{
  117. return res.json(ingredients);
  118. })
  119. .catch((err)=>{
  120. console.log(err);
  121. return res.render("error");
  122. });
  123. },
  124. createIngredient: (req, res)=>{
  125. Ingredient.create(req.body.ingredient)
  126. .then((ingredient)=>{
  127. Merchant.updateOne(
  128. {cloverId: merchantId},
  129. {$push: {inventory: {
  130. ingredient: ingredient._id.toString(),
  131. quantity: req.body.quantity
  132. }}}
  133. )
  134. .then((merchant)=>{
  135. return res.json(merchant);
  136. })
  137. .catch((err)=>{
  138. return res.json(err);
  139. });
  140. })
  141. .catch((err)=>{
  142. console.log(err);
  143. return res.json("error");
  144. });
  145. },
  146. displayRecipes: function(req, res){
  147. Merchant.findOne({posId: merchantId})
  148. .populate("recipes.ingredients.ingredient")
  149. .then((merchant)=>{
  150. console.log(merchant.recipes[0].ingredients);
  151. return res.render("recipesPage/recipes", {merchant: merchant});
  152. })
  153. .catch((err)=>{
  154. console.log(err);
  155. return res.render("error");
  156. });
  157. },
  158. deleteRecipeIngredient: function(req, res){
  159. Recipe.findOne({_id: req.body.recipeId})
  160. .populate("ingredients.id")
  161. .then((recipe)=>{
  162. for(let i = 0; i < recipe.ingredients.length; i++){
  163. if(recipe.ingredients[i]._id.toString() === req.body.ingredientId){
  164. recipe.ingredients.splice(i, 1);
  165. break;
  166. }
  167. }
  168. recipe.save()
  169. .then((recipe)=>{
  170. return res.json(recipe);
  171. })
  172. .catch((err)=>{
  173. console.log(err);
  174. return res.render("error");
  175. });
  176. })
  177. .catch((err)=>{
  178. console.log(err);
  179. return res.render("error");
  180. });
  181. },
  182. updateMerchant: function(req, res){
  183. Merchant.updateOne({_id: req.body._id}, req.body)
  184. .then((merchant)=>{
  185. return res.json(merchant);
  186. })
  187. .catch((err)=>{
  188. console.log(err);
  189. return res.render("error");
  190. });
  191. },
  192. updateRecipes: function(req, res){
  193. Merchant.findOne({posId: merchantId})
  194. .then((merchant)=>{
  195. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  196. .then((result)=>{
  197. let deletedRecipes = merchant.recipes.slice();
  198. for(let i = 0; i < result.data.elements.length; i++){
  199. for(let j = 0; j < deletedRecipes.length; j++){
  200. if(result.data.elements[i].id === deletedRecipes[j].posId){
  201. result.data.elements.splice(i, 1);
  202. deletedRecipes.splice(j, 1);
  203. i--;
  204. break;
  205. }
  206. }
  207. }
  208. for(let recipe of deletedRecipes){
  209. for(let i = 0; i < merchant.recipes.length; i++){
  210. if(recipe._id === merchant.recipes[i]._id){
  211. merchant.recipes.splice(i, 1);
  212. break;
  213. }
  214. }
  215. }
  216. for(let recipe of result.data.elements){
  217. merchant.recipes.push({
  218. posId: recipe.id,
  219. name: recipe.name,
  220. ingredients: []
  221. });
  222. }
  223. merchant.save()
  224. .then((newMerchant)=>{
  225. newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
  226. .then((newestMerchant)=>{
  227. return res.json({merchant: newestMerchant, count: result.data.elements.length});
  228. })
  229. .catch((err)=>{
  230. console.log(err);
  231. return res.render("error");
  232. });
  233. })
  234. .catch((err)=>{
  235. console.log(err);
  236. return res.render("error");
  237. });
  238. })
  239. .catch((err)=>{
  240. console.log(err);
  241. return res.render("error");
  242. });
  243. })
  244. .catch((err)=>{
  245. console.log(err);
  246. return res.render("error");
  247. });
  248. }
  249. }