فهرست منبع

Convert add ingredients units upon creation

Lee Morgan 6 سال پیش
والد
کامیت
a8bb698907
3فایلهای تغییر یافته به همراه57 افزوده شده و 8 حذف شده
  1. 3 1
      controllers/merchantData.js
  2. 41 2
      views/dashboardPage/Merchant.js
  3. 13 5
      views/dashboardPage/sidebars/sidebars.js

+ 3 - 1
controllers/merchantData.js

@@ -162,6 +162,7 @@ module.exports = {
     req.body = [{
         id: ingredient id,
         quantity: quantity of ingredient for the merchant
+        defaultUnit: default unit of measurement to display
     }]
     */
     addMerchantIngredient: function(req, res){
@@ -190,7 +191,8 @@ module.exports = {
                     
                     merchant.inventory.push({
                         ingredient: req.body[i].id,
-                        quantity: req.body[i].quantity
+                        quantity: req.body[i].quantity,
+                        defaultUnit: req.body[i].defaultUnit
                     });
                 }
 

+ 41 - 2
views/dashboardPage/Merchant.js

@@ -193,7 +193,8 @@ class Merchant{
     If ingredient doesn't exist, add it
     ingredients = {
         ingredient: Ingredient object,
-        quantity: new quantity
+        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
@@ -219,7 +220,8 @@ class Merchant{
             if(isNew){
                 merchant.ingredients.push({
                     ingredient: ingredients[i].ingredient,
-                    quantity: parseFloat(ingredients[i].quantity)
+                    quantity: parseFloat(ingredients[i].quantity),
+                    defaultUnit: ingredients[i].defaultUnit
                 });
             }
         }
@@ -560,4 +562,41 @@ class Merchant{
 
         return recipes;
     }
+}
+
+let convertToMain = (unit, quantity)=>{
+    let converted = 0;
+
+    if(merchant.units.mass.includes(unit)){
+        switch(unit){
+            case "g": break;
+            case "kg": converted = quantity * 1000; break;
+            case "oz": converted = quantity * 28.3495; break;
+            case "lb": converted = quantity * 453.5924; break;
+        }
+    }else if(merchant.units.volume.includes(unit)){
+        switch(unit){
+            case "ml": converted = quantity / 1000; break;
+            case "l": break;
+            case "tsp": converted = quantity / 202.8842; break;
+            case "tbsp": converted = quantity / 67.6278; break;
+            case "ozfl": converted = quantity / 33.8141; break;
+            case "cup": converted = quantity / 4.1667; break;
+            case "pt": converted = quantity / 2.1134; break;
+            case "qt": converted = quantity / 1.0567; break;
+            case "gal": converted = quantity * 3.7854; break;
+        }
+    }else if(merchant.units.length.includes(unit)){
+        switch(unit){
+            case "mm": converted = quantity / 1000; break;
+            case "cm": converted = quantity / 100; break;
+            case "m": break;
+            case "in": converted = quantity / 39.3701; break;
+            case "ft": converted = quantity / 3.2808; break;
+        }
+    }else{
+        converted = quantity;
+    }
+
+    return converted;
 }

+ 13 - 5
views/dashboardPage/sidebars/sidebars.js

@@ -594,19 +594,27 @@ let addIngredientsComp = {
         let fetchable = [];
 
         for(let i = 0; i < ingredients.length; i++){
-            if(ingredients[i].children[1].value === ""){
+            let quantity = ingredients[i].children[1].value;
+            let unit = ingredients[i].children[2].value;
+
+            if(quantity === ""){
                 banner.createError("PLEASE ENTER A QUANTITY FOR EACH INGREDIENT YOU WANT TO ADD TO YOUR INVENTORY");
                 return;
             }
+            quantity = convertToMain(unit, quantity);
 
-            newIngredients.push({
+            let newIngredient = {
                 ingredient: ingredients[i].ingredient,
-                quantity: ingredients[i].children[1].value
-            });
+                quantity: quantity
+            }
+            newIngredient.ingredient.unit = unit;
+
+            newIngredients.push(newIngredient);
 
             fetchable.push({
                 id: ingredients[i].ingredient.id,
-                quantity: ingredients[i].children[1].value
+                quantity: quantity,
+                defaultUnit: unit
             });
         }