Răsfoiți Sursa

Add choosing and retrieval transactions for certain dates. Nothing displays on the frontend yet.

Lee Morgan 5 ani în urmă
părinte
comite
1f104b3617

+ 28 - 0
controllers/transactionData.js

@@ -110,6 +110,34 @@ module.exports = {
             });
     },
 
+    getTransactionsByDate: function(req, res){
+        if(!req.session.user){
+            req.session.error = "MUST BE LOGGED IN TO DO THAT";
+            return res.redirect("/");
+        }
+
+        const from = new Date(req.body.from);
+        let to = new Date(req.body.to);
+        to.setDate(to.getDate() + 1);
+
+        Transaction.aggregate([
+            {$match: {
+                merchant: ObjectId(req.session.user),
+                date: {
+                    $gte: from,
+                    $lt: to
+                }
+            }},
+            {$sort: {date: 1}}
+        ])
+            .then((transactions)=>{
+                return res.json(transactions);
+            })
+            .catch((err)=>{
+                return res.json("ERROR: UNABLE TO RETRIEVE YOUR DATA");
+            });
+    },
+
     /*
     GET - Creates 5000 transactions for logged in merchant for testing
     */

+ 1 - 0
routes.js

@@ -44,6 +44,7 @@ module.exports = function(app){
     app.post("/transaction", transactionData.getTransactions);
     app.post("/transaction/create", transactionData.createTransaction);
     app.delete("/transaction/:id", transactionData.remove);
+    app.post("/transaction/retrieve", transactionData.getTransactionsByDate);
     app.get("/populatesometransactions", transactionData.populate);
 
     //Other

+ 45 - 1
views/dashboardPage/bundle.js

@@ -604,6 +604,13 @@ let analytics = {
     display: function(){
         const itemsList = document.getElementById("itemsList");
 
+        let now = new Date();
+        let lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate(), now.getHours(), now.getMinutes());
+
+        document.getElementById("analStartDate").valueAsDate = lastMonth;
+        document.getElementById("analEndDate").valueAsDate = now;
+        document.getElementById("analDateBtn").onclick = ()=>{this.changeDates()};
+
         while(itemsList.children.length > 0){
             itemsList.removeChild(itemsList.firstChild);
         }
@@ -626,7 +633,7 @@ let analytics = {
         li.classList.add("analItemActive");
 
         let startDate = new Date();
-        startDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() - 30);
+        startDate = new Date(startDate.getFullYear(), startDate.getMonth() - 1, startDate.getDate(), startDate.getHours(), startDate.getMinutes());
 
         //Get list of recipes that contain the ingredient
         let containingRecipes = [];
@@ -694,6 +701,43 @@ let analytics = {
             sum += quantities[i];
         }
         document.getElementById("analAvgUse").innerText = `${(sum / 30).toFixed(2)} ${ingredient.ingredient.unit}`;        
+    },
+
+    changeDates: function(){
+        let dates = {
+            from: document.getElementById("analStartDate").valueAsDate,
+            to: document.getElementById("analEndDate").valueAsDate
+        }
+
+        if(dates.from > dates.to || dates.from === "" || dates.to === "" || dates.to > new Date()){
+            banner.createError("INVALID DATE");
+            return;
+        }
+
+        let loader = document.getElementById("loaderContainer");
+        loader.style.display = "flex";
+
+        fetch("/transaction/retrieve", {
+            method: "post",
+            headers: {
+                "Content-Type": "application/json;charset=utf-8"
+            },
+            body: JSON.stringify(dates)
+        })
+            .then((response)=>response.json())
+            .then((response)=>{
+                if(typeof(response) === "string"){
+                    banner.createError(response.data);
+                }else{
+                    console.log(response);
+                }
+            })
+            .catch((err)=>{
+                banner.createError("ERROR: UNABLE TO DISPLAY THE DATA");
+            })
+            .finally(()=>{
+                loader.style.display = "none";
+            });
     }
 }
 

+ 12 - 0
views/dashboardPage/dashboard.ejs

@@ -257,6 +257,18 @@
             <div id="analyticsStrand" class="strand">
                 <div class="strandHead">
                     <h1 class="strandTitle">ANALYTICS</h1>
+
+                    <div>
+                        <label>FROM:
+                            <input id="analStartDate" type="date">
+                        </label>
+    
+                        <label>TO:
+                            <input id="analEndDate" type="date">
+                        </label>
+    
+                        <button id="analDateBtn" class="button">SUBMIT</button>
+                    </div>
                 </div>
 
                 <div class="analContent">

+ 45 - 1
views/dashboardPage/js/analytics.js

@@ -2,6 +2,13 @@ let analytics = {
     display: function(){
         const itemsList = document.getElementById("itemsList");
 
+        let now = new Date();
+        let lastMonth = new Date(now.getFullYear(), now.getMonth() - 1, now.getDate(), now.getHours(), now.getMinutes());
+
+        document.getElementById("analStartDate").valueAsDate = lastMonth;
+        document.getElementById("analEndDate").valueAsDate = now;
+        document.getElementById("analDateBtn").onclick = ()=>{this.changeDates()};
+
         while(itemsList.children.length > 0){
             itemsList.removeChild(itemsList.firstChild);
         }
@@ -24,7 +31,7 @@ let analytics = {
         li.classList.add("analItemActive");
 
         let startDate = new Date();
-        startDate = new Date(startDate.getFullYear(), startDate.getMonth(), startDate.getDate() - 30);
+        startDate = new Date(startDate.getFullYear(), startDate.getMonth() - 1, startDate.getDate(), startDate.getHours(), startDate.getMinutes());
 
         //Get list of recipes that contain the ingredient
         let containingRecipes = [];
@@ -92,6 +99,43 @@ let analytics = {
             sum += quantities[i];
         }
         document.getElementById("analAvgUse").innerText = `${(sum / 30).toFixed(2)} ${ingredient.ingredient.unit}`;        
+    },
+
+    changeDates: function(){
+        let dates = {
+            from: document.getElementById("analStartDate").valueAsDate,
+            to: document.getElementById("analEndDate").valueAsDate
+        }
+
+        if(dates.from > dates.to || dates.from === "" || dates.to === "" || dates.to > new Date()){
+            banner.createError("INVALID DATE");
+            return;
+        }
+
+        let loader = document.getElementById("loaderContainer");
+        loader.style.display = "flex";
+
+        fetch("/transaction/retrieve", {
+            method: "post",
+            headers: {
+                "Content-Type": "application/json;charset=utf-8"
+            },
+            body: JSON.stringify(dates)
+        })
+            .then((response)=>response.json())
+            .then((response)=>{
+                if(typeof(response) === "string"){
+                    banner.createError(response.data);
+                }else{
+                    console.log(response);
+                }
+            })
+            .catch((err)=>{
+                banner.createError("ERROR: UNABLE TO DISPLAY THE DATA");
+            })
+            .finally(()=>{
+                loader.style.display = "none";
+            });
     }
 }