Преглед изворни кода

Add sorting by date selection for data

Lee Morgan пре 6 година
родитељ
комит
f0e4b129c5

+ 2 - 1
controllers/renderer.js

@@ -153,7 +153,8 @@ module.exports = {
                     let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
 
                     Transaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}},
-                        {date: 1, recipes: 1, _id: 0})
+                        {date: 1, recipes: 1, _id: 0},
+                        {sort: {date: 1}})
                         .then((transactions)=>{
                             resolve({merchant: merchant, transactions: transactions});
                         })

+ 18 - 3
controllers/transactionData.js

@@ -3,6 +3,12 @@ const Purchase = require("../models/purchase");
 const Merchant = require("../models/merchant");
 
 module.exports = {
+    //POST - returns all transactions for a merchant between given dates
+    //Inputs:
+    //  req.body.from: start date
+    //  req.body.to: end date
+    //Returns:
+    //  List of transactions
     getTransactions: function(req, res){
         if(!req.session.user){
             req.session.error = "You must be logged in to view that page";
@@ -10,10 +16,19 @@ module.exports = {
         }
 
         let date = new Date();
-        let firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
-        let lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
+        let firstDay, lastDay;
+
+        if(req.body.from && req.body.to){
+            firstDay = new Date(req.body.from);
+            lastDay = new Date(req.body.to);
+        }else{
+            firstDay = new Date(date.getFullYear(), date.getMonth(), 1);
+            lastDay = new Date(date.getFullYear(), date.getMonth() + 1, 0);
+        }
 
-        Transaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}})
+        Transaction.find({merchant: req.session.user, date: {$gte: firstDay, $lt: lastDay}},
+            {_id: 0, date: 1, recipes: 1},
+            {sort: {date: 1}})
             .then((transactions)=>{
                 return res.json(transactions);
             })

+ 1 - 1
routes.js

@@ -41,7 +41,7 @@ module.exports = function(app){
     app.get("/cloverauth*", otherData.cloverAuth);
 
     //Transactions
-    app.get("/transactions", transactionData.getTransactions);
+    app.post("/transactions", transactionData.getTransactions);
     app.get("/purchases", transactionData.getPurchases);
     app.post("/transactions/create", transactionData.createTransaction);  //Creates transaction for non-pos merchant
     app.get("/populate", transactionData.populate);

+ 15 - 0
views/dataPage/data.ejs

@@ -19,6 +19,18 @@
         <div id="homeStrand" class="strand">
             <h2 id="month"></h2>
 
+            <div class="buttonBox">
+                <p>From: </p>
+
+                <input id="from" type="date">
+
+                <p>To: </p>
+
+                <input id="to" type="date">
+
+                <button id="dateSort" class="buttonDisabled">Submit</button>
+            </div>
+
             <div class="buttonBox">
                 <a class="button" href="/dashboard">Display Inventory</a>
             </div>
@@ -78,7 +90,10 @@
         </div>
 
         <script>let data = <%- JSON.stringify(data); %></script>
+        <script src="https://unpkg.com/axios/dist/axios.min.js"></script>
+        <script src="/shared/validation.js"></script>
         <script src="/dataPage/home.js"></script>
+        <script src="/dataPage/fetchData.js"></script>
         <script src="/shared/controller.js"></script>
     </body>
 </html>

+ 21 - 0
views/dataPage/fetchData.js

@@ -0,0 +1,21 @@
+let date = new Date();
+let yearAgo = new Date(date.getFullYear() - 1, date.getMonth(), date.getDate());
+
+let onDataLoad = function(){
+    let dateSort = document.querySelector("#dateSort");
+    dateSort.onclick = ()=>{window.homeObj.newDates()};
+    dateSort.classList = "button";
+}
+
+axios.post("/transactions", {from: yearAgo, to: date})
+    .then((response)=>{
+        if(typeof(response.data) === "string"){
+            banner.createError(response.data);
+        }else{
+            data.transactions = response.data;
+            onDataLoad();
+        }
+    })
+    .catch((err)=>{
+        console.log(err);
+    });

+ 56 - 3
views/dataPage/home.js

@@ -1,6 +1,8 @@
 window.homeObj = {
     recipeTotal: 0,
     revenueTotal: 0,
+    dateFrom: "",
+    dateTo: "",
 
     display: function(){
         clearScreen();
@@ -10,10 +12,15 @@ window.homeObj = {
         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();
+        document.querySelector("#to").valueAsDate = new Date();
+
+        this.populate(data.transactions);
     },
 
-    populate: function(){
+    populate: function(transactions){
+        this.recipeTotal = 0;
+        this.revenueTotal = 0;
+        
         //Create object to store number of recipes sold
         let recipes = [];
         for(let recipe of data.merchant.recipes){
@@ -50,7 +57,7 @@ window.homeObj = {
         }
         
         //Populate number of recipes sold
-        for(let transaction of data.transactions){
+        for(let transaction of transactions){
             for(let recipe of transaction.recipes){
                 for(let newRecipe of recipes){
                     if(recipe.recipe === newRecipe.id){
@@ -89,6 +96,10 @@ window.homeObj = {
 
         //Populate Ingredients table
         let ingredientsBody = document.querySelector("#ingredientsData tbody");
+
+        while(ingredientsBody.children.length > 0){
+            ingredientsBody.removeChild(ingredientsBody.firstChild);
+        }
         
         for(let ingredient of soldIngredients){
             let row = document.createElement("tr");
@@ -110,6 +121,10 @@ window.homeObj = {
         //Populate recipes table
         let recipesBody = document.querySelector("#recipesData tbody");
 
+        while(recipesBody.children.length > 0){
+            recipesBody.removeChild(recipesBody.firstChild);
+        }
+
         for(let recipe of recipes){
             let row = document.createElement("tr");
             recipesBody.appendChild(row);
@@ -129,6 +144,10 @@ window.homeObj = {
 
         //Populate purchases table
         let purchasesBody = document.querySelector("#purchasesData tbody");
+
+        while(purchasesBody.children.length > 0){
+            purchasesBody.removeChild(purchasesBody.firstChild);
+        }
         
         for(let ingredient of purchaseIngredients){
             let row = document.createElement("tr");
@@ -146,5 +165,39 @@ window.homeObj = {
         //Populate totals
         document.querySelector("#revenueTotal").innerText = `$${this.revenueTotal.toFixed(2)}`;
         document.querySelector("#soldTotal").innerText = this.recipeTotal;
+    },
+
+    newDates: function(){
+        let from = document.querySelector("#from").value;
+        let to = new Date(document.querySelector("#to").value);
+
+        if(from === "" || to === ""){
+            banner.createError("Invalid date");
+            return;
+        }else{
+            from = new Date(from);
+            to = new Date(to);
+        }
+
+        if(validator.transaction.date(from, to)){
+            let startIndex = 0;
+            let endIndex = 0;
+
+            for(let i = 0; i < data.transactions.length; i++){
+                if(from < new Date(data.transactions[i].date)){
+                    startIndex = i;
+                    break;
+                }
+            }
+
+            for(let i = 0; i < data.transactions.length; i++){
+                if(to < new Date(data.transactions[i].date)){
+                    endIndex = i;
+                    break;
+                }
+            }
+
+            this.populate(data.transactions.slice(startIndex, endIndex));
+        }
     }
 }

+ 27 - 0
views/shared/validation.js

@@ -110,6 +110,33 @@ let validator = {
         }
     },
 
+    transaction: {
+        date: function(from, to = new Date(), createBanner = true){
+            let errors = [];
+            let today = new Date();
+
+            if(from > to){
+                errors.push("Starting date must be before ending date");
+            }
+
+            if(from > today || to > today){
+                errors.push("Cannot choose a date in the future");
+            }
+
+            if(errors.length > 0){
+                if(createBanner){
+                    for(let error of errors){
+                        banner.createError(error);
+                    }
+
+                    return false;
+                }
+            }
+
+            return true;
+        }
+    },
+
     isSanitary: function(str){
         let disallowed = ["\\", "<", ">", "$"];