renderer.js 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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")) throw "unverified";
  32. // if(res.locals.merchant.square !== undefined){
  33. // await helper.getSquareData(res.locals.merchant);
  34. // }
  35. return res.locals.merchant.save();
  36. })
  37. .then((merchant)=>{
  38. let date = new Date();
  39. let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  40. return Transaction.aggregate([
  41. {$match: {
  42. merchant: new ObjectId(res.locals.merchant._id),
  43. date: {$gte: firstDay},
  44. }},
  45. {$sort: {date: -1}},
  46. {$project: {
  47. date: 1,
  48. recipes: 1
  49. }}
  50. ]);
  51. })
  52. .then((transactions)=>{
  53. res.locals.merchant._id = undefined;
  54. res.locals.password = undefined;
  55. res.locals.merchant.status = undefined;
  56. res.locals.square = undefined;
  57. res.locals.session = undefined;
  58. return res.render("dashboardPage/dashboard", {merchant: res.locals.merchant, transactions: transactions});
  59. })
  60. .catch((err)=>{
  61. console.log(err);
  62. if(err === "unverified"){
  63. req.session.error = "PLEASE VERIFY YOUR EMAIL ADDRESS";
  64. return res.redirect(`/verify/email/${res.locals.merchant._id}`);
  65. }
  66. req.session.error = "ERROR: UNABLE TO RETRIEVE DATA";
  67. return res.redirect("/");
  68. });
  69. },
  70. //GET - Renders the page to reset your password
  71. displayPassReset: function(req, res){
  72. return res.render("passResetPage/passReset");
  73. }
  74. }