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

Remove some uneccessary promise chaining when removing ingredients.
Update classes/models to handle the new data format for bottles.

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

+ 2 - 5
controllers/ingredientData.js

@@ -298,12 +298,9 @@ module.exports = {
                     }
                 }
 
-                return merchant.save()
+                return Promise.all([merchant.save(), Ingredient.deleteOne({_id: req.params.id})]);
             })
-            .then((merchant)=>{
-                return Ingredient.deleteOne({_id: req.params.id});
-            })
-            .then((ingredient)=>{
+            .then((response)=>{
                 return res.json({});
             })
             .catch((err)=>{

+ 1 - 3
models/ingredient.js

@@ -24,9 +24,7 @@ const IngredientSchema = new mongoose.Schema({
     unitType: {
         type: String,
         required: [true, "UNIT TYPE IS REQUIRED"]
-    },
-    specialUnit: String,
-    unitSize: String
+    }
 });
 
 module.exports = mongoose.model("Ingredient", IngredientSchema);

+ 2 - 37
views/dashboardPage/bundle.js

@@ -7,10 +7,6 @@ class Ingredient{
         this._unitType = unitType;
         this._unit = unit;
         this._parent = parent;
-        if(specialUnit){
-            this._specialUnit = specialUnit;
-            this._unitSize = unitSize;
-        }
     }
 
     get id(){
@@ -90,10 +86,6 @@ class Ingredient{
     }
 
     getNameAndUnit(){
-        if(this._specialUnit === "bottle"){
-            return `${this._name} (BOTTLES)`;
-        }
-
         return `${this._name} (${this._unit.toUpperCase()})`;
     }
 }
@@ -111,10 +103,6 @@ class MerchantIngredient{
     }
 
     get quantity(){
-        if(this._ingredient.specialUnit === "bottle"){
-            return this._quantity / this._ingredient._unitSize;
-        }
-
         switch(this._ingredient.unit){
             case "g":return this._quantity; 
             case "kg": return this._quantity / 1000;
@@ -167,10 +155,6 @@ class MerchantIngredient{
     }
 
     getQuantityDisplay(){
-        if(this._ingredient.specialUnit === "bottle"){
-            return `${this.quantity.toFixed(2)} BOTTLES`;
-        }
-
         return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
     }
 }
@@ -722,12 +706,8 @@ class Merchant{
 
             if(!unitExists){
                 let unit = "";
-                if(innerIngredient.specialUnit === "bottle"){
-                    unit = "bottle";
-                }else{
-                    unit = innerIngredient.unit;
-                }
-
+                unit = innerIngredient.unit;
+                
                 ingredientsByUnit.push({
                     name: unit,
                     ingredients: [this.ingredients[i]]
@@ -808,10 +788,6 @@ class OrderIngredient{
     }
 
     get quantity(){
-        if(this._ingredient.specialUnit === "bottle"){
-            return this._quantity / this._ingredient._unitSize;
-        }
-
         switch(this._ingredient.unit){
             case "g":return this._quantity;
             case "kg": return this._quantity / 1000;
@@ -868,10 +844,6 @@ class OrderIngredient{
     }
 
     get pricePerUnit(){
-        if(this._ingredient.specialUnit === "bottle"){
-            return (this._pricePerUnit * this._ingredient._unitSize) / 100;
-        }
-
         switch(this._ingredient.unit){
             case "g": return this._pricePerUnit / 100;
             case "kg": return (this._pricePerUnit * 1000) / 100; 
@@ -1005,10 +977,6 @@ class RecipeIngredient{
     }
 
     get quantity(){
-        if(this._ingredient.specialUnit === "bottle"){
-            return this._quantity;
-        }
-
         switch(this._ingredient.unit){
             case "g":return this._quantity;
             case "kg": return this._quantity / 1000;
@@ -1042,9 +1010,6 @@ class RecipeIngredient{
     }
 
     getQuantityDisplay(){
-        if(this._ingredient.specialUnit === "bottle"){
-            return `${this.quantity.toFixed(2)} BOTTLES`;
-        }
         return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
     }
 

+ 0 - 8
views/dashboardPage/js/classes/Ingredient.js

@@ -6,10 +6,6 @@ class Ingredient{
         this._unitType = unitType;
         this._unit = unit;
         this._parent = parent;
-        if(specialUnit){
-            this._specialUnit = specialUnit;
-            this._unitSize = unitSize;
-        }
     }
 
     get id(){
@@ -89,10 +85,6 @@ class Ingredient{
     }
 
     getNameAndUnit(){
-        if(this._specialUnit === "bottle"){
-            return `${this._name} (BOTTLES)`;
-        }
-
         return `${this._name} (${this._unit.toUpperCase()})`;
     }
 }

+ 2 - 14
views/dashboardPage/js/classes/Merchant.js

@@ -9,10 +9,6 @@ class MerchantIngredient{
     }
 
     get quantity(){
-        if(this._ingredient.specialUnit === "bottle"){
-            return this._quantity / this._ingredient._unitSize;
-        }
-
         switch(this._ingredient.unit){
             case "g":return this._quantity; 
             case "kg": return this._quantity / 1000;
@@ -65,10 +61,6 @@ class MerchantIngredient{
     }
 
     getQuantityDisplay(){
-        if(this._ingredient.specialUnit === "bottle"){
-            return `${this.quantity.toFixed(2)} BOTTLES`;
-        }
-
         return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
     }
 }
@@ -620,12 +612,8 @@ class Merchant{
 
             if(!unitExists){
                 let unit = "";
-                if(innerIngredient.specialUnit === "bottle"){
-                    unit = "bottle";
-                }else{
-                    unit = innerIngredient.unit;
-                }
-
+                unit = innerIngredient.unit;
+                
                 ingredientsByUnit.push({
                     name: unit,
                     ingredients: [this.ingredients[i]]

+ 0 - 8
views/dashboardPage/js/classes/Order.js

@@ -13,10 +13,6 @@ class OrderIngredient{
     }
 
     get quantity(){
-        if(this._ingredient.specialUnit === "bottle"){
-            return this._quantity / this._ingredient._unitSize;
-        }
-
         switch(this._ingredient.unit){
             case "g":return this._quantity;
             case "kg": return this._quantity / 1000;
@@ -73,10 +69,6 @@ class OrderIngredient{
     }
 
     get pricePerUnit(){
-        if(this._ingredient.specialUnit === "bottle"){
-            return (this._pricePerUnit * this._ingredient._unitSize) / 100;
-        }
-
         switch(this._ingredient.unit){
             case "g": return this._pricePerUnit / 100;
             case "kg": return (this._pricePerUnit * 1000) / 100; 

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

@@ -13,10 +13,6 @@ class RecipeIngredient{
     }
 
     get quantity(){
-        if(this._ingredient.specialUnit === "bottle"){
-            return this._quantity;
-        }
-
         switch(this._ingredient.unit){
             case "g":return this._quantity;
             case "kg": return this._quantity / 1000;
@@ -50,9 +46,6 @@ class RecipeIngredient{
     }
 
     getQuantityDisplay(){
-        if(this._ingredient.specialUnit === "bottle"){
-            return `${this.quantity.toFixed(2)} BOTTLES`;
-        }
         return `${this.quantity.toFixed(2)} ${this._ingredient.unit.toUpperCase()}`;
     }