Przeglądaj źródła

Remove clover and square backend code.
The code has been saved for potential future use.

Lee Morgan 5 lat temu
rodzic
commit
ee25dd58ab
3 zmienionych plików z 1 dodań i 250 usunięć
  1. 0 146
      controllers/merchantData.js
  2. 0 97
      controllers/otherData.js
  3. 1 7
      routes.js

+ 0 - 146
controllers/merchantData.js

@@ -75,152 +75,6 @@ module.exports = {
             });
     },
 
-    /*
-    POST - Creates new Clover merchant
-    Redirects to /dashboard
-    */
-    createMerchantClover: async function(req, res){
-        let merchant = {}
-        axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${req.session.merchantId}?access_token=${req.session.accessToken}`)
-            .then((response)=>{
-                merchant = new Merchant({
-                    name: response.data.name,
-                    pos: "clover",
-                    posId: req.session.merchantId,
-                    posAccessToken: req.session.accessToken,
-                    lastUpdatedTime: Date.now(),
-                    createdAt: Date.now(),
-                    inventory: [],
-                    recipes: []
-                });
-
-                return axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${req.session.merchantId}/items?access_token=${req.session.accessToken}`);
-            })
-            .then((response)=>{
-                let recipes = [];
-                for(let i = 0; i < response.data.elements.length; i++){
-                    let recipe = new Recipe({
-                        posId: response.data.elements[i].id,
-                        merchant: merchant,
-                        name: response.data.elements[i].name,
-                        price: response.data.elements[i].price,
-                        ingredients: []
-                    });
-
-                    recipes.push(recipe);
-                    merchant.recipes.push(recipe);                                
-                }
-
-                Recipe.create(recipes).catch((err)=>{});
-
-                return merchant.save();
-            })
-            .then((newMerchant)=>{
-                req.session.accessToken = undefined;
-                req.session.user = newMerchant._id;
-
-                return res.redirect("/dashboard");
-            })
-            .catch((err)=>{
-                if(typeof(err) === "string"){
-                    req.session.error = err;
-                }else if(err.name === "ValidationError"){
-                    req.session.error = err.errors[Object.keys(err.errors)[0]].properties.message;
-                }else{
-                    req.session.error = "ERROR: UNABLE TO RETRIEVE DATA FROM CLOVER";
-                }
-                
-                return res.redirect("/");
-            });
-    },
-
-    createMerchantSquare: function(req, res){
-        let merchant = {}
-
-        axios.get(`${process.env.SQUARE_ADDRESS}/v2/merchants/${req.session.merchantId}`, {
-            headers: {
-                Authorization: `Bearer ${req.session.accessToken}`
-            }
-        })
-            .then((response)=>{
-                req.session.merchantId = undefined;
-
-                return new Merchant({
-                    name: response.data.merchant.business_name,
-                    pos: "square",
-                    posId: response.data.merchant.id,
-                    posAccessToken: req.session.accessToken,
-                    lastUpdatedTime: new Date(),
-                    createdAt: new Date(),
-                    squareLocation: response.data.merchant.main_location_id,
-                    inventory: [],
-                    recipes: []
-                });
-            })
-            .then((newMerchant)=>{
-                req.session.accessToken = undefined;
-                merchant = newMerchant;
-                
-                return axios.post(`${process.env.SQUARE_ADDRESS}/v2/catalog/search`, {
-                    object_types: ["ITEM"]
-                }, {
-                    headers: {
-                        Authorization: `Bearer ${merchant.posAccessToken}`
-                    }
-                });
-            })
-            .then((response)=>{
-                let recipes = [];
-                
-                for(let i = 0; i < response.data.objects.length; i++){
-                    if(response.data.objects[i].item_data.variations.length > 1){
-                        for(let j = 0; j < response.data.objects[i].item_data.variations.length; j++){
-                            let recipe = new Recipe({
-                                posId: response.data.objects[i].item_data.variations[j].id,
-                                merchant: merchant._id,
-                                name: `${response.data.objects[i].item_data.name} '${response.data.objects[i].item_data.variations[j].item_variation_data.name}'`,
-                                price: response.data.objects[i].item_data.variations[j].item_variation_data.price_money.amount
-                            });
-
-                            recipes.push(recipe);
-                            merchant.recipes.push(recipe);
-                        }
-                    }else{
-                        let recipe = new Recipe({
-                            posId: response.data.objects[i].item_data.variations[0].id,
-                            merchant: merchant._id,
-                            name: response.data.objects[i].item_data.name,
-                            price: response.data.objects[i].item_data.variations[0].item_variation_data.price_money.amount,
-                            ingredients: []
-                        });
-
-                        recipes.push(recipe);
-                        merchant.recipes.push(recipe);
-                    }
-                }
-
-                return Recipe.create(recipes);
-            })
-            .then((recipes)=>{
-                return merchant.save();
-            })
-            .then((merchant)=>{
-                req.session.user = merchant._id;
-
-                return res.redirect("/dashboard");
-            })
-            .catch((err)=>{
-                if(typeof(err) === "string"){
-                    req.session.error = err;
-                }else if(err.name === "ValidationError"){
-                    req.session.error = err.errors[Object.keys(err.errors)[0]].properties.message;
-                }else{
-                    req.session.error = "ERROR: UNABLE TO CREATE NEW USER";
-                }
-                return res.redirect("/");
-            });
-    },
-
     /*
     POST - Update the quantity for a merchant inventory item
     req.body = [{

+ 0 - 97
controllers/otherData.js

@@ -45,102 +45,5 @@ module.exports = {
         req.session.user = null;
 
         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 - Redirects user to Square OAuth page
-    squareRedirect: function(req, res){
-        return res.redirect(`${process.env.SQUARE_ADDRESS}/oauth2/authorize?client_id=${process.env.SUBLINE_SQUARE_APPID}&scope=INVENTORY_READ+ITEMS_READ+MERCHANT_PROFILE_READ+ORDERS_READ+PAYMENTS_READ`);
-    },
-
-    //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 i = 0; i < dataArr.length; i++){
-            if(dataArr[i].slice(0, dataArr[i].indexOf("=")) === "merchant_id"){
-                merchantId = dataArr[i].slice(dataArr[i].indexOf("=") + 1);
-            }else if(dataArr[i].slice(0, dataArr[i].indexOf("=")) === "code"){
-                authorizationCode = dataArr[i].slice(dataArr[i].indexOf("=") + 1);
-            }
-        }
-
-        let response = {}
-        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((data)=>{
-                response = data;
-                
-                return 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 CREATE MERCHANT";
-                            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: UNABLE TO RETRIEVE DATA FROM CLOVER";
-                return res.redirect("/");
-            });
-    },
-
-    squareAuth: function(req, res){
-        const code = req.url.slice(req.url.indexOf("code=") + 5, req.url.indexOf("&"));
-        const url = `${process.env.SQUARE_ADDRESS}/oauth2/token?`;
-        let data = {
-            client_id: process.env.SUBLINE_SQUARE_APPID,
-            client_secret: process.env.SUBLINE_SQUARE_APPSECRET,
-            grant_type: "authorization_code",
-            code: code
-        };
-
-        axios.post(url, data)
-            .then((response)=>{
-                data = response.data;
-                return Merchant.findOne({posId: data.merchant_id});
-            })
-            .then((merchant)=>{
-                if(merchant){
-                    merchant.posAccessToken = data.access_token;
-
-                    return merchant.save()
-                        .then((merchant)=>{
-                            req.session.user = merchant._id;
-                            return res.redirect("/dashboard");
-                        })
-                        .catch((err)=>{
-                            req.session.error = "ERROR: UNABLE TO CREATE NEW USER";
-                            return res.redirect("/");
-                        })
-                }else{
-                    req.session.merchantId = data.merchant_id;
-                    req.session.accessToken = data.access_token;
-
-                    return res.redirect("/merchant/create/square");
-                }
-            })
-            .catch((err)=>{
-                req.session.error = "ERROR: UNABLE TO RETRIEVE DATA FROM SQUARE";
-                return res.redirect("/");
-            });
     }
 }

+ 1 - 7
routes.js

@@ -21,8 +21,6 @@ module.exports = function(app){
 
     //Merchant
     app.post("/merchant/create/none", merchantData.createMerchantNone);
-    app.get("/merchant/create/clover", merchantData.createMerchantClover);
-    app.get("/merchant/create/square", merchantData.createMerchantSquare);
     app.put("/merchant/ingredients/update", session, merchantData.updateMerchantIngredient); //also updates some data in ingredients
     app.post("/merchant/password", merchantData.updatePassword); //TODO: change to work with session
 
@@ -58,11 +56,7 @@ module.exports = function(app){
     //Other
     app.post("/login", otherData.login);
     app.get("/logout", otherData.logout);
-    app.get("/cloverlogin", otherData.cloverRedirect);
-    app.get("/squarelogin", otherData.squareRedirect);
-    app.get("/cloverauth*", otherData.cloverAuth);
-    app.get("/squareauth", otherData.squareAuth);
-
+    
     //Information Pages
     app.get("/privacy", informationPages.privacy);
     app.get("/terms", informationPages.terms);