Kaynağa Gözat

Update promises in the updating of clover recipes. Fix bug in frontend updating of recipes

Lee Morgan 5 yıl önce
ebeveyn
işleme
fd85ea1d32

+ 41 - 54
controllers/recipeData.js

@@ -153,69 +153,56 @@ module.exports = {
             return res.redirect("/");
         }
 
+        let merchant = {};
+        let newRecipes = [];
+        let deletedRecipes = []
         Merchant.findOne({_id: req.session.user})
             .populate("recipes")
-            .then((merchant)=>{
-                axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${merchant.posAccessToken}`)
-                    .then((result)=>{
-                        let deletedRecipes = merchant.recipes.slice();
-                        for(let i = 0; i < result.data.elements.length; i++){
-                            for(let j = 0; j < deletedRecipes.length; j++){
-                                if(result.data.elements[i].id === deletedRecipes[j].posId){
-                                    result.data.elements.splice(i, 1);
-                                    deletedRecipes.splice(j, 1);
-                                    i--;
-                                    break;
-                                }
-                            }
+            .then((response)=>{
+                merchant = response;
+                return axios.get(`https://apisandbox.dev.clover.com/v3/merchants/${merchant.posId}/items?access_token=${merchant.posAccessToken}`);
+            })
+            .then((result)=>{
+                deletedRecipes = merchant.recipes.slice();
+                for(let i = 0; i < result.data.elements.length; i++){
+                    for(let j = 0; j < deletedRecipes.length; j++){
+                        if(result.data.elements[i].id === deletedRecipes[j].posId){
+                            result.data.elements.splice(i, 1);
+                            deletedRecipes.splice(j, 1);
+                            i--;
+                            break;
                         }
+                    }
+                }
 
-                        for(let i = 0; i < deletedRecipes.length; i++){
-                            for(let j = 0; j < merchant.recipes.length; j++){
-                                if(deletedRecipes[i]._id === merchant.recipes[j]._id){
-                                    merchant.recipes.splice(j, 1);
-                                    break;
-                                }
-                            }
+                for(let i = 0; i < deletedRecipes.length; i++){
+                    for(let j = 0; j < merchant.recipes.length; j++){
+                        if(deletedRecipes[i]._id === merchant.recipes[j]._id){
+                            merchant.recipes.splice(j, 1);
+                            break;
                         }
+                    }
+                }
 
-                        let newRecipes = []
-                        for(let i = 0; i < result.data.elements.length; i++){
-                            let newRecipe = new Recipe({
-                                posId: result.data.elements[i].id,
-                                merchant: merchant._id,
-                                name: result.data.elements[i].name,
-                                ingredients: [],
-                                price: result.data.elements[i].price
-                            });
+                for(let i = 0; i < result.data.elements.length; i++){
+                    let newRecipe = new Recipe({
+                        posId: result.data.elements[i].id,
+                        merchant: merchant._id,
+                        name: result.data.elements[i].name,
+                        ingredients: [],
+                        price: result.data.elements[i].price
+                    });
 
-                            merchant.recipes.push(newRecipe);
-                            newRecipes.push(newRecipe);
-                        }
+                    merchant.recipes.push(newRecipe);
+                    newRecipes.push(newRecipe);
+                }
 
-                        Recipe.create(newRecipes)
-                            .catch((err)=>{
-                                return res.json("ERROR: UNABLE TO SAVE RECIPES");
-                            });
+                Recipe.create(newRecipes).catch((err)=>{});
 
-                        merchant.save()
-                            .then((newMerchant)=>{
-                                newMerchant.populate("recipes.ingredients.ingredient").execPopulate()
-                                    .then((newestMerchant)=>{
-                                        merchant.password = undefined;
-                                        return res.json({new: newRecipes, removed: deletedRecipes});
-                                    })
-                                    .catch((err)=>{
-                                        return res.json("ERROR: UNABLE TO RETRIEVE DATA");
-                                    });
-                            })
-                            .catch((err)=>{
-                                return res.json("ERROR: UNABLE TO SAVE CHANGES FROM CLOVER");
-                            });
-                    })
-                    .catch((err)=>{
-                        return res.json("ERROR: UNABLE TO RETRIEVE DATA FROM CLOVER");
-                    });
+                return merchant.save();
+            })
+            .then((newMerchant)=>{
+                return res.json({new: newRecipes, removed: deletedRecipes});
             })
             .catch((err)=>{
                 return res.json("ERROR: UNABLE TO RETRIEVE MERCHANT DATA");

+ 25 - 25
views/dashboardPage/bundle.js

@@ -1007,9 +1007,9 @@ class RecipeIngredient{
     }
 
     get quantity(){
-        // if(this._ingredient.specialUnit === "bottle"){
-        //     return this._quantity / this._ingredient.unitSize;
-        // }
+        if(this._ingredient.specialUnit === "bottle"){
+            return this._quantity / this._ingredient.unitSize;
+        }
 
         switch(this._ingredient.unit){
             case "g":return this._quantity;
@@ -1112,9 +1112,6 @@ class Recipe{
 
             this._ingredients.push(recipeIngredient);
         }
-
-        this._parent.modules.recipeBook.isPopulated = false;
-        this._parent.modules.analytics.isPopulated = false;
     }
 
     get id(){
@@ -3456,28 +3453,31 @@ let recipeBook = {
         })
             .then(response => response.json())
             .then((response)=>{
-                for(let i = 0; i < response.new.length; i++){
-                    const recipe = new Recipe(
-                        response.new[i]._id,
-                        response.new[i].name,
-                        response.new[i].price,
-                        merchant,
-                        []
-                    );
+                if(typeof(response) === "string"){
+                    banner.createError(response);
+                }else{
+                    for(let i = 0; i < response.new.length; i++){
+                        const recipe = new Recipe(
+                            response.new[i]._id,
+                            response.new[i].name,
+                            response.new[i].price,
+                            merchant,
+                            []
+                        );
 
-                    merchant.addRecipe(recipe);
-                }
+                        merchant.addRecipe(recipe);
+                    }
 
-                for(let i = 0; i < response.removed.length; i++){
-                    const recipe = new Recipe(
-                        response.removed[i]._id,
-                        response.removed[i].name,
-                        response.removed[i].price,
-                        merchant,
-                        []
-                    );
+                    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){
+                                merchant.removeRecipe(merchant.recipes[j]);
+                                break;
+                            }
+                        }
+                    }
 
-                    merchant.removeRecipe(recipe);
+                    this.display();
                 }
             })
             .catch((err)=>{

+ 3 - 6
views/dashboardPage/js/Recipe.js

@@ -13,9 +13,9 @@ class RecipeIngredient{
     }
 
     get quantity(){
-        // if(this._ingredient.specialUnit === "bottle"){
-        //     return this._quantity / this._ingredient.unitSize;
-        // }
+        if(this._ingredient.specialUnit === "bottle"){
+            return this._quantity / this._ingredient.unitSize;
+        }
 
         switch(this._ingredient.unit){
             case "g":return this._quantity;
@@ -118,9 +118,6 @@ class Recipe{
 
             this._ingredients.push(recipeIngredient);
         }
-
-        this._parent.modules.recipeBook.isPopulated = false;
-        this._parent.modules.analytics.isPopulated = false;
     }
 
     get id(){

+ 25 - 22
views/dashboardPage/js/recipeBook.js

@@ -84,28 +84,31 @@ let recipeBook = {
         })
             .then(response => response.json())
             .then((response)=>{
-                for(let i = 0; i < response.new.length; i++){
-                    const recipe = new Recipe(
-                        response.new[i]._id,
-                        response.new[i].name,
-                        response.new[i].price,
-                        merchant,
-                        []
-                    );
-
-                    merchant.addRecipe(recipe);
-                }
-
-                for(let i = 0; i < response.removed.length; i++){
-                    const recipe = new Recipe(
-                        response.removed[i]._id,
-                        response.removed[i].name,
-                        response.removed[i].price,
-                        merchant,
-                        []
-                    );
-
-                    merchant.removeRecipe(recipe);
+                if(typeof(response) === "string"){
+                    banner.createError(response);
+                }else{
+                    for(let i = 0; i < response.new.length; i++){
+                        const recipe = new Recipe(
+                            response.new[i]._id,
+                            response.new[i].name,
+                            response.new[i].price,
+                            merchant,
+                            []
+                        );
+
+                        merchant.addRecipe(recipe);
+                    }
+
+                    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){
+                                merchant.removeRecipe(merchant.recipes[j]);
+                                break;
+                            }
+                        }
+                    }
+
+                    this.display();
                 }
             })
             .catch((err)=>{

+ 0 - 1
views/landingPage/register.js

@@ -20,7 +20,6 @@ let registerObj = {
 
     submit: function(){
         event.preventDefault();
-        console.log("something");
 
         let form = document.querySelector("#registerStrand form");
         let checkbox = document.getElementById("checkAgree");