renderer.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. const transaction = require("../models/transaction");
  6. const ingredient = require("../models/ingredient");
  7. module.exports = {
  8. /*
  9. GET - Shows the public landing page
  10. Return = a single error message (only if there is an error)
  11. Renders landingPage
  12. */
  13. landingPage: function(req, res){
  14. let error = {};
  15. let isLoggedIn = req.session.isLoggedIn || false;
  16. if(req.session.error){
  17. error = req.session.error;
  18. req.session.error = undefined;
  19. }else{
  20. error = null;
  21. }
  22. return res.render("landingPage/landing", {error: error, isLoggedIn: isLoggedIn});
  23. },
  24. /*
  25. GET - Displays the main inventory page for merchants
  26. Returns = the logged in merchant and his/her data
  27. Renders inventoryPage
  28. */
  29. displayDashboard: function(req, res){
  30. if(!req.session.user){
  31. req.session.error = "MUST BE LOGGED IN TO DO THAT";
  32. return res.redirect("/");
  33. }
  34. Merchant.findOne({_id: req.session.user}, {password: 0, createdAt: 0})
  35. .populate("inventory.ingredient")
  36. .populate("recipes")
  37. .then(async (merchant)=>{
  38. let promiseArray = [];
  39. if(merchant.pos === "clover"){
  40. const subscriptionCheck = axios.get(`${process.env.CLOVER_ADDRESS}/v3/apps/${process.env.SUBLINE_CLOVER_APPID}/merchants/${merchant.posId}/billing_info?access_token=${merchant.posAccessToken}`);
  41. const transactionRetrieval = axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${merchant.posId}/orders?filter=modifiedTime>=${merchant.lastUpdatedTime}&expand=lineItems&expand=payment&access_token=${merchant.posAccessToken}`);
  42. await Promise.all([subscriptionCheck, transactionRetrieval])
  43. .then((response)=>{
  44. if(response[0].data.status !== "ACTIVE"){
  45. req.session.error = "SUBSCRIPTION EXPIRED. PLEASE RENEW ON CLOVER";
  46. return res.redirect("/");
  47. }
  48. const updatedTime = Date.now();
  49. //Create Subline transactions from Clover Transactions
  50. let transactions = [];
  51. for(let i = 0; i < response[1].data.elements.length; i++){
  52. let order = response[1].data.elements[i];
  53. if(order.paymentState !== "PAID"){
  54. break;
  55. }
  56. let newTransaction = new Transaction({
  57. merchant: merchant._id,
  58. date: new Date(order.createdTime),
  59. device: order.device.id
  60. });
  61. //Go through lineItems from Clover
  62. //Get the appropriate recipe from Subline
  63. //Add it to the transaction or increment if existing
  64. for(let j = 0; j < order.lineItems.elements.length; j++){
  65. let recipe = {}
  66. for(let k = 0; k < merchant.recipes.length; k++){
  67. if(merchant.recipes[k].posId === order.lineItems.elements[j].item.id){
  68. recipe = merchant.recipes[k];
  69. break;
  70. }
  71. }
  72. if(recipe){
  73. let isNewRecipe = true;
  74. for(let k = 0; k < newTransaction.recipes.length; k++){
  75. if(newTransaction.recipes[k].recipe === recipe._id){
  76. newTransaction.recipes[k].quantity++;
  77. isNewRecipe = false;
  78. break;
  79. }
  80. }
  81. if(isNewRecipe){
  82. newTransaction.recipes.push({
  83. recipe: recipe._id,
  84. quantity: 1
  85. });
  86. }
  87. //Subtract ingredients from merchants total for each ingredient in a recipe
  88. for(let k = 0; k < recipe.ingredients.length; k++){
  89. let inventoryIngredient = {};
  90. for(let l = 0; l < merchant.inventory.length; l++){
  91. if(merchant.inventory[l].ingredient._id.toString() === recipe.ingredients[k].ingredient._id.toString()){
  92. inventoryIngredient = merchant.inventory[l];
  93. break;
  94. }
  95. }
  96. inventoryIngredient.quantity = inventoryIngredient.quantity - ingredient.quantity;
  97. }
  98. }
  99. }
  100. transactions.push(newTransaction);
  101. }
  102. merchant.lastUpdatedTime = updatedTime;
  103. promiseArray.push(Transaction.create(transactions));
  104. })
  105. .catch((err)=>{
  106. req.session.error = "ERROR: UNABLE TO RETRIEVE DATA FROM CLOVER";
  107. return res.redirect("/");
  108. });
  109. }
  110. return Promise.all([merchant.save()].concat(promiseArray));
  111. })
  112. .then((response)=>{
  113. let date = new Date();
  114. let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  115. Transaction.aggregate([
  116. {$match: {
  117. merchant: new ObjectId(req.session.user),
  118. date: {$gte: firstDay},
  119. }},
  120. {$sort: {date: 1}},
  121. {$project: {
  122. date: 1,
  123. recipes: 1
  124. }}
  125. ])
  126. .then((transactions)=>{
  127. return res.render("dashboardPage/dashboard", {merchant: response[0], transactions: transactions});
  128. })
  129. .catch((err)=>{});
  130. })
  131. .catch((err)=>{
  132. req.session.error = "ERROR: UNABLE TO RETRIEVE USER DATA";
  133. return res.redirect("/");
  134. });
  135. },
  136. //GET - Renders the information page
  137. displayLegal: function(req, res){
  138. return res.render("informationPage/information");
  139. },
  140. //GET - Renders the page to reset your password
  141. displayPassReset: function(req, res){
  142. return res.render("passResetPage/passReset");
  143. }
  144. }