Bläddra i källkod

Created outline for the classes

Lee Morgan 6 år sedan
förälder
incheckning
e79f481a21

+ 9 - 8
controllers/renderer.js

@@ -37,14 +37,15 @@ module.exports = {
 
         Merchant.findOne({_id: req.session.user}, {password: 0, createdAt: 0})
             .populate("inventory.ingredient")
-            .populate({
-                path: "recipes",
-                model: "Recipe",
-                populate: {
-                    path: "ingredients.ingredient",
-                    model: "Ingredient"
-                }
-            })
+            .populate("recipes")
+            // .populate({
+            //     path: "recipes",
+            //     model: "Recipe",
+            //     populate: {
+            //         path: "ingredients.ingredient",
+            //         model: "Ingredient"
+            //     }
+            // })
             .then((merchant)=>{
                 if(merchant.pos === "clover"){
                     axios.get(`${process.env.CLOVER_ADDRESS}/v3/merchants/${merchant.posId}/orders?filter=clientCreatedTime>=${merchant.lastUpdatedTime}&expand=lineItems&access_token=${merchant.posAccessToken}`)

+ 271 - 0
views/dashboardPage/Merchant.js

@@ -0,0 +1,271 @@
+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
+        }
+    }
+
+    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");
+        }
+
+        if(errors.length > 0){
+            if(createBanner){
+                for(let i = 0; i < errors.length; i++){
+                    banner.createError(errors[i]);
+                }
+            }
+
+            return false;
+        }
+
+        return true;
+    }
+}
+
+class Recipe{
+    constructor(name, price, ingredients){
+        this.name = name;
+        this.price = price
+        this.ingredients = ingredients;
+    }
+}
+
+class Merchant{
+    constructor(oldMerchant, transactions){
+        this.name = oldMerchant.name;
+        this.inventory = [];
+        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
+            ));
+        }
+
+        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;
+                    }
+                }
+            }
+        }
+
+        for(let i = 0; i < transactions.length; i++){
+            this.transactions.push(new Transaction(
+                transactions[i].date,
+                transactions[i].recipes
+            ));
+        }
+    }
+
+    /*
+    Updates all specified item in the merchant's inventory and updates the page
+    If ingredient doesn't exist, add it
+    Inputs:
+    Array of objects
+        id: id of ingredient
+        quantityChange: change in quantity (if not removing)
+        name: name of ingredient (only for new ingredient)
+        category: category of ingredient (only for new ingredient)
+        unit: unit of measurement (only for new ingredient)
+    remove: if true, remove ingredient from inventory
+    */
+    addIngredients(ingredients, remove = false){
+        for(let i = 0; i < ingredients.length; i++){
+            let isNew = true;
+            for(let j = 0; j < merchant.inventory.length; j++){
+                if(merchant.inventory[j].ingredient._id === ingredients[i].id){
+                    if(remove){
+                        merchant.inventory.splice(j, 1);
+                    }else{
+    
+                        merchant.inventory[j].quantity += ingredients[i].quantity;
+                    }
+    
+                    isNew = false;
+                    break;
+                }
+            }
+    
+            if(isNew){
+                merchant.inventory.push({
+    
+                    ingredient: {
+                        _id: ingredients[i].id,
+                        category: ingredients[i].category,
+                        name: ingredients[i].name,
+                        unit: ingredients[i].unit
+                    },
+                    quantity: parseFloat(ingredients[i].quantityChange)
+                });
+            }
+        }
+    
+        homeStrandObj.drawInventoryCheckCard();
+        ingredientsStrandObj.populateByProperty("category");
+        addIngredientsComp.isPopulated = false;
+        closeSidebar();
+    }
+
+    /*
+    Updates a recipe in the merchants list of recipes
+    Can create, edit or remove
+    Inputs:
+        recipe: object
+            _id: id of recipe
+            name: name of recipe
+            price: price of recipe
+            ingredients: list of ingredients
+                ingredient: id of ingredient
+                quantity: quantity of ingredient
+        remove: if true, remove ingredient from inventory
+    */
+    addRecipe(recipe, remove = false){
+        let isNew = true;
+        let index = 0;
+
+        for(let i = 0; i < recipe.ingredients.length; i++){
+            for(let j = 0; j < merchant.inventory.length; j++){
+                if(merchant.inventory[j].ingredient._id === recipe.ingredients[i].ingredient){
+                    recipe.ingredients[i].ingredient = merchant.inventory[j].ingredient;
+                    break;
+                }
+            }
+        }
+
+        for(let i = 0; i < merchant.recipes.length; i++){
+            if(recipe._id === merchant.recipes[i]._id){
+                if(remove){
+                    merchant.recipes.splice(i, 1);
+                }else{
+                    merchant.recipes[i] = recipe;
+                    index = i;
+                }
+
+                isNew = false;
+                break;
+            }
+        }
+
+        if(isNew){
+            merchant.recipes.push(recipe);
+            index = merchant.recipes.length - 1;
+        }
+
+        recipeBookStrandObj.populateRecipes();
+        closeSidebar();
+    }
+
+    /*
+    Gets the indices of two dates from transactions
+    Inputs
+    from: starting date
+    to: ending date (default to now)
+    Output
+    Array containing starting index and ending index
+    Note: Will return false if it cannot find both necessary dates
+    */
+    getTransactionIndices(from, to = new Date()){
+        let indices = [];
+
+        for(let i = 0; i < this.transactions.length; i++){
+            if(this.transactions[i].date > from){
+                indices.push(i);
+                break;
+            }
+        }
+
+        for(let i = this.transactions.length - 1; i >=0; i--){
+            if(this.transactions[i].date < to){
+                indices.push(i);
+                break;
+            }
+        }
+
+        if(indices.length < 2){
+            return false;
+        }
+
+        return indices;
+    }
+
+    calculateRevenue(indices){
+        let total = 0;
+
+        for(let i = indices[0]; i <= indices[1]; i++){
+            for(let j = 0; j < this.transactions[i].recipes.length; j++){
+                for(let k = 0; k < this.recipes.length; k++){
+                    if(this.transactions[i].recipes[j].recipe === this.recipes[k]._id){
+                        total += this.transactions[i].recipes[j].quantity * this.recipes[k].price;
+                    }
+                }
+            }
+        }
+
+        return total / 100;
+    }
+}
+
+class Order{
+    constructor(date, ingredients){
+        this.date = date;
+        this.ingredients = ingredients;
+    }
+}
+
+class Transaction{
+    constructor(date, recipes){
+        this.date = new Date(date);
+        this.recipes = recipes;
+    }
+}
+
+let isSanitary =  (str, createBanner = true)=>{
+    let disallowed = ["\\", "<", ">", "$", "{", "}", "(", ")"];
+
+    for(let char of disallowed){
+        if(str.includes(char)){
+            if(createBanner){
+                banner.createError("Your string contains illegal characters");
+            }
+            return false;
+        }
+    }
+
+    return true;
+}

+ 0 - 37
views/dashboardPage/controller.js

@@ -182,39 +182,6 @@ let openSidebar = (sidebar)=>{
 
     sidebar.style.display = "flex";
 }
-
-/*
-Gets the indices of two dates from transactions
-Inputs
- from: starting date
- to: ending date (default to now)
-Output
- Array containing starting index and ending index
-Note: Will return false if it cannot find both necessary dates
-*/
-let dateIndices = (from, to = new Date())=>{
-    let indices = [];
-
-    for(let i = 0; i < transactions.length; i++){
-        if(transactions[i].date > from){
-            indices.push(i);
-            break;
-        }
-    }
-
-    for(let i = transactions.length - 1; i >=0; i--){
-        if(transactions[i].date < to){
-            indices.push(i);
-            break;
-        }
-    }
-
-    if(indices.length < 2){
-        return false;
-    }
-
-    return indices;
-}
 /*
 Gets the quantity of each ingredient sold between two dates (dateRange)
 Inputs
@@ -457,8 +424,4 @@ let recipesForIngredient = (ingredientId)=>{
     return recipes;
 }
 
-for(let transaction of transactions){
-    transaction.date = new Date(transaction.date);
-}
-
 homeStrandObj.display();

+ 10 - 2
views/dashboardPage/dashboard.ejs

@@ -209,8 +209,16 @@
             <% include ./components/newOrder %>
         </div>
 
-        <script>let merchant = <%- JSON.stringify(merchant) %>;</script>
-        <script>let transactions = <%- JSON.stringify(transactions) %>;</script>
+
+        <script src="/dashboardPage/Merchant.js"></script>
+        <script>
+            console.time("Classify");
+            let merchant = new Merchant(
+                <%- JSON.stringify(merchant) %>,
+                <%- JSON.stringify(transactions) %>
+            );
+            console.timeEnd("Classify");
+        </script>
         <script src="../shared/graphs.js"></script>
         <script src="/dashboardPage/home.js"></script>
         <script src="/dashboardPage/ingredients.js"></script>

+ 5 - 20
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 = this.calculateRevenue(dateIndices(firstOfMonth));
-        let revenueLastmonthToDay = this.calculateRevenue(dateIndices(firstOfLastMonth, lastMonthtoDay));
+        let revenueThisMonth = merchant.calculateRevenue(merchant.dateIndices(firstOfMonth));
+        let revenueLastmonthToDay = merchant.calculateRevenue(merchant.dateIndices(firstOfLastMonth, lastMonthtoDay));
 
         document.querySelector("#revenue").innerText = `$${revenueThisMonth.toLocaleString("en")}`;
 
@@ -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]].ingredient.name;
+            ingredientCheck.children[0].innerText = merchant.inventory[rands[i]].name;
             ingredientCheck.children[1].children[0].onclick = ()=>{input.value--};
             input.value = merchant.inventory[rands[i]].quantity;
             ingredientCheck.children[1].children[2].onclick = ()=>{input.value++}
-            ingredientCheck.children[2].innerText = merchant.inventory[rands[i]].ingredient.unit.toUpperCase();
+            ingredientCheck.children[2].innerText = merchant.inventory[rands[i]].unit.toUpperCase();
 
             ul.appendChild(ingredientCheck);
         }
@@ -110,6 +110,7 @@ window.homeStrandObj = {
         let thisMonth = new Date(now.getFullYear(), now.getMonth(), 1);
 
         let ingredientList = ingredientsSold(dateIndices(thisMonth));
+        console.log(ingredientList)
         if(ingredientList){
             for(let i = 0; i < 5; i++){
                 let max = ingredientList[0].quantity
@@ -144,22 +145,6 @@ window.homeStrandObj = {
         }
     },
 
-    calculateRevenue: function(indices){
-        let total = 0;
-
-        for(let i = indices[0]; i <= indices[1]; i++){
-            for(let j = 0; j < transactions[i].recipes.length; j++){
-                for(let k = 0; k < merchant.recipes.length; k++){
-                    if(transactions[i].recipes[j].recipe === merchant.recipes[k]._id){
-                        total += transactions[i].recipes[j].quantity * merchant.recipes[k].price;
-                    }
-                }
-            }
-        }
-
-        return total / 100;
-    },
-
     /*
     Create the data for the revenue graph
     Input: