From febcfe740a357165979c94dd48d66d733927f42c Mon Sep 17 00:00:00 2001 From: hemant10yadav Date: Wed, 4 Dec 2024 08:29:11 +0530 Subject: [PATCH 1/2] ignore the visit that comes again from hq with same form data --- commcare_connect/form_receiver/processor.py | 19 ++++++++++++++++--- .../tests/test_receiver_integration.py | 14 ++++++++++++++ 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/commcare_connect/form_receiver/processor.py b/commcare_connect/form_receiver/processor.py index 2872e81e..d9640d73 100644 --- a/commcare_connect/form_receiver/processor.py +++ b/commcare_connect/form_receiver/processor.py @@ -231,8 +231,22 @@ 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) + entity_id = deliver_unit_block.get("entity_id") + entity_name = deliver_unit_block.get("entity_name") + + existing_user_visit = UserVisit.objects.filter( + xform_id=xform.id, + visit_date=xform.metadata.timeStart, + opportunity=opportunity, + opportunity_access=access, + entity_id=entity_id, + entity_name=entity_name, + ) + if existing_user_visit: + return + + deliver_unit = get_or_create_deliver_unit(app, deliver_unit_block) counts = ( UserVisit.objects.filter(opportunity_access=access, deliver_unit=deliver_unit) .exclude(status__in=[VisitValidationStatus.over_limit, VisitValidationStatus.trial]) @@ -243,9 +257,8 @@ def process_deliver_unit(user, xform: XForm, app: CommCareApp, opportunity: Oppo ) ) claim = OpportunityClaim.objects.get(opportunity_access=access) - entity_id = deliver_unit_block.get("entity_id") - entity_name = deliver_unit_block.get("entity_name") payment_unit = deliver_unit.payment_unit + user_visit = UserVisit( opportunity=opportunity, user=user, diff --git a/commcare_connect/form_receiver/tests/test_receiver_integration.py b/commcare_connect/form_receiver/tests/test_receiver_integration.py index 7af90d3a..d462ca5c 100644 --- a/commcare_connect/form_receiver/tests/test_receiver_integration.py +++ b/commcare_connect/form_receiver/tests/test_receiver_integration.py @@ -196,6 +196,20 @@ def submit_form_for_random_entity(form_json): assert user_visits[4].status == VisitValidationStatus.over_limit +@pytest.mark.django_db +@pytest.mark.parametrize("paymentunit_options", [pytest.param({"max_daily": 2})]) +def test_receiver_same_visit_twice( + mobile_user_with_connect_link: User, api_client: APIClient, opportunity: Opportunity +): + payment_units = opportunity.paymentunit_set.all() + form_json1 = get_form_json_for_payment_unit(payment_units[0]) + form_json2 = deepcopy(form_json1) + make_request(api_client, form_json1, mobile_user_with_connect_link) + make_request(api_client, form_json2, mobile_user_with_connect_link) + user_visits = UserVisit.objects.filter(user=mobile_user_with_connect_link) + assert user_visits.count() == 1 + + @pytest.mark.django_db def test_receiver_deliver_form_end_date_reached( user_with_connectid_link: User, api_client: APIClient, opportunity: Opportunity From 9a83f50436eee7e9f57f34e363c91ceb44624b04 Mon Sep 17 00:00:00 2001 From: hemant10yadav Date: Wed, 4 Dec 2024 08:35:30 +0530 Subject: [PATCH 2/2] fix test --- .../form_receiver/tests/test_receiver_integration.py | 1 - 1 file changed, 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 d462ca5c..6da540d6 100644 --- a/commcare_connect/form_receiver/tests/test_receiver_integration.py +++ b/commcare_connect/form_receiver/tests/test_receiver_integration.py @@ -197,7 +197,6 @@ def submit_form_for_random_entity(form_json): @pytest.mark.django_db -@pytest.mark.parametrize("paymentunit_options", [pytest.param({"max_daily": 2})]) def test_receiver_same_visit_twice( mobile_user_with_connect_link: User, api_client: APIClient, opportunity: Opportunity ):