From e4a329946e95c70676f37658c158700c22a17def Mon Sep 17 00:00:00 2001 From: Pawan Verma Date: Fri, 15 Nov 2024 17:14:57 +0530 Subject: [PATCH 1/3] Add only approved visits count towards over_limit counts --- commcare_connect/form_receiver/processor.py | 14 ++++++-------- 1 file changed, 6 insertions(+), 8 deletions(-) diff --git a/commcare_connect/form_receiver/processor.py b/commcare_connect/form_receiver/processor.py index 641213c5..480110e9 100644 --- a/commcare_connect/form_receiver/processor.py +++ b/commcare_connect/form_receiver/processor.py @@ -233,14 +233,12 @@ def clean_form_submission(access: OpportunityAccess, user_visit: UserVisit, xfor def process_deliver_unit(user, xform: XForm, app: CommCareApp, opportunity: Opportunity, deliver_unit_block: dict): deliver_unit = get_or_create_deliver_unit(app, deliver_unit_block) access = OpportunityAccess.objects.get(opportunity=opportunity, user=user) - counts = ( - UserVisit.objects.filter(opportunity_access=access, deliver_unit=deliver_unit) - .exclude(status__in=[VisitValidationStatus.over_limit, VisitValidationStatus.trial]) - .aggregate( - daily=Count("pk", filter=Q(visit_date__date=xform.metadata.timeStart)), - total=Count("*"), - entity=Count("pk", filter=Q(entity_id=deliver_unit_block.get("entity_id"), deliver_unit=deliver_unit)), - ) + counts = UserVisit.objects.filter( + opportunity_access=access, deliver_unit=deliver_unit, status=VisitValidationStatus.approved, flagged=False + ).aggregate( + daily=Count("pk", filter=Q(visit_date__date=xform.metadata.timeStart)), + total=Count("*"), + entity=Count("pk", filter=Q(entity_id=deliver_unit_block.get("entity_id"), deliver_unit=deliver_unit)), ) claim = OpportunityClaim.objects.get(opportunity_access=access) entity_id = deliver_unit_block.get("entity_id") From 30f4edd6370bb99bf63b8104aa4a2028fef01cfd Mon Sep 17 00:00:00 2001 From: Pawan Verma Date: Fri, 15 Nov 2024 17:15:20 +0530 Subject: [PATCH 2/3] Fix test for updated counts --- .../form_receiver/tests/test_receiver_integration.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/commcare_connect/form_receiver/tests/test_receiver_integration.py b/commcare_connect/form_receiver/tests/test_receiver_integration.py index e24857da..5ea4f70d 100644 --- a/commcare_connect/form_receiver/tests/test_receiver_integration.py +++ b/commcare_connect/form_receiver/tests/test_receiver_integration.py @@ -1,4 +1,5 @@ import datetime +import random from copy import deepcopy from uuid import uuid4 @@ -176,6 +177,8 @@ def test_receiver_deliver_form_max_visits_reached( def submit_form_for_random_entity(form_json): duplicate_json = deepcopy(form_json) duplicate_json["form"]["deliver"]["entity_id"] = str(uuid4()) + # generate random locations for form submissions + duplicate_json["metadata"]["location"] = " ".join([str(random.uniform(10, 100)) for _ in range(4)]) make_request(api_client, duplicate_json, mobile_user_with_connect_link) payment_units = opportunity.paymentunit_set.all() @@ -190,7 +193,7 @@ def submit_form_for_random_entity(form_json): user_visits = UserVisit.objects.filter(user=mobile_user_with_connect_link) assert user_visits.count() == 5 # First four are not over-limit - assert {u.status for u in user_visits[0:4]} == {VisitValidationStatus.pending, VisitValidationStatus.approved} + assert {u.status for u in user_visits[0:4]} == {VisitValidationStatus.approved} # Last one is over limit assert user_visits[4].status == VisitValidationStatus.over_limit From 8faecda7943be1eff9f83dab2bb1384a8409e341 Mon Sep 17 00:00:00 2001 From: Pawan Verma Date: Tue, 19 Nov 2024 15:54:53 +0530 Subject: [PATCH 3/3] Fix latitude error in test --- .../form_receiver/tests/test_receiver_integration.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/commcare_connect/form_receiver/tests/test_receiver_integration.py b/commcare_connect/form_receiver/tests/test_receiver_integration.py index 5ea4f70d..13eebfc9 100644 --- a/commcare_connect/form_receiver/tests/test_receiver_integration.py +++ b/commcare_connect/form_receiver/tests/test_receiver_integration.py @@ -178,7 +178,7 @@ def submit_form_for_random_entity(form_json): duplicate_json = deepcopy(form_json) duplicate_json["form"]["deliver"]["entity_id"] = str(uuid4()) # generate random locations for form submissions - duplicate_json["metadata"]["location"] = " ".join([str(random.uniform(10, 100)) for _ in range(4)]) + duplicate_json["metadata"]["location"] = " ".join([str(random.uniform(-90, 90)) for _ in range(4)]) make_request(api_client, duplicate_json, mobile_user_with_connect_link) payment_units = opportunity.paymentunit_set.all()