@@ -146,6 +146,24 @@ def onchange_target_registry(self):
146146 for rec in self :
147147 rec .target_program = None
148148
149+ @api .constrains ("query_params" )
150+ def _validate_query_params (self ):
151+ for rec in self :
152+ if rec .query_params :
153+ try :
154+ json .loads (rec .query_params )
155+ except json .JSONDecodeError as e :
156+ raise ValidationError (_ ("Query Parameters must be valid JSON. Error: %s" ) % str (e )) from e
157+
158+ def get_parsed_query_params (self ):
159+ """Parse query_params JSON and return as dict"""
160+ self .ensure_one ()
161+ try :
162+ return json .loads (self .query_params ) if self .query_params else {}
163+ except json .JSONDecodeError :
164+ _logger .error ("Invalid JSON in query_params for record %s" , self .id )
165+ return {}
166+
149167 @api .constrains ("output_mapping" )
150168 def constraint_json_fields (self ):
151169 for rec in self :
@@ -226,10 +244,12 @@ def get_auth_token(self, auth_url):
226244 else :
227245 raise ValidationError (_ ("{reason}: Unable to connect to API." ).format (reason = response .reason ))
228246
229- def get_header_for_body (self , social_registry_version , today_isoformat , message_id ):
247+ def build_request_body (self , today_isoformat , message_id , transaction_id , reference_id , query ):
248+ """Build the request body for API call"""
230249 sender_id = self .env ["ir.config_parameter" ].sudo ().get_param ("web.base.url" ) or ""
231250 receiver_id = "Social Registry"
232- return {
251+
252+ header = {
233253 "version" : "1.0.0" ,
234254 "message_id" : message_id ,
235255 "message_ts" : today_isoformat ,
@@ -238,6 +258,8 @@ def get_header_for_body(self, social_registry_version, today_isoformat, message_
238258 "sender_uri" : "" ,
239259 "receiver_id" : receiver_id ,
240260 "total_count" : 0 ,
261+ "is_msg_encrypted" : False ,
262+ "meta" : {},
241263 }
242264
243265 search_request = {
@@ -314,22 +336,21 @@ def get_graphql_query(self, query_fields=None, effective_limit=None, effective_o
314336 _logger .debug ("updated graphql query: %s" , graphql_query )
315337 return graphql_query .strip ()
316338
317- def get_search_request (self , reference_id , today_isoformat ):
318- search_requests = {
319- "reference_id" : reference_id ,
320- "timestamp" : today_isoformat ,
321- "search_criteria" : {
322- "reg_type" : "G2P:RegistryType:Individual" ,
323- "query_type" : "graphql" ,
324- "query" : self .get_graphql_query (),
325- },
326- }
339+ def get_total_registrant_count (self ):
340+ self .ensure_one ()
327341
328- return search_requests
342+ try :
343+ # Get paths and auth
344+ paths = self .get_data_source_paths ()
345+ auth_url = self .get_social_registry_auth_url (paths )
346+ auth_token = self .get_auth_token (auth_url )
347+ search_url = self .get_social_registry_search_url (paths )
329348
330- def get_message (self , today_isoformat , transaction_id , reference_id ):
331- # Define Search Requests
332- search_request = self .get_search_request (reference_id , today_isoformat )
349+ # Prepare count query
350+ today_isoformat = datetime .now (timezone .utc ).isoformat ()
351+ message_id = str (uuid .uuid4 ())
352+ transaction_id = str (uuid .uuid4 ())
353+ reference_id = str (uuid .uuid4 ())
333354
334355 graphql_query = self .get_graphql_query ()
335356 data = self .build_request_body (
@@ -645,71 +666,18 @@ def process_registrants_async(self, registrants, count):
645666 main_job .delay ()
646667
647668 def fetch_social_registry_beneficiary (self ):
648- self .write ({"job_status" : "running" , "start_datetime" : fields .Datetime .now ()})
649-
650- config_parameters = self .env ["ir.config_parameter" ].sudo ()
651- today_isoformat = datetime .now (timezone .utc ).isoformat ()
652- social_registry_version = config_parameters .get_param ("social_registry_version" )
653- max_registrant = int (
654- config_parameters .get_param ("g2p_import_social_registry.max_registrants_count_job_queue" )
655- )
656-
657- message_id = str (uuid .uuid4 ())
658- transaction_id = str (uuid .uuid4 ())
659- reference_id = str (uuid .uuid4 ())
660-
661- # Define Data Source
662- paths = self .get_data_source_paths ()
663-
664- # Define Social Registry auth url
665-
666- full_social_registry_auth_url = self .get_social_registry_auth_url (paths )
667-
668- # Retrieve auth token
669-
670- auth_token = self .get_auth_token (full_social_registry_auth_url )
671-
672- # Define Social Registry search url
673- full_social_registry_search_url = self .get_social_registry_search_url (paths )
674-
675- # Define header
676- header = self .get_header_for_body (
677- social_registry_version ,
678- today_isoformat ,
679- message_id ,
680- )
681-
682- # Define message
683- message = self .get_message (
684- today_isoformat ,
685- transaction_id = transaction_id ,
686- reference_id = reference_id ,
687- )
688-
689- signature = ""
690-
691- # Define data
692- data = self .get_data (
693- signature ,
694- header ,
695- message ,
696- )
697-
698- data = json .dumps (data )
699-
700- # POST Request
701- response = requests .post (
702- full_social_registry_search_url ,
703- data = data ,
704- headers = {"Authorization" : auth_token },
705- timeout = constants .REQUEST_TIMEOUT ,
706- )
669+ """
670+ Main method to fetch beneficiaries from Social Registry.
707671
708- if not response .ok :
709- _logger .error ("Social Registry Search API response: %s" , response .text )
710- response .raise_for_status ()
672+ Supports both synchronous and asynchronous processing based on batch size.
673+ Uses pagination to handle large datasets efficiently.
711674
675+ Returns:
676+ dict: Action dictionary for UI notification
677+ """
712678 sticky = False
679+ kind = "info"
680+ message = _ ("No registrants were processed." )
713681
714682 try :
715683 self .write ({"job_status" : "running" , "start_datetime" : fields .Datetime .now ()})
@@ -844,15 +812,16 @@ def fetch_social_registry_beneficiary(self):
844812 )
845813
846814 self .last_sync_date = fields .Datetime .now ()
815+ end_time = fields .Datetime .now ()
816+ self .write ({"job_status" : "completed" , "end_datetime" : end_time })
847817
848- else :
849- self .write ({"job_status" : "failed" , "end_datetime" : fields .Datetime .now ()})
818+ except Exception as e :
819+ _logger .exception ("Failed to fetch registrants from Social Registry: %s" , e )
820+ end_time = fields .Datetime .now ()
821+ self .write ({"job_status" : "failed" , "end_datetime" : end_time })
822+ message = _ ("Failed to fetch registrants: %s" ) % str (e )
850823 kind = "danger"
851- message = response .json ().get ("error" , {}).get ("message" , "" )
852- if not message :
853- message = _ ("{reason}: Unable to connect to API." ).format (reason = response .reason )
854-
855- self .write ({"job_status" : "completed" , "end_datetime" : fields .Datetime .now ()})
824+ sticky = True
856825
857826 action = {
858827 "type" : "ir.actions.client" ,
0 commit comments