| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211 |
- const bcrypt = require("bcryptjs");
- const axios = require("axios");
- const Merchant = require("../models/merchant");
- const Order = require("../models/order");
- const Transaction = require("../models/transaction");
- module.exports = {
- //POST - Creates a new order for a merchant
- //Inputs:
- // req.body: list of orders (ingredient id and quantity)
- createOrder: function(req, res){
- if(!req.session.user){
- req.session.error = "Must be logged in to do that";
- return res.redirect("/");
- }
- Merchant.findOne({_id: req.session.user})
- .then((merchant)=>{
- for(let order of req.body){
- let merchantIngredient = merchant.inventory.find(i => i.ingredient._id.toString() === order.ingredient);
- merchantIngredient.quantity += Number(order.quantity);
- }
-
- merchant.save()
- .then((merchant)=>{
- res.json({});
- })
- .catch((err)=>{
- return res.json("Error: Unable to save data");
- });
- })
- .catch((err)=>{
- return res.json("Error: Unable to retrieve user data");
- });
- let order = new Order({
- merchant: req.session.user,
- date: Date.now(),
- ingredients: req.body
- });
- order.save().catch((err)=>{});
- },
- //POST - logs the user in
- //Inputs:
- // req.body.email
- // req.body.password
- //Redirects to "/dashboard" on success
- login: function(req, res){
- Merchant.findOne({email: req.body.email.toLowerCase()})
- .then((merchant)=>{
- if(merchant){
- bcrypt.compare(req.body.password, merchant.password, (err, result)=>{
- if(result){
- req.session.user = merchant._id;
- return res.redirect("/dashboard");
- }else{
- req.session.error = "Invalid email or password";
- return res.redirect("/");
- }
- });
- }else{
- req.session.error = "Invalid email or password";
- return res.redirect("/");
- }
- })
- .catch((err)=>{
- req.session.error = "There was an error and your data could not be retrieved";
- return res.redirect("/");
- });
- },
- //GET - logs the user out
- //Redirects to "/"
- logout: function(req, res){
- req.session.user = undefined;
- return res.redirect("/");
- },
- //GET - Redirects user to Clover OAuth page
- cloverRedirect: function(req, res){
- return res.redirect(`${process.env.CLOVER_ADDRESS}/oauth/authorize?client_id=${process.env.SUBLINE_CLOVER_APPID}&redirect_uri=${process.env.SUBLINE_CLOVER_URI}`);
- },
- //GET - Get access token from clover and redirect to merchant creation
- cloverAuth: function(req, res){
- let dataArr = req.url.slice(req.url.indexOf("?") + 1).split("&");
- let authorizationCode = "";
- let merchantId = "";
- for(let str of dataArr){
- if(str.slice(0, str.indexOf("=")) === "merchant_id"){
- merchantId = str.slice(str.indexOf("=") + 1);
- }else if(str.slice(0, str.indexOf("=")) === "code"){
- authorizationCode = str.slice(str.indexOf("=") + 1);
- }
- }
- axios.get(`${process.env.CLOVER_ADDRESS}/oauth/token?client_id=${process.env.SUBLINE_CLOVER_APPID}&client_secret=${process.env.SUBLINE_CLOVER_APPSECRET}&code=${authorizationCode}`)
- .then((response)=>{
- Merchant.findOne({posId: merchantId})
- .then((merchant)=>{
- if(merchant){
- merchant.posAccessToken = response.data.access_token;
- merchant.save()
- .then((updatedMerchant)=>{
- req.session.user = updatedMerchant._id;
- return res.redirect("/dashboard");
- })
- .catch((err)=>{
- req.session.error("Error: unable to save critical data. Try again.");
- return res.redirect("/")
- });
- }else{
- req.session.merchantId = merchantId;
- req.session.accessToken = response.data.access_token;
- return res.redirect("/merchant/create/clover");
- }
- })
- .catch((err)=>{
- req.session.error = "Error: there was an oopsies";
- });
-
- })
- .catch((err)=>{
- req.session.error = "Error: Unable to retrieve data from Clover";
- return res.redirect("/");
- });
- },
- //POST - Gets transactions and orders between 2 dates for a merchant
- //Inputs:
- // req.body.from = start date
- // req.body.to = end date
- //Returns:
- // transactions = list of transactions between the dates provided
- // orders = list of orders between the dates provided
- getData: function(req, res){
- if(!req.session.user){
- req.session.error = "Must be logged in to do that";
- return res.redirect("/");
- }
- let promiseList = [];
- for(let i = 0; i < req.body.dates.length; i+=2){
- promiseList.push(new Promise((resolve, reject)=>{
- Transaction.find({merchant: req.session.user, date: {$gte: req.body.dates[i], $lt: req.body.dates[i+1]}},
- {date: 1, recipes: 1, _id: 0},
- {sort: {date: 1}})
- .then((transactions)=>{
- resolve(transactions);
- })
- .catch((err)=>{});
- }));
- promiseList.push(new Promise((resolve, reject)=>{
- Order.find({merchant: req.session.user, date: {$gte: req.body.dates[i], $lt: req.body.dates[i+1]}},
- {date: 1, ingredients: 1, _id: 0},
- {sort: {date: 1}})
- .then((orders)=>{
- resolve(orders);
- })
- .catch((err)=>{})
- }));
- }
- Promise.all(promiseList)
- .then((response)=>{
- let newList = [];
- for(let i = 0; i < response.length; i+=2){
- newList.push({
- transactions: response[i],
- orders: response[i+1]
- });
- }
- return res.json(newList);
- })
- .catch((err)=>{
- return res.json("Error: unable to retrieve user data");
- });
- },
- resetPassword: function(req, res){
- Merchant.findOne({password: req.body.hash})
- .then((merchant)=>{
- if(merchant){
- let salt = bcrypt.genSaltSync(10);
- let hash = bcrypt.hashSync(req.body.pass, salt);
- merchant.password = hash;
- return merchant.save();
- }else{
- req.session.error = "Error: unable to retrieve merchant data";
- return res.redirect("/");
- }
- })
- .then((merchant)=>{
- req.session.error = "Password successfully reset. Please log in";
- return res.redirect("/");
- })
- .catch((err)=>{});
- }
- }
|