renderer.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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
  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. res.render("inventoryPage/inventory", {merchant: updatedMerchant});
  64. Transaction.create(transactions);
  65. return;
  66. })
  67. .catch((err)=>{
  68. console.log(err);
  69. return res.render("error");
  70. });
  71. })
  72. .catch((err)=>{
  73. console.log(err);
  74. });
  75. }else if(merchant.pos === "none"){
  76. return res.render("inventoryPage/inventory", {merchant: merchant})
  77. }else{
  78. return res.redirect("/");
  79. }
  80. })
  81. .catch((err)=>{
  82. console.log(err);
  83. return res.render("error");
  84. });
  85. },
  86. //GET - Renders the merchant setup page for a clover client
  87. //Returns:
  88. // ingredients: all ingredients from database
  89. // recipes: recipes from the users clover account
  90. //Renders merchantSetupPage
  91. merchantSetupClover: function(req, res){
  92. req.session.posId = "YHVPCQMVB1P81";
  93. Ingredient.find()
  94. .then((ingredients)=>{
  95. axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${req.session.posId}/items?access_token=${token}`)
  96. .then((recipes)=>{
  97. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: recipes.data});
  98. })
  99. .catch((err)=>{
  100. console.log(err);
  101. return res.render("error");
  102. });
  103. })
  104. .catch((err)=>{
  105. console.log(err);
  106. return res.render("error");
  107. });
  108. },
  109. //GET - Renders the merchant setup page for a non-pos client
  110. //Returns:
  111. // ingredients: all ingredients from database
  112. // recipes: null (to signify non-post client)
  113. //Renders merchantSetupPage
  114. merchantSetupNone: function(req, res){
  115. Ingredient.find()
  116. .then((ingredients)=>{
  117. return res.render("merchantSetupPage/merchantSetup", {ingredients: ingredients, recipes: null});
  118. })
  119. .catch((err)=>{
  120. console.log(err);
  121. return res.render("error");
  122. });
  123. },
  124. //GET - Renders the recipe display page
  125. //Returns:
  126. // merchant: merchant with recipes and recipe ingredients populated
  127. //Renders recipesPage
  128. displayRecipes: function(req, res){
  129. if(!req.session.user){
  130. return res.render("error");
  131. }
  132. Merchant.findOne({_id: req.session.user})
  133. .populate({
  134. path: "recipes",
  135. model: "Recipe",
  136. populate: {
  137. path: "ingredients.ingredient",
  138. model: "Ingredient"
  139. }
  140. })
  141. .populate("inventory.ingredient")
  142. .then((merchant)=>{
  143. return res.render("recipesPage/recipes", {merchant: merchant});
  144. })
  145. .catch((err)=>{
  146. console.log(err);
  147. return res.render("error");
  148. });
  149. }
  150. }