Bladeren bron

Move data to a seperate page

Lee Morgan 6 jaren geleden
bovenliggende
commit
3c45bdf02b

+ 54 - 0
controllers/renderer.js

@@ -3,6 +3,7 @@ const axios = require("axios");
 const Merchant = require("../models/merchant");
 const Ingredient = require("../models/ingredient");
 const Transaction = require("../models/transaction");
+const Purchase = require("../models/purchase");
 
 module.exports = {
     //GET - Shows the public landing page
@@ -205,5 +206,58 @@ module.exports = {
     //Renders information page
     displayLegal: function(req, res){
         return res.render("informationPage/information");
+    },
+
+    displayData: function(req, res){
+        if(!req.session.user){
+            req.session.error = "You must be logged in to view that page";
+            return res.redirect("/");
+        }
+
+        let merchantPromise = new Promise((resolve, reject)=>{
+            Merchant.findOne({_id: req.session.user})
+                .populate("recipes")
+                .populate("inventory.ingredient")
+                .then((merchant)=>{
+                    resolve(merchant);
+                })
+                .catch((err)=>{});
+        });
+
+        let transactionPromise = new Promise((resolve, reject)=>{
+            let date = new Date();
+            let firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
+            let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
+
+            Transaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}})
+                .then((transactions)=>{
+                    resolve(transactions);
+                })
+                .catch((err)=>{});
+        });
+
+        let purchasePromise = new Promise((resolve, reject)=>{
+            Purchase.find({merchant: req.session.user})
+                .then((purchases)=>{
+                    resolve(purchases);
+                })
+                .catch((err)=>{});
+        });
+
+        Promise.all([merchantPromise, transactionPromise, purchasePromise])
+            .then((response)=>{
+                let data = {
+                    merchant: response[0],
+                    transactions: response[1],
+                    purchases: response[2]
+                }
+
+                return res.render("dataPage/data", {data: data});
+            })
+            .catch((err)=>{
+                req.session.error = "Error: unable to retrieve user data";
+
+                return res.redirect("/");
+            });
     }
 }

+ 1 - 3
controllers/transactionData.js

@@ -35,6 +35,4 @@ module.exports = {
                 return res.json("Error: could not retrieve purchases data");
             })
     }
-}
-
-// {merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}}
+}

+ 1 - 0
routes.js

@@ -12,6 +12,7 @@ module.exports = function(app){
     app.get("/merchant/new/none", renderer.merchantSetupNone);
     app.get("/recipes", renderer.displayRecipes);
     app.get("/information", renderer.displayLegal);
+    app.get("/data", renderer.displayData);
 
     //Merchant
     app.get("/merchant/recipes/update", merchantData.updateRecipes);

+ 28 - 0
views/dataPage/data.css

@@ -0,0 +1,28 @@
+#title{
+    font-size: 45px;
+    color: rgb(255, 99, 107);
+    text-align: center;
+    margin-top: 25px;
+}
+
+/* Home Strand */
+#homeStrand{
+    flex-direction: column;
+    align-items: center;
+}
+
+    #homeStrand > *{
+        margin: 15px;
+    }
+
+    .tables{
+        display: flex;
+        justify-content: space-around;
+        width: 100%;
+    }
+
+    .dataTable{
+        display: flex;
+        flex-direction: column;
+        align-items: center;
+    }

+ 79 - 0
views/dataPage/data.ejs

@@ -0,0 +1,79 @@
+<!DOCTYPE html>
+<html lang="en">
+    <head>
+        <meta charset="utf-8">
+        <title>The Subline</title>
+        <link rel="icon" type="img/png" href="/shared/images/logo.png">
+        <link rel="stylesheet" href="/shared/shared.css">
+        <link rel="stylesheet" href="/dataPage/data.css">
+    </head>
+    <body>
+        <% include ../shared/header %>
+
+        <% include ../shared/banner %>
+
+        <h1 id="title"><%=data.merchant.name%></h1>
+
+        <strand-selector></strand-selector>
+
+        <div id="homeStrand" class="strand">
+            <h2 id="month"></h2>
+            <div class="tables">
+                <!-- Ingredients used -->
+                <div id="ingredientsData" class="dataTable">
+                    <h3>Ingredients</h3>
+                    <table>
+                        <thead>
+                            <tr>
+                                <th>Ingredient</th>
+                                <th>Used</th>
+                                <th>Remaining</th>
+                            </tr>
+                        </thead>
+                        <tbody></tbody>
+                    </table>
+                </div>
+
+                <!-- Recipes used -->
+                <div id="recipesData" class="dataTable">
+                    <h3>Recipes</h3>
+
+                    <div>
+                        <p>Total Revenue: <span id="revenueTotal"></span></p>
+                        <p>Total sold: <span id="soldTotal"></span></p>
+                    </div>
+
+                    <table>
+                        <thead>
+                            <tr>
+                                <th>Recipe</th>
+                                <th># sold</th>
+                                <th>Revenue</th>
+                            </tr>
+                        </thead>
+                        <tbody></tbody>
+                    </table>
+                </div>
+
+                <!-- Purchases -->
+                <div id="purchasesData" class="dataTable">
+                    <h3>Purchases</h3>
+
+                    <table>
+                        <thead>
+                            <tr>
+                                <th>Ingredient</th>
+                                <th>Amount</th>
+                            </tr>
+                        </thead>
+                        <tbody></tbody>
+                    </table>
+                </div>
+            </div>
+        </div>
+
+        <script>let data = <%- JSON.stringify(data); %></script>
+        <script src="/dataPage/home.js"></script>
+        <script src="/shared/controller.js"></script>
+    </body>
+</html>

+ 48 - 51
views/inventoryPage/data.js → views/dataPage/home.js

@@ -1,17 +1,19 @@
-window.dataObj = {
+window.homeObj = {
     display: function(){
         clearScreen();
-        document.querySelector("#dataStrand").style.display = "flex";
+        document.querySelector("#homeStrand").style.display = "flex";
 
         //Fill in month
         let months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
         document.querySelector("#month").innerText = `Month of ${months[new Date().getMonth()]}`;
+
+        this.populate();
     },
 
-    populate: function(transactions){
+    populate: function(){
         //Create object to store number of recipes sold
         let recipes = [];
-        for(let recipe of merchant.recipes){
+        for(let recipe of data.merchant.recipes){
             recipes.push({
                 id: recipe._id,
                 name: recipe.name,
@@ -21,12 +23,33 @@ window.dataObj = {
             });
         }
 
-        console.log(transactions);
+        //Create object to store amount of ingredients sold
+        let soldIngredients = [];
+        for(let item of data.merchant.inventory){
+            soldIngredients.push({
+                id: item.ingredient._id,
+                name: item.ingredient.name,
+                quantity: 0,
+                quantityRemaining: item.quantity,
+                unit: item.ingredient.unit
+            });
+        }
 
+        //Create object for each merchant ingredient
+        let purchaseIngredients = [];
+        for(let item of data.merchant.inventory){
+            purchaseIngredients.push({
+                id: item.ingredient._id,
+                name: item.ingredient.name,
+                amount: 0,
+                unit: item.ingredient.unit
+            });
+        }
+        
         //Populate number of recipes sold
         let revenueTotal = 0;
         let recipeTotal = 0;
-        for(let transaction of transactions){
+        for(let transaction of data.transactions){
             for(let transactionRecipe of transaction.recipes){
                 for(let recipeCounter of recipes){
                     if(transactionRecipe === recipeCounter.id){
@@ -39,22 +62,10 @@ window.dataObj = {
             }
         }
 
-        //Create object to store amount of ingredients sold
-        let ingredients = [];
-        for(let item of merchant.inventory){
-            ingredients.push({
-                id: item.ingredient._id,
-                name: item.ingredient.name,
-                quantity: 0,
-                quantityRemaining: item.quantity,
-                unit: item.ingredient.unit
-            });
-        }
-
         //Populate amount of ingredients sold
         for(let recipe of recipes){
             for(let recipeIngredient of recipe.ingredients){
-                for(let newIngredient of ingredients){
+                for(let newIngredient of soldIngredients){
                     if(newIngredient.id === recipeIngredient.ingredient._id){
                         newIngredient.quantity += (recipeIngredient.quantity * recipe.quantity);
                         break;
@@ -63,10 +74,22 @@ window.dataObj = {
             }
         }
 
+        //Populate amount of ingredients purchased
+        for(let purchase of data.purchases){
+            for(let newPurchaseIngredient of purchase.ingredients){
+                for(let newIngredient of purchaseIngredients){
+                    if(newIngredient.id === newPurchaseIngredient.ingredient){
+                        newIngredient.amount += newPurchaseIngredient.quantity;
+                        break;
+                    }
+                }
+            }
+        }
+
         //Populate Ingredients table
         let ingredientsBody = document.querySelector("#ingredientsData tbody");
         
-        for(let ingredient of ingredients){
+        for(let ingredient of soldIngredients){
             let row = document.createElement("tr");
             ingredientsBody.appendChild(row);
 
@@ -103,40 +126,10 @@ window.dataObj = {
             row.appendChild(revenue);
         }
 
-        //Populate totals
-        document.querySelector("#revenueTotal").innerText = `$${revenueTotal.toFixed(2)}`;
-        document.querySelector("#soldTotal").innerText = recipeTotal;
-    },
-
-    populatePurchases: function(purchases){
-        //Create object for each merchant ingredient
-        let ingredients = [];
-
-        for(let item of merchant.inventory){
-            ingredients.push({
-                id: item.ingredient._id,
-                name: item.ingredient.name,
-                amount: 0,
-                unit: item.ingredient.unit
-            });
-        }
-
-        //Populate amount of ingredients purchased
-        for(let purchase of purchases){
-            for(let purchaseIngredient of purchase.ingredients){
-                for(let newIngredient of ingredients){
-                    if(newIngredient.id === purchaseIngredient.ingredient){
-                        newIngredient.amount += purchaseIngredient.quantity;
-                        break;
-                    }
-                }
-            }
-        }
-
         //Populate purchases table
         let purchasesBody = document.querySelector("#purchasesData tbody");
         
-        for(let ingredient of ingredients){
+        for(let ingredient of purchaseIngredients){
             let row = document.createElement("tr");
             purchasesBody.appendChild(row);
             
@@ -148,5 +141,9 @@ window.dataObj = {
             amount.innerText = ingredient.amount;
             row.appendChild(amount);
         }
+
+        //Populate totals
+        document.querySelector("#revenueTotal").innerText = `$${revenueTotal.toFixed(2)}`;
+        document.querySelector("#soldTotal").innerText = recipeTotal;
     }
 }

+ 0 - 22
views/inventoryPage/inventory.css

@@ -81,28 +81,6 @@
                 color: rgb(0, 27, 45);
             }
 
-/* Data Strand */
-#dataStrand{
-    flex-direction: column;
-    align-items: center;
-}
-
-    #dataStrand > *{
-        margin: 15px;
-    }
-
-    .tables{
-        display: flex;
-        justify-content: space-around;
-        width: 100%;
-    }
-
-    .dataTable{
-        display: flex;
-        flex-direction: column;
-        align-items: center;
-    }
-
 /* Account Strand */
 #accountStrand{
     flex-direction: column;

+ 1 - 90
views/inventoryPage/inventory.ejs

@@ -16,7 +16,6 @@
 
         <strand-selector></strand-selector>
 
-
         <div id="inventoryStrand" class="strand">
             <div class="options">
                 <button class="button" onclick="addIngredientObj.display()">Add Ingredient</button>
@@ -44,7 +43,6 @@
             </table>
         </div>
 
-
         <div id="recipesStrand" class="strand">
             <div>
                 <% if(merchant.pos !== "none"){ %>
@@ -55,62 +53,6 @@
             <div id="recipesContainer"></div>
         </div>
 
-        <div id="dataStrand" class="strand">
-            <h2 id="month"></h2>
-            <div class="tables">
-                <!-- Ingredients used -->
-                <div id="ingredientsData" class="dataTable">
-                    <h3>Ingredients</h3>
-                    <table>
-                        <thead>
-                            <tr>
-                                <th>Ingredient</th>
-                                <th>Used</th>
-                                <th>Remaining</th>
-                            </tr>
-                        </thead>
-                        <tbody></tbody>
-                    </table>
-                </div>
-
-                <!-- Recipes used -->
-                <div id="recipesData" class="dataTable">
-                    <h3>Recipes</h3>
-
-                    <div>
-                        <p>Total Revenue: <span id="revenueTotal"></span></p>
-                        <p>Total sold: <span id="soldTotal"></span></p>
-                    </div>
-
-                    <table>
-                        <thead>
-                            <tr>
-                                <th>Recipe</th>
-                                <th># sold</th>
-                                <th>Revenue</th>
-                            </tr>
-                        </thead>
-                        <tbody></tbody>
-                    </table>
-                </div>
-
-                <!-- Purchases -->
-                <div id="purchasesData" class="dataTable">
-                    <h3>Purchases</h3>
-
-                    <table>
-                        <thead>
-                            <tr>
-                                <th>Ingredient</th>
-                                <th>Amount</th>
-                            </tr>
-                        </thead>
-                        <tbody></tbody>
-                    </table>
-                </div>
-            </div>
-        </div>
-
         <div id="accountStrand" class="strand">
             <form id="accountDisplay" onsubmit="accountObj.editAccount()">
                 <label>Name:&nbsp;&nbsp;
@@ -269,47 +211,16 @@
                     let error = undefined;
                 <% } %>
         </script>
-        <script>
-            let merchant = <%- JSON.stringify(merchant) %>;
-        </script>
+        <script>let merchant = <%- JSON.stringify(merchant) %>;</script>
         <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
         <script src="../shared/validation.js"></script>
         <script src="/inventoryPage/inventory.js"></script>
         <script src="/inventoryPage/recipes.js"></script>
-        <script src="/inventoryPage/data.js"></script>
         <script src="/inventoryPage/account.js"></script>
         <script src="/inventoryPage/addIngredient.js"></script>
         <script src="/inventoryPage/enterTransactions.js"></script>
         <script src="/inventoryPage/enterPurchase.js"></script>
         <script src="/inventoryPage/singleRecipe.js"></script>
         <script src="/shared/controller.js"></script>
-
-        <script>
-            axios.get("/transactions")
-                .then((response)=>{
-                    if(typeof(response.data) === "string"){
-                        banner.createError(response.data);
-                    }else{
-                        dataObj.populate(response.data);
-                    }
-                })
-                .catch((err)=>{
-                    console.log(err);
-                    banner.createError("Error: unable to render sales data");
-                });
-
-            axios.get("/purchases")
-                .then((response)=>{
-                    if(typeof(response.data) === "string"){
-                        banner.createError(response.data);
-                    }else{
-                        dataObj.populatePurchases(response.data);
-                    }
-                })
-                .catch((err)=>{
-                    console.log(err);
-                    banner.createError("Error: unable to render purchases data");
-                })
-        </script>
     </body>
 </html>

+ 1 - 1
views/shared/header.ejs

@@ -4,7 +4,7 @@
         <h1>SUBLINE</h1>
     </a>
     
-    <% if(locals.merchant){ %>
+    <% if(locals.merchant || locals.data){ %>
         <a class="logout" href="/logout">Log out</a>
     <% } %>
 </div>