renderer.js 8.2 KB

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