home.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. const axios = require("axios");
  2. const Merchant = require("../models/merchant");
  3. const Ingredient = require("../models/ingredient");
  4. const Recipe = require("../models/recipe");
  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: (req, res)=>{
  11. Merchant.findOne({cloverId: merchantId})
  12. .populate("inventory.ingredient")
  13. .populate("recipes")
  14. .then((merchant)=>{
  15. if(merchant){
  16. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.cloverId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${token}`)
  17. .then((result)=>{
  18. for(let order of result.data.elements){
  19. for(let item of order.lineItems.elements){
  20. let recipe = merchant.recipes.find(r => r.cloverId === item.item.id);
  21. if(recipe){
  22. for(let ingredient of recipe.ingredients){
  23. let inventoryIngredient = {};
  24. for(let invItem of merchant.inventory){
  25. if(invItem.ingredient._id.toString() === ingredient.id.toString()){
  26. inventoryIngredient = invItem;
  27. }
  28. }
  29. inventoryIngredient.quantity -= ingredient.quantity;
  30. }
  31. }
  32. }
  33. }
  34. merchant.lastUpdatedTime = Date.now();
  35. merchant.save()
  36. .then((updatedMerchant)=>{
  37. return res.render("inventory/inventory", {merchant: updatedMerchant});
  38. })
  39. .catch((err)=>{
  40. console.log(err);
  41. return res.render("error");
  42. });
  43. })
  44. .catch((err)=>{
  45. console.log(err);
  46. });
  47. }else{
  48. return res.redirect("/merchant/new");
  49. }
  50. })
  51. .catch((err)=>{
  52. console.log(err);
  53. return res.render("error");
  54. });
  55. },
  56. merchantSetup: (req, res)=>{
  57. Ingredient.find()
  58. .then((ingredients)=>{
  59. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  60. .then((recipes)=>{
  61. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
  62. })
  63. .catch((err)=>{
  64. console.log(err);
  65. return res.render("error");
  66. });
  67. })
  68. .catch((err)=>{
  69. console.log(err);
  70. return res.render("error");
  71. })
  72. },
  73. getRecipes: (req, res)=>{
  74. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  75. .then((recipes)=>{
  76. return res.json(recipes);
  77. })
  78. .catch((err)=>{
  79. return res.json(err);
  80. });
  81. },
  82. createMerchant: (req, res)=>{
  83. let data = JSON.parse(req.body.data);
  84. Recipe.create(data.recipes)
  85. .then((recipes)=>{
  86. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
  87. .then((merchant)=>{
  88. let newMerchant = new Merchant({
  89. name: merchant.data.name,
  90. cloverId: merchant.data.id,
  91. lastUpdatedTime: Date.now(),
  92. inventory: [],
  93. recipes: []
  94. });
  95. for(let ingredient of data.ingredients){
  96. let newIngredient = {
  97. ingredient: ingredient.id,
  98. quantity: parseInt(ingredient.quantity)
  99. }
  100. newMerchant.inventory.push(newIngredient);
  101. }
  102. for(let recipe of recipes){
  103. newMerchant.recipes.push(recipe._id);
  104. }
  105. newMerchant.save()
  106. .then((newMerchant)=>{
  107. return res.redirect("/");
  108. })
  109. .catch((err)=>{
  110. console.log(err);
  111. return res.render("error");
  112. })
  113. })
  114. .catch((err)=>{
  115. console.log(err);
  116. });
  117. })
  118. .catch((err)=>{
  119. console.log(err);
  120. return res.render("error");
  121. });
  122. },
  123. createNewIngredients: (req, res)=>{
  124. Ingredient.create(req.body)
  125. .then((ingredients)=>{
  126. return res.json(ingredients);
  127. })
  128. .catch((err)=>{
  129. console.log(err);
  130. return res.render("error");
  131. });
  132. },
  133. updateIngredient: (req, res)=>{
  134. Merchant.findOne({cloverId: merchantId})
  135. .then((merchant)=>{
  136. for(let item of merchant.inventory){
  137. if(req.body.id === item.ingredient.toString()){
  138. item.quantity = req.body.quantity;
  139. break;
  140. }
  141. }
  142. merchant.save()
  143. .then((updatedMerchant)=>{
  144. return res.json(updatedMerchant);
  145. })
  146. .catch((err)=>{
  147. console.log(err);
  148. return res.json(err);
  149. })
  150. })
  151. .catch((err)=>{
  152. console.log(err);
  153. return res.render("error");
  154. });
  155. },
  156. removeIngredient: (req, res)=>{
  157. console.log(req.body);
  158. Merchant.findOne({cloverId: merchantId})
  159. .then((merchant)=>{
  160. for(let i = 0; i < merchant.inventory.length; i++){
  161. console.log(`${req.body.id} ${merchant.inventory[i].ingredient.toString()}`);
  162. if(req.body.id === merchant.inventory[i].ingredient.toString()){
  163. merchant.inventory.splice(i, 1);
  164. }
  165. }
  166. merchant.save()
  167. .then((merchant)=>{
  168. return res.json(merchant);
  169. })
  170. .catch((err)=>{
  171. console.log(err);
  172. return res.json(err);
  173. });
  174. })
  175. .catch((err)=>{
  176. console.log(err);
  177. return res.json(err);
  178. });
  179. }
  180. }