Просмотр исходного кода

Ad functionality for adding new ingredients to the merchant

Lee Morgan 6 лет назад
Родитель
Сommit
329db46a7d

+ 11 - 13
controllers/merchantData.js

@@ -158,7 +158,10 @@ module.exports = {
 
     /*
     //POST - Adds an ingredient to merchant's inventory
-    req.body = [ingredient objects]
+    req.body = [{
+        id: ingredient id,
+        quantity: quantity of ingredient for the merchant
+    }]
     */
     addMerchantIngredient: function(req, res){
         if(!req.session.user){
@@ -166,30 +169,25 @@ module.exports = {
             return res.redirect("/");
         }
 
+
         Merchant.findOne({_id: req.session.user})
             .then((merchant)=>{
-                for(let ingredient of req.body){
-                    for(let item of merchant.inventory){
-                        if(item.ingredient.toString() === ingredient.ingredient._id){
+                for(let i = 0; i < req.body.length; i++){
+                    for(let j = 0; j < merchant.inventory.length; j++){
+                        if(merchant.inventory[j].ingredient.toString() === req.body[i].id){
                             return res.json("Error: Duplicate ingredient detected");
                         }
                     }
                     
                     merchant.inventory.push({
-                        ingredient: ingredient.ingredient._id,
-                        quantity: ingredient.quantity
+                        ingredient: req.body[i].id,
+                        quantity: req.body[i].quantity
                     });
                 }
 
                 merchant.save()
                     .then((newMerchant)=>{
-                        newMerchant.populate("inventory.ingredient", (err)=>{
-                            if(err){
-                                return res.json("Warning: refresh page to view updates");
-                            }else{
-                                return res.json({});
-                            }
-                        });
+                        return res.json({});
                     })
                     .catch((err)=>{
                         return res.json("Error: unable to save new ingredient");

+ 1 - 1
routes.js

@@ -17,7 +17,7 @@ module.exports = function(app){
     app.post("/merchant/create/none", merchantData.createMerchantNone);
     app.get("/merchant/create/clover", merchantData.createMerchantClover);
     app.delete("/merchant/recipes/remove/:id", merchantData.removeRecipe);
-    app.put("/merchant/ingredients/add", merchantData.addMerchantIngredient);
+    app.post("/merchant/ingredients/add", merchantData.addMerchantIngredient);
     app.delete("/merchant/ingredients/remove/:id", merchantData.removeMerchantIngredient);
     app.put("/merchant/ingredients/update", merchantData.updateMerchantIngredient);
     app.post("/merchant/password", merchantData.updatePassword);

+ 1 - 1
views/dashboardPage/components/addIngredients.ejs

@@ -25,7 +25,7 @@
     <div class="buttonBox">
         <button class="button addIngredientsBtn" onclick="newIngredientComp.display()">Create New</button>
 
-        <button class="button addIngredientsBtn" onclick="addIngredientsComp.submitAddIngredients()">Add</button>
+        <button class="button addIngredientsBtn" onclick="addIngredientsComp.submit()">Add</button>
     </div>
 
     <template id="addIngredientsCategory">

+ 47 - 46
views/dashboardPage/components/components.js

@@ -447,52 +447,6 @@ let addIngredientsComp = {
         }
     },
 
-    // submitAddIngredients: function(){
-    //     let addIngredients = [];
-
-    //     for(let i = 0; i < this.addIngredientsDiv.length; i++){
-    //         let ingredient = this.addIngredientsDiv[i];
-
-    //         if(ingredient.children[1].value !== ""){
-    //             if(!validator.ingredientQuantity(ingredient.children[1].value)){
-    //                 return;
-    //             }
-
-    //             addIngredients.push({
-    //                 ingredient: {
-    //                     _id: ingredient._id,
-    //                     name: ingredient._name,
-    //                     category: ingredient._category,
-    //                     unit: ingredient._unit
-    //                 },
-    //                 quantity: ingredient.children[1].value,
-    //             });
-    //         }
-    //     }
-
-    //     if(addIngredients.length > 0){
-    //         fetch("/merchant/ingredients/add", {
-    //             method: "PUT",
-    //             headers: {
-    //                 "Content-Type": "application/json;charset=utf-8"
-    //             },
-    //             body: JSON.stringify(addIngredients)
-    //         })
-    //             .then((response) => response.json())
-    //             .then((response)=>{
-    //                 if(typeof(response) === "string"){
-    //                     banner.createError(response);
-    //                 }else{
-    //                     banner.createNotification("Ingredients added");
-    //                     updateInventory(addIngredients);
-    //                 }
-    //             })
-    //             .catch((err)=>{
-    //                 banner.createError("Unable to update data.  Please refresh the page");
-    //             });
-    //     }
-    // },
-
     addOne: function(element){
         element.parentElement.removeChild(element);
         document.getElementById("myIngredients").appendChild(element);
@@ -535,6 +489,53 @@ let addIngredientsComp = {
             unit: element._unit
         });
         this.populateAddIngredients();
+    },
+
+    submit: function(){
+        let ingredients = document.getElementById("myIngredients").children;
+        let added = [];
+
+        for(let i = 0; i < ingredients.length; i++){
+            if(ingredients[i].children[1].value === ""){
+                banner.createError("Please enter a quantity for each ingredient you want to add to your inventory");
+                return;
+            }
+
+            let ingredient = {
+                id: ingredients[i]._id,
+                quantity: ingredients[i].children[1].value,
+                quantityChange: ingredients[i].children[1].value,
+                name: ingredients[i]._name,
+                category: ingredients[i]._category,
+                unit: ingredients[i]._unit
+            }
+
+            if(validator.ingredientQuantity(ingredient.quantity)){
+                added.push(ingredient);
+            }else{
+                return;
+            }
+        }
+
+        fetch("/merchant/ingredients/add", {
+            method: "POST",
+            headers: {
+                "Content-Type": "application/json;charset=utf-8"
+            },
+            body: JSON.stringify(added)
+        })
+            .then((response)=>{
+                if(typeof(response) === "string"){
+                    banner.createError(response);
+                }else{
+                    updateInventory(added);
+                    banner.createNotification("All ingredients added successfully");
+                }
+            })
+            .catch((err)=>{
+                console.log(err);
+                banner.createError("Something went wrong.  Try refreshing the page");
+            });
     }
 }
 

+ 10 - 1
views/dashboardPage/controller.js

@@ -53,7 +53,16 @@ let updateInventory = (ingredients, remove = false)=>{
         }
 
         if(isNew){
-            merchant.inventory.push(ingredients[i]);
+            merchant.inventory.push({
+
+                ingredient: {
+                    _id: ingredients[i].id,
+                    category: ingredients[i].category,
+                    name: ingredients[i].name,
+                    unit: ingredients[i].unit
+                },
+                quantity: parseFloat(ingredients[i].quantityChange)
+            });
         }
     }