renderer.js 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 Order = require("../models/order");
  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. merchant = updatedMerchant;
  90. Transaction.create(transactions);
  91. let date = new Date();
  92. let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  93. return Transaction.aggregate([
  94. {$match: {
  95. merchant: new ObjectId(req.session.user),
  96. date: {$gte: firstDay}
  97. }},
  98. {$sort: {date: 1}},
  99. {$project: {
  100. date: 1,
  101. recipes: 1
  102. }}
  103. ])
  104. })
  105. .then((transactions)=>{
  106. res.render("dashboardPage/dashboard", {merchant: merchant, transactions: transactions});
  107. })
  108. .catch((err)=>{
  109. let errorMessage = "Error: unable to update data";
  110. merchant.password = undefined;
  111. return res.render("dashboardPage/dashboard", {merchant: merchant, error: errorMessage});
  112. });
  113. })
  114. .catch((err)=>{
  115. let errorMessage = "There was an error and we could not retrieve your transactions from Clover";
  116. merchant.password = undefined;
  117. return res.render("dashboardPage/dashboard", {merchant: merchant, error: errorMessage});
  118. });
  119. }else if(merchant.pos === "none"){
  120. merchant.password = undefined;
  121. let date = new Date();
  122. let firstDay = new Date(date.getFullYear(), date.getMonth() - 1, 1);
  123. Transaction.aggregate([
  124. {$match: {
  125. merchant: new ObjectId(req.session.user),
  126. date: {$gte: firstDay},
  127. }},
  128. {$sort: {date: 1}},
  129. {$project: {
  130. _id: 0,
  131. date: 1,
  132. recipes: 1
  133. }}
  134. ])
  135. .then((transactions)=>{
  136. return res.render("dashboardPage/dashboard", {merchant: merchant, transactions: transactions})
  137. })
  138. .catch((err)=>{
  139. console.log(err);
  140. });
  141. }else{
  142. req.session.error = "Error: WEBSITE PANIC";
  143. return res.redirect("/");
  144. }
  145. })
  146. .catch((err)=>{
  147. req.session.error = "Error: could not retrieve user data";
  148. return res.redirect("/");
  149. });
  150. },
  151. //GET - Renders the information page
  152. displayLegal: function(req, res){
  153. return res.render("informationPage/information");
  154. },
  155. //GET - Renders the data display page
  156. //Returns data
  157. displayData: function(req, res){
  158. if(!req.session.user){
  159. req.session.error = "You must be logged in to view that page";
  160. return res.redirect("/");
  161. }
  162. let date = new Date();
  163. let firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
  164. let lastDay = new Date();
  165. let merchTransPromise = new Promise((resolve, reject)=>{
  166. Merchant.findOne({_id: req.session.user},
  167. {name: 1, inventory: 1, recipes: 1, _id: 0})
  168. .populate("recipes")
  169. .populate("inventory.ingredient")
  170. .then((merchant)=>{
  171. Transaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}},
  172. {date: 1, recipes: 1, _id: 0},
  173. {sort: {date: 1}})
  174. .then((transactions)=>{
  175. resolve({merchant: merchant, transactions: transactions});
  176. })
  177. .catch((err)=>{});
  178. })
  179. .catch((err)=>{});
  180. });
  181. let orderPromise = new Promise((resolve, reject)=>{
  182. Order.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}},
  183. {date: 1, ingredients: 1, _id: 0},
  184. {sort: {date: 1}})
  185. .then((orders)=>{
  186. resolve(orders);
  187. })
  188. .catch((err)=>{});
  189. });
  190. Promise.all([merchTransPromise, orderPromise])
  191. .then((response)=>{
  192. let data = {
  193. merchant: response[0].merchant,
  194. transactions: response[0].transactions,
  195. orders: response[1],
  196. dates: [firstDay, lastDay]
  197. }
  198. return res.render("dataPage/data", {data: data});
  199. })
  200. .catch((err)=>{
  201. req.session.error = "Error: unable to retrieve user data";
  202. return res.redirect("/");
  203. });
  204. },
  205. displayPassReset: function(req, res){
  206. return res.render("passResetPage/passReset");
  207. }
  208. }