Explorar o código

Retrieve transaction data from backend. Manipulate data into usable format.

Lee Morgan %!s(int64=6) %!d(string=hai) anos
pai
achega
7ffa2453c9

+ 1 - 1
controllers/renderer.js

@@ -27,7 +27,7 @@ module.exports = {
     //Renders inventoryPage
     displayInventory: function(req, res){
         if(!req.session.user){
-            req.session.error = "You must logged in to view that page";
+            req.session.error = "You must be logged in to view that page";
             return res.redirect("/");
         }
 

+ 24 - 0
controllers/transactionData.js

@@ -0,0 +1,24 @@
+const Transaction = require("../models/transaction");
+
+module.exports = {
+    getTransactions: function(req, res){
+        if(!req.session.user){
+            req.session.error = "You must be logged in to view that page";
+            return res.redirect("/");
+        }
+
+        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})
+            .then((transactions)=>{
+                return res.json(transactions);
+            })
+            .catch((err)=>{
+                return res.json("Error: could not retrieve sales data");
+            });
+    }
+}
+
+// {merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}}

+ 4 - 0
routes.js

@@ -2,6 +2,7 @@ const renderer = require("./controllers/renderer");
 const merchantData = require("./controllers/merchantData");
 const ingredientData = require("./controllers/ingredientData");
 const otherData = require("./controllers/otherData");
+const transactionData = require("./controllers/transactionData");
 
 module.exports = function(app){
     //Render page
@@ -38,4 +39,7 @@ module.exports = function(app){
     app.post("/email", otherData.checkUniqueEmail);
     app.get("/cloverlogin", otherData.clover);
     app.get("/cloverauth*", otherData.cloverAuth);
+
+    //Transactions
+    app.get("/transactions", transactionData.getTransactions);
 }

+ 56 - 0
views/inventoryPage/data.js

@@ -0,0 +1,56 @@
+window.dataObj = {
+    display: function(){
+        clearScreen();
+        document.querySelector("#dataStrand").style.display = "flex";
+    },
+
+    populate: function(transactions){
+        //Create object to store number of recipes sold
+        let recipes = [];
+        for(let recipe of merchant.recipes){
+            recipes.push({
+                id: recipe._id,
+                name: recipe.name,
+                quantity: 0,
+                ingredients: recipe.ingredients 
+            });
+        }
+
+        //Populate number of recipes sold
+        for(let transaction of transactions){
+            for(let transactionRecipe of transaction.recipes){
+                for(let recipeCounter of recipes){
+                    if(transactionRecipe === recipeCounter.id){
+                        recipeCounter.quantity++;
+                    }
+                }
+            }
+        }
+
+        //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
+            });
+        }
+
+        //Populate amount of ingredients sold
+        for(let recipe of recipes){
+            for(let recipeIngredient of recipe.ingredients){
+                for(let newIngredient of ingredients){
+                    if(newIngredient.id === recipeIngredient.ingredient._id){
+                        newIngredient.quantity += recipeIngredient.quantity;
+                        break;
+                    }
+                }
+            }
+        }
+
+        //Populate Ingredients table
+        
+    }
+}

+ 14 - 0
views/inventoryPage/inventory.css

@@ -81,12 +81,26 @@
                 color: rgb(0, 27, 45);
             }
 
+/* Data Strand */
+#dataStrand{
+    flex-direction: column;
+    align-items: center;
+}
+
+    #dataStrand > *{
+        margin: 15px;
+    }
+
 /* Account Strand */
 #accountStrand{
     flex-direction: column;
     align-items: center;
 }
 
+    #accountStrand > *{
+        margin: 15px;
+    }
+
     #accountStrand label{
         display: flex;
     }

+ 34 - 0
views/inventoryPage/inventory.ejs

@@ -55,6 +55,25 @@
             <div id="recipesContainer"></div>
         </div>
 
+        <div id="dataStrand" class="strand">
+            <!-- Ingredients used -->
+            <table>
+                <thead>
+                    <tr>
+                        <th>Ingredient</th>
+                        <th>Amount used</th>
+                        <th>Amount remaining</th>
+                    </tr>
+                </thead>
+                <tbody></tbody>
+            </table>
+
+            <!-- Recipes used -->
+
+            <!-- Purchases -->
+
+            <!-- Totals -->
+        </div>
 
         <div id="accountStrand" class="strand">
             <form id="accountDisplay" onsubmit="accountObj.editAccount()">
@@ -221,11 +240,26 @@
         <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)=>{
+                    banner.createError("Error: unable to render sales data");
+                });
+        </script>
     </body>
 </html>