renderer.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. const axios = require("axios");
  2. const ObjectId = require("mongoose").Types.ObjectId;
  3. const Merchant = require("../models/merchant");
  4. const Transaction = require("../models/transaction");
  5. module.exports = {
  6. /*
  7. GET - Shows the public landing page
  8. Return = a single error message (only if there is an error)
  9. Renders landingPage
  10. */
  11. landingPage: function(req, res){
  12. let error = {};
  13. let isLoggedIn = req.session.isLoggedIn || false;
  14. if(req.session.error){
  15. error = req.session.error;
  16. req.session.error = undefined;
  17. }else{
  18. error = null;
  19. }
  20. return res.render("landingPage/landing", {error: error, isLoggedIn: isLoggedIn});
  21. },
  22. /*
  23. GET - Displays the main inventory page for merchants
  24. Returns = the logged in merchant and his/her data
  25. Renders inventoryPage
  26. */
  27. displayDashboard: function(req, res){
  28. if(!req.session.user){
  29. req.session.error = "You must be logged in to view that page";
  30. return res.redirect("/");
  31. }
  32. Merchant.findOne({_id: req.session.user}, {password: 0, createdAt: 0})
  33. .populate("inventory.ingredient")
  34. .populate("recipes")
  35. .then((merchant)=>{
  36. if(merchant.pos === "clover"){
  37. axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${merchant.posAccessToken}`)
  38. .then((result)=>{
  39. let transactions = [];
  40. for(let order of result.data.elements){
  41. let newTransaction = new Transaction({
  42. merchant: merchant._id,
  43. date: new Date(order.createdTime),
  44. device: order.device.id
  45. });
  46. for(let item of order.lineItems.elements){
  47. let recipe = merchant.recipes.find(r => r.posId === item.item.id);
  48. if(recipe){
  49. //Search and increment/add instead of just push
  50. // newTransaction.recipes.push(recipe._id);
  51. let isNewRecipe = true;
  52. for(let newRecipe of newTransaction.recipes){
  53. if(newRecipe.recipe === recipe._id){
  54. newRecipe.quantity++;
  55. isNewRecipe = false;
  56. break;
  57. }
  58. }
  59. if(isNewRecipe){
  60. newTransaction.recipes.push({
  61. recipe: recipe._id,
  62. quantity: 1
  63. });
  64. }
  65. //End modifications
  66. for(let ingredient of recipe.ingredients){
  67. let inventoryIngredient = {};
  68. for(let invItem of merchant.inventory){
  69. if(invItem.ingredient._id.toString() === ingredient.ingredient._id.toString()){
  70. inventoryIngredient = invItem;
  71. }
  72. }
  73. inventoryIngredient.quantity = (inventoryIngredient.quantity - ingredient.quantity).toFixed(2);
  74. }
  75. }
  76. }
  77. transactions.push(newTransaction);
  78. merchant.lastUpdatedTime = Date.now();
  79. }
  80. merchant.save()
  81. .then((updatedMerchant)=>{
  82. updatedMerchant.accessToken = undefined;
  83. merchant = updatedMerchant;
  84. Transaction.create(transactions);
  85. let date = new Date();
  86. let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  87. return Transaction.aggregate([
  88. {$match: {
  89. merchant: new ObjectId(req.session.user),
  90. date: {$gte: firstDay}
  91. }},
  92. {$sort: {date: 1}},
  93. {$project: {
  94. date: 1,
  95. recipes: 1
  96. }}
  97. ])
  98. })
  99. .then((transactions)=>{
  100. res.render("dashboardPage/dashboard", {merchant: merchant, transactions: transactions});
  101. })
  102. .catch((err)=>{
  103. let errorMessage = "Error: unable to update data";
  104. merchant.password = undefined;
  105. return res.render("dashboardPage/dashboard", {merchant: merchant, error: errorMessage, transactions: []});
  106. });
  107. })
  108. .catch((err)=>{
  109. let errorMessage = "There was an error and we could not retrieve your transactions from Clover";
  110. merchant.password = undefined;
  111. return res.render("dashboardPage/dashboard", {merchant: merchant, error: errorMessage, transactions: []});
  112. });
  113. }else if(merchant.pos === "none"){
  114. merchant.password = undefined;
  115. let date = new Date();
  116. let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  117. Transaction.aggregate([
  118. {$match: {
  119. merchant: new ObjectId(req.session.user),
  120. date: {$gte: firstDay},
  121. }},
  122. {$sort: {date: 1}},
  123. {$project: {
  124. date: 1,
  125. recipes: 1
  126. }}
  127. ])
  128. .then((transactions)=>{
  129. return res.render("dashboardPage/dashboard", {merchant: merchant, transactions: transactions})
  130. })
  131. .catch((err)=>{});
  132. }else{
  133. req.session.error = "Error: WEBSITE PANIC";
  134. return res.redirect("/");
  135. }
  136. })
  137. .catch((err)=>{
  138. req.session.error = "Error: could not retrieve user data";
  139. return res.redirect("/");
  140. });
  141. },
  142. //GET - Renders the information page
  143. displayLegal: function(req, res){
  144. return res.render("informationPage/information");
  145. },
  146. //GET - Renders the page to reset your password
  147. displayPassReset: function(req, res){
  148. return res.render("passResetPage/passReset");
  149. }
  150. }