renderer.js 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. axios.get(`${process.env.CLOVER_ADDRESS}/v3/apps/${process.env.SUBLINE_CLOVER_APPID}/merchants/${merchant.posId}/billing_info?access_token=${merchant.posAccessToken}`)
  38. .then((response)=>{
  39. if(response.data.status !== "ACTIVE"){
  40. req.session.error = "SUBSCRIPTION EXPIRED. PLEASE RENEW ON CLOVER";
  41. return res.redirect("/");
  42. }
  43. })
  44. .catch((err)=>{
  45. console.log(err);
  46. });
  47. axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${merchant.posAccessToken}`)
  48. .then((result)=>{
  49. let transactions = [];
  50. for(let order of result.data.elements){
  51. let newTransaction = new Transaction({
  52. merchant: merchant._id,
  53. date: new Date(order.createdTime),
  54. device: order.device.id
  55. });
  56. for(let item of order.lineItems.elements){
  57. let recipe = merchant.recipes.find(r => r.posId === item.item.id);
  58. if(recipe){
  59. //Search and increment/add instead of just push
  60. // newTransaction.recipes.push(recipe._id);
  61. let isNewRecipe = true;
  62. for(let newRecipe of newTransaction.recipes){
  63. if(newRecipe.recipe === recipe._id){
  64. newRecipe.quantity++;
  65. isNewRecipe = false;
  66. break;
  67. }
  68. }
  69. if(isNewRecipe){
  70. newTransaction.recipes.push({
  71. recipe: recipe._id,
  72. quantity: 1
  73. });
  74. }
  75. //End modifications
  76. for(let ingredient of recipe.ingredients){
  77. let inventoryIngredient = {};
  78. for(let invItem of merchant.inventory){
  79. if(invItem.ingredient._id.toString() === ingredient.ingredient._id.toString()){
  80. inventoryIngredient = invItem;
  81. }
  82. }
  83. inventoryIngredient.quantity = (inventoryIngredient.quantity - ingredient.quantity).toFixed(2);
  84. }
  85. }
  86. }
  87. transactions.push(newTransaction);
  88. merchant.lastUpdatedTime = Date.now();
  89. }
  90. merchant.save()
  91. .then((updatedMerchant)=>{
  92. updatedMerchant.accessToken = undefined;
  93. merchant = updatedMerchant;
  94. Transaction.create(transactions);
  95. let date = new Date();
  96. let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  97. return Transaction.aggregate([
  98. {$match: {
  99. merchant: new ObjectId(req.session.user),
  100. date: {$gte: firstDay}
  101. }},
  102. {$sort: {date: 1}},
  103. {$project: {
  104. date: 1,
  105. recipes: 1
  106. }}
  107. ])
  108. })
  109. .then((transactions)=>{
  110. res.render("dashboardPage/dashboard", {merchant: merchant, transactions: transactions});
  111. })
  112. .catch((err)=>{
  113. let errorMessage = "Error: unable to update data";
  114. merchant.password = undefined;
  115. return res.render("dashboardPage/dashboard", {merchant: merchant, error: errorMessage, transactions: []});
  116. });
  117. })
  118. .catch((err)=>{
  119. let errorMessage = "There was an error and we could not retrieve your transactions from Clover";
  120. merchant.password = undefined;
  121. return res.render("dashboardPage/dashboard", {merchant: merchant, error: errorMessage, transactions: []});
  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. }