Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle empty location with catchment areas #406

Merged
merged 1 commit into from
Oct 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions commcare_connect/form_receiver/processor.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,13 +179,14 @@ def clean_form_submission(access: OpportunityAccess, user_visit: UserVisit, xfor
if opportunity_flags.catchment_areas:
areas = access.catchmentarea_set.filter(active=True)
if areas:
cur_lat, cur_lon, *_ = xform.metadata.location.split(" ")
within_catchment = False
for area in areas:
dist = distance((area.latitude, area.longitude), (cur_lat, cur_lon))
if dist.meters < area.radius:
within_catchment = True
break
if xform.metadata.location is not None:
cur_lat, cur_lon, *_ = xform.metadata.location.split(" ")
for area in areas:
dist = distance((area.latitude, area.longitude), (cur_lat, cur_lon))
if dist.meters < area.radius:
within_catchment = True
break
if not within_catchment:
flags.append(["catchment", "Visit outside worker catchment areas"])
if (
Expand Down
20 changes: 20 additions & 0 deletions commcare_connect/form_receiver/tests/test_receiver_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
)
from commcare_connect.opportunity.tasks import bulk_approve_completed_work
from commcare_connect.opportunity.tests.factories import (
CatchmentAreaFactory,
DeliverUnitFactory,
DeliverUnitFlagRulesFactory,
FormJsonValidationRulesFactory,
Expand Down Expand Up @@ -576,6 +577,25 @@ def test_reciever_verification_flags_form_json_rule_flagged(
] in visit.flag_reason.get("flags", [])


def test_reciever_verification_flags_catchment_areas(
user_with_connectid_link: User, api_client: APIClient, opportunity: Opportunity
):
verification_flags = OpportunityVerificationFlags.objects.get(opportunity=opportunity)
verification_flags.catchment_areas = True
verification_flags.save()

form_json = _create_opp_and_form_json(opportunity, user=user_with_connectid_link)
form_json["metadata"]["location"] = None

access = OpportunityAccess.objects.get(user=user_with_connectid_link, opportunity=opportunity)
CatchmentAreaFactory(opportunity=opportunity, opportunity_access=access, active=True)

make_request(api_client, form_json, user_with_connectid_link)
visit = UserVisit.objects.get(user=user_with_connectid_link)
assert visit.flagged
assert ["catchment", "Visit outside worker catchment areas"] in visit.flag_reason.get("flags", [])


def _get_form_json(learn_app, module_id, form_block=None):
form_json = get_form_json(
form_block=form_block or LearnModuleJsonFactory(id=module_id).json,
Expand Down
Loading