浏览代码

Add getters and setters to Merchant

Lee Morgan 5 年之前
父节点
当前提交
7fea13f7a8

+ 141 - 143
views/dashboardPage/bundle.js

@@ -50,19 +50,16 @@ class Ingredient{
 
 module.exports = Ingredient;
 },{}],2:[function(require,module,exports){
-const Ingredient = require("./Ingredient.js");
-const Recipe = require("./Recipe.js");
-const Transaction = require("./Transaction.js");
-
 class Merchant{
-    constructor(oldMerchant, transactions){
-        this.name = oldMerchant.name;
-        this.pos = oldMerchant.pos;
-        this.ingredients = [];
-        this.recipes = [];
-        this.transactions = [];
-        this.orders = [];
-        this.units = {
+    constructor(oldMerchant, transactions, modules){
+        this._modules = modules;
+        this._name = oldMerchant.name;
+        this._pos = oldMerchant.pos;
+        this._ingredients = [];
+        this._recipes = [];
+        this._transactions = [];
+        this._orders = [];
+        this._units = {
             mass: ["g", "kg", "oz", "lb"],
             volume: ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"],
             length: ["mm", "cm", "m", "in", "ft"],
@@ -70,9 +67,8 @@ class Merchant{
         }
         
         for(let i = 0; i < oldMerchant.inventory.length; i++){
-
             this.ingredients.push({
-                ingredient: new Ingredient(
+                ingredient: new modules.Ingredient(
                     oldMerchant.inventory[i].ingredient._id,
                     oldMerchant.inventory[i].ingredient.name,
                     oldMerchant.inventory[i].ingredient.category,
@@ -87,7 +83,7 @@ class Merchant{
         }
 
         for(let i = 0; i < oldMerchant.recipes.length; i++){
-            this.recipes.push(new Recipe(
+            this.recipes.push(new modules.Recipe(
                 oldMerchant.recipes[i]._id,
                 oldMerchant.recipes[i].name,
                 oldMerchant.recipes[i].price,
@@ -97,7 +93,7 @@ class Merchant{
         }
 
         for(let i = 0; i < transactions.length; i++){
-            this.transactions.push(new Transaction(
+            this.transactions.push(new modules.Transaction(
                 transactions[i]._id,
                 transactions[i].date,
                 transactions[i].recipes,
@@ -106,152 +102,121 @@ class Merchant{
         }
     }
 
-    /*
-    Updates all specified item in the merchant's inventory and updates the page
-    If ingredient doesn't exist, add it
-    ingredients = [{
-        ingredient: Ingredient object,
-        quantity: new quantity,
-    }]
-    remove = set true if removing
-    isOrder = set true if this is coming from an order
-    */
-    editIngredients(ingredients, remove = false, isOrder = false){
-        for(let i = 0; i < ingredients.length; i++){
-            let isNew = true;
-            for(let j = 0; j < this.ingredients.length; j++){
-                if(this.ingredients[j].ingredient === ingredients[i].ingredient){
-                    if(remove && !isOrder){
-                        this.ingredients.splice(j, 1);
-                    }else if(!remove && isOrder){
-                        this.ingredients[j].quantity += ingredients[i].quantity;
-                    }else{
-                        this.ingredients[j].quantity = ingredients[i].quantity;
-                    }
-    
-                    isNew = false;
-                    break;
-                }
-            }
-    
-            if(isNew){
-                this.ingredients.push({
-                    ingredient: ingredients[i].ingredient,
-                    quantity: parseFloat(ingredients[i].quantity)
-                });
-            }
+    get name(){
+        return this._name;
+    }
+
+    set name(name){
+        if(sanitaryString(name)){
+            this._name = name;
         }
-    
-        controller.updateData("ingredient");
-        controller.closeSidebar();
+        return false;
     }
 
-    /*
-    Updates a recipe in the merchants list of recipes
-    Can create, edit or remove
-    recipe = [Recipe object]
-    remove = will remove recipe when true
-    */
-    editRecipes(recipes, remove = false){
-        let isNew = true;
+    get pos(){
+        return this._pos;
+    }
 
-        for(let i = 0; i < recipes.length; i++){
-            for(let j = 0; j < this.recipes.length; j++){
-                if(recipes[i] === this.recipes[j]){
-                    if(remove){
-                        this.recipes.splice(j, 1);
-                    }else{
-                        this.recipes[j] = recipes[i];
-                    }
+    get ingredients(){
+        return this._ingredients;
+    }
 
-                    isNew = false;
-                    break;
-                }
-            }
+    addIngredient(ingredient){
+        if(ingredient.quantity <  0 || ingredient.quantity === undefined){
+            return false;
+        }
 
-            if(isNew){
-                this.recipes.push(recipes[i]);
-            }
+        this.ingredients.push(ingredient);
+
+        this._modules.home.isPopulated = false;
+        this._modules.ingredients.isPopulated = false;
+    }
+
+    removeIngredient(ingredient){
+        const index = this._ingredients.indexOf(ingredient);
+        if(index === undefined){
+            return false;
         }
 
-        controller.updateData("recipe");
-        controller.closeSidebar();
+        this._ingredients.splice(index, 1);
+
+        this._modules.home.isPopulated = false;
+        this._modules.ingredients.isPopulated = false;
     }
 
-    /*
-    Updates a list of orders in the merchants list of orders
-    Create/edit/remove
-    orders = [Order object]
-    remove = will remove order when true
-    */
-    editOrders(orders, remove = false){
-        for(let i = 0; i < orders.length; i++){
-            let isNew = true;
-            for(let j = 0; j < this.orders.length; j++){
-                if(orders[i] === this.orders[j]){
-                    if(remove){
-                        this.orders.splice(j, 1);
-                    }else{
-                        this.orders[j] = orders[i];
-                    }
+    get recipes(){
+        return this._recipes;
+    }
 
-                    isNew = false;
-                    break;
-                }
-            }
+    addRecipe(recipe){
+        this._recipes.push(recipe);
 
-            if(isNew){
-                this.orders.push(orders[i]);
-            }
+        this._modules.transactions.isPopulated = false;
+        this._modules.recipeBook.isPopulated = false;
+    }
+
+    removeRecipe(recipe){
+        const index = this._recipes.indexOf(recipe);
+        if(index === undefined){
+            return false;
         }
 
-        controller.updateData("order");
-        controller.closeSidebar();
+        this._recipes.splice(index, 1);
+
+        this._modules.transactions.isPopulated = false;
+        this._modules.recipeBook.isPopulated = false;
     }
 
-    /*
-    transaction = Transaction Object to add
-    ingredients = The ingredients that need to be updated
-        keys = ingredient ids
-        values = quantity to change in grams
-    remove = If true, removes transaction
-    */
-    editTransactions(transaction, ingredients, remove = false, ){
-        let isNew = true;
-        for(let i = 0; i < this.transactions.length; i++){
-            if(this.transactions[i] === transaction){
-                if(remove){
-                    this.transactions.splice(i, 1);
-                }
+    get transactions(){
+        return this._transactions;
+    }
 
-                isNew = false;
-                break;
-            }
-        }
+    addTransaction(transaction){
+        this._transactions.push(transaction);
+
+        this._modules.home.isPopulated = false;
+        this._modules.ingredients.isPopulated = false;
+        this._modules.transactions.isPopulated = false;
+        this._modules.analytics.newData = true;
+    }
 
-        if(isNew){
-            this.transactions.push(transaction);
-            this.transactions.sort((a, b) => a.date > b.date ? -1 : 1);
+    removeTransaction(transaction){
+        const index = this._transactions.indexOf(transaction);
+        if(index === undefined){
+            return false;
         }
 
-        let keys = Object.keys(ingredients);
-        for(let i = 0; i < keys.length; i++){
-            for(let j = 0; j < this.ingredients.length; j++){
-                if(this.ingredients[j].ingredient.id === keys[i]){
-                    if(remove === false){
-                        this.ingredients[j].quantity -= ingredients[keys[i]];
-                    }else{
-                        this.ingredients[j].quantity += ingredients[keys[i]];
-                    }
+        this._transactions.splice(index, 1);
 
-                    break;
-                }
-            }
+        this._modules.home.isPopulated = false;
+        this._modules.ingredients.isPopulated = false;
+        this._modules.transactions.isPopulated = false;
+        this._modules.analytics.newData = true;
+    }
+
+    get orders(){
+        return this._orders;
+    }
+
+    addOrder(order){
+        this._orders.push(order);
+
+        this._modules.orders.isPopulated = false;
+    }
+
+    removeOrder(order){
+        const index = this._orders.indexOf(order);
+        if(index === undefined){
+            return false;
         }
 
-        controller.updateData("ingredient");
-        controller.updateData("transaction");
-        controller.closeSidebar();
+        this._orders.splice(index, 1);
+
+        this._modules.orders.isPopulated = false;
+    }
+
+    get units(){
+        return this._units;
     }
 
     revenue(indices){
@@ -446,7 +411,7 @@ class Merchant{
 }
 
 module.exports = Merchant;
-},{"./Ingredient.js":1,"./Recipe.js":4,"./Transaction.js":5}],3:[function(require,module,exports){
+},{}],3:[function(require,module,exports){
 class Order{
     constructor(id, name, date, taxes, fees, ingredients, parent){
         this.id = id;
@@ -864,9 +829,31 @@ const Recipe = require("./Recipe.js");
 const Order = require("./Order.js");
 const Transaction = require("./Transaction.js");
 
-merchant = new Merchant(data.merchant, data.transactions);
+merchant = new Merchant(data.merchant, data.transactions, {
+    home: home,
+    ingredients: ingredients,
+    transactions: transactions,
+    recipeBook: recipeBook,
+    analytics: analytics,
+    orders: orders,
+    Ingredient: Ingredient,
+    Recipe: Recipe,
+    Transaction: Transaction
+});
 
 controller = {
+    sanitaryString: function(str){
+        let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
+
+        for(let i = 0; i < disallowed.length; j++){
+            if(str.includes(disallowed[i])){
+                return false;
+            }
+        }
+
+        return true;
+    },
+
     openStrand: function(strand){
         this.closeSidebar();
 
@@ -1072,7 +1059,6 @@ controller = {
             case "ingredient":
                 home.isPopulated = false;
                 ingredients.populateByProperty("category");
-                home.isPopulated = false;
                 break;
             case "recipe":
                 transactions.isPopulated = false;
@@ -1792,6 +1778,8 @@ let ingredientDetails = {
 
 module.exports = ingredientDetails;
 },{}],10:[function(require,module,exports){
+const { populate } = require("./orders");
+
 let ingredients = {
     isPopulated: false,
     ingredients: [],
@@ -1804,6 +1792,8 @@ let ingredients = {
             document.getElementById("ingredientClearButton").onclick = ()=>{this.clearSorting()};
             document.getElementById("ingredientSelect").onchange = ()=>{this.sort()};
 
+            this.populateByProperty();
+
             this.isPopulated = true;
         }
     },
@@ -1934,7 +1924,7 @@ let ingredients = {
 }
 
 module.exports = ingredients;
-},{}],11:[function(require,module,exports){
+},{"./orders":16}],11:[function(require,module,exports){
 let newIngredient = {
     display: function(Ingredient){
         const selector = document.getElementById("unitSelector");
@@ -2461,6 +2451,7 @@ let orderDetails = {
 module.exports = orderDetails;
 },{}],16:[function(require,module,exports){
 let orders = {
+    isPopulated: false,
     isFetched: false,
 
     display: async function(Order){
@@ -2505,6 +2496,11 @@ let orders = {
                     loader.style.display = "none";
                 });
         }
+
+        if(!this.isPopulated){
+            this.populate();
+            this.isPopulated = true;
+        }
     },
 
     populate: function(){
@@ -2670,6 +2666,8 @@ let recipeBook = {
             document.getElementById("recipeSearch").oninput = ()=>{this.search()};
             document.getElementById("recipeClearButton").onclick = ()=>{this.clearSorting()};
 
+            this.populateRecipes();
+
             this.isPopulated = true;
         }
     },

+ 104 - 139
views/dashboardPage/js/Merchant.js

@@ -1,16 +1,13 @@
-const Ingredient = require("./Ingredient.js");
-const Recipe = require("./Recipe.js");
-const Transaction = require("./Transaction.js");
-
 class Merchant{
-    constructor(oldMerchant, transactions){
-        this.name = oldMerchant.name;
-        this.pos = oldMerchant.pos;
-        this.ingredients = [];
-        this.recipes = [];
-        this.transactions = [];
-        this.orders = [];
-        this.units = {
+    constructor(oldMerchant, transactions, modules){
+        this._modules = modules;
+        this._name = oldMerchant.name;
+        this._pos = oldMerchant.pos;
+        this._ingredients = [];
+        this._recipes = [];
+        this._transactions = [];
+        this._orders = [];
+        this._units = {
             mass: ["g", "kg", "oz", "lb"],
             volume: ["ml", "l", "tsp", "tbsp", "ozfl", "cup", "pt", "qt", "gal"],
             length: ["mm", "cm", "m", "in", "ft"],
@@ -18,9 +15,8 @@ class Merchant{
         }
         
         for(let i = 0; i < oldMerchant.inventory.length; i++){
-
             this.ingredients.push({
-                ingredient: new Ingredient(
+                ingredient: new modules.Ingredient(
                     oldMerchant.inventory[i].ingredient._id,
                     oldMerchant.inventory[i].ingredient.name,
                     oldMerchant.inventory[i].ingredient.category,
@@ -35,7 +31,7 @@ class Merchant{
         }
 
         for(let i = 0; i < oldMerchant.recipes.length; i++){
-            this.recipes.push(new Recipe(
+            this.recipes.push(new modules.Recipe(
                 oldMerchant.recipes[i]._id,
                 oldMerchant.recipes[i].name,
                 oldMerchant.recipes[i].price,
@@ -45,7 +41,7 @@ class Merchant{
         }
 
         for(let i = 0; i < transactions.length; i++){
-            this.transactions.push(new Transaction(
+            this.transactions.push(new modules.Transaction(
                 transactions[i]._id,
                 transactions[i].date,
                 transactions[i].recipes,
@@ -54,152 +50,121 @@ class Merchant{
         }
     }
 
-    /*
-    Updates all specified item in the merchant's inventory and updates the page
-    If ingredient doesn't exist, add it
-    ingredients = [{
-        ingredient: Ingredient object,
-        quantity: new quantity,
-    }]
-    remove = set true if removing
-    isOrder = set true if this is coming from an order
-    */
-    editIngredients(ingredients, remove = false, isOrder = false){
-        for(let i = 0; i < ingredients.length; i++){
-            let isNew = true;
-            for(let j = 0; j < this.ingredients.length; j++){
-                if(this.ingredients[j].ingredient === ingredients[i].ingredient){
-                    if(remove && !isOrder){
-                        this.ingredients.splice(j, 1);
-                    }else if(!remove && isOrder){
-                        this.ingredients[j].quantity += ingredients[i].quantity;
-                    }else{
-                        this.ingredients[j].quantity = ingredients[i].quantity;
-                    }
-    
-                    isNew = false;
-                    break;
-                }
-            }
-    
-            if(isNew){
-                this.ingredients.push({
-                    ingredient: ingredients[i].ingredient,
-                    quantity: parseFloat(ingredients[i].quantity)
-                });
-            }
+    get name(){
+        return this._name;
+    }
+
+    set name(name){
+        if(sanitaryString(name)){
+            this._name = name;
         }
-    
-        controller.updateData("ingredient");
-        controller.closeSidebar();
+        return false;
     }
 
-    /*
-    Updates a recipe in the merchants list of recipes
-    Can create, edit or remove
-    recipe = [Recipe object]
-    remove = will remove recipe when true
-    */
-    editRecipes(recipes, remove = false){
-        let isNew = true;
+    get pos(){
+        return this._pos;
+    }
 
-        for(let i = 0; i < recipes.length; i++){
-            for(let j = 0; j < this.recipes.length; j++){
-                if(recipes[i] === this.recipes[j]){
-                    if(remove){
-                        this.recipes.splice(j, 1);
-                    }else{
-                        this.recipes[j] = recipes[i];
-                    }
+    get ingredients(){
+        return this._ingredients;
+    }
 
-                    isNew = false;
-                    break;
-                }
-            }
+    addIngredient(ingredient){
+        if(ingredient.quantity <  0 || ingredient.quantity === undefined){
+            return false;
+        }
 
-            if(isNew){
-                this.recipes.push(recipes[i]);
-            }
+        this.ingredients.push(ingredient);
+
+        this._modules.home.isPopulated = false;
+        this._modules.ingredients.isPopulated = false;
+    }
+
+    removeIngredient(ingredient){
+        const index = this._ingredients.indexOf(ingredient);
+        if(index === undefined){
+            return false;
         }
 
-        controller.updateData("recipe");
-        controller.closeSidebar();
+        this._ingredients.splice(index, 1);
+
+        this._modules.home.isPopulated = false;
+        this._modules.ingredients.isPopulated = false;
     }
 
-    /*
-    Updates a list of orders in the merchants list of orders
-    Create/edit/remove
-    orders = [Order object]
-    remove = will remove order when true
-    */
-    editOrders(orders, remove = false){
-        for(let i = 0; i < orders.length; i++){
-            let isNew = true;
-            for(let j = 0; j < this.orders.length; j++){
-                if(orders[i] === this.orders[j]){
-                    if(remove){
-                        this.orders.splice(j, 1);
-                    }else{
-                        this.orders[j] = orders[i];
-                    }
+    get recipes(){
+        return this._recipes;
+    }
 
-                    isNew = false;
-                    break;
-                }
-            }
+    addRecipe(recipe){
+        this._recipes.push(recipe);
 
-            if(isNew){
-                this.orders.push(orders[i]);
-            }
+        this._modules.transactions.isPopulated = false;
+        this._modules.recipeBook.isPopulated = false;
+    }
+
+    removeRecipe(recipe){
+        const index = this._recipes.indexOf(recipe);
+        if(index === undefined){
+            return false;
         }
 
-        controller.updateData("order");
-        controller.closeSidebar();
+        this._recipes.splice(index, 1);
+
+        this._modules.transactions.isPopulated = false;
+        this._modules.recipeBook.isPopulated = false;
     }
 
-    /*
-    transaction = Transaction Object to add
-    ingredients = The ingredients that need to be updated
-        keys = ingredient ids
-        values = quantity to change in grams
-    remove = If true, removes transaction
-    */
-    editTransactions(transaction, ingredients, remove = false, ){
-        let isNew = true;
-        for(let i = 0; i < this.transactions.length; i++){
-            if(this.transactions[i] === transaction){
-                if(remove){
-                    this.transactions.splice(i, 1);
-                }
+    get transactions(){
+        return this._transactions;
+    }
 
-                isNew = false;
-                break;
-            }
-        }
+    addTransaction(transaction){
+        this._transactions.push(transaction);
+
+        this._modules.home.isPopulated = false;
+        this._modules.ingredients.isPopulated = false;
+        this._modules.transactions.isPopulated = false;
+        this._modules.analytics.newData = true;
+    }
 
-        if(isNew){
-            this.transactions.push(transaction);
-            this.transactions.sort((a, b) => a.date > b.date ? -1 : 1);
+    removeTransaction(transaction){
+        const index = this._transactions.indexOf(transaction);
+        if(index === undefined){
+            return false;
         }
 
-        let keys = Object.keys(ingredients);
-        for(let i = 0; i < keys.length; i++){
-            for(let j = 0; j < this.ingredients.length; j++){
-                if(this.ingredients[j].ingredient.id === keys[i]){
-                    if(remove === false){
-                        this.ingredients[j].quantity -= ingredients[keys[i]];
-                    }else{
-                        this.ingredients[j].quantity += ingredients[keys[i]];
-                    }
+        this._transactions.splice(index, 1);
 
-                    break;
-                }
-            }
+        this._modules.home.isPopulated = false;
+        this._modules.ingredients.isPopulated = false;
+        this._modules.transactions.isPopulated = false;
+        this._modules.analytics.newData = true;
+    }
+
+    get orders(){
+        return this._orders;
+    }
+
+    addOrder(order){
+        this._orders.push(order);
+
+        this._modules.orders.isPopulated = false;
+    }
+
+    removeOrder(order){
+        const index = this._orders.indexOf(order);
+        if(index === undefined){
+            return false;
         }
 
-        controller.updateData("ingredient");
-        controller.updateData("transaction");
-        controller.closeSidebar();
+        this._orders.splice(index, 1);
+
+        this._modules.orders.isPopulated = false;
+    }
+
+    get units(){
+        return this._units;
     }
 
     revenue(indices){

+ 23 - 2
views/dashboardPage/js/dashboard.js

@@ -20,9 +20,31 @@ const Recipe = require("./Recipe.js");
 const Order = require("./Order.js");
 const Transaction = require("./Transaction.js");
 
-merchant = new Merchant(data.merchant, data.transactions);
+merchant = new Merchant(data.merchant, data.transactions, {
+    home: home,
+    ingredients: ingredients,
+    transactions: transactions,
+    recipeBook: recipeBook,
+    analytics: analytics,
+    orders: orders,
+    Ingredient: Ingredient,
+    Recipe: Recipe,
+    Transaction: Transaction
+});
 
 controller = {
+    sanitaryString: function(str){
+        let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
+
+        for(let i = 0; i < disallowed.length; j++){
+            if(str.includes(disallowed[i])){
+                return false;
+            }
+        }
+
+        return true;
+    },
+
     openStrand: function(strand){
         this.closeSidebar();
 
@@ -228,7 +250,6 @@ controller = {
             case "ingredient":
                 home.isPopulated = false;
                 ingredients.populateByProperty("category");
-                home.isPopulated = false;
                 break;
             case "recipe":
                 transactions.isPopulated = false;

+ 4 - 0
views/dashboardPage/js/ingredients.js

@@ -1,3 +1,5 @@
+const { populate } = require("./orders");
+
 let ingredients = {
     isPopulated: false,
     ingredients: [],
@@ -10,6 +12,8 @@ let ingredients = {
             document.getElementById("ingredientClearButton").onclick = ()=>{this.clearSorting()};
             document.getElementById("ingredientSelect").onchange = ()=>{this.sort()};
 
+            this.populateByProperty();
+
             this.isPopulated = true;
         }
     },

+ 6 - 0
views/dashboardPage/js/orders.js

@@ -1,4 +1,5 @@
 let orders = {
+    isPopulated: false,
     isFetched: false,
 
     display: async function(Order){
@@ -43,6 +44,11 @@ let orders = {
                     loader.style.display = "none";
                 });
         }
+
+        if(!this.isPopulated){
+            this.populate();
+            this.isPopulated = true;
+        }
     },
 
     populate: function(){

+ 2 - 0
views/dashboardPage/js/recipeBook.js

@@ -12,6 +12,8 @@ let recipeBook = {
             document.getElementById("recipeSearch").oninput = ()=>{this.search()};
             document.getElementById("recipeClearButton").onclick = ()=>{this.clearSorting()};
 
+            this.populateRecipes();
+
             this.isPopulated = true;
         }
     },