renderer.js 4.1 KB

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