renderer.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. const ObjectId = require("mongoose").Types.ObjectId;
  2. const Transaction = require("../models/transaction.js");
  3. const helper = require("./helper.js");
  4. module.exports = {
  5. /*
  6. GET - Shows the public landing page
  7. */
  8. landingPage: function(req, res){
  9. return res.render("otherPages/landing", {banner: res.locals.banner});
  10. },
  11. //GET: Renders the login page
  12. loginPage: function(req, res){
  13. if(req.session.user !== undefined) return res.redirect("/dashboard");
  14. return res.render("otherPages/login", {banner: res.locals.banner});
  15. },
  16. //GET: Renders the registration page
  17. registerPage: function(req, res){
  18. return res.render("otherPages/register", {banner: res.locals.banner});
  19. },
  20. /*
  21. GET - Displays the main inventory page for merchants
  22. Returns = the logged in merchant and his/her data
  23. Renders inventoryPage
  24. */
  25. displayDashboard: function(req, res){
  26. res.locals.merchant
  27. .populate("inventory.ingredient")
  28. .populate("recipes")
  29. .execPopulate()
  30. .then(async (merchant)=>{
  31. if(res.locals.merchant.status.includes("unverified")){
  32. throw "unverified";
  33. }
  34. if(res.locals.merchant.pos === "clover"){
  35. await helper.getCloverData(res.locals.merchant);
  36. }else if(res.locals.merchant.pos === "square"){
  37. await helper.getSquareData(res.locals.merchant);
  38. }else{
  39. return;
  40. }
  41. return res.locals.merchant.save();
  42. })
  43. .then((merchant)=>{
  44. let date = new Date();
  45. let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  46. return Transaction.aggregate([
  47. {$match: {
  48. merchant: new ObjectId(res.locals.merchant._id),
  49. date: {$gte: firstDay},
  50. }},
  51. {$sort: {date: -1}},
  52. {$project: {
  53. date: 1,
  54. recipes: 1
  55. }}
  56. ]);
  57. })
  58. .then((transactions)=>{
  59. res.locals.merchant._id = undefined;
  60. res.locals.merchant.posAccessToken = undefined;
  61. res.locals.merchant.lastUpdatedTime = undefined;
  62. res.locals.merchant.accountStatus = undefined;
  63. res.locals.merchant.status = undefined;
  64. return res.render("dashboardPage/dashboard", {merchant: res.locals.merchant, transactions: transactions});
  65. })
  66. .catch((err)=>{
  67. if(err === "unverified"){
  68. req.session.error = "PLEASE VERIFY YOUR EMAIL ADDRESS";
  69. return res.redirect(`/verify/email/${res.locals.merchant._id}`);
  70. }
  71. req.session.error = "ERROR: UNABLE TO RETRIEVE DATA";
  72. return res.redirect("/");
  73. });
  74. },
  75. //GET - Renders the page to reset your password
  76. displayPassReset: function(req, res){
  77. return res.render("passResetPage/passReset");
  78. }
  79. }