otherData.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. const bcrypt = require("bcryptjs");
  2. const axios = require("axios");
  3. const Error = require("../models/error");
  4. const NonPosTransaction = require("../models/nonPosTransaction");
  5. const Merchant = require("../models/merchant");
  6. const Purchase = require("../models/purchase");
  7. module.exports = {
  8. //POST - Update non-pos merchant inventory and create a transaction
  9. //Inputs:
  10. // recipesSold: list of recipes sold and how much (recipe._id and quantity)
  11. //Returns:
  12. // merchant.inventory: entire merchant inventory after being updated
  13. createTransaction: function(req, res){
  14. if(!req.session.user){
  15. res.session.error = "Must be logged in to do that";
  16. return res.redirect("/");
  17. }
  18. let transaction = new NonPosTransaction({
  19. date: Date.now(),
  20. author: "None",
  21. merchant: req.session.user,
  22. recipes: req.body
  23. });
  24. //Calculate all ingredients used, store to list
  25. Merchant.findOne({_id: req.session.user})
  26. .populate("recipes")
  27. .then((merchant)=>{
  28. for(let reqRecipe of req.body){
  29. let merchRecipe = merchant.recipes.find(r => r._id.toString() === reqRecipe.id);
  30. for(let recipeIngredient of merchRecipe.ingredients){
  31. let merchInvIngredient = merchant.inventory.find(i => i.ingredient.toString() === recipeIngredient.ingredient.toString());
  32. merchInvIngredient.quantity -= recipeIngredient.quantity * reqRecipe.quantity;
  33. }
  34. }
  35. merchant.save()
  36. .then((merchant)=>{
  37. res.json({});
  38. })
  39. .catch((err)=>{
  40. return res.json("Error: unable to save user data");
  41. });
  42. })
  43. .catch((err)=>{
  44. return res.json("Error: unable to retrieve user data");
  45. });
  46. transaction.save()
  47. .then((transaction)=>{
  48. return;
  49. })
  50. .catch((err)=>{});
  51. },
  52. //POST - Creates a new purchase for a merchant
  53. //Inputs:
  54. // req.body: list of purchases (ingredient id and quantity)
  55. createPurchase: function(req, res){
  56. if(!req.session.user){
  57. req.session.error = "Must be logged in to do that";
  58. return res.redirect("/");
  59. }
  60. Merchant.findOne({_id: req.session.user})
  61. .then((merchant)=>{
  62. for(let purchase of req.body){
  63. let merchantIngredient = merchant.inventory.find(i => i.ingredient._id.toString() === purchase.ingredient);
  64. merchantIngredient.quantity += Number(purchase.quantity);
  65. }
  66. merchant.save()
  67. .then((merchant)=>{
  68. res.json({});
  69. })
  70. .catch((err)=>{
  71. return res.json("Error: Unable to save data");
  72. });
  73. })
  74. .catch((err)=>{
  75. return res.json("Error: Unable to retrieve user data");
  76. });
  77. let purchase = new Purchase({
  78. merchant: req.session.user,
  79. date: Date.now(),
  80. ingredients: req.body
  81. });
  82. purchase.save().catch((err)=>{});
  83. },
  84. //POST - logs the user in
  85. //Inputs:
  86. // req.body.email
  87. // req.body.password
  88. //Redirects to "/inventory" on success
  89. login: function(req, res){
  90. Merchant.findOne({email: req.body.email.toLowerCase()})
  91. .then((merchant)=>{
  92. if(merchant){
  93. bcrypt.compare(req.body.password, merchant.password, (err, result)=>{
  94. if(result){
  95. req.session.user = merchant._id;
  96. return res.redirect("/inventory");
  97. }else{
  98. req.session.error = "Invalid email or password";
  99. return res.redirect("/");
  100. }
  101. });
  102. }else{
  103. req.session.error = "Invalid email or password";
  104. return res.redirect("/");
  105. }
  106. })
  107. .catch((err)=>{
  108. req.session.error = "There was an error and your data could not be retrieved";
  109. return res.redirect("/");
  110. });
  111. },
  112. //GET - logs the user out
  113. //Redirects to "/"
  114. logout: function(req, res){
  115. req.session.user = undefined;
  116. return res.redirect("/");
  117. },
  118. //POST - check an email for uniqueness
  119. //Inputs:
  120. // req.body.email: email to check
  121. //Returns:
  122. // Boolean
  123. checkUniqueEmail: function(req, res){
  124. Merchant.findOne({email: req.body.email})
  125. .then((merchant)=>{
  126. if(merchant){
  127. return res.json(false);
  128. }
  129. return res.json(true);
  130. })
  131. .catch((err)=>{
  132. return res.json("Error: unable to validate email address");
  133. });
  134. },
  135. //GET - Redirects user to Clover OAuth page
  136. clover: function(req, res){
  137. return res.redirect(`${process.env.CLOVER_ADDRESS}/oauth/authorize?client_id=${process.env.SUBLINE_CLOVER_APPID}&redirect_uri=${process.env.SUBLINE_CLOVER_URI}`);
  138. },
  139. //GET - Get access token from clover and redirect to mearchant creation
  140. cloverAuth: function(req, res){
  141. let dataArr = req.url.slice(req.url.indexOf("?") + 1).split("&");
  142. let authorizationCode = "";
  143. for(let str of dataArr){
  144. if(str.slice(0, str.indexOf("=")) === "merchant_id"){
  145. req.session.merchantId = str.slice(str.indexOf("=") + 1);
  146. }else if(str.slice(0, str.indexOf("=")) === "code"){
  147. authorizationCode = str.slice(str.indexOf("=") + 1);
  148. }
  149. }
  150. axios.get(`${process.env.CLOVER_ADDRESS}/oauth/token?client_id=${process.env.SUBLINE_CLOVER_APPID}&client_secret=${process.env.SUBLINE_CLOVER_APPSECRET}&code=${authorizationCode}`)
  151. .then((response)=>{
  152. req.session.accessToken = response.data.access_token;
  153. return res.redirect("/merchant/new/clover");
  154. })
  155. .catch((err)=>{
  156. req.session.error = "Error: Unable to retrieve data from Clover";
  157. return res.redirect("/");
  158. });
  159. }
  160. }