renderer.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. /*
  41. GET - Displays the main inventory page for merchants
  42. Returns = the logged in merchant and his/her data
  43. Renders inventoryPage
  44. */
  45. displayDashboard: function(req, res){
  46. new Activity({
  47. ipAddr: req.headers['x-forwarded-for'] || req.connection.remoteAddress,
  48. merchant: res.locals.merchant._id,
  49. route: "dashboard",
  50. date: new Date()
  51. })
  52. .save()
  53. .catch(()=>{});
  54. let merchant2 = {};
  55. res.locals.merchant
  56. .populate("inventory.ingredient")
  57. .populate("recipes")
  58. .execPopulate()
  59. .then(async (merchant)=>{
  60. if(res.locals.merchant.status.includes("unverified")){
  61. throw "unverified";
  62. }
  63. if(res.locals.merchant.pos === "clover"){
  64. await helper.getCloverData(res.locals.merchant);
  65. }else if(res.locals.merchant.pos === "square"){
  66. await helper.getSquareData(res.locals.merchant);
  67. }else{
  68. return;
  69. }
  70. return res.locals.merchant.save();
  71. })
  72. .then((merchant)=>{
  73. let date = new Date();
  74. let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  75. return Transaction.aggregate([
  76. {$match: {
  77. merchant: new ObjectId(res.locals.merchant._id),
  78. date: {$gte: firstDay},
  79. }},
  80. {$sort: {date: -1}},
  81. {$project: {
  82. date: 1,
  83. recipes: 1
  84. }}
  85. ]);
  86. })
  87. .then((transactions)=>{
  88. res.locals.merchant._id = undefined;
  89. res.locals.merchant.posAccessToken = undefined;
  90. res.locals.merchant.lastUpdatedTime = undefined;
  91. res.locals.merchant.accountStatus = undefined;
  92. res.locals.merchant.status = undefined;
  93. return res.render("dashboardPage/dashboard", {merchant: res.locals.merchant, transactions: transactions});
  94. })
  95. .catch((err)=>{
  96. if(err === "unverified"){
  97. return res.redirect(`/verify/email/${merchant2._id}`);
  98. }
  99. req.session.error = "ERROR: UNABLE TO RETRIEVE USER DATA";
  100. return res.redirect("/");
  101. });
  102. },
  103. //GET - Renders the page to reset your password
  104. displayPassReset: function(req, res){
  105. return res.render("passResetPage/passReset");
  106. }
  107. }