From 5fcbab01610bb4fb6f0c687c1ab74a96984f473d Mon Sep 17 00:00:00 2001 From: Tim <6887938+digitalmnt@users.noreply.github.com> Date: Mon, 14 Aug 2023 11:21:48 -0600 Subject: [PATCH] feat(cgrants): only return contributions greater than 1 dollar (#351) * feat(cgrants): only return contributions greater than 1 dollar * fix(cgrants): round total amount --- api/cgrants/api.py | 21 ++++++++++---------- api/cgrants/test/test_cgrants_api.py | 29 +++++++++++++++++++++++++++- 2 files changed, 39 insertions(+), 11 deletions(-) diff --git a/api/cgrants/api.py b/api/cgrants/api.py index 2fa1d6e8a..e230096a3 100644 --- a/api/cgrants/api.py +++ b/api/cgrants/api.py @@ -112,20 +112,21 @@ def _get_contributor_statistics_for_cgrants( def _get_contributor_statistics_for_protocol(address: str) -> dict: - total_amount_usd = ProtocolContributions.objects.filter( - contributor=address - ).aggregate(Sum("amount"))["amount__sum"] - num_rounds = ProtocolContributions.objects.filter(contributor=address).aggregate( - Count("round", distinct=True) - )["round__count"] - num_projects = ProtocolContributions.objects.filter(contributor=address).aggregate( - Count("project", distinct=True) - )["project__count"] + protocol_filter = ProtocolContributions.objects.filter( + contributor=address, amount__gte=1 + ) + total_amount_usd = protocol_filter.aggregate(Sum("amount"))["amount__sum"] + num_rounds = protocol_filter.aggregate(Count("round", distinct=True))[ + "round__count" + ] + num_projects = protocol_filter.aggregate(Count("project", distinct=True))[ + "project__count" + ] return { "num_grants_contribute_to": num_projects if num_projects is not None else 0, "num_rounds_contribute_to": num_rounds if num_rounds is not None else 0, - "total_contribution_amount": total_amount_usd + "total_valid_contribution_amount": round(total_amount_usd, 3) if total_amount_usd is not None else 0, "num_gr14_contributions": 0, diff --git a/api/cgrants/test/test_cgrants_api.py b/api/cgrants/test/test_cgrants_api.py index 4e60ea4fb..b90b19ea2 100644 --- a/api/cgrants/test/test_cgrants_api.py +++ b/api/cgrants/test/test_cgrants_api.py @@ -270,7 +270,34 @@ def test_contributor_statistics_with_only_protocol_contributions(self): { "num_grants_contribute_to": 4, "num_rounds_contribute_to": 2, - "total_contribution_amount": "10", + "total_valid_contribution_amount": "10.000", + "num_gr14_contributions": 0, + }, + ) + + def test_contributor_statistics_with_below_threshold_contributions_contributions( + self, + ): + existing_contributions = ProtocolContributions.objects.filter( + contributor=self.address + ).all() + for contribution in existing_contributions: + contribution.amount = 0.5 + contribution.save() + + # Edge case: User has made no contributions + response = self.client.get( + reverse("cgrants:allo_contributor_statistics"), + {"address": self.address}, + **self.headers, + ) + self.assertEqual(response.status_code, 200) + self.assertEqual( + response.json(), + { + "num_grants_contribute_to": 0, + "num_rounds_contribute_to": 0, + "total_valid_contribution_amount": 0, "num_gr14_contributions": 0, }, )