render.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. const axios = require("axios");
  2. const Merchant = require("../models/merchant");
  3. const Ingredient = require("../models/ingredient");
  4. const Transaction = require("../models/transaction");
  5. const token = "b48068eb-411a-918e-ea64-52007147e42c";
  6. module.exports = {
  7. //Public page including login and registration
  8. landingPage: function(req, res){
  9. let error = {};
  10. if(req.session.error){
  11. error = req.session.error;
  12. req.session.error = undefined;
  13. }else{
  14. error = undefined;
  15. }
  16. return res.render("landingPage/landing", {error: error});
  17. },
  18. //Render the main page for merchants
  19. displayInventory: function(req, res){
  20. if(!req.session.user){
  21. return res.redirect("/");
  22. }
  23. Merchant.findOne({_id: req.session.user})
  24. .populate("inventory.ingredient")
  25. .populate("recipes")
  26. .then((merchant)=>{
  27. if(merchant.pos === "clover"){
  28. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${token}`)
  29. .then((result)=>{
  30. let transactions = [];
  31. for(let order of result.data.elements){
  32. let newTransaction = new Transaction({
  33. merchant: merchant._id,
  34. date: new Date(order.createdTime),
  35. device: order.device.id
  36. });
  37. for(let item of order.lineItems.elements){
  38. let recipe = merchant.recipes.find(r => r.posId === item.item.id);
  39. if(recipe){
  40. newTransaction.recipes.push(recipe._id);
  41. for(let ingredient of recipe.ingredients){
  42. let inventoryIngredient = {};
  43. for(let invItem of merchant.inventory){
  44. if(invItem.ingredient._id.toString() === ingredient.ingredient.toString()){
  45. inventoryIngredient = invItem;
  46. }
  47. }
  48. inventoryIngredient.quantity -= ingredient.quantity;
  49. }
  50. }
  51. }
  52. transactions.push(newTransaction);
  53. }
  54. merchant.lastUpdatedTime = Date.now();
  55. merchant.save()
  56. .then((updatedMerchant)=>{
  57. res.render("inventoryPage/inventory", {merchant: updatedMerchant});
  58. Transaction.create(transactions);
  59. return;
  60. })
  61. .catch((err)=>{
  62. console.log(err);
  63. return res.render("error");
  64. });
  65. })
  66. .catch((err)=>{
  67. console.log(err);
  68. });
  69. }else if(merchant.pos === "none"){
  70. return res.render("inventoryPage/inventory", {merchant: merchant})
  71. }else{
  72. return res.redirect("/");
  73. }
  74. })
  75. .catch((err)=>{
  76. console.log(err);
  77. return res.render("error");
  78. });
  79. },
  80. //Display Merchant Setup Page
  81. merchantSetupClover: function(req, res){
  82. req.session.posId = "YHVPCQMVB1P81";
  83. Ingredient.find()
  84. .then((ingredients)=>{
  85. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}/items?access_token=${token}`)
  86. .then((recipes)=>{
  87. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
  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. //Display page to set up merchant with no POS system
  100. merchantSetupNone: function(req, res){
  101. Ingredient.find()
  102. .then((ingredients)=>{
  103. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: null});
  104. })
  105. .catch((err)=>{
  106. console.log(err);
  107. return res.render("error");
  108. });
  109. },
  110. //Display page with recipe information
  111. displayRecipes: function(req, res){
  112. if(!req.session.user){
  113. return res.render("error");
  114. }
  115. Merchant.findOne({_id: req.session.user})
  116. .populate({
  117. path: "recipes",
  118. model: "Recipe",
  119. populate: {
  120. path: "ingredients.ingredient",
  121. model: "Ingredient"
  122. }
  123. })
  124. .populate("inventory.ingredient")
  125. .then((merchant)=>{
  126. return res.render("recipesPage/recipes", {merchant: merchant});
  127. })
  128. .catch((err)=>{
  129. console.log(err);
  130. return res.render("error");
  131. });
  132. }
  133. }