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") diff --git a/commcare_connect/form_receiver/tests/test_receiver_integration.py b/commcare_connect/form_receiver/tests/test_receiver_integration.py index e24857da..13eebfc9 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(-90, 90)) 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