renderer.js 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. const axios = require("axios");
  2. const ObjectId = require("mongoose").Types.ObjectId;
  3. const Merchant = require("../models/merchant");
  4. const Transaction = require("../models/transaction");
  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. let error = {};
  13. let isLoggedIn = req.session.isLoggedIn || false;
  14. if(req.session.error){
  15. error = req.session.error;
  16. req.session.error = undefined;
  17. }else{
  18. error = null;
  19. }
  20. return res.render("landingPage/landing", {error: error, isLoggedIn: isLoggedIn});
  21. },
  22. /*
  23. GET - Displays the main inventory page for merchants
  24. Returns = the logged in merchant and his/her data
  25. Renders inventoryPage
  26. */
  27. displayDashboard: function(req, res){
  28. if(!req.session.user){
  29. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  30. return res.redirect("/");
  31. }
  32. Merchant.findOne({_id: req.session.user}, {password: 0, createdAt: 0})
  33. .populate("inventory.ingredient")
  34. .populate("recipes")
  35. .then((merchant)=>{
  36. if(merchant.pos === "clover"){
  37. let subscriptionCheck = axios.get(`${process.env.CLOVER_ADDRESS}/v3/apps/${process.env.SUBLINE_CLOVER_APPID}/merchants/${merchant.posId}/billing_info?access_token=${merchant.posAccessToken}`);
  38. let transactionRetrieval = axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${merchant.posAccessToken}`)
  39. .catch((err)=>{
  40. let errorMessage = "There was an error and we could not retrieve your transactions from Clover";
  41. merchant.password = undefined;
  42. return res.render("dashboardPage/dashboard", {merchant: merchant, error: errorMessage, transactions: []});
  43. });
  44. Promise.all([subscriptionCheck, transactionRetrieval])
  45. .then((response)=>{
  46. if(response[0].data.status !== "ACTIVE"){
  47. req.session.error = "SUBSCRIPTION EXPIRED. PLEASE RENEW ON CLOVER";
  48. return res.redirect("/");
  49. }
  50. let transactions = [];
  51. for(let order of response[1].data.elements){
  52. let newTransaction = new Transaction({
  53. merchant: merchant._id,
  54. date: new Date(order.createdTime),
  55. device: order.device.id
  56. });
  57. for(let item of order.lineItems.elements){
  58. let recipe = merchant.recipes.find(r => r.posId === item.item.id);
  59. if(recipe){
  60. //Search and increment/add instead of just push
  61. // newTransaction.recipes.push(recipe._id);
  62. let isNewRecipe = true;
  63. for(let newRecipe of newTransaction.recipes){
  64. if(newRecipe.recipe === recipe._id){
  65. newRecipe.quantity++;
  66. isNewRecipe = false;
  67. break;
  68. }
  69. }
  70. if(isNewRecipe){
  71. newTransaction.recipes.push({
  72. recipe: recipe._id,
  73. quantity: 1
  74. });
  75. }
  76. //End modifications
  77. for(let ingredient of recipe.ingredients){
  78. let inventoryIngredient = {};
  79. for(let invItem of merchant.inventory){
  80. if(invItem.ingredient._id.toString() === ingredient.ingredient._id.toString()){
  81. inventoryIngredient = invItem;
  82. }
  83. }
  84. inventoryIngredient.quantity = (inventoryIngredient.quantity - ingredient.quantity).toFixed(2);
  85. }
  86. }
  87. }
  88. transactions.push(newTransaction);
  89. merchant.lastUpdatedTime = Date.now();
  90. }
  91. merchant.save()
  92. .then((updatedMerchant)=>{
  93. updatedMerchant.accessToken = undefined;
  94. merchant = updatedMerchant;
  95. Transaction.create(transactions);
  96. let date = new Date();
  97. let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  98. return Transaction.aggregate([
  99. {$match: {
  100. merchant: new ObjectId(req.session.user),
  101. date: {$gte: firstDay}
  102. }},
  103. {$sort: {date: 1}},
  104. {$project: {
  105. date: 1,
  106. recipes: 1
  107. }}
  108. ])
  109. })
  110. .then((transactions)=>{
  111. res.render("dashboardPage/dashboard", {merchant: merchant, transactions: transactions});
  112. })
  113. .catch((err)=>{
  114. let errorMessage = "Error: unable to update data";
  115. merchant.password = undefined;
  116. return res.render("dashboardPage/dashboard", {merchant: merchant, error: errorMessage, transactions: []});
  117. });
  118. })
  119. .catch((err)=>{
  120. req.session.error = "ERROR: UNABLE TO RETRIEVE DATA FROM CLOVER";
  121. return res.redirect("/");
  122. })
  123. }else if(merchant.pos === "none"){
  124. merchant.password = undefined;
  125. let date = new Date();
  126. let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  127. Transaction.aggregate([
  128. {$match: {
  129. merchant: new ObjectId(req.session.user),
  130. date: {$gte: firstDay},
  131. }},
  132. {$sort: {date: 1}},
  133. {$project: {
  134. date: 1,
  135. recipes: 1
  136. }}
  137. ])
  138. .then((transactions)=>{
  139. return res.render("dashboardPage/dashboard", {merchant: merchant, transactions: transactions})
  140. })
  141. .catch((err)=>{});
  142. }else{
  143. req.session.error = "ERROR: WEBSITE PANIC!";
  144. return res.redirect("/");
  145. }
  146. })
  147. .catch((err)=>{
  148. req.session.error = "ERROR: COULD NOT RETRIEVE USER DATA";
  149. return res.redirect("/");
  150. });
  151. },
  152. //GET - Renders the information page
  153. displayLegal: function(req, res){
  154. return res.render("informationPage/information");
  155. },
  156. //GET - Renders the page to reset your password
  157. displayPassReset: function(req, res){
  158. return res.render("passResetPage/passReset");
  159. }
  160. }