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: function(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: function(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. updateMerchant: function(req, res){
  72. Merchant.updateOne({_id: req.body._id}, req.body)
  73. .then((merchant)=>{
  74. return res.json(merchant);
  75. })
  76. .catch((err)=>{
  77. console.log(err);
  78. return res.render("error");
  79. });
  80. },
  81. getCloverRecipes: function(req, res){
  82. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  83. .then((recipes)=>{
  84. return res.json(recipes);
  85. })
  86. .catch((err)=>{
  87. return res.json(err);
  88. });
  89. },
  90. createMerchant: function(req, res){
  91. let data = JSON.parse(req.body.data);
  92. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
  93. .then((merchant)=>{
  94. let newMerchant = new Merchant({
  95. name: merchant.data.name,
  96. posId: merchant.data.id,
  97. lastUpdatedTime: Date.now(),
  98. inventory: [],
  99. recipes: []
  100. });
  101. for(let ingredient of data.ingredients){
  102. let newIngredient = {
  103. ingredient: ingredient.id,
  104. quantity: parseInt(ingredient.quantity)
  105. }
  106. newMerchant.inventory.push(newIngredient);
  107. }
  108. for(let recipe of data.recipes){
  109. newMerchant.recipes.push(recipe);
  110. }
  111. newMerchant.save()
  112. .then((newMerchant)=>{
  113. return res.redirect("/");
  114. })
  115. .catch((err)=>{
  116. console.log(err);
  117. return res.render("error");
  118. })
  119. })
  120. .catch((err)=>{
  121. console.log(err);
  122. });
  123. },
  124. createNewIngredients: function(req, res){
  125. Ingredient.create(req.body)
  126. .then((ingredients)=>{
  127. return res.json(ingredients);
  128. })
  129. .catch((err)=>{
  130. console.log(err);
  131. return res.render("error");
  132. });
  133. },
  134. createIngredient: function(req, res){
  135. Ingredient.create(req.body.ingredient)
  136. .then((ingredient)=>{
  137. Merchant.updateOne(
  138. {cloverId: merchantId},
  139. {$push: {inventory: {
  140. ingredient: ingredient._id.toString(),
  141. quantity: req.body.quantity
  142. }}}
  143. )
  144. .then((merchant)=>{
  145. return res.json(merchant);
  146. })
  147. .catch((err)=>{
  148. return res.json(err);
  149. });
  150. })
  151. .catch((err)=>{
  152. console.log(err);
  153. return res.json("error");
  154. });
  155. },
  156. displayRecipes: function(req, res){
  157. Merchant.findOne({posId: merchantId})
  158. .populate("recipes.ingredients.ingredient")
  159. .then((merchant)=>{
  160. console.log(merchant.recipes[0].ingredients);
  161. return res.render("recipesPage/recipes", {merchant: merchant});
  162. })
  163. .catch((err)=>{
  164. console.log(err);
  165. return res.render("error");
  166. });
  167. },
  168. deleteRecipeIngredient: function(req, res){
  169. Recipe.findOne({_id: req.body.recipeId})
  170. .populate("ingredients.id")
  171. .then((recipe)=>{
  172. for(let i = 0; i < recipe.ingredients.length; i++){
  173. if(recipe.ingredients[i]._id.toString() === req.body.ingredientId){
  174. recipe.ingredients.splice(i, 1);
  175. break;
  176. }
  177. }
  178. recipe.save()
  179. .then((recipe)=>{
  180. return res.json(recipe);
  181. })
  182. .catch((err)=>{
  183. console.log(err);
  184. return res.render("error");
  185. });
  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. }