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

Update ingredient updation for new unit system.

Lee Morgan 5 лет назад
Родитель
Сommit
9011efa13c

+ 19 - 22
controllers/ingredientData.js

@@ -60,41 +60,38 @@ module.exports = {
     /*
     PUT: Updates data for a single ingredient
     req.body = {
-        id: id of the ingredient,
-        name: new name of the ingredient,
-        quantity: new quantity of the unit (in grams),
-        category: new category of the unit,
-        unit: new default unit of the ingredient
+        ingredient: {
+            id: String (id of Ingredient)
+            name: String
+            category: String
+            convert: {
+                toMass: Number
+                toVolume: Number
+                toLength: Number
+            }
+        }
+        quantity: Number
+        unit: String
     }
     response = Ingredient
-    error response = '$' delimited String
     */
     updateIngredient: function(req, res){
         Ingredient.findOne({_id: req.body.id})
-            .then((response)=>{
-                response.name = req.body.name;
-                response.category = req.body.category;
+            .then((ingredient)=>{
+                ingredient.name = req.body.ingredient.name;
+                ingredient.category = req.body.ingredient.category;
+                ingredient.convert = req.body.convert;
                 
                 //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;
-                        }
+                        res.locals.merchant.inventory[i].unit = req.body.unit;
+                        res.locals.merchant.inventory[i].quantity = req.body.quantity;
                         
                         break;
                     }
                 }
-                return Promise.all([response.save(), res.locals.merchant.save()])
+                return Promise.all([ingredient.save(), res.locals.merchant.save()])
             })
             .then((response)=>{
                 return res.json({

+ 25 - 0
views/dashboardPage/js/dashboard.js

@@ -376,6 +376,7 @@ controller = {
         document.getElementById("mobileMenuSelector").onclick = ()=>{this.openMenu()};
     },
 
+    //TOREMOVE
     baseUnit(quantity, unit){
         switch(unit){
             case "g": return quantity;
@@ -424,6 +425,7 @@ controller = {
         }
     },
 
+    //TOREMOVE
     unitType(unit){
         if(["g", "kg", "oz", "lb"].includes(unit)) return "mass";
         if(["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"].includes(unit)) return "volume";
@@ -431,6 +433,29 @@ controller = {
         return "other";
     },
 
+    getBaseUnit(unit){
+        switch(unit){
+            case "g": return "g";
+            case "kg": return "g";
+            case "oz": return "g";
+            case "lb": return "g";
+            case "l": return "l";
+            case "ml": return "l";
+            case "tsp": return "l";
+            case "tbsp": return "l";
+            case "ozfl": return "l";
+            case "cup": return "l";
+            case "pt": return "l";
+            case "qt": return "l";
+            case "gal": return "l";
+            case "mm": return "m";
+            case "cm": return "m";
+            case "m": return "m";
+            case "in": return "m";
+            case "ft": return "m";
+        }
+    },
+
     unitMultiplier(from, to){
         let multiplier = 1;
         

+ 14 - 16
views/dashboardPage/js/sidebars/editIngredient.js

@@ -67,26 +67,24 @@ let editIngredient = {
     },
 
     submit(ingredient){
-        const quantity = parseFloat(document.getElementById("editIngQuantityLabel").children[0].value);
+        let quantity = parseFloat(document.getElementById("editIngQuantityLabel").children[0].value);
+        let unit = document.getElementById("unitButtons").querySelector(".unitActive").innerText.toLowerCase();
 
         let data = {
-            id: ingredient.ingredient.id,
-            name: document.getElementById("editIngName").value,
-            category: document.getElementById("editIngCategory").value,
-            ingredients: [],
-        };
-
-        //Get the measurement unit
-        let units = document.getElementById("unitButtons");
-        for(let i = 0; i < units.children.length; i++){
-            if(units.children[i].classList.contains("unitActive")){
-                data.unit = units.children[i].innerText.toLowerCase();
-                break;
-            }
+            ingredient: {
+                id: ingredient.ingredient.id,
+                name: document.getElementById("editIngName").value,
+                category: document.getElementById("editIngCategory").value,
+                convert: {
+                    toMass: 0,
+                    toVolume: 0,
+                    toLength: 0
+                }
+            },
+            quantity: quantity * controller.unitMultiplier(unit, controller.getBaseUnit(unit)),
+            unit: unit
         }
 
-        data.quantity = controller.baseUnit(quantity, data.unit);
-
         let loader = document.getElementById("loaderContainer");
         loader.style.display = "flex";