renderer.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. let merchant2 = {};
  27. res.locals.merchant
  28. .populate("inventory.ingredient")
  29. .populate("recipes")
  30. .execPopulate()
  31. .then(async (merchant)=>{
  32. if(res.locals.merchant.status.includes("unverified")){
  33. throw "unverified";
  34. }
  35. if(res.locals.merchant.pos === "clover"){
  36. await helper.getCloverData(res.locals.merchant);
  37. }else if(res.locals.merchant.pos === "square"){
  38. await helper.getSquareData(res.locals.merchant);
  39. }else{
  40. return;
  41. }
  42. return res.locals.merchant.save();
  43. })
  44. .then((merchant)=>{
  45. let date = new Date();
  46. let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  47. return Transaction.aggregate([
  48. {$match: {
  49. merchant: new ObjectId(res.locals.merchant._id),
  50. date: {$gte: firstDay},
  51. }},
  52. {$sort: {date: -1}},
  53. {$project: {
  54. date: 1,
  55. recipes: 1
  56. }}
  57. ]);
  58. })
  59. .then((transactions)=>{
  60. res.locals.merchant._id = undefined;
  61. res.locals.merchant.posAccessToken = undefined;
  62. res.locals.merchant.lastUpdatedTime = undefined;
  63. res.locals.merchant.accountStatus = undefined;
  64. res.locals.merchant.status = undefined;
  65. return res.render("dashboardPage/dashboard", {merchant: res.locals.merchant, transactions: transactions});
  66. })
  67. .catch((err)=>{
  68. //TODO: add banners to the necessary pages
  69. if(err === "unverified"){
  70. req.session.error = "PLEASE VERIFY YOUR EMAIL ADDRESS";
  71. return res.redirect(`/verify/email/${merchant2._id}`);
  72. }
  73. req.session.error = "ERROR: UNABLE TO RETRIEVE DATA";
  74. return res.redirect("/");
  75. });
  76. },
  77. //GET - Renders the page to reset your password
  78. displayPassReset: function(req, res){
  79. return res.render("passResetPage/passReset");
  80. }
  81. }