otherData.js 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. const bcrypt = require("bcryptjs");
  2. const axios = require("axios");
  3. const Merchant = require("../models/merchant");
  4. const Order = require("../models/order");
  5. const Transaction = require("../models/transaction");
  6. module.exports = {
  7. //POST - Creates a new order for a merchant
  8. //Inputs:
  9. // req.body: list of orders (ingredient id and quantity)
  10. createOrder: function(req, res){
  11. if(!req.session.user){
  12. req.session.error = "Must be logged in to do that";
  13. return res.redirect("/");
  14. }
  15. Merchant.findOne({_id: req.session.user})
  16. .then((merchant)=>{
  17. for(let order of req.body){
  18. let merchantIngredient = merchant.inventory.find(i => i.ingredient._id.toString() === order.ingredient);
  19. merchantIngredient.quantity += Number(order.quantity);
  20. }
  21. merchant.save()
  22. .then((merchant)=>{
  23. res.json({});
  24. })
  25. .catch((err)=>{
  26. return res.json("Error: Unable to save data");
  27. });
  28. })
  29. .catch((err)=>{
  30. return res.json("Error: Unable to retrieve user data");
  31. });
  32. let order = new Order({
  33. merchant: req.session.user,
  34. date: Date.now(),
  35. ingredients: req.body
  36. });
  37. order.save().catch((err)=>{});
  38. },
  39. //POST - logs the user in
  40. //Inputs:
  41. // req.body.email
  42. // req.body.password
  43. //Redirects to "/dashboard" on success
  44. login: function(req, res){
  45. Merchant.findOne({email: req.body.email.toLowerCase()})
  46. .then((merchant)=>{
  47. if(merchant){
  48. bcrypt.compare(req.body.password, merchant.password, (err, result)=>{
  49. if(result){
  50. req.session.user = merchant._id;
  51. return res.redirect("/dashboard");
  52. }else{
  53. req.session.error = "Invalid email or password";
  54. return res.redirect("/");
  55. }
  56. });
  57. }else{
  58. req.session.error = "Invalid email or password";
  59. return res.redirect("/");
  60. }
  61. })
  62. .catch((err)=>{
  63. req.session.error = "There was an error and your data could not be retrieved";
  64. return res.redirect("/");
  65. });
  66. },
  67. //GET - logs the user out
  68. //Redirects to "/"
  69. logout: function(req, res){
  70. req.session.user = undefined;
  71. return res.redirect("/");
  72. },
  73. //GET - Redirects user to Clover OAuth page
  74. cloverRedirect: function(req, res){
  75. return res.redirect(`${process.env.CLOVER_ADDRESS}/oauth/authorize?client_id=${process.env.SUBLINE_CLOVER_APPID}&redirect_uri=${process.env.SUBLINE_CLOVER_URI}`);
  76. },
  77. //GET - Get access token from clover and redirect to merchant creation
  78. cloverAuth: function(req, res){
  79. let dataArr = req.url.slice(req.url.indexOf("?") + 1).split("&");
  80. let authorizationCode = "";
  81. let merchantId = "";
  82. for(let str of dataArr){
  83. if(str.slice(0, str.indexOf("=")) === "merchant_id"){
  84. merchantId = str.slice(str.indexOf("=") + 1);
  85. }else if(str.slice(0, str.indexOf("=")) === "code"){
  86. authorizationCode = str.slice(str.indexOf("=") + 1);
  87. }
  88. }
  89. axios.get(`${process.env.CLOVER_ADDRESS}/oauth/token?client_id=${process.env.SUBLINE_CLOVER_APPID}&client_secret=${process.env.SUBLINE_CLOVER_APPSECRET}&code=${authorizationCode}`)
  90. .then((response)=>{
  91. Merchant.findOne({posId: merchantId})
  92. .then((merchant)=>{
  93. if(merchant){
  94. merchant.posAccessToken = response.data.access_token;
  95. merchant.save()
  96. .then((updatedMerchant)=>{
  97. req.session.user = updatedMerchant._id;
  98. return res.redirect("/dashboard");
  99. })
  100. .catch((err)=>{
  101. req.session.error("Error: unable to save critical data. Try again.");
  102. return res.redirect("/")
  103. });
  104. }else{
  105. req.session.merchantId = merchantId;
  106. req.session.accessToken = response.data.access_token;
  107. return res.redirect("/merchant/create/clover");
  108. }
  109. })
  110. .catch((err)=>{
  111. req.session.error = "Error: there was an oopsies";
  112. });
  113. })
  114. .catch((err)=>{
  115. req.session.error = "Error: Unable to retrieve data from Clover";
  116. return res.redirect("/");
  117. });
  118. },
  119. //POST - Gets transactions and orders between 2 dates for a merchant
  120. //Inputs:
  121. // req.body.from = start date
  122. // req.body.to = end date
  123. //Returns:
  124. // transactions = list of transactions between the dates provided
  125. // orders = list of orders between the dates provided
  126. getData: function(req, res){
  127. if(!req.session.user){
  128. req.session.error = "Must be logged in to do that";
  129. return res.redirect("/");
  130. }
  131. let promiseList = [];
  132. for(let i = 0; i < req.body.dates.length; i+=2){
  133. promiseList.push(new Promise((resolve, reject)=>{
  134. Transaction.find({merchant: req.session.user, date: {$gte: req.body.dates[i], $lt: req.body.dates[i+1]}},
  135. {date: 1, recipes: 1, _id: 0},
  136. {sort: {date: 1}})
  137. .then((transactions)=>{
  138. resolve(transactions);
  139. })
  140. .catch((err)=>{});
  141. }));
  142. promiseList.push(new Promise((resolve, reject)=>{
  143. Order.find({merchant: req.session.user, date: {$gte: req.body.dates[i], $lt: req.body.dates[i+1]}},
  144. {date: 1, ingredients: 1, _id: 0},
  145. {sort: {date: 1}})
  146. .then((orders)=>{
  147. resolve(orders);
  148. })
  149. .catch((err)=>{})
  150. }));
  151. }
  152. Promise.all(promiseList)
  153. .then((response)=>{
  154. let newList = [];
  155. for(let i = 0; i < response.length; i+=2){
  156. newList.push({
  157. transactions: response[i],
  158. orders: response[i+1]
  159. });
  160. }
  161. return res.json(newList);
  162. })
  163. .catch((err)=>{
  164. return res.json("Error: unable to retrieve user data");
  165. });
  166. },
  167. resetPassword: function(req, res){
  168. Merchant.findOne({password: req.body.hash})
  169. .then((merchant)=>{
  170. if(merchant){
  171. let salt = bcrypt.genSaltSync(10);
  172. let hash = bcrypt.hashSync(req.body.pass, salt);
  173. merchant.password = hash;
  174. return merchant.save();
  175. }else{
  176. req.session.error = "Error: unable to retrieve merchant data";
  177. return res.redirect("/");
  178. }
  179. })
  180. .then((merchant)=>{
  181. req.session.error = "Password successfully reset. Please log in";
  182. return res.redirect("/");
  183. })
  184. .catch((err)=>{});
  185. }
  186. }