renderer.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. const axios = require("axios");
  2. const Merchant = require("../models/merchant");
  3. const Ingredient = require("../models/ingredient");
  4. const Transaction = require("../models/transaction");
  5. module.exports = {
  6. //GET - Shows the public landing page
  7. //Returns:
  8. // Error: a single error message (only if there is an error)
  9. //Renders landingPage
  10. landingPage: function(req, res){
  11. let error = {};
  12. if(req.session.error){
  13. error = req.session.error;
  14. req.session.error = undefined;
  15. }else{
  16. error = null;
  17. }
  18. return res.render("landingPage/landing", {error: error});
  19. },
  20. //GET - Displays the main inventory page for merchants
  21. //Returns:
  22. // merchant: the logged in merchant
  23. //Renders inventoryPage
  24. displayInventory: function(req, res){
  25. if(!req.session.user){
  26. req.session.error = "You must logged in to view that page";
  27. return res.redirect("/");
  28. }
  29. Merchant.findOne({_id: req.session.user})
  30. .populate("inventory.ingredient")
  31. .populate({
  32. path: "recipes",
  33. model: "Recipe",
  34. populate: {
  35. path: "ingredients.ingredient",
  36. model: "Ingredient"
  37. }
  38. })
  39. .then((merchant)=>{
  40. if(merchant.pos === "clover"){
  41. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${merchant.posAccessToken}`)
  42. .then((result)=>{
  43. let transactions = [];
  44. for(let order of result.data.elements){
  45. let newTransaction = new Transaction({
  46. merchant: merchant._id,
  47. date: new Date(order.createdTime),
  48. device: order.device.id
  49. });
  50. for(let item of order.lineItems.elements){
  51. let recipe = merchant.recipes.find(r => r.posId === item.item.id);
  52. if(recipe){
  53. newTransaction.recipes.push(recipe._id);
  54. for(let ingredient of recipe.ingredients){
  55. let inventoryIngredient = {};
  56. for(let invItem of merchant.inventory){
  57. if(invItem.ingredient._id.toString() === ingredient.ingredient._id.toString()){
  58. inventoryIngredient = invItem;
  59. }
  60. }
  61. inventoryIngredient.quantity = (inventoryIngredient.quantity - ingredient.quantity).toFixed(2);
  62. }
  63. }
  64. }
  65. transactions.push(newTransaction);
  66. }
  67. merchant.lastUpdatedTime = Date.now();
  68. merchant.save()
  69. .then((updatedMerchant)=>{
  70. updatedMerchant.password = undefined;
  71. updatedMerchant.accessToken = undefined;
  72. res.render("inventoryPage/inventory", {merchant: updatedMerchant, error: undefined});
  73. Transaction.create(transactions);
  74. return;
  75. })
  76. .catch((err)=>{
  77. let errorMessage = "Error: unable to save user data";
  78. merchant.password = undefined;
  79. return res.render("inventoryPage/inventory", {merchant: updatedMerchant, error: errorMessage});
  80. });
  81. })
  82. .catch((err)=>{
  83. let errorMessage = "There was an error and we could not retrieve your transactions from Clover";
  84. merchant.password = undefined;
  85. return res.render("inventoryPage/inventory", {merchant: merchant, error: errorMessage});
  86. });
  87. }else if(merchant.pos === "none"){
  88. merchant.password = undefined;
  89. return res.render("inventoryPage/inventory", {merchant: merchant, error: undefined})
  90. }else{
  91. req.session.error = "Error: WEBSITE PANIC";
  92. return res.redirect("/");
  93. }
  94. })
  95. .catch((err)=>{
  96. req.session.error = "Error: could not retrieve user data";
  97. return res.redirect("/");
  98. });
  99. },
  100. //GET - Renders the merchant setup page for a clover client
  101. //Returns:
  102. // ingredients: all ingredients from database
  103. // recipes: recipes from the users clover account
  104. // error: returns error (if any) from session
  105. //Renders merchantSetupPage
  106. merchantSetupClover: function(req, res){
  107. let errorMessage = {};
  108. if(req.session.error){
  109. errorMessage = req.session.error;
  110. req.session.error = undefined;
  111. }else{
  112. errorMessage = null;
  113. }
  114. Ingredient.find()
  115. .then((ingredients)=>{
  116. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.merchantId}/items?access_token=${req.session.accessToken}`)
  117. .then((recipes)=>{
  118. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data, error: errorMessage});
  119. })
  120. .catch((err)=>{
  121. req.session.error = "Error: unable to retrieve data from Clover";
  122. return res.redirect("/");
  123. });
  124. })
  125. .catch((err)=>{
  126. req.session.error = "Error: data for new merchants could not be retrieved";
  127. return res.redirect("/");
  128. });
  129. },
  130. //GET - Renders the merchant setup page for a non-pos client
  131. //Returns:
  132. // ingredients: all ingredients from database
  133. // recipes: null (to signify non-post client)
  134. // error: returns error (if any) from session
  135. //Renders merchantSetupPage
  136. merchantSetupNone: function(req, res){
  137. let errorMessage = {};
  138. if(req.session.error){
  139. errorMessage = req.session.error;
  140. req.session.error = undefined;
  141. }else{
  142. errorMessage = null;
  143. }
  144. Ingredient.find()
  145. .then((ingredients)=>{
  146. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: null, error: errorMessage});
  147. })
  148. .catch((err)=>{
  149. req.session.error = "Error: data for new merchants could not be retrieved";
  150. return res.redirect("/");
  151. });
  152. },
  153. //GET - Renders the recipe display page
  154. //Returns:
  155. // merchant: merchant with recipes and recipe ingredients populated
  156. //Renders recipesPage
  157. displayRecipes: 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. Merchant.findOne({_id: req.session.user})
  163. .populate({
  164. path: "recipes",
  165. model: "Recipe",
  166. populate: {
  167. path: "ingredients.ingredient",
  168. model: "Ingredient"
  169. }
  170. })
  171. .populate("inventory.ingredient")
  172. .then((merchant)=>{
  173. merchant.password = undefined;
  174. return res.render("recipesPage/recipes", {merchant: merchant});
  175. })
  176. .catch((err)=>{
  177. req.session.error = "Error: unable to retrieve user data";
  178. return res.redirect("/");
  179. });
  180. },
  181. //GET - Renders the information page
  182. //Renders information page
  183. displayLegal: function(req, res){
  184. return res.render("informationPage/information");
  185. }
  186. }