home.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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. console.log(recipes);
  29. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
  30. })
  31. .catch((err)=>{
  32. console.log(err);
  33. return res.render("error");
  34. });
  35. })
  36. .catch((err)=>{
  37. console.log(err);
  38. return res.render("error");
  39. })
  40. },
  41. getRecipes: (req, res)=>{
  42. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}/items?access_token=${token}`)
  43. .then((recipes)=>{
  44. return res.json(recipes);
  45. })
  46. .catch((err)=>{
  47. return res.json(err);
  48. });
  49. },
  50. createMerchant: (req, res)=>{
  51. let newIngredient = [];
  52. let data = JSON.parse(req.body.data);
  53. for(let ingredient of data.new){
  54. newIngredient.push({
  55. name: ingredient.name,
  56. category: ingredient.category,
  57. unitType: ingredient.unitType
  58. });
  59. }
  60. Ingredient.create(newIngredient)
  61. .then((ingredients)=>{
  62. ingredientIds = [];
  63. for(let i = 0; i < ingredients.length; i++){
  64. data.existing.push({
  65. id: ingredient[i]._id,
  66. quantity: data.new[i].quantity
  67. });
  68. ingredientIds.push({
  69. tempId: data.new[i].id,
  70. permId: ingredients[i]._id
  71. });
  72. }
  73. Recipe.create()
  74. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchantId}?access_token=${token}`)
  75. .then((merchant)=>{
  76. Merchant.create({
  77. cloverId: merchant._id,
  78. lastUpdateTime: Date.now,
  79. ingredients: data.existing
  80. })
  81. .then((merchant)=>{
  82. console.log(merchant);
  83. })
  84. .catch((err)=>{
  85. console.log(err);
  86. return res.render("error");
  87. });
  88. })
  89. .catch((err)=>{
  90. console.log(err);
  91. return res.render("error");
  92. })
  93. })
  94. .catch((err)=>{
  95. console.log(err);
  96. return res.render("error");
  97. });
  98. }
  99. }