renderer.js 10 KB

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