renderer.js 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. const axios = require("axios");
  2. const Merchant = require("../models/merchant");
  3. const Ingredient = require("../models/ingredient");
  4. const Transaction = require("../models/transaction");
  5. const Purchase = require("../models/purchase");
  6. module.exports = {
  7. //GET - Shows the public landing page
  8. //Returns:
  9. // Error: a single error message (only if there is an error)
  10. //Renders landingPage
  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. //GET - Displays the main inventory page for merchants
  23. //Returns:
  24. // merchant: the logged in merchant
  25. //Renders inventoryPage
  26. displayDashboard: function(req, res){
  27. if(!req.session.user){
  28. req.session.error = "You must be logged in to view that page";
  29. return res.redirect("/");
  30. }
  31. Merchant.findOne({_id: req.session.user}, {password: 0, createdAt: 0})
  32. .populate("inventory.ingredient")
  33. .populate({
  34. path: "recipes",
  35. model: "Recipe",
  36. populate: {
  37. path: "ingredients.ingredient",
  38. model: "Ingredient"
  39. }
  40. })
  41. .then((merchant)=>{
  42. if(merchant.pos === "clover"){
  43. axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${merchant.posAccessToken}`)
  44. .then((result)=>{
  45. let transactions = [];
  46. for(let order of result.data.elements){
  47. let newTransaction = new Transaction({
  48. merchant: merchant._id,
  49. date: new Date(order.createdTime),
  50. device: order.device.id
  51. });
  52. for(let item of order.lineItems.elements){
  53. let recipe = merchant.recipes.find(r => r.posId === item.item.id);
  54. if(recipe){
  55. //Search and increment/add instead of just push
  56. // newTransaction.recipes.push(recipe._id);
  57. let isNewRecipe = true;
  58. for(let newRecipe of newTransaction.recipes){
  59. if(newRecipe.recipe === recipe._id){
  60. newRecipe.quantity++;
  61. isNewRecipe = false;
  62. break;
  63. }
  64. }
  65. if(isNewRecipe){
  66. newTransaction.recipes.push({
  67. recipe: recipe._id,
  68. quantity: 1
  69. });
  70. }
  71. //End modifications
  72. for(let ingredient of recipe.ingredients){
  73. let inventoryIngredient = {};
  74. for(let invItem of merchant.inventory){
  75. if(invItem.ingredient._id.toString() === ingredient.ingredient._id.toString()){
  76. inventoryIngredient = invItem;
  77. }
  78. }
  79. inventoryIngredient.quantity = (inventoryIngredient.quantity - ingredient.quantity).toFixed(2);
  80. }
  81. }
  82. }
  83. transactions.push(newTransaction);
  84. merchant.lastUpdatedTime = Date.now();
  85. }
  86. merchant.save()
  87. .then((updatedMerchant)=>{
  88. updatedMerchant.accessToken = undefined;
  89. res.render("dashboardPage/dashboard", {merchant: updatedMerchant, error: undefined});
  90. Transaction.create(transactions);
  91. return;
  92. })
  93. .catch((err)=>{
  94. let errorMessage = "Error: unable to save user data";
  95. merchant.password = undefined;
  96. return res.render("dashboardPage/dashboard", {merchant: updatedMerchant, error: errorMessage});
  97. });
  98. })
  99. .catch((err)=>{
  100. let errorMessage = "There was an error and we could not retrieve your transactions from Clover";
  101. merchant.password = undefined;
  102. return res.render("dashboardPage/dashboard", {merchant: merchant, error: errorMessage});
  103. });
  104. }else if(merchant.pos === "none"){
  105. merchant.password = undefined;
  106. return res.render("dashboardPage/dashboard", {merchant: merchant, error: undefined});
  107. }else{
  108. req.session.error = "Error: WEBSITE PANIC";
  109. return res.redirect("/");
  110. }
  111. })
  112. .catch((err)=>{
  113. req.session.error = "Error: could not retrieve user data";
  114. return res.redirect("/");
  115. });
  116. },
  117. //GET - Renders the information page
  118. displayLegal: function(req, res){
  119. return res.render("informationPage/information");
  120. },
  121. //GET - Renders the data display page
  122. //Returns data
  123. displayData: function(req, res){
  124. if(!req.session.user){
  125. req.session.error = "You must be logged in to view that page";
  126. return res.redirect("/");
  127. }
  128. let merchTransPromise = new Promise((resolve, reject)=>{
  129. Merchant.findOne({_id: req.session.user},
  130. {name: 1, inventory: 1, recipes: 1, _id: 0})
  131. .populate("recipes")
  132. .populate("inventory.ingredient")
  133. .then((merchant)=>{
  134. let date = new Date();
  135. let firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
  136. let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  137. Transaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}},
  138. {date: 1, recipes: 1, _id: 0})
  139. .then((transactions)=>{
  140. resolve({merchant: merchant, transactions: transactions});
  141. })
  142. .catch((err)=>{});
  143. })
  144. .catch((err)=>{});
  145. });
  146. let purchasePromise = new Promise((resolve, reject)=>{
  147. Purchase.find({merchant: req.session.user},
  148. {date: 1, ingredients: 1, _id: 0})
  149. .then((purchases)=>{
  150. resolve(purchases);
  151. })
  152. .catch((err)=>{});
  153. });
  154. Promise.all([merchTransPromise, purchasePromise])
  155. .then((response)=>{
  156. let data = {
  157. merchant: response[0].merchant,
  158. transactions: response[0].transactions,
  159. purchases: response[1]
  160. }
  161. return res.render("dataPage/data", {data: data});
  162. })
  163. .catch((err)=>{
  164. req.session.error = "Error: unable to retrieve user data";
  165. return res.redirect("/");
  166. });
  167. }
  168. }