Skip to content

Commit

Permalink
Add MVP "case search count" endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
esoergel committed Oct 10, 2024
1 parent d38aa29 commit f1b6119
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
4 changes: 4 additions & 0 deletions corehq/apps/case_search/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -535,3 +535,7 @@ def _get_case_search_cases(helper, case_ids):
# Warning: '_tag_is_related_case' may cause the relevant user-defined properties to be overwritten.
def _tag_is_related_case(case):
case.case_json[IS_RELATED_CASE] = "true"


def get_case_search_count(domain, xpath_query):
return CaseSearchES().domain(domain).xpath_query(domain, xpath_query).count()
7 changes: 7 additions & 0 deletions corehq/apps/ota/tests/test_search_claim_endpoints.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,3 +313,10 @@ def test_duplicate_claim_after_case_changes(self):
response = self.client.post(self.url, {'case_id': self.case_id},
HTTP_X_COMMCAREHQ_LASTSYNCTOKEN=self.synclog.synclog_id)
self.assertEqual(response.status_code, 201)

def test_case_search_count(self):
url = reverse('case_search_count', kwargs={'domain': DOMAIN})
response = self.client.post(url, {'xpath': f'opened_by = "{OWNER_ID}"'})
self.assertEqual(response.content, b'2')
response = self.client.post(url, {'xpath': f'case_name = "{CASE_NAME}"'})
self.assertEqual(response.content, b'1')
6 changes: 4 additions & 2 deletions corehq/apps/ota/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,15 @@
from corehq.apps.hqadmin.views.users import DomainAdminRestoreView
from corehq.apps.ota.views import (
app_aware_search,
case_fixture,
case_restore,
case_search_count,
claim,
get_next_id,
heartbeat,
recovery_measures,
restore,
search,
case_fixture,
case_restore,
)

urlpatterns = [
Expand All @@ -20,6 +21,7 @@
url(r'^restore/(?P<app_id>[\w-]+)/$', restore, name='app_aware_restore'),
url(r'^search/$', search, name='remote_search'),
url(r'^search/(?P<app_id>[\w-]+)/$', app_aware_search, name='app_aware_remote_search'),
url(r'^case_search_count/$', case_search_count, name='case_search_count'),
url(r'^claim-case/$', claim, name='claim_case'),
url(r'^heartbeat/(?P<app_build_id>[\w-]+)/$', heartbeat, name='phone_heartbeat'),
url(r'^get_next_id/$', get_next_id, name='get_next_id'),
Expand Down
14 changes: 13 additions & 1 deletion corehq/apps/ota/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
from corehq.apps.case_search.const import COMMCARE_PROJECT
from corehq.apps.case_search.exceptions import CaseSearchUserError
from corehq.apps.case_search.models import CASE_SEARCH_REGISTRY_ID_KEY, CASE_SEARCH_TAGS_MAPPING
from corehq.apps.case_search.utils import get_case_search_results_from_request
from corehq.apps.case_search.utils import get_case_search_results_from_request, get_case_search_count
from corehq.apps.domain.auth import formplayer_auth
from corehq.apps.domain.decorators import check_domain_mobile_access
from corehq.apps.domain.models import Domain
Expand Down Expand Up @@ -166,6 +166,18 @@ def _log_search_timing(start_time, request_dict, domain, app_id):
})


@tracer.wrap(name="ota.case_search_count")
@location_safe_bypass
@csrf_exempt
@mobile_auth
@check_domain_mobile_access
@toggles.SYNC_SEARCH_CASE_CLAIM.required_decorator()
@require_POST # since these queries can be very long
def case_search_count(request, domain):
count = get_case_search_count(domain, request.POST.get('xpath'))
return HttpResponse(count, content_type="text/xml; charset=utf-8")


@tracer.wrap(name="ota.claim")
@location_safe_bypass
@csrf_exempt
Expand Down

0 comments on commit f1b6119

Please sign in to comment.