Lee Morgan 6 лет назад
Родитель
Сommit
16efedbf4c
3 измененных файлов с 72 добавлено и 1 удалено
  1. 5 0
      controllers/helper.js
  2. 63 1
      controllers/merchantData.js
  3. 4 0
      controllers/renderer.js

+ 5 - 0
controllers/helper.js

@@ -0,0 +1,5 @@
+module.exports = {
+    getSquareData: function(){
+        
+    }
+}

+ 63 - 1
controllers/merchantData.js

@@ -125,7 +125,69 @@ module.exports = {
     },
 
     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(),
+                    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++){
+                    let recipe = new Recipe({
+                        posId: response.data.objects[i].item_data.variations[0].item_variation_data.item_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)=>{
+                console.log(err);
+                banner.createError("ERROR: UNABLE TO CREATE NEW USER AT THIS TIME");
+            });
     },
 
     //DELETE - removes a single recipe from the merchant

+ 4 - 0
controllers/renderer.js

@@ -5,6 +5,8 @@ const Merchant = require("../models/merchant.js");
 const Transaction = require("../models/transaction.js");
 const Activity = require("../models/activity.js");
 
+const helper = require("./helper.js");
+
 module.exports = {
     /*
     GET - Shows the public landing page
@@ -155,6 +157,8 @@ module.exports = {
                             req.session.error = "ERROR: UNABLE TO RETRIEVE DATA FROM CLOVER";
                             return res.redirect("/");
                         });
+                }else if(merchant.pos === "square"){
+                    promiseArray = helper.getSquareData(merchant);
                 }
 
                 return Promise.all([merchant.save()].concat(promiseArray));