home.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. Merchant.findOne({cloverId: merchantId})
  158. .then((merchant)=>{
  159. for(let i = 0; i < merchant.inventory.length; i++){
  160. if(req.body.id === merchant.inventory[i].ingredient.toString()){
  161. merchant.inventory.splice(i, 1);
  162. }
  163. }
  164. merchant.save()
  165. .then((merchant)=>{
  166. return res.json(merchant);
  167. })
  168. .catch((err)=>{
  169. console.log(err);
  170. return res.json(err);
  171. });
  172. })
  173. .catch((err)=>{
  174. console.log(err);
  175. return res.json(err);
  176. });
  177. },
  178. createIngredient: (req, res)=>{
  179. Ingredient.create(req.body.ingredient)
  180. .then((ingredient)=>{
  181. Merchant.updateOne(
  182. {cloverId: merchantId},
  183. {$push: {inventory: {
  184. ingredient: ingredient._id.toString(),
  185. quantity: req.body.quantity
  186. }}}
  187. )
  188. .then((merchant)=>{
  189. return res.json(merchant);
  190. })
  191. .catch((err)=>{
  192. return res.json(err);
  193. });
  194. })
  195. .catch((err)=>{
  196. console.log(err);
  197. return res.json("error");
  198. });
  199. },
  200. displayRecipes: function(req, res){
  201. Merchant.findOne({cloverId: merchantId})
  202. .populate({
  203. path: "recipes",
  204. populate: {
  205. path: "ingredients.id",
  206. model: "Ingredient"
  207. }
  208. })
  209. .then((merchant)=>{
  210. return res.render("recipesPage/recipes", {recipes: merchant.recipes});
  211. })
  212. .catch((err)=>{
  213. console.log(err);
  214. return res.render("error");
  215. });
  216. },
  217. deleteRecipeIngredient: function(req, res){
  218. Recipe.findOne({_id: req.body.recipeId})
  219. .populate("ingredients.id")
  220. .then((recipe)=>{
  221. for(let i = 0; i < recipe.ingredients.length; i++){
  222. if(recipe.ingredients[i]._id.toString() === req.body.ingredientId){
  223. recipe.ingredients.splice(i, 1);
  224. break;
  225. }
  226. }
  227. recipe.save()
  228. .then((recipe)=>{
  229. return res.json(recipe);
  230. })
  231. .catch((err)=>{
  232. console.log(err);
  233. return res.render("error");
  234. })
  235. })
  236. .catch((err)=>{
  237. console.log(err);
  238. return res.render("error");
  239. });
  240. },
  241. updateRecipeIngredient: function(req, res){
  242. Recipe.findOne({_id: req.body.recipeId})
  243. .populate("ingredients.id")
  244. .then((recipe)=>{
  245. for(let i = 0; i < recipe.ingredients.length; i++){
  246. if(recipe.ingredients[i]._id.toString() === req.body.ingredient._id){
  247. recipe.ingredients.splice(i, 1);
  248. recipe.ingredients.push(req.body.ingredient);
  249. break;
  250. }
  251. }
  252. recipe.save()
  253. .then((recipe)=>{
  254. return res.json(recipe);
  255. })
  256. .catch((err)=>{
  257. console.log(err);
  258. return res.render("error");
  259. })
  260. })
  261. .catch((err)=>{
  262. console.log(err);
  263. res.render("error");
  264. })
  265. }
  266. }