renderer.js 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 token = "b48068eb-411a-918e-ea64-52007147e42c";
  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. if(req.session.error){
  14. error = req.session.error;
  15. req.session.error = undefined;
  16. }else{
  17. error = undefined;
  18. }
  19. return res.render("landingPage/landing", {error: error});
  20. },
  21. //GET - Displays the main inventory page for merchants
  22. //Returns:
  23. // merchant: the logged in merchant
  24. //Renders inventoryPage
  25. displayInventory: function(req, res){
  26. if(!req.session.user){
  27. return res.redirect("/");
  28. }
  29. Merchant.findOne({_id: req.session.user})
  30. .populate("inventory.ingredient")
  31. .populate("recipes")
  32. .then((merchant)=>{
  33. if(merchant.pos === "clover"){
  34. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${token}`)
  35. .then((result)=>{
  36. let transactions = [];
  37. for(let order of result.data.elements){
  38. let newTransaction = new Transaction({
  39. merchant: merchant._id,
  40. date: new Date(order.createdTime),
  41. device: order.device.id
  42. });
  43. for(let item of order.lineItems.elements){
  44. let recipe = merchant.recipes.find(r => r.posId === item.item.id);
  45. if(recipe){
  46. newTransaction.recipes.push(recipe._id);
  47. for(let ingredient of recipe.ingredients){
  48. let inventoryIngredient = {};
  49. for(let invItem of merchant.inventory){
  50. if(invItem.ingredient._id.toString() === ingredient.ingredient.toString()){
  51. inventoryIngredient = invItem;
  52. }
  53. }
  54. inventoryIngredient.quantity -= ingredient.quantity;
  55. }
  56. }
  57. }
  58. transactions.push(newTransaction);
  59. }
  60. merchant.lastUpdatedTime = Date.now();
  61. merchant.save()
  62. .then((updatedMerchant)=>{
  63. merchant.password = undefined;
  64. res.render("inventoryPage/inventory", {merchant: updatedMerchant});
  65. Transaction.create(transactions);
  66. return;
  67. })
  68. .catch((err)=>{
  69. console.log(err);
  70. return res.render("error");
  71. });
  72. })
  73. .catch((err)=>{
  74. console.log(err);
  75. });
  76. }else if(merchant.pos === "none"){
  77. merchant.password = undefined;
  78. return res.render("inventoryPage/inventory", {merchant: merchant})
  79. }else{
  80. return res.redirect("/");
  81. }
  82. })
  83. .catch((err)=>{
  84. console.log(err);
  85. return res.render("error");
  86. });
  87. },
  88. //GET - Renders the merchant setup page for a clover client
  89. //Returns:
  90. // ingredients: all ingredients from database
  91. // recipes: recipes from the users clover account
  92. //Renders merchantSetupPage
  93. merchantSetupClover: function(req, res){
  94. req.session.posId = "YHVPCQMVB1P81";
  95. Ingredient.find()
  96. .then((ingredients)=>{
  97. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}/items?access_token=${token}`)
  98. .then((recipes)=>{
  99. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
  100. })
  101. .catch((err)=>{
  102. console.log(err);
  103. return res.render("error");
  104. });
  105. })
  106. .catch((err)=>{
  107. console.log(err);
  108. return res.render("error");
  109. });
  110. },
  111. //GET - Renders the merchant setup page for a non-pos client
  112. //Returns:
  113. // ingredients: all ingredients from database
  114. // recipes: null (to signify non-post client)
  115. //Renders merchantSetupPage
  116. merchantSetupNone: function(req, res){
  117. Ingredient.find()
  118. .then((ingredients)=>{
  119. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: null});
  120. })
  121. .catch((err)=>{
  122. console.log(err);
  123. return res.render("error");
  124. });
  125. },
  126. //GET - Renders the recipe display page
  127. //Returns:
  128. // merchant: merchant with recipes and recipe ingredients populated
  129. //Renders recipesPage
  130. displayRecipes: function(req, res){
  131. if(!req.session.user){
  132. return res.render("error");
  133. }
  134. Merchant.findOne({_id: req.session.user})
  135. .populate({
  136. path: "recipes",
  137. model: "Recipe",
  138. populate: {
  139. path: "ingredients.ingredient",
  140. model: "Ingredient"
  141. }
  142. })
  143. .populate("inventory.ingredient")
  144. .then((merchant)=>{
  145. merchant.password = undefined;
  146. return res.render("recipesPage/recipes", {merchant: merchant});
  147. })
  148. .catch((err)=>{
  149. console.log(err);
  150. return res.render("error");
  151. });
  152. }
  153. }