Parcourir la source

Bug fix: getting merchant was not filtering the transactions.

Lee Morgan il y a 5 ans
Parent
commit
5f668e8f8c
2 fichiers modifiés avec 17 ajouts et 3 suppressions
  1. 17 2
      controllers/merchantData.js
  2. 0 1
      views/dashboardPage/js/dashboard.js

+ 17 - 2
controllers/merchantData.js

@@ -8,17 +8,32 @@ const verifyEmail = require("../emails/verifyEmail.js");
 
 const bcrypt = require("bcryptjs");
 const mailgun = require("mailgun-js")({apiKey: process.env.MG_SUBLINE_APIKEY, domain: "mail.thesubline.net"});
+const ObjectId = require("mongoose").Types.ObjectId;
 
 module.exports = {
     /*
     GET: gets a merchant to send back to its owner
     req.params.id = String (merchant id)
-    response = [Owner, Merchant];
+    response = [Owner, Merchant, [Transaction]];
     */
     getMerchant: function(req, res){
         let owner = Owner.findOne({"session.sessionId": req.session.owner}).populate("merchants", "name");
         let merchant = Merchant.findOne({_id: req.params.id}).populate("inventory.ingredient").populate("recipes");
-        let transactions = Transaction.find({merchant: req.params.id});
+
+        let now = new Date();
+        let then = new Date(now.getFullYear(), now.getMonth() - 1, 1);
+
+        let transactions = Transaction.aggregate([
+            {$match: {
+                merchant: ObjectId(req.params.id),
+                date: {$gte: then}
+            }},
+            {$sort: {date: -1}},
+            {$project: {
+                date: 1,
+                recipes: 1
+            }}
+        ]);
 
         Promise.all([owner, merchant, transactions])
             .then((response)=>{

+ 0 - 1
views/dashboardPage/js/dashboard.js

@@ -238,7 +238,6 @@ controller = {
                 content.children[2].children[1].onclick = ()=>{account.deleteMerchant()};
                 break;
             case "confirmDeleteIngredient":
-                console.log(data);
                 content = document.getElementById("modalConfirm");
                 content.style.display = "flex";
                 content.children[1].innerText = `Are you sure you want to delete ingredient: ${data.ingredient.name}?`;