11"""Implementation for /discoverable-reporting-orgs end point"""
22
3+ from typing import Annotated
4+
35import fastapi
46import starlette .requests
5- from fastapi import Security
7+ from fastapi import Query , Security
68from libsuitecrm import Filter , SuiteCRM # type: ignore
79
810from ..auth import authz
@@ -26,6 +28,14 @@ def get_discoverable_reporting_orgs(
2628 request : starlette .requests .Request ,
2729 user : auth_models .UserAndCredentials = Security (authz .get_user_authnz , scopes = ["ryd" , "ryd:reporting_org" ]),
2830 paging : PaginationQueryParams = fastapi .Depends (),
31+ q : Annotated [
32+ str | None ,
33+ Query (
34+ min_length = 2 ,
35+ max_length = 40 ,
36+ description = "The search term. Searches through the reporting organisation's human readable name." ,
37+ ),
38+ ] = None ,
2939) -> PaginatedResultsPage [DiscoverableReportingOrg ]:
3040
3141 context : Context = request .app .state .context
@@ -35,7 +45,10 @@ def get_discoverable_reporting_orgs(
3545 fields = get_discoverable_reporting_org_suitecrm_fields ()
3646
3747 filters = Filter ()
38- filters .equal ("iati_registry_discoverable" , "1" )
48+ if q is not None :
49+ search_q = q .replace ("_" , "\\ _" ).replace ("%" , "\\ %" ) # Escape wildcard chars in SuiteCRM search
50+ filters .op_and ().equal ("iati_registry_discoverable" , "1" ).like ("name" , f"%{ search_q } %" )
51+
3952 suitecrm_reporting_orgs = crm .get_records (
4053 "Accounts" ,
4154 fields = fields ,
@@ -52,6 +65,6 @@ def get_discoverable_reporting_orgs(
5265
5366 # SuiteCRM doesn't return the total_records, so we set page size = 1, limit fields to id and fetch one record
5467 total_records_resp = crm .get_records ("Accounts" , filters = filters , fields = ["id" ], page_number = 1 , page_size = 1 )
55- total_records = total_records_resp .get ("meta" , {}).get ("total-pages" , 1 )
68+ total_records = total_records_resp .get ("meta" , {}).get ("total-pages" , 0 )
5669
5770 return PaginatedResultsPage .create (discoverable_orgs , paging .page , paging .page_size , total_records , request )
0 commit comments