renderer.js 8.3 KB

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