otherData.js 6.4 KB

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