Selaa lähdekoodia

Fixed class outlining

Lee Morgan 6 vuotta sitten
vanhempi
sitoutus
d4fe58cff7
3 muutettua tiedostoa jossa 173 lisäystä ja 165 poistoa
  1. 156 72
      views/dashboardPage/Merchant.js
  2. 0 77
      views/dashboardPage/controller.js
  3. 17 16
      views/dashboardPage/home.js

+ 156 - 72
views/dashboardPage/Merchant.js

@@ -1,94 +1,97 @@
 class Ingredient{
-    constructor(id, name, category, unit, quantity){
-        if(this.validate(name, category, unit, quantity)){
-            this.id = id;
-            this.name = name;
-            this.category = category;
-            this.unit = unit;
-            this.quantity = quantity
-        }
+    constructor(id, name, category, unit){
+        this.id = id;
+        this.name = name;
+        this.category = category;
+        this.unit = unit;
     }
+}
 
-    validate(name, category, unit, quantity, createBanner = true){
-        let errors = [];
-        if(!isSanitary(name) ||
-        !isSanitary(category) ||
-        !isSanitary(unit)){
-            errors.push("Contains illegal characters");
-        }
-
-        if(isNaN(quantity) || quantity === ""){
-            errors.push("Must enter a valid number");
-        }
-
-        if(quantity < 0){
-            banner.createError("Quantity cannot be a negative number");
-        }
+class Recipe{
+    constructor(id, name, price, ingredients, parent){
+        this.id = id;
+        this.name = name;
+        this.price = price;
+        this.parent = parent;
+        this.ingredients = [];
 
-        if(errors.length > 0){
-            if(createBanner){
-                for(let i = 0; i < errors.length; i++){
-                    banner.createError(errors[i]);
+        for(let i = 0; i < ingredients.length; i++){
+            for(let j = 0; j < parent.ingredients.length; j++){
+                if(ingredients[i].ingredient === parent.ingredients[j].ingredient.id){
+                    this.ingredients.push({
+                        ingredient: parent.ingredients[j].ingredient,
+                        quantity: ingredients[i].quantity
+                    });
+                    break;
                 }
             }
-
-            return false;
         }
-
-        return true;
     }
 }
 
-class Recipe{
-    constructor(name, price, ingredients){
-        this.name = name;
-        this.price = price
-        this.ingredients = ingredients;
+/*
+parent: merchant associated with,
+date: date created,
+recipes: [{
+    recipe: Recipe Object,
+    quantity: quantity of the recipe
+}
+*/
+class Transaction{
+    constructor(date, recipes, parent){
+        this.parent = parent;
+        this.date = new Date(date);
+        this.recipes = [];
+
+        for(let i = 0; i < recipes.length; i++){
+            for(let j = 0; j < parent.recipes.length; j++){
+                if(recipes[i].recipe === parent.recipes[j].id){
+                    this.recipes.push({
+                        recipe: parent.recipes[j],
+                        quantity: recipes[i].quantity
+                    });
+                    break;
+                }
+            }
+        }
     }
 }
 
 class Merchant{
     constructor(oldMerchant, transactions){
         this.name = oldMerchant.name;
-        this.inventory = [];
+        this.ingredients = [];
         this.recipes = [];
         this.transactions = [];
         
         for(let i = 0; i < oldMerchant.inventory.length; i++){
-            this.inventory.push(new Ingredient(
-                oldMerchant.inventory[i].ingredient._id,
-                oldMerchant.inventory[i].ingredient.name,
-                oldMerchant.inventory[i].ingredient.category,
-                oldMerchant.inventory[i].ingredient.unit,
-                oldMerchant.inventory[i].quantity
-            ));
+            this.ingredients.push({
+                ingredient: new Ingredient(
+                    oldMerchant.inventory[i].ingredient._id,
+                    oldMerchant.inventory[i].ingredient.name,
+                    oldMerchant.inventory[i].ingredient.category,
+                    oldMerchant.inventory[i].ingredient.unit,
+                ),
+                quantity: oldMerchant.inventory[i].quantity
+            });
         }
 
         for(let i = 0; i < oldMerchant.recipes.length; i++){
-            let newRecipe = {
-                name: oldMerchant.recipes[i].name,
-                price: oldMerchant.recipes[i].price,
-                ingredients: [],
-            }
-
-            for(let j = 0; j < oldMerchant.recipes[i].ingredients.length; j++){
-                for(let k = 0; k < this.inventory.length; k++){
-                    if(oldMerchant.recipes[i].ingredients[j].ingredient._id === this.inventory[k].id){
-                        newRecipe.ingredients.push({
-                            ingredient: this.inventory[k],
-                            quantity: oldMerchant.recipes[i].ingredients[j].quantity
-                        });
-
-                        break;
-                    }
-                }
-            }
+            this.recipes.push(new Recipe(
+                oldMerchant.recipes[i]._id,
+                oldMerchant.recipes[i].name,
+                oldMerchant.recipes[i].price,
+                oldMerchant.recipes[i].ingredients,
+                this
+            ));
         }
 
+        
         for(let i = 0; i < transactions.length; i++){
             this.transactions.push(new Transaction(
                 transactions[i].date,
-                transactions[i].recipes
+                transactions[i].recipes,
+                this
             ));
         }
     }
@@ -200,7 +203,7 @@ class Merchant{
     Array containing starting index and ending index
     Note: Will return false if it cannot find both necessary dates
     */
-    getTransactionIndices(from, to = new Date()){
+    transactionIndices(from, to = new Date()){
         let indices = [];
 
         for(let i = 0; i < this.transactions.length; i++){
@@ -224,7 +227,7 @@ class Merchant{
         return indices;
     }
 
-    calculateRevenue(indices){
+    revenue(indices){
         let total = 0;
 
         for(let i = indices[0]; i <= indices[1]; i++){
@@ -239,6 +242,94 @@ class Merchant{
 
         return total / 100;
     }
+
+    /*
+    Gets the quantity of each ingredient sold between two dates (dateRange)
+    Inputs
+    dateRange: list containing a start date and an end date
+    Output
+    List of objects
+        id: id of specific ingredient
+        quantity: quantity sold of that ingredient
+        name: name of the ingredient
+    */
+    ingredientsSold(dateRange){
+        if(!dateRange){
+            return false;
+        }
+        
+        let recipes = this.recipesSold(dateRange);
+        let ingredientList = [];
+
+        // for(let i = 0; i < recipes.length; i++){
+        //     for(let j = 0; j < this.recipes[i].ingredients.length; j++){
+
+        //     }
+        // }
+    
+        // for(let i = 0; i < recipes.length; i++){
+        //     for(let j = 0; j < this.recipes.length; j++){
+        //         for(let k = 0; k < this.recipes[j].ingredients.length; k++){
+        //             let exists = false;
+        //             for(let l = 0; l < ingredientList.length; l++){
+        //                 if(ingredientList[l].id === this.recipes[j].ingredients[k].ingredient._id){
+        //                     exists = true;
+        //                     ingredientList[l].quantity += this.recipes[j].ingredients[k].quantity * recipes[i].quantity;
+        //                     break;
+        //                 }
+        //             }
+    
+        //             if(!exists){
+        //                 ingredientList.push({
+        //                     id: this.recipes[j].ingredients[k].ingredient._id,
+        //                     quantity: this.recipes[j].ingredients[k].quantity * recipes[i].quantity,
+        //                     name: this.recipes[j].ingredients[k].ingredient.name,
+        //                     unit: this.recipes[j].ingredients[k].ingredient.unit
+        //                 })
+        //             }
+        //         }
+        //     }
+        // }
+    
+        return ingredientList;
+    }
+
+    /*
+    Gets the number of recipes sold between two dates (dateRange)
+    Inputs:
+        dateRange: array containing a start date and an end date
+    Return:
+        [{
+            name: a recipe object
+            quantity: quantity of the recipe sold
+        }]
+    */
+    recipesSold(dateRange){
+        let recipeList = [];
+
+        for(let i = dateRange[0]; i <= dateRange[1]; i++){
+            for(let j = 0; j < this.transactions[i].recipes.length; j++){
+                let exists = false;
+                for(let k = 0; k < recipeList.length; k++){
+                    if(recipeList[k] === this.transactions[i].recipes[j]){
+                        exists = true;
+                        recipeList[k].quantity += this.transactions[i].recipes[j].quantity;
+                        break;
+                    }
+                }
+
+                if(!exists){
+                    recipeList.push({
+                        recipe: this.transactions[i].recipes[j],
+                        quantity: this.transactions[i].recipes[j].quantity
+                    });
+                }
+            }
+        }
+
+        console.log(recipeList);
+        return recipeList;
+    }
 }
 
 class Order{
@@ -248,13 +339,6 @@ class Order{
     }
 }
 
-class Transaction{
-    constructor(date, recipes){
-        this.date = new Date(date);
-        this.recipes = recipes;
-    }
-}
-
 let isSanitary =  (str, createBanner = true)=>{
     let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
 

+ 0 - 77
views/dashboardPage/controller.js

@@ -182,50 +182,7 @@ let openSidebar = (sidebar)=>{
 
     sidebar.style.display = "flex";
 }
-/*
-Gets the quantity of each ingredient sold between two dates (dateRange)
-Inputs
- dateRange: list containing a start date and an end date
-Output
- List of objects
-     id: id of specific ingredient
-     quantity: quantity sold of that ingredient
-     name: name of the ingredient
-*/
-let ingredientsSold = (dateRange)=>{
-    if(!dateRange){
-        return false;
-    }
-    
-    let recipes = recipesSold(dateRange);
-    let ingredientList = [];
-
-    for(let i = 0; i < recipes.length; i++){
-        for(let j = 0; j < merchant.recipes.length; j++){
-            for(let k = 0; k < merchant.recipes[j].ingredients.length; k++){
-                let exists = false;
-                for(let l = 0; l < ingredientList.length; l++){
-                    if(ingredientList[l].id === merchant.recipes[j].ingredients[k].ingredient._id){
-                        exists = true;
-                        ingredientList[l].quantity += merchant.recipes[j].ingredients[k].quantity * recipes[i].quantity;
-                        break;
-                    }
-                }
 
-                if(!exists){
-                    ingredientList.push({
-                        id: merchant.recipes[j].ingredients[k].ingredient._id,
-                        quantity: merchant.recipes[j].ingredients[k].quantity * recipes[i].quantity,
-                        name: merchant.recipes[j].ingredients[k].ingredient.name,
-                        unit: merchant.recipes[j].ingredients[k].ingredient.unit
-                    })
-                }
-            }
-        }
-    }
-
-    return ingredientList;
-}
 /*
 Gets the quantity of a single ingredient sold between two dates (dateRange)
 Input:
@@ -260,40 +217,6 @@ let ingredientSold = (dateRange,  id)=>{
 
     return total;
 }
-/*
-Gets the number of recipes sold between two dates (dateRange)
-Inputs:
-    dateRange: array containing a start date and an end date
-Return:
-    List of objects
-        id: id of specific recipe
-        quantity: quantity sold of that recipe
-*/
-let recipesSold = (dateRange)=>{
-    let recipeList = [];
-
-    for(let i = dateRange[0]; i <= dateRange[1]; i++){
-        for(let j = 0; j < transactions[i].recipes.length; j++){
-            let exists = false;
-            for(let k = 0; k < recipeList.length; k++){
-                if(recipeList[k].id === transactions[i].recipes[j].recipe){
-                    exists = true;
-                    recipeList[k].quantity += transactions[i].recipes[j].quantity;
-                    break;
-                }
-            }
-
-            if(!exists){
-                recipeList.push({
-                    id: transactions[i].recipes[j].recipe,
-                    quantity: transactions[i].recipes[j].quantity
-                })
-            }
-        }
-    }
-
-    return recipeList;
-}
 
 /*
 Groups all of the merchant's ingredients by their category

+ 17 - 16
views/dashboardPage/home.js

@@ -19,8 +19,8 @@ window.homeStrandObj = {
         let firstOfLastMonth = new Date(today.getFullYear(), today.getMonth() - 1, 1);
         let lastMonthtoDay = new Date(new Date().setMonth(today.getMonth() - 1));
 
-        let revenueThisMonth = merchant.calculateRevenue(merchant.dateIndices(firstOfMonth));
-        let revenueLastmonthToDay = merchant.calculateRevenue(merchant.dateIndices(firstOfLastMonth, lastMonthtoDay));
+        let revenueThisMonth = merchant.revenue(merchant.transactionIndices(firstOfMonth));
+        let revenueLastmonthToDay = merchant.revenue(merchant.transactionIndices(firstOfLastMonth, lastMonthtoDay));
 
         document.querySelector("#revenue").innerText = `$${revenueThisMonth.toLocaleString("en")}`;
 
@@ -49,7 +49,7 @@ window.homeStrandObj = {
         let thirtyAgo = new Date(today);
         thirtyAgo.setDate(today.getDate() - 29);
 
-        let data = this.graphData(dateIndices(thirtyAgo));
+        let data = this.graphData(merchant.transactionIndices(thirtyAgo));
         if(data){
             this.graph.addData(
                 data,
@@ -68,14 +68,14 @@ window.homeStrandObj = {
 
     drawInventoryCheckCard: function(){
         let num;
-        if(merchant.inventory.length < 5){
+        if(merchant.ingredients.length < 5){
             num = merchant.inventory.length;
         }else{
             num = 5;
         }
         let rands = [];
         for(let i = 0; i < num; i++){
-            let rand = Math.floor(Math.random() * merchant.inventory.length);
+            let rand = Math.floor(Math.random() * merchant.ingredients.length);
 
             if(rands.includes(rand)){
                 i--;
@@ -94,11 +94,11 @@ window.homeStrandObj = {
             let input = ingredientCheck.children[1].children[1];
 
             ingredientCheck.ingredientIndex = rands[i];
-            ingredientCheck.children[0].innerText = merchant.inventory[rands[i]].name;
+            ingredientCheck.children[0].innerText = merchant.ingredients[rands[i]].name;
             ingredientCheck.children[1].children[0].onclick = ()=>{input.value--};
-            input.value = merchant.inventory[rands[i]].quantity;
+            input.value = merchant.ingredients[rands[i]].quantity;
             ingredientCheck.children[1].children[2].onclick = ()=>{input.value++}
-            ingredientCheck.children[2].innerText = merchant.inventory[rands[i]].unit.toUpperCase();
+            ingredientCheck.children[2].innerText = merchant.ingredients[rands[i]].unit.toUpperCase();
 
             ul.appendChild(ingredientCheck);
         }
@@ -109,8 +109,9 @@ window.homeStrandObj = {
         let now = new Date();
         let thisMonth = new Date(now.getFullYear(), now.getMonth(), 1);
 
-        let ingredientList = ingredientsSold(dateIndices(thisMonth));
-        console.log(ingredientList)
+        console.time("Ingredients Sold");
+        let ingredientList = merchant.ingredientsSold(merchant.transactionIndices(thisMonth));
+        console.timeEnd("Ingredients Sold");
         if(ingredientList){
             for(let i = 0; i < 5; i++){
                 let max = ingredientList[0].quantity
@@ -157,18 +158,18 @@ window.homeStrandObj = {
         }
 
         let dataList = new Array(30).fill(0);
-        let currentDate = transactions[dateRange[0]].date;
+        let currentDate = merchant.transactions[dateRange[0]].date;
         let arrayIndex = 0;
 
         for(let i = dateRange[0]; i <= dateRange[1]; i++){
-            if(transactions[i].date.getDate() !== currentDate.getDate()){
-                currentDate = transactions[i].date;
+            if(merchant.transactions[i].date.getDate() !== currentDate.getDate()){
+                currentDate = merchant.transactions[i].date;
                 arrayIndex++;
             }
-            for(let j = 0; j < transactions[i].recipes.length; j++){
+            for(let j = 0; j < merchant.transactions[i].recipes.length; j++){
                 for(let merchRecipe of merchant.recipes){
-                    if(transactions[i].recipes[j].recipe === merchRecipe._id){
-                        dataList[arrayIndex] = parseFloat((dataList[arrayIndex] + (transactions[i].recipes[j].quantity * merchRecipe.price) / 100).toFixed(2));
+                    if(merchant.transactions[i].recipes[j].recipe === merchRecipe._id){
+                        dataList[arrayIndex] = parseFloat((dataList[arrayIndex] + (merchant.transactions[i].recipes[j].quantity * merchRecipe.price) / 100).toFixed(2));
                         break;
                     }
                 }