Prechádzať zdrojové kódy

Bug fix: recipes were not properly storing the quantities of their ingredients.
Fix conversion errors.

Lee Morgan 5 rokov pred
rodič
commit
bdeb8bb428

+ 2 - 5
controllers/transactionData.js

@@ -63,7 +63,7 @@ module.exports = {
         ingredientUpdates: an object that contains all of the ingredients that
             need to be updated as well as the amount to change. 
             keys = id
-            values = quantity to change in grams
+            values = quantity to change in base unit
     }
     */
     createTransaction: function(req, res){
@@ -73,7 +73,6 @@ module.exports = {
             for(let j = 0; j < res.locals.merchant.inventory.length; j++){
                 if(res.locals.merchant.inventory[j].ingredient._id.toString() === keys[i]){
                     res.locals.merchant.inventory[j].quantity -= req.body.ingredientUpdates[keys[i]];
-
                     break;
                 }
             }
@@ -81,9 +80,7 @@ module.exports = {
 
         res.locals.merchant.save()
             .then((merchant)=>{
-                if(req.body.date === null){
-                    throw "NEW TRANSACTIONS MUST CONTAIN A DATE";
-                }
+                if(req.body.date === null) throw "NEW TRANSACTIONS MUST CONTAIN A DATE";
 
                 return new Transaction({
                     merchant: res.locals.merchant._id,

+ 1 - 1
views/dashboardPage/js/classes/Merchant.js

@@ -472,7 +472,7 @@ class Merchant{
                     let recipe = transaction.recipes[j].recipe;
                     for(let k = 0; k < recipe.ingredients.length; k++){
                         let ingredient = recipe.ingredients[k].ingredient;
-                        let quantity = transaction.recipes[j].quantity * recipe.ingredients[k].quantity;
+                        let quantity = transaction.recipes[j].quantity * recipe.ingredients[k]._quantity;
 
                         this.getIngredient(ingredient.id).updateQuantity(-quantity);
                     }

+ 7 - 14
views/dashboardPage/js/classes/Recipe.js

@@ -13,17 +13,11 @@ class RecipeIngredient{
     }
 
     get quantity(){
-        return this._quantity;
+        return this._quantity * controller.unitMultiplier(controller.getBaseUnit(this._unit), this._unit);
     }
 
     set quantity(quantity){
-        quantity *= controller.unitMultiplier(unit, controller.getBaseUnit(unit))
-        switch(controller.getUnitType(this._ingredient.unit)){
-            case "mass": quantity /= this._ingredient.convert.toMass; break;
-            case "volume": quantity /= this._ingredient.convert.toVolume; break;
-            case "length": quantity /= this._ingredient.convert.toLength; break;
-        }
-        this._quantity += quantity;
+        this._quantity = quantity;
     }
 
     get unit(){
@@ -31,7 +25,7 @@ class RecipeIngredient{
     }
 
     getQuantityDisplay(){
-        return `${this._quantity.toFixed(2)} ${this._unit.toUpperCase()}`;
+        return `${this.quantity.toFixed(2)} ${this._unit.toUpperCase()}`;
     }
 }
 
@@ -61,12 +55,11 @@ class Recipe{
         this._ingredientTotals = {};
 
         for(let i = 0; i < ingredients.length; i++){
-            const ingredient = parent.getIngredient(ingredients[i].ingredient);
-            const recipeIngredient = new RecipeIngredient(
+            let ingredient = parent.getIngredient(ingredients[i].ingredient);
+            let recipeIngredient = new RecipeIngredient(
                 ingredient.ingredient,
                 ingredients[i].quantity,
                 ingredients[i].unit,
-                ingredients[i].baseUnitMultiplier
             );
 
             this._ingredients.push(recipeIngredient);
@@ -156,7 +149,7 @@ class Recipe{
 
         let traverseIngredient = (ingredient, multiplier)=>{
             for(let i = 0; i < ingredient.subIngredients.length; i++){
-                traverseIngredient(ingredient.subIngredients[i].ingredient, multiplier * ingredient.subIngredients[i].quantity);
+                traverseIngredient(ingredient.subIngredients[i].ingredient, multiplier * ingredient.subIngredients[i]._quantity);
             }
 
             if(this._ingredientTotals[ingredient.id] === undefined){
@@ -167,7 +160,7 @@ class Recipe{
         }
 
         for(let i = 0; i < this._ingredients.length; i++){
-            traverseIngredient(this._ingredients[i]._ingredient, this._ingredients[i].quantity);
+            traverseIngredient(this._ingredients[i]._ingredient, this._ingredients[i]._quantity);
         }
     }
 }

+ 3 - 2
views/dashboardPage/js/sidebars/newRecipe.js

@@ -80,10 +80,11 @@ let newRecipe = {
         let ingredients = document.getElementById("newRecipeChosenList").children;
         for(let i = 0; i < ingredients.length; i++){
             let ingredient = ingredients[i].ingredient;
+            let unit = ingredients[i].children[1].children[1].value;
             data.ingredients.push({
                 ingredient: ingredient.id,
-                quantity: ingredients[i].children[1].children[0].value,
-                unit: ingredients[i].children[1].children[1].value
+                quantity: controller.toBase(ingredients[i].children[1].children[0].value, unit),
+                unit: unit
             });
         }
 

+ 6 - 5
views/dashboardPage/js/sidebars/newTransaction.js

@@ -44,12 +44,13 @@ let newTransaction = {
                     quantity: quantity
                 });
 
-                for(let j = 0; j < recipe.ingredients.length; j++){
-                    let ingredient = recipe.ingredients[j];
-                    if(data.ingredientUpdates[ingredient.ingredient.id]){
-                        data.ingredientUpdates[ingredient.ingredient.id] += controller.baseUnit(ingredient.quantity, ingredient.ingredient.unit) * quantity;
+                let keys = Object.keys(recipe.ingredientTotals);
+
+                for(let j = 0; j < keys.length; j++){
+                    if(data.ingredientUpdates[keys[j]] === undefined){
+                        data.ingredientUpdates[keys[j]] = recipe.ingredientTotals[keys[j]] * quantity;
                     }else{
-                        data.ingredientUpdates[ingredient.ingredient.id] = controller.baseUnit(ingredient.quantity, ingredient.ingredient.unit) * quantity;
+                        data.ingredientUpdates[keys[j]] += recipe.ingredientTotals[keys[j]] * quantity
                     }
                 }
             }else if(quantity < 0){