فهرست منبع

Update the reading of ingredient spreadsheets to follow the new format.
Update promises in ingredient spreadsheet creation to be faster.
Bug fix: banner creation was inside for loop for successful spreadsheet ingredient creation.

Lee Morgan 5 سال پیش
والد
کامیت
0ef2f73489
5فایلهای تغییر یافته به همراه49 افزوده شده و 56 حذف شده
  1. 28 4
      controllers/helper.js
  2. 13 45
      controllers/ingredientData.js
  3. 2 1
      models/ingredient.js
  4. 3 3
      views/dashboardPage/bundle.js
  5. 3 3
      views/dashboardPage/js/sidebars/newIngredient.js

+ 28 - 4
controllers/helper.js

@@ -1,7 +1,6 @@
 const axios = require("axios");
 
 const Transaction = require("../models/transaction.js");
-const Merchant = require("../models/merchant.js");
 
 module.exports = {
     getCloverData: async function(merchant){
@@ -186,12 +185,10 @@ module.exports = {
 
     convertQuantityToBaseUnit: function(quantity, unit){
         switch(unit){
-            case "g":return quantity; 
             case "kg": return quantity * 1000;
             case "oz": return quantity * 28.3495;
             case "lb": return quantity * 453.5924;
             case "ml": return quantity / 1000;
-            case "l": return quantity;
             case "tsp": return quantity / 202.8842;
             case "tbsp": return quantity / 67.6278;
             case "ozfl": return quantity / 33.8141;
@@ -201,7 +198,6 @@ module.exports = {
             case "gal": return quantity * 3.7854;
             case "mm": return quantity / 1000;
             case "cm": return quantity / 100;
-            case "m": return quantity;
             case "in": return quantity / 39.3701;
             case "ft": return quantity / 3.2808;
             default: return quantity;
@@ -255,5 +251,33 @@ module.exports = {
         }
 
         return true;
+    },
+
+    getUnitType: function(unit){
+        let unitType = "";
+
+        switch(unit){
+            case "g": unitType = "mass"; break;
+            case "kg": unitType = "mass"; break;
+            case "oz": unitType = "mass"; break;
+            case "lb": unitType = "mass"; break;
+            case "ml": unitType = "volume"; break;
+            case "l": unitType = "volume"; break;
+            case "tsp": unitType = "volume"; break;
+            case "tbsp": unitType = "volume"; break;
+            case "ozfl": unitType = "volume"; break;
+            case "cup": unitType = "volume"; break;
+            case "pt": unitType = "volume"; break;
+            case "qt": unitType = "volume"; break;
+            case "gal": unitType = "volume"; break;
+            case "mm": unitType = "length"; break;
+            case "cm": unitType = "length"; break;
+            case "m": unitType = "length"; break;
+            case "in": unitType = "length"; break;
+            case "ft": unitType = "length"; break;
+            default: unitType = "other"; break;
+        }
+
+        return unitType;
     }
 }

+ 13 - 45
controllers/ingredientData.js

@@ -158,8 +158,8 @@ module.exports = {
                 case "category": locations.category = i; break;
                 case "quantity": locations.quantity = i; break;
                 case "unit": locations.unit = i; break;
-                case "bottle": locations.bottle = i; break;
                 case "bottle size": locations.bottleSize = i; break;
+                case "bottle unit": locations.bottleUnit = i; break;
             }
         }
 
@@ -169,67 +169,35 @@ module.exports = {
         for(let i = 1; i < array.length; i++){
             let ingredient = new Ingredient({
                 name: array[i][locations.name],
-                category: array[i][locations.category]
+                category: array[i][locations.category],
+                unitType: helper.getUnitType(array[i][locations.unit].toLowerCase())
             });
 
+            if(array[i][locations.unit] === "bottle"){
+                ingredient.unitType = array[i][locations.bottleUnit];
+                ingredient.unitSize = helper.convertQuantityToBaseUnit(array[i][locations.bottleSize], array[i][locations.bottleUnit]);
+            }
+
             let merchantItem = {
                 ingredient: ingredient,
                 quantity: helper.convertQuantityToBaseUnit(array[i][locations.quantity], array[i][locations.unit]),
                 defaultUnit: array[i][locations.unit]
             }
-
-            if(array[i][locations.bottle] === true){
-                let quantity = array[i][locations.quantity] * array[i][locations.bottleSize];
-                merchantItem.quantity = helper.convertQuantityToBaseUnit(quantity, array[i][locations.unit]);
-                
-                ingredient.unitType = "volume";
-                ingredient.specialUnit = "bottle";
-                ingredient.unitSize = helper.convertQuantityToBaseUnit(array[i][locations.bottleSize], array[i][locations.unit]);
-            }else{
-                let unitType = "";
-                //TODO: this should probably be in a helper
-                switch(array[i][locations.unit].toLowerCase()){
-                    case "g": unitType = "mass"; break;
-                    case "kg": unitType = "mass"; break;
-                    case "oz": unitType = "mass"; break;
-                    case "lb": unitType = "mass"; break;
-                    case "ml": unitType = "volume"; break;
-                    case "l": unitType = "volume"; break;
-                    case "tsp": unitType = "volume"; break;
-                    case "tbsp": unitType = "volume"; break;
-                    case "ozfl": unitType = "volume"; break;
-                    case "cup": unitType = "volume"; break;
-                    case "pt": unitType = "volume"; break;
-                    case "qt": unitType = "volume"; break;
-                    case "gal": unitType = "volume"; break;
-                    case "mm": unitType = "length"; break;
-                    case "cm": unitType = "length"; break;
-                    case "m": unitType = "length"; break;
-                    case "in": unitType = "length"; break;
-                    case "ft": unitType = "length"; break;
-                    default: unitType = "other";
-                }
-
-                ingredient.unitType = unitType;
-            }
-
+            
             merchantData.push(merchantItem);
             ingredients.push(ingredient);
         }
 
         //Update the database
-        Ingredient.create(ingredients)
-            .then((ingredients)=>{
-                return Merchant.findOne({_id: req.session.user});
-            })
+        Merchant.findOne({_id: req.session.user})
             .then((merchant)=>{
                 for(let i = 0; i < merchantData.length; i++){
                     merchant.inventory.push(merchantData[i]);
                 }
 
-                return merchant.save();
+                return Promise.all([Ingredient.create(ingredients), merchant.save()]);
             })
-            .then((merchant)=>{
+            .then((response)=>{
                 return res.json(merchantData);
             })
             .catch((err)=>{
@@ -295,5 +263,5 @@ module.exports = {
                 }
                 return res.json("ERROR: UNABLE TO RETRIEVE USER DATA");
             });
-    },
+    }
 }

+ 2 - 1
models/ingredient.js

@@ -27,7 +27,8 @@ const IngredientSchema = new mongoose.Schema({
     },
     unitSize: {
         type: Number,
-        min: [0, "SIZE CANNOT BE A NEGATIVE NUMBER"]
+        min: [0, "SIZE CANNOT BE A NEGATIVE NUMBER"],
+        required: false
     }
 });
 

+ 3 - 3
views/dashboardPage/bundle.js

@@ -2032,10 +2032,10 @@ let newIngredient = {
                 }else{
                     for(let i = 0; i < response.length; i++){
                         merchant.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
-
-                        controller.createBanner("INGREDINETS SUCCESSFULLY ADDED", "success");
-                        controller.openStrand("ingredients");
                     }
+
+                    controller.createBanner("INGREDIENTS SUCCESSFULLY ADDED", "success");
+                    controller.openStrand("ingredients");
                 }
                 
             })

+ 3 - 3
views/dashboardPage/js/sidebars/newIngredient.js

@@ -97,10 +97,10 @@ let newIngredient = {
                 }else{
                     for(let i = 0; i < response.length; i++){
                         merchant.addIngredient(response[i].ingredient, response[i].quantity, response[i].defaultUnit);
-
-                        controller.createBanner("INGREDINETS SUCCESSFULLY ADDED", "success");
-                        controller.openStrand("ingredients");
                     }
+
+                    controller.createBanner("INGREDIENTS SUCCESSFULLY ADDED", "success");
+                    controller.openStrand("ingredients");
                 }
                 
             })