Explorar el Código

Add ability to update recipes for Square users.y

Lee Morgan hace 5 años
padre
commit
7e2f30f9bf

+ 3 - 0
controllers/recipeData.js

@@ -93,6 +93,9 @@ module.exports = {
         if(res.locals.merchant.pos === "clover"){
             return res.json("YOU MUST EDIT YOUR RECIPES INSIDE CLOVER");
         }
+        if(res.locals.merchant.pos === "square"){
+            return res.json("YOU MUST EDIT YOUR RECIPES INSIDE SQUARE");
+        }
         
         for(let i = 0; i < res.locals.merchant.recipes.length; i++){
             if(res.locals.merchant.recipes[i].toString() === req.params.id){

+ 89 - 2
controllers/squareData.js

@@ -145,7 +145,7 @@ module.exports = {
                 req.session.user = response[1].session.sessionId;
     
                 res.redirect("/dashboard");
-                return;
+
                 let body = {
                     location_ids: [merchant.squareLocation],
                     limit: 10000,
@@ -160,7 +160,6 @@ module.exports = {
                 return axios.post(`${process.env.SQUARE_ADDRESS}/v2/orders/search`, body, options);
             })
             .then(async (response)=>{
-                return;
                 let transactions = [];
 
                 for(let i = 0; i < response.data.orders.length; i++){
@@ -243,5 +242,93 @@ module.exports = {
                 }
                 return res.redirect("/");
             });
+    },
+
+    updateRecipes: function(req, res){
+        let merchant = {};
+        let merchantRecipes = [];
+        let newRecipes = [];
+    
+        res.locals.merchant
+            .populate("recipes")
+            .execPopulate()
+            .then((fetchedMerchant)=>{
+                merchant = fetchedMerchant;
+                return axios.post(`${process.env.SQUARE_ADDRESS}/v2/catalog/search`, {
+                    object_types: ["ITEM"]
+                }, {
+                    headers: {
+                        Authorization: `Bearer ${merchant.posAccessToken}`
+                    }
+                });
+            })
+            .then((response)=>{
+                merchantRecipes = merchant.recipes.slice();
+    
+                
+                for(let i = 0; i < response.data.objects.length; i++){
+                    let itemData = response.data.objects[i].item_data;
+                    for(let j = 0; j < itemData.variations.length; j++){
+                        let isFound = false;
+    
+                        for(let k = 0; k < merchantRecipes.length; k++){
+                            if(itemData.variations[j].id === merchantRecipes[k].posId){
+                                merchantRecipes.splice(k, 1);
+                                k--;
+                                isFound = true;
+                                break;
+                            }
+                        }
+    
+                        if(!isFound){
+                            let newRecipe = new Recipe({
+                                posId: itemData.variations[j].id,
+                                merchant: merchant._id,
+                                name: "",
+                                price: itemData.variations[j].item_variation_data.price_money.amount,
+                                ingredients: []
+                            });
+    
+                            if(itemData.variations.length > 1){
+                                newRecipe.name = `${itemData.name} '${itemData.variations[j].item_variation_data.name}'`;
+                            }else{
+                                newRecipe.name = itemData.name;
+                            }
+    
+                            newRecipes.push(newRecipe);
+                            merchant.recipes.push(newRecipe);
+                        }
+                    }
+                }
+    
+                let ids = [];
+                for(let i = 0; i < merchantRecipes.length; i++){
+                    ids.push(merchantRecipes[i]._id);
+                    for(let j = 0; j < merchant.recipes.length; j++){
+                        if(merchantRecipes[i]._id.toString() === merchant.recipes[j]._id.toString()){
+                            merchant.recipes.splice(j, 1);
+                            j--;
+                            break;
+                        }
+                    }
+                }
+    
+                if(newRecipes.length > 0) Recipe.create(newRecipes);
+                if(merchantRecipes.length > 0) Recipe.deleteMany({_id: {$in: ids}});    
+
+                return merchant.save();
+            })
+            .then((merchant)=>{
+                return res.json({new: newRecipes, removed: merchantRecipes});
+            })
+            .catch((err)=>{
+                if(typeof(err) === "string"){
+                    return res.json(err);
+                }
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
+                }
+                return res.json("ERROR: UNABLE TO RETRIEVE RECIPE DATA FROM SQUARE");
+            });
     }
 }

+ 1 - 0
routes.js

@@ -84,4 +84,5 @@ module.exports = function(app){
     app.get("/squarelogin", squareData.redirect);
     app.get("/squareauth", squareData.authorize);
     app.get("/merchant/create/square", squareData.createMerchant);
+    app.get("/recipes/update/square", session, squareData.updateRecipes);
 }

+ 6 - 5
views/dashboardPage/bundle.js

@@ -3853,7 +3853,7 @@ let recipeBook = {
     posUpdate: function(){
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
-        let url = `/recipe/update/${merchant.pos}`;
+        let url = `/recipes/update/${merchant.pos}`;
 
         fetch(url, {
             method: "GET",
@@ -3866,18 +3866,19 @@ let recipeBook = {
                 if(typeof(response) === "string"){
                     controller.createBanner(response, "error");
                 }else{
+                    let newRecipes = [];
                     for(let i = 0; i < response.new.length; i++){
-                        const recipe = new Recipe(
+                        newRecipes.push(new Recipe(
                             response.new[i]._id,
                             response.new[i].name,
                             response.new[i].price,
                             merchant,
                             []
-                        );
-
-                        merchant.addRecipe(recipe);
+                        ));
                     }
 
+                    merchant.addRecipes(newRecipes);
+
                     for(let i = 0; i < response.removed.length; i++){
                         for(let j = 0; j < merchant.recipes.length; j++){
                             if(merchant.recipes[j].id === response.removed[i]._id){

+ 6 - 5
views/dashboardPage/js/strands/recipeBook.js

@@ -67,7 +67,7 @@ let recipeBook = {
     posUpdate: function(){
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
-        let url = `/recipe/update/${merchant.pos}`;
+        let url = `/recipes/update/${merchant.pos}`;
 
         fetch(url, {
             method: "GET",
@@ -80,18 +80,19 @@ let recipeBook = {
                 if(typeof(response) === "string"){
                     controller.createBanner(response, "error");
                 }else{
+                    let newRecipes = [];
                     for(let i = 0; i < response.new.length; i++){
-                        const recipe = new Recipe(
+                        newRecipes.push(new Recipe(
                             response.new[i]._id,
                             response.new[i].name,
                             response.new[i].price,
                             merchant,
                             []
-                        );
-
-                        merchant.addRecipe(recipe);
+                        ));
                     }
 
+                    merchant.addRecipes(newRecipes);
+
                     for(let i = 0; i < response.removed.length; i++){
                         for(let j = 0; j < merchant.recipes.length; j++){
                             if(merchant.recipes[j].id === response.removed[i]._id){