home.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. const axios = require("axios");
  2. // const session = require("express-session");
  3. const Merchant = require("../models/merchant");
  4. const Ingredient = require("../models/ingredient");
  5. // const merchantId = "HCVKASXH94531";
  6. // const token = "f1c88a69-e3e4-059a-da06-8858d0636e82";
  7. const merchantId = "YHVPCQMVB1P81";
  8. const token = "b48068eb-411a-918e-ea64-52007147e42c";
  9. module.exports = {
  10. displayInventory: function(req, res){
  11. Merchant.findOne({posId: merchantId})
  12. .populate("inventory.ingredient")
  13. .then((merchant)=>{
  14. if(merchant){
  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.body._id}, 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.updateOne(
  139. {cloverId: merchantId},
  140. {$push: {inventory: {
  141. ingredient: ingredient._id.toString(),
  142. quantity: req.body.quantity
  143. }}}
  144. )
  145. .then((merchant)=>{
  146. return res.json(merchant);
  147. })
  148. .catch((err)=>{
  149. return res.json(err);
  150. });
  151. })
  152. .catch((err)=>{
  153. console.log(err);
  154. return res.json("error");
  155. });
  156. },
  157. displayRecipes: function(req, res){
  158. Merchant.findOne({posId: merchantId})
  159. .populate("recipes.ingredients.ingredient")
  160. .then((merchant)=>{
  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. demoLogin: function(req, res){
  250. return res.render("loginPage/login");
  251. }
  252. }