Browse Source

Change /inventory to /dashboard. Register with clover will log in existing user or create new one if none exists.

Lee Morgan 6 years ago
parent
commit
daa13d4b4e
5 changed files with 30 additions and 13 deletions
  1. 5 4
      controllers/merchantData.js
  2. 22 6
      controllers/otherData.js
  3. 1 1
      controllers/renderer.js
  4. 1 1
      routes.js
  5. 1 1
      views/dataPage/data.ejs

+ 5 - 4
controllers/merchantData.js

@@ -13,7 +13,7 @@ module.exports = {
     //  req.body.email: registration email
     //  req.body.password: password
     //  req.body.confirmPassword: confirmation password
-    //Redirects to /inventory
+    //Redirects to /dashboard
     createMerchantNone: function(req, res){
         if(req.body.password === req.body.confirmPassword){
             var salt = bcrypt.genSaltSync(10);
@@ -35,7 +35,7 @@ module.exports = {
                 .then((merchant)=>{
                     req.session.user = merchant._id;
 
-                    return res.redirect("/inventory");
+                    return res.redirect("/dashboard");
                 })
                 .catch((err)=>{
                     req.session.error = "Error: Unable to create account at this time";
@@ -52,13 +52,14 @@ module.exports = {
     //POST - Creates a Clover merchant from all entered data
     //Inputs:
     //  req.body.data: All data from frontend in form of merchant model
-    //Redirect to "/inventory"
+    //Redirect to /dashboard
     createMerchantClover: async function(req, res){
         axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${req.session.merchantId}?access_token=${req.session.accessToken}`)
             .then((response)=>{
                 let merchant = new Merchant({
                     name: response.data.name,
                     pos: "clover",
+                    posId: req.session.merchantId,
                     posAccessToken: req.session.accessToken,
                     lastUpdatedTime: Date.now(),
                     createdAt: Date.now(),
@@ -70,7 +71,7 @@ module.exports = {
                     .then((newMerchant)=>{
                         req.session.user = newMerchant._id;
 
-                        return res.redirect("/inventory");
+                        return res.redirect("/dashboard");
                     })
                     .catch((err)=>{
                         req.session.error = "Error: unable to save data from Clover";

+ 22 - 6
controllers/otherData.js

@@ -45,7 +45,7 @@ module.exports = {
     //Inputs:
     //  req.body.email
     //  req.body.password
-    //Redirects to "/inventory" on success
+    //Redirects to "/dashboard" on success
     login: function(req, res){
         Merchant.findOne({email: req.body.email.toLowerCase()})
             .then((merchant)=>{
@@ -53,7 +53,7 @@ module.exports = {
                     bcrypt.compare(req.body.password, merchant.password, (err, result)=>{
                         if(result){
                             req.session.user = merchant._id;
-                            return res.redirect("/inventory");
+                            return res.redirect("/dashboard");
                         }else{
                             req.session.error = "Invalid email or password";
                             return res.redirect("/");
@@ -108,23 +108,39 @@ module.exports = {
     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"){
-                req.session.merchantId = str.slice(str.indexOf("=") + 1);
+                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)=>{
-                req.session.accessToken = response.data.access_token;
-                return res.redirect("/merchant/create/clover");
+                Merchant.findOne({posId: merchantId})
+                    .then((merchant)=>{
+                        if(merchant){
+                            req.session.user = merchant._id;
+                            return res.redirect("/dashboard");
+                        }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 a oopsies";
+                    });
+                
             })
             .catch((err)=>{
+                console.log("catch");
                 req.session.error = "Error: Unable to retrieve data from Clover";
                 return res.redirect("/");
             });
+        
     }
 }

+ 1 - 1
controllers/renderer.js

@@ -27,7 +27,7 @@ module.exports = {
     //Returns:
     //  merchant: the logged in merchant
     //Renders inventoryPage
-    displayInventory: function(req, res){
+    displayDashboard: function(req, res){
         if(!req.session.user){
             req.session.error = "You must be logged in to view that page";
             return res.redirect("/");

+ 1 - 1
routes.js

@@ -8,7 +8,7 @@ const recipeData = require("./controllers/recipeData");
 module.exports = function(app){
     //Render page
     app.get("/", renderer.landingPage);
-    app.get("/inventory", renderer.displayInventory);
+    app.get("/dashboard", renderer.displayDashboard);
     app.get("/information", renderer.displayLegal);
     app.get("/data", renderer.displayData);
 

+ 1 - 1
views/dataPage/data.ejs

@@ -20,7 +20,7 @@
             <h2 id="month"></h2>
 
             <div class="buttonBox">
-                <a class="button" href="/inventory">Display Inventory</a>
+                <a class="button" href="/dashboard">Display Inventory</a>
             </div>
             
             <div class="tables">