88from fastapi import BackgroundTasks , Depends , Security
99from fastapi .exceptions import HTTPException
1010from fastapi .responses import JSONResponse
11- from libsuitecrm import Filter , RequestFailed , SuiteCRM # type: ignore
11+ from libsuitecrm import Filter , SuiteCRM # type: ignore
1212
1313from register_your_data_api import email_generator
1414from register_your_data_api .background_tasks import ActionType , enqueue_task
1515from register_your_data_api .dependencies import get_suitecrm_audit_headers
1616from register_your_data_api .exceptions import RYDUserException
17+ from register_your_data_api .services .suitecrm_common import get_reporting_orgs_for_user
1718
1819from ..auth import authz
1920from ..auth import models as auth_models
2223 SUITECRM_REPORTING_ORG_FIELDS ,
2324 get_dataset_actions_from_suitecrm_response ,
2425 get_dataset_list_from_suitecrm_response ,
25- get_discoverable_reporting_org_meta_from_suitecrm_response ,
2626 get_fga_role_as_str ,
2727 get_reporting_org_meta_from_suitecrm_response ,
2828 get_suitecrm_dict_from_reporting_org ,
3131 CRMUser ,
3232 CRMUserListResponse ,
3333 DatasetReadModel ,
34- DiscoverableReportingOrgMetadata ,
3534 PaginationQueryParams ,
3635 ReportingOrgAction ,
3736 ReportingOrgCreateModel ,
38- ReportingOrgMetadata ,
3937 ReportingOrgUpdateModel ,
4038 ReportingOrgUserCreateModel ,
4139 UserReportingOrgDiscoverableMetadataRelation ,
@@ -60,74 +58,11 @@ def get_reporting_orgs(
6058
6159 context : Context = request .app .state .context
6260
63- crm : SuiteCRM = context .suitecrm_client_factory .get_client ()
64-
65- try :
66- orgs_for_user = crm .get_relationship (
67- "Contacts" ,
68- user .user_id_crm ,
69- "Accounts" ,
70- page_number = paging .page ,
71- page_size = paging .page_size ,
72- sort_field = "name" ,
73- sort_dir = "ascending" ,
74- filters = Filter ().equal ("iati_registry_discoverable" , "1" ),
75- )
76- except RequestFailed as e :
77- error_id = uuid .uuid4 ()
78- public_error_message = (
79- "There was a problem fetching the list of reporting orgs you are associated with. "
80- f"Please try again later, or contact IATI Support quoting error id: { error_id } "
81- )
82- context .app_logger .error (
83- f"error id: { error_id } - user id: { user .user_id_crm } - GET /reporting-orgs - Problem when fetching "
84- "the list of reporting organisations for this user from SuiteCRM. "
85- f"Details: { str (e )} "
86- )
87- raise HTTPException (status_code = fastapi .status .HTTP_500_INTERNAL_SERVER_ERROR , detail = public_error_message )
88-
89- total_records = orgs_for_user .get ("meta" , {}).get ("total-records" , 0 )
90-
91- reporting_orgs_list : list [UserReportingOrgRelation | UserReportingOrgDiscoverableMetadataRelation ] = []
92-
93- for reporting_org_from_suitecrm in orgs_for_user ["data" ]:
94- role_for_org = user .validator .get_user_role_for_reporting_org (reporting_org_from_suitecrm ["id" ])
95-
96- if role_for_org is None :
97- context .app_logger .info (
98- f"user id: { user .user_id_crm } - GET /reporting-orgs - user is associated with organisation "
99- f"{ reporting_org_from_suitecrm ["id" ]} in the CRM but has no role for that organisation in the FGA DB. "
100- "Organisation was omitted from the list returned to the user."
101- )
102- continue
103-
104- reporting_org_obj : ReportingOrgMetadata | DiscoverableReportingOrgMetadata
105-
106- if role_for_org == fga_models .FineGrainedAuthorisationRole .CONTRIBUTOR_PENDING :
107- reporting_org_obj = get_discoverable_reporting_org_meta_from_suitecrm_response (
108- reporting_org_from_suitecrm ["attributes" ]
109- )
110- else :
111- reporting_org_obj = get_reporting_org_meta_from_suitecrm_response (
112- reporting_org_from_suitecrm ["attributes" ]
113- )
114-
115- reporting_orgs_list .append (
116- UserReportingOrgRelation (
117- id = reporting_org_from_suitecrm ["id" ],
118- user_role = get_fga_role_as_str (role_for_org ),
119- metadata = reporting_org_obj ,
120- reporting_org_actions = (
121- get_reporting_org_actions (crm , reporting_org_from_suitecrm ["id" ])
122- if include_actions == "yes"
123- else []
124- ),
125- )
126- )
127-
128- reporting_orgs_list .sort (key = lambda org : org .metadata .human_readable_name .lower ())
61+ total_records , reporting_orgs = get_reporting_orgs_for_user (
62+ context , user , uuid .UUID (user .user_id_crm ), paging .page , paging .page_size
63+ )
12964
130- return PaginatedResultsPage .create (reporting_orgs_list , paging .page , paging .page_size , total_records , request )
65+ return PaginatedResultsPage .create (reporting_orgs , paging .page , paging .page_size , total_records , request )
13166
13267
13368@router .get ("/{org_id}" )
@@ -648,7 +583,7 @@ def get_reporting_org_datasets(
648583
649584 # 4. SuiteCRM doesn't tell us the total number of records, so we set page size = 1 and make a request
650585 total_records_resp = crm .get_records ("IATI_Datasets" , filters = filters , fields = ["id" ], page_number = 1 , page_size = 1 )
651- total_records = total_records_resp .get ("meta" , {}).get ("total-pages" , 1 )
586+ total_records = total_records_resp .get ("meta" , {}).get ("total-pages" , 0 )
652587
653588 datasets = get_dataset_list_from_suitecrm_response (datasets_from_suitecrm )
654589
0 commit comments