Ver código fonte

Update backing searching transactions to include everything in the mongoDB query

Lee Morgan 6 anos atrás
pai
commit
aed3d392b7
2 arquivos alterados com 37 adições e 24 exclusões
  1. 34 21
      controllers/transactionData.js
  2. 3 3
      views/dashboardPage/transactions.js

+ 34 - 21
controllers/transactionData.js

@@ -13,11 +13,16 @@ module.exports = {
     }
     */
     getTransactions: function(req, res){
+        console.time("query");
         if(!req.session.user){
             req.session.error = "MUST BE LOGGED IN TO DO THAT";
             return res.redirect("/");
         }
 
+        let objectifiedRecipes = [];
+        for(let i = 0; i < req.body.recipes.length; i++){
+            objectifiedRecipes.push(new ObjectId(req.body.recipes[i]));
+        }
         let startDate = new Date(req.body.startDate);
         let endDate = new Date(req.body.endDate);
         Transaction.aggregate([
@@ -26,6 +31,13 @@ module.exports = {
                 date: {
                     $gte: startDate,
                     $lt: endDate
+                },
+                recipes: {
+                    $elemMatch: {
+                        recipe: {
+                            $in: objectifiedRecipes
+                        }
+                    }
                 }
             }},
             {$sort: {
@@ -33,28 +45,29 @@ module.exports = {
             }}
         ])
             .then((transactions)=>{
-                if(req.body.recipes.length > 0){
-                    for(let i = 0; i < transactions.length; i++){
-                        let hasRecipe = false;
-                        for(let j = 0; j < transactions[i].recipes.length; j++){
-                            for(let k = 0; k < req.body.recipes.length; k++){
+                // if(req.body.recipes.length > 0){
+                //     for(let i = 0; i < transactions.length; i++){
+                //         let hasRecipe = false;
+                //         for(let j = 0; j < transactions[i].recipes.length; j++){
+                //             for(let k = 0; k < req.body.recipes.length; k++){
                                 
-                                if(transactions[i].recipes[j].recipe.toString() === req.body.recipes[k]){
-                                    hasRecipe = true;
-                                    break;
-                                }
-                            }
-                            if(hasRecipe){
-                                break;
-                            }
-                        }
-                        if(!hasRecipe){
-                            transactions.splice(i, 1);
-                            i--;
-                        }
-                    }
-                }
-
+                //                 if(transactions[i].recipes[j].recipe.toString() === req.body.recipes[k]){
+                //                     hasRecipe = true;
+                //                     break;
+                //                 }
+                //             }
+                //             if(hasRecipe){
+                //                 break;
+                //             }
+                //         }
+                //         if(!hasRecipe){
+                //             transactions.splice(i, 1);
+                //             i--;
+                //         }
+                //     }
+                // }
+
+                console.timeEnd("query");
                 return res.json(transactions);
             })
             .catch((err)=>{

+ 3 - 3
views/dashboardPage/transactions.js

@@ -77,9 +77,9 @@ window.transactionsStrandObj = {
         }
 
         let recipeChoices = document.getElementById("transFilCheckboxes");
-        for(let i = 0; i < recipeChoices.children.length; i++){
-            if(recipeChoices.children[i].children[0].checked){
-                data.recipes.push(recipeChoices.children[i].children[0].recipe.id);
+        for(let i = 0; i < recipeChoices.children.length; i += 3){
+            if(recipeChoices.children[i].checked){
+                data.recipes.push(recipeChoices.children[i].recipe.id);
             }
         }