otherData.js 6.8 KB

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