Kaynağa Gözat

Remove ingredient from database when removed from merchant

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

+ 30 - 1
controllers/ingredientData.js

@@ -123,5 +123,34 @@ module.exports = {
             .catch((err)=>{
                 return res.json("ERROR: UNABLE TO UPDATE INGREDIENT");
             });
-    }
+    },
+
+    //POST - Removes an ingredient from the merchant's inventory
+    removeIngredient: function(req, res){
+        if(!req.session.user){
+            req.session.error = "MUST BE LOGGED IN TO DO THAT";
+            return res.redirect("/");
+        }
+
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                for(let i = 0; i < merchant.inventory.length; i++){
+                    if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
+                        merchant.inventory.splice(i, 1);
+                        break;
+                    }
+                }
+
+                return merchant.save()
+            })
+            .then((merchant)=>{
+                return Ingredient.deleteOne({_id: req.params.id});
+            })
+            .then((ingredient)=>{
+                return res.json({});
+            })
+            .catch((err)=>{
+                return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
+            });
+    },
 }

+ 0 - 29
controllers/merchantData.js

@@ -237,35 +237,6 @@ module.exports = {
             });
     },
 
-    //POST - Removes an ingredient from the merchant's inventory
-    removeMerchantIngredient: function(req, res){
-        if(!req.session.user){
-            req.session.error = "MUST BE LOGGED IN TO DO THAT";
-            return res.redirect("/");
-        }
-
-        Merchant.findOne({_id: req.session.user})
-            .then((merchant)=>{
-                for(let i = 0; i < merchant.inventory.length; i++){
-                    if(req.params.id === merchant.inventory[i].ingredient._id.toString()){
-                        merchant.inventory.splice(i, 1);
-                        break;
-                    }
-                }
-
-                merchant.save()
-                    .then((merchant)=>{
-                        return res.json({});
-                    })
-                    .catch((err)=>{
-                        return res.json("ERROR: UNABLE TO SAVE USER DATA");
-                    });
-            })
-            .catch((err)=>{
-                return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
-            });
-    },
-
     //PUT - Update the default unit for a single ingredient
     ingredientDefaultUnit: function(req, res){
         if(!req.session.user){

+ 1 - 1
routes.js

@@ -19,7 +19,6 @@ module.exports = function(app){
     app.get("/merchant/create/clover", merchantData.createMerchantClover);
     app.get("/merchant/create/square", merchantData.createMerchantSquare);
     app.delete("/merchant/recipes/remove/:id", merchantData.removeRecipe);
-    app.delete("/merchant/ingredients/remove/:id", merchantData.removeMerchantIngredient);
     app.put("/merchant/ingredients/update/:id/:unit", merchantData.ingredientDefaultUnit);
     app.put("/merchant/ingredients/update", merchantData.updateMerchantIngredient); //also updates some data in ingredients
     app.post("/merchant/password", merchantData.updatePassword);
@@ -28,6 +27,7 @@ module.exports = function(app){
     app.get("/ingredients", ingredientData.getIngredients);
     app.post("/ingredients/create", ingredientData.createIngredient);  //also adds to merchant
     app.put("/ingredients/update", ingredientData.updateIngredient);
+    app.delete("/ingredients/remove/:id", ingredientData.removeIngredient);
 
     //Recipes
     app.post("/recipe/create", recipeData.createRecipe);

+ 4 - 5
views/dashboardPage/bundle.js

@@ -109,11 +109,10 @@ class Merchant{
     /*
     Updates all specified item in the merchant's inventory and updates the page
     If ingredient doesn't exist, add it
-    ingredients = {
+    ingredients = [{
         ingredient: Ingredient object,
         quantity: new quantity,
-        defaultUnit: the default unit to be displayed
-    }
+    }]
     remove = set true if removing
     isOrder = set true if this is coming from an order
     */
@@ -122,7 +121,7 @@ class Merchant{
             let isNew = true;
             for(let j = 0; j < this.ingredients.length; j++){
                 if(this.ingredients[j].ingredient === ingredients[i].ingredient){
-                    if(remove){
+                    if(remove && !isOrder){
                         this.ingredients.splice(j, 1);
                     }else if(!remove && isOrder){
                         this.ingredients[j].quantity += ingredients[i].quantity;
@@ -1573,7 +1572,7 @@ let ingredientDetails = {
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
 
-        fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
+        fetch(`/ingredients/remove/${this.ingredient.ingredient.id}`, {
             method: "DELETE",
         })
             .then((response) => response.json())

+ 1 - 1
views/dashboardPage/js/ingredientDetails.js

@@ -119,7 +119,7 @@ let ingredientDetails = {
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";
 
-        fetch(`/merchant/ingredients/remove/${this.ingredient.ingredient.id}`, {
+        fetch(`/ingredients/remove/${this.ingredient.ingredient.id}`, {
             method: "DELETE",
         })
             .then((response) => response.json())