Przeglądaj źródła

Split subingredient modification on backend into its own route.

Lee Morgan 5 lat temu
rodzic
commit
2eae1cfc5c
2 zmienionych plików z 62 dodań i 33 usunięć
  1. 61 33
      controllers/ingredientData.js
  2. 1 0
      routes.js

+ 61 - 33
controllers/ingredientData.js

@@ -76,10 +76,64 @@ module.exports = {
     updateIngredient: function(req, res){
         let popMerchant = res.locals.merchant.populate("inventory.ingredient").execPopulate();
 
-        let stack = [];
         Promise.all([Ingredient.findOne({_id: req.body.id}), popMerchant])
             .then((response)=>{
-                response[0].ingredients = req.body.ingredients;
+                response[0].name = req.body.name;
+                response[0].category = req.body.category;
+                
+                //find and update ingredient on merchant
+                for(let i = 0; i < res.locals.merchant.inventory.length; i++){
+                    if(res.locals.merchant.inventory[i].ingredient.toString() === req.body.id){
+                        res.locals.merchant.inventory[i].defaultUnit = req.body.unit;
+
+                        if(res.locals.merchant.inventory[i].quantity !== req.body.quantity){
+                            new InventoryAdjustment({
+                                date: new Date(),
+                                merchant: req.session.owner,
+                                ingredient: req.body.id,
+                                quantity: req.body.quantity - res.locals.merchant.inventory[i].quantity
+                            }).save().catch(()=>{});
+
+                            res.locals.merchant.inventory[i].quantity = req.body.quantity;
+                        }
+                        
+                        break;
+                    }
+                }
+                return Promise.all([response[0].save(), res.locals.merchant.save()])
+            })
+            .then((response)=>{
+                return res.json({
+                    ingredient: response[0],
+                    quantity: req.body.quantity,
+                    defaultUnit: req.body.unit
+                });
+            })
+            .catch((err)=>{
+                if(err.name === "ValidationError"){
+                    return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
+                }
+                return res.json("ERROR: UNABLE TO UPDATE DATA");
+            });
+    },
+
+    /*
+    PUT: updates subingredients on an ingredient
+    req.body = {
+        id: String (top-level ingredient id),
+        ingredients: {
+            ingredient: String (id)
+            quantity: Number
+        }
+    }
+    response = Ingredient
+    error response = '$' delimited String
+    */
+    updateSubIngredients: function(req, res){
+        let stack = [];
+        Ingredient.findOne({_id: req.body.id})
+            .then((response)=>{
+                response.ingredients = req.body.ingredients;
 
                 // Check ingredients for circular references
                 let isCircular = (ingredient, original)=>{
@@ -106,39 +160,16 @@ module.exports = {
                             let ingredient = res.locals.merchant.inventory[j].ingredient;
                             stack = [ingredient];
                             if(ingredient._id.toString() === req.body.id) throw "circular";
-                            if(isCircular(ingredient, response[0]) === true) throw "circular";
+                            if(isCircular(ingredient, response) === true) throw "circular";
                             break;
                         }
                     }
                 }
 
-                //find and update ingredient on merchant
-                for(let i = 0; i < res.locals.merchant.inventory.length; i++){
-                    if(res.locals.merchant.inventory[i].ingredient.toString() === req.body.id){
-                        res.locals.merchant.inventory[i].defaultUnit = req.body.unit;
-
-                        if(res.locals.merchant.inventory[i].quantity !== req.body.quantity){
-                            new InventoryAdjustment({
-                                date: new Date(),
-                                merchant: req.session.owner,
-                                ingredient: req.body.id,
-                                quantity: req.body.quantity - res.locals.merchant.inventory[i].quantity
-                            }).save().catch(()=>{});
-
-                            res.locals.merchant.inventory[i].quantity = req.body.quantity;
-                        }
-                        
-                        break;
-                    }
-                }
-                return Promise.all([response[0].save(), res.locals.merchant.save()])
+                return response.save();
             })
-            .then((response)=>{
-                return res.json({
-                    ingredient: response[0],
-                    quantity: req.body.quantity,
-                    defaultUnit: req.body.unit
-                });
+            .then((ingredient)=>{
+                return res.json(ingredient);
             })
             .catch((err)=>{
                 if(err === "circular"){
@@ -158,10 +189,7 @@ module.exports = {
                     
                     return res.json(string);
                 }
-                if(err.name === "ValidationError"){
-                    return res.json(err.errors[Object.keys(err.errors)[0]].properties.message);
-                }
-                return res.json("ERROR: UNABLE TO UPDATE DATA");
+                console.log(err);
             });
     },
 

+ 1 - 0
routes.js

@@ -37,6 +37,7 @@ module.exports = function(app){
     //Ingredients
     app.post("/ingredients/create", session, ingredientData.createIngredient);  //also adds to merchant
     app.put("/ingredients/update", session, ingredientData.updateIngredient);
+    app.put("/ingredients/subingredients", session, ingredientData.updateSubIngredients);
     app.post("/ingredients/create/spreadsheet", session, upload.single("ingredients"), ingredientData.createFromSpreadsheet);
     app.get("/ingredients/download/spreadsheet", session, ingredientData.spreadsheetTemplate);
     app.delete("/ingredients/remove/:id", session, ingredientData.removeIngredient);