renderer.js 3.8 KB

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