renderer.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. const NonPosTransaction = require("../models/nonPosTransaction");
  7. module.exports = {
  8. //GET - Shows the public landing page
  9. //Returns:
  10. // Error: a single error message (only if there is an error)
  11. //Renders landingPage
  12. landingPage: function(req, res){
  13. let error = {};
  14. let isLoggedIn = req.session.isLoggedIn || false;
  15. if(req.session.error){
  16. error = req.session.error;
  17. req.session.error = undefined;
  18. }else{
  19. error = null;
  20. }
  21. return res.render("landingPage/landing", {error: error, isLoggedIn: isLoggedIn});
  22. },
  23. //GET - Displays the main inventory page for merchants
  24. //Returns:
  25. // merchant: the logged in merchant
  26. //Renders inventoryPage
  27. displayInventory: function(req, res){
  28. if(!req.session.user){
  29. req.session.error = "You must be logged in to view that page";
  30. return res.redirect("/");
  31. }
  32. Merchant.findOne({_id: req.session.user})
  33. .populate("inventory.ingredient")
  34. .populate({
  35. path: "recipes",
  36. model: "Recipe",
  37. populate: {
  38. path: "ingredients.ingredient",
  39. model: "Ingredient"
  40. }
  41. })
  42. .then((merchant)=>{
  43. if(merchant.pos === "clover"){
  44. axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${merchant.posAccessToken}`)
  45. .then((result)=>{
  46. let transactions = [];
  47. for(let order of result.data.elements){
  48. let newTransaction = new Transaction({
  49. merchant: merchant._id,
  50. date: new Date(order.createdTime),
  51. device: order.device.id
  52. });
  53. for(let item of order.lineItems.elements){
  54. let recipe = merchant.recipes.find(r => r.posId === item.item.id);
  55. if(recipe){
  56. //Search and increment/add instead of just push
  57. // newTransaction.recipes.push(recipe._id);
  58. let isNewRecipe = true;
  59. for(let newRecipe of newTransaction.recipes){
  60. if(newRecipe.recipe === recipe._id){
  61. newRecipe.quantity++;
  62. isNewRecipe = false;
  63. break;
  64. }
  65. }
  66. if(isNewRecipe){
  67. newTransaction.recipes.push({
  68. recipe: recipe._id,
  69. quantity: 1
  70. });
  71. }
  72. //End modifications
  73. for(let ingredient of recipe.ingredients){
  74. let inventoryIngredient = {};
  75. for(let invItem of merchant.inventory){
  76. if(invItem.ingredient._id.toString() === ingredient.ingredient._id.toString()){
  77. inventoryIngredient = invItem;
  78. }
  79. }
  80. inventoryIngredient.quantity = (inventoryIngredient.quantity - ingredient.quantity).toFixed(2);
  81. }
  82. }
  83. }
  84. transactions.push(newTransaction);
  85. }
  86. merchant.lastUpdatedTime = Date.now();
  87. merchant.save()
  88. .then((updatedMerchant)=>{
  89. updatedMerchant.password = undefined;
  90. updatedMerchant.accessToken = undefined;
  91. res.render("inventoryPage/inventory", {merchant: updatedMerchant, error: undefined});
  92. Transaction.create(transactions);
  93. return;
  94. })
  95. .catch((err)=>{
  96. let errorMessage = "Error: unable to save user data";
  97. merchant.password = undefined;
  98. return res.render("inventoryPage/inventory", {merchant: updatedMerchant, error: errorMessage});
  99. });
  100. })
  101. .catch((err)=>{
  102. let errorMessage = "There was an error and we could not retrieve your transactions from Clover";
  103. merchant.password = undefined;
  104. return res.render("inventoryPage/inventory", {merchant: merchant, error: errorMessage});
  105. });
  106. }else if(merchant.pos === "none"){
  107. merchant.password = undefined;
  108. return res.render("inventoryPage/inventory", {merchant: merchant, error: undefined});
  109. }else{
  110. req.session.error = "Error: WEBSITE PANIC";
  111. return res.redirect("/");
  112. }
  113. })
  114. .catch((err)=>{
  115. req.session.error = "Error: could not retrieve user data";
  116. return res.redirect("/");
  117. });
  118. },
  119. //GET - Renders the merchant setup page for a clover client
  120. //Returns:
  121. // ingredients: all ingredients from database
  122. // recipes: recipes from the users clover account
  123. // error: returns error (if any) from session
  124. //Renders merchantSetupPage
  125. merchantSetupClover: function(req, res){
  126. let errorMessage = {};
  127. if(req.session.error){
  128. errorMessage = req.session.error;
  129. req.session.error = undefined;
  130. }else{
  131. errorMessage = null;
  132. }
  133. Ingredient.find()
  134. .then((ingredients)=>{
  135. axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${req.session.merchantId}/items?access_token=${req.session.accessToken}`)
  136. .then((recipes)=>{
  137. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data, error: errorMessage});
  138. })
  139. .catch((err)=>{
  140. req.session.error = "Error: unable to retrieve data from Clover";
  141. return res.redirect("/");
  142. });
  143. })
  144. .catch((err)=>{
  145. req.session.error = "Error: data for new merchants could not be retrieved";
  146. return res.redirect("/");
  147. });
  148. },
  149. //GET - Renders the merchant setup page for a non-pos client
  150. //Returns:
  151. // ingredients: all ingredients from database
  152. // recipes: null (to signify non-post client)
  153. // error: returns error (if any) from session
  154. //Renders merchantSetupPage
  155. merchantSetupNone: function(req, res){
  156. let errorMessage = {};
  157. if(req.session.error){
  158. errorMessage = req.session.error;
  159. req.session.error = undefined;
  160. }else{
  161. errorMessage = null;
  162. }
  163. Ingredient.find()
  164. .then((ingredients)=>{
  165. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: null, error: errorMessage});
  166. })
  167. .catch((err)=>{
  168. req.session.error = "Error: data for new merchants could not be retrieved";
  169. return res.redirect("/");
  170. });
  171. },
  172. //GET - Renders the recipe display page
  173. //Returns:
  174. // merchant: merchant with recipes and recipe ingredients populated
  175. //Renders recipesPage
  176. displayRecipes: function(req, res){
  177. if(!req.session.user){
  178. req.session.error = "You must be logged in to view that page";
  179. return res.redirect("/");
  180. }
  181. Merchant.findOne({_id: req.session.user})
  182. .populate({
  183. path: "recipes",
  184. model: "Recipe",
  185. populate: {
  186. path: "ingredients.ingredient",
  187. model: "Ingredient"
  188. }
  189. })
  190. .populate("inventory.ingredient")
  191. .then((merchant)=>{
  192. merchant.password = undefined;
  193. return res.render("recipesPage/recipes", {merchant: merchant});
  194. })
  195. .catch((err)=>{
  196. req.session.error = "Error: unable to retrieve user data";
  197. return res.redirect("/");
  198. });
  199. },
  200. //GET - Renders the information page
  201. //Renders information page
  202. displayLegal: function(req, res){
  203. return res.render("informationPage/information");
  204. },
  205. displayData: function(req, res){
  206. if(!req.session.user){
  207. req.session.error = "You must be logged in to view that page";
  208. return res.redirect("/");
  209. }
  210. let merchTransPromise = new Promise((resolve, reject)=>{
  211. Merchant.findOne({_id: req.session.user})
  212. .populate("recipes")
  213. .populate("inventory.ingredient")
  214. .then((merchant)=>{
  215. let date = new Date();
  216. let firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
  217. let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
  218. if(merchant.pos === "clover"){
  219. Transaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}})
  220. .then((transactions)=>{
  221. resolve({merchant: merchant, transactions: transactions});
  222. })
  223. .catch((err)=>{});
  224. }else{
  225. NonPosTransaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}})
  226. .then((transactions)=>{
  227. resolve({merchant: merchant, transactions: transactions});
  228. })
  229. .catch((err)=>{});
  230. }
  231. })
  232. .catch((err)=>{});
  233. });
  234. let purchasePromise = new Promise((resolve, reject)=>{
  235. Purchase.find({merchant: req.session.user})
  236. .then((purchases)=>{
  237. resolve(purchases);
  238. })
  239. .catch((err)=>{});
  240. });
  241. Promise.all([merchTransPromise, purchasePromise])
  242. .then((response)=>{
  243. let data = {
  244. merchant: response[0].merchant,
  245. transactions: response[0].transactions,
  246. purchases: response[1]
  247. }
  248. return res.render("dataPage/data", {data: data});
  249. })
  250. .catch((err)=>{
  251. req.session.error = "Error: unable to retrieve user data";
  252. return res.redirect("/");
  253. });
  254. }
  255. }