Explorar o código

Create backend aggregation for a simple query on transactions

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

+ 78 - 43
controllers/transactionData.js

@@ -1,59 +1,39 @@
 const Transaction = require("../models/transaction");
 const Merchant = require("../models/merchant");
 
+const ObjectId = require("mongoose").Types.ObjectId;
+
 module.exports = {
     /*
-    GET - Creates 5000 transactions for logged in merchant for testing
+    POST - retrieves a list of transactions based on the filter
+    req.body = {
+        startDate: starting date to filter on,
+        endDate: ending date to filter on,
+        recipes: list of recipes to filter on
+    }
     */
-    populate: function(req, res){
+    getTransactions: function(req, res){
         if(!req.session.user){
-            res.session.error = "Must be logged in to do that";
+            req.session.error = "MUST BE LOGGED IN TO DO THAT";
             return res.redirect("/");
         }
 
-        function randomDate() {
-            let now = new Date();
-            let start = new Date();
-            start.setFullYear(now.getFullYear() - 1);
-            return new Date(start.getTime() + Math.random() * (now.getTime() - start.getTime()));
-        }
-
-        Merchant.findOne({_id: req.session.user})
-            .then((merchant)=>{
-                let newTransactions = [];
-
-                for(let i = 0; i < 5000; i++){
-                    let newTransaction = new Transaction({
-                        merchant: merchant._id,
-                        date: randomDate(),
-                        recipes: []
-                    });
-
-                    let numberOfRecipes = Math.floor((Math.random() * 5) + 1);
-
-                    for(let j = 0; j < numberOfRecipes; j++){
-                        let recipeNumber = Math.floor(Math.random() * merchant.recipes.length);
-                        let randQuantity = Math.floor((Math.random() * 3) + 1);
-
-                        newTransaction.recipes.push({
-                            recipe: merchant.recipes[recipeNumber],
-                            quantity: randQuantity
-                        });
-                    }
-
-                    newTransactions.push(newTransaction);
+        let startDate = new Date(req.body.startDate);
+        let endDate = new Date(req.body.endDate);
+        Transaction.aggregate([
+            {$match: {
+                merchant: new ObjectId(req.session.user),
+                date: {
+                    $gte: startDate,
+                    $lt: endDate
                 }
-
-                Transaction.create(newTransactions)
-                    .then((transactions)=>{
-                        return res.redirect("/dashboard");
-                    })
-                    .catch((err)=>{
-                        return;
-                    });
+            }}
+        ])
+            .then((transactions)=>{
+                return res.json(transactions);
             })
             .catch((err)=>{
-                return;
+                return res.json("ERROR: UNABLE TO RETRIEVE YOUR TRANSACTIONS");
             });
     },
 
@@ -105,5 +85,60 @@ module.exports = {
             .catch((err)=>{
                 return res.json("ERROR: UNABLE TO DELETE TRANSACTION");
             });
+    },
+
+    /*
+    GET - Creates 5000 transactions for logged in merchant for testing
+    */
+    populate: function(req, res){
+        if(!req.session.user){
+            res.session.error = "Must be logged in to do that";
+            return res.redirect("/");
+        }
+
+        function randomDate() {
+            let now = new Date();
+            let start = new Date();
+            start.setFullYear(now.getFullYear() - 1);
+            return new Date(start.getTime() + Math.random() * (now.getTime() - start.getTime()));
+        }
+
+        Merchant.findOne({_id: req.session.user})
+            .then((merchant)=>{
+                let newTransactions = [];
+
+                for(let i = 0; i < 5000; i++){
+                    let newTransaction = new Transaction({
+                        merchant: merchant._id,
+                        date: randomDate(),
+                        recipes: []
+                    });
+
+                    let numberOfRecipes = Math.floor((Math.random() * 5) + 1);
+
+                    for(let j = 0; j < numberOfRecipes; j++){
+                        let recipeNumber = Math.floor(Math.random() * merchant.recipes.length);
+                        let randQuantity = Math.floor((Math.random() * 3) + 1);
+
+                        newTransaction.recipes.push({
+                            recipe: merchant.recipes[recipeNumber],
+                            quantity: randQuantity
+                        });
+                    }
+
+                    newTransactions.push(newTransaction);
+                }
+
+                Transaction.create(newTransactions)
+                    .then((transactions)=>{
+                        return res.redirect("/dashboard");
+                    })
+                    .catch((err)=>{
+                        return;
+                    });
+            })
+            .catch((err)=>{
+                return;
+            });
     }
 }

+ 2 - 1
routes.js

@@ -38,7 +38,8 @@ module.exports = function(app){
     app.delete("/order/:id", orderData.removeOrder);
 
     //Transactions
-    app.post("/transaction", transactionData.createTransaction);
+    app.post("/transaction", transactionData.getTransactions);
+    app.post("/transaction/create", transactionData.createTransaction);
     app.delete("/transaction/:id", transactionData.remove);
     app.get("/populatesometransactions", transactionData.populate);
 

+ 1 - 1
views/dashboardPage/sidebars/sidebars.js

@@ -1088,7 +1088,7 @@ let newTransactionComp = {
             let loader = document.getElementById("loaderContainer");
             loader.style.display = "flex";
 
-            fetch("/transaction", {
+            fetch("/transaction/create", {
                 method: "post",
                 headers: {
                     "Content-Type": "application/json;charset=utf-8"

+ 23 - 1
views/dashboardPage/transactions.js

@@ -58,6 +58,28 @@ window.transactionsStrandObj = {
             recipes: document.getElementById("transFilRecipes").value.split(", ")
         }
 
-        
+        let loader = document.getElementById("loaderContainer");
+        loader.style.display = "flex";
+
+        fetch("/transaction", {
+            method: "POST",
+            headers: {
+                "Content-Type": "application/json;charset=utf-8"
+            },
+            body: JSON.stringify(data)
+        })
+            .then((response)=>{
+                if(typeof(response) === "string"){
+                    banner.createError(response);
+                }else{
+                    console.log("doing things");
+                }
+            })
+            .catch((err)=>{
+                banner.createError("UNABLE TO DISPLAY THE TRANSACTIONS");
+            })
+            .finally(()=>{
+                loader.style.display = "none";
+            });
     }
 }