home.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. module.exports = {
  8. displayInventory: (req, res)=>{
  9. Merchant.findOne({cloverId: merchantId})
  10. .populate("ingredients")
  11. .then((merchant)=>{
  12. if(merchant){
  13. return res.render("inventory");
  14. }else{
  15. return res.redirect("/merchant/new");
  16. }
  17. })
  18. .catch((err)=>{
  19. console.log(err);
  20. return res.render("error");
  21. });
  22. },
  23. merchantSetup: (req, res)=>{
  24. Ingredient.find()
  25. .then((ingredients)=>{
  26. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  27. .then((recipes)=>{
  28. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
  29. })
  30. .catch((err)=>{
  31. console.log(err);
  32. return res.render("error");
  33. });
  34. })
  35. .catch((err)=>{
  36. console.log(err);
  37. return res.render("error");
  38. })
  39. },
  40. getRecipes: (req, res)=>{
  41. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  42. .then((recipes)=>{
  43. return res.json(recipes);
  44. })
  45. .catch((err)=>{
  46. return res.json(err);
  47. });
  48. },
  49. createMerchant: (req, res)=>{
  50. let data = JSON.parse(req.body.data);
  51. Recipe.create(data.recipes)
  52. .then((recipes)=>{
  53. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
  54. .then((merchant)=>{
  55. console.log(merchant.data);
  56. let newMerchant = new Merchant({
  57. name: merchant.data.name,
  58. cloverId: merchant.data.id,
  59. lastUpdateTime: Date.now,
  60. ingredients: [],
  61. recipes: []
  62. });
  63. for(let ingredient of data.ingredients){
  64. let newIngredient = {
  65. id: ingredient.id,
  66. quantity: parseInt(ingredient.quantity)
  67. }
  68. newMerchant.ingredients.push(newIngredient);
  69. }
  70. for(let recipe of recipes){
  71. newMerchant.recipes.push(recipe._id);
  72. }
  73. newMerchant.save()
  74. .then((newMerchant)=>{
  75. return res.redirect("/");
  76. })
  77. .catch((err)=>{
  78. console.log(err);
  79. return res.render("error");
  80. })
  81. })
  82. .catch((err)=>{
  83. console.log(err);
  84. });
  85. })
  86. .catch((err)=>{
  87. console.log(err);
  88. return res.render("error");
  89. });
  90. },
  91. createNewIngredients: (req, res)=>{
  92. Ingredient.create(req.body)
  93. .then((ingredients)=>{
  94. return res.json(ingredients);
  95. })
  96. .catch((err)=>{
  97. console.log(err);
  98. return res.render("error");
  99. });
  100. }
  101. }