renderer.js 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const ObjectId = require("mongoose").Types.ObjectId;
  2. const Merchant = require("../models/merchant.js");
  3. const Transaction = require("../models/transaction.js");
  4. const Activity = require("../models/activity.js");
  5. const helper = require("./helper.js");
  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. new Activity({
  14. ipAddr: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
  15. merchant: req.session.user,
  16. route: "landing",
  17. date: new Date()
  18. })
  19. .save()
  20. .catch(()=>{});
  21. let error = {};
  22. let isLoggedIn = req.session.isLoggedIn || false;
  23. if(req.session.error){
  24. error = req.session.error;
  25. req.session.error = undefined;
  26. }else{
  27. error = null;
  28. }
  29. if(req.session.success){
  30. success = req.session.success;
  31. req.session.success = undefined;
  32. }else{
  33. success = null;
  34. }
  35. return res.render("landingPage/landing", {error: error, success: success, isLoggedIn: isLoggedIn});
  36. },
  37. /*
  38. GET - Displays the main inventory page for merchants
  39. Returns = the logged in merchant and his/her data
  40. Renders inventoryPage
  41. */
  42. displayDashboard: function(req, res){
  43. if(!req.session.user){
  44. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  45. return res.redirect("/");
  46. }
  47. new Activity({
  48. ipAddr: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
  49. merchant: req.session.user,
  50. route: "dashboard",
  51. date: new Date()
  52. })
  53. .save()
  54. .catch(()=>{});
  55. let merchant2 = {};
  56. // Merchant.findOne(
  57. // {_id: res.locals.merchant._id},
  58. // {
  59. // name: 1,
  60. // pos: 1,
  61. // posId: 1,
  62. // posAccessToken: 1,
  63. // lastUpdatedTime: 1,
  64. // inventory: 1,
  65. // recipes: 1,
  66. // squareLocation: 1,
  67. // status: 1
  68. // }
  69. // )
  70. res.locals.merchant
  71. .populate("inventory.ingredient")
  72. .populate("recipes")
  73. .execPopulate()
  74. .then(async (merchant)=>{
  75. if(res.locals.merchant.status.includes("unverified")){
  76. throw "unverified";
  77. }
  78. if(res.locals.merchant.pos === "clover"){
  79. await helper.getCloverData(res.locals.merchant);
  80. }else if(res.locals.merchant.pos === "square"){
  81. await helper.getSquareData(res.locals.merchant);
  82. }else{
  83. return;
  84. }
  85. return res.locals.merchant.save();
  86. })
  87. .then((merchant)=>{
  88. let date = new Date();
  89. let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  90. return Transaction.aggregate([
  91. {$match: {
  92. merchant: new ObjectId(res.locals.merchant._id),
  93. date: {$gte: firstDay},
  94. }},
  95. {$sort: {date: -1}},
  96. {$project: {
  97. date: 1,
  98. recipes: 1
  99. }}
  100. ]);
  101. })
  102. .then((transactions)=>{
  103. res.locals.merchant._id = undefined;
  104. res.locals.merchant.posAccessToken = undefined;
  105. res.locals.merchant.lastUpdatedTime = undefined;
  106. res.locals.merchant.accountStatus = undefined;
  107. res.locals.merchant.status = undefined;
  108. return res.render("dashboardPage/dashboard", {merchant: res.locals.merchant, transactions: transactions});
  109. })
  110. .catch((err)=>{
  111. if(err === "unverified"){
  112. return res.redirect(`/verify/email/${merchant2._id}`);
  113. }
  114. req.session.error = "ERROR: UNABLE TO RETRIEVE USER DATA";
  115. return res.redirect("/");
  116. });
  117. },
  118. //GET - Renders the page to reset your password
  119. displayPassReset: function(req, res){
  120. return res.render("passResetPage/passReset");
  121. }
  122. }