3636import org .slf4j .LoggerFactory ;
3737import org .springframework .beans .factory .annotation .Autowired ;
3838import org .springframework .web .bind .annotation .RequestBody ;
39+ import org .springframework .web .bind .annotation .RequestHeader ;
3940import org .springframework .web .bind .annotation .RequestMapping ;
4041import org .springframework .web .bind .annotation .RequestMethod ;
4142import org .springframework .web .bind .annotation .RestController ;
7172import com .iemr .common .service .userbeneficiarydata .MaritalStatusService ;
7273import com .iemr .common .service .userbeneficiarydata .StatusService ;
7374import com .iemr .common .service .userbeneficiarydata .TitleService ;
75+ import com .iemr .common .utils .CookieUtil ;
76+ import com .iemr .common .utils .JwtUtil ;
7477import com .iemr .common .utils .mapper .InputMapper ;
7578import com .iemr .common .utils .mapper .OutputMapper ;
7679import com .iemr .common .utils .response .OutputResponse ;
@@ -105,6 +108,8 @@ public class BeneficiaryRegistrationController {
105108 private BeneficiaryOccupationService beneficiaryOccupationService ;
106109 private GovtIdentityTypeService govtIdentityTypeService ;
107110
111+ @ Autowired
112+ private JwtUtil jwtUtil ;
108113
109114 @ Autowired
110115 public void setBenRelationshipTypeService (BenRelationshipTypeService benRelationshipTypeService ) {
@@ -344,6 +349,54 @@ public String searchUserByPhone(
344349 return response .toString ();
345350 }
346351
352+ @ Operation (summary = "Provide the list of beneficiaries using Elasticsearch" )
353+ @ RequestMapping (value = "/searchUser" , method = RequestMethod .POST , headers = "Authorization" )
354+ public String searchUser (@ RequestBody String request , HttpServletRequest httpRequest ) {
355+ OutputResponse response = new OutputResponse ();
356+ try {
357+ logger .info ("Universal search request received" );
358+
359+ JsonParser parser = new JsonParser ();
360+ JsonObject requestObj = parser .parse (request ).getAsJsonObject ();
361+
362+ String searchQuery = null ;
363+ if (requestObj .has ("search" ) && !requestObj .get ("search" ).isJsonNull ()) {
364+ searchQuery = requestObj .get ("search" ).getAsString ();
365+ }
366+
367+ if (searchQuery == null || searchQuery .trim ().isEmpty ()) {
368+ response .setError (400 , "Search query is required" );
369+ return response .toString ();
370+ }
371+
372+ String auth = httpRequest .getHeader ("Authorization" );
373+
374+ Integer userID = jwtUtil .getUserIdFromRequest (httpRequest );
375+
376+ logger .info ("ES search for userId: {}" , userID );
377+
378+ Boolean is1097 = false ;
379+ if (requestObj .has ("is1097" ) && !requestObj .get ("is1097" ).isJsonNull ()) {
380+ is1097 = requestObj .get ("is1097" ).getAsBoolean ();
381+ }
382+
383+ logger .info ("Searching with query: {}, userId: {}, is1097: {}" , searchQuery , userID , is1097 );
384+ String result = iemrSearchUserService .searchUser (searchQuery , userID , auth , is1097 );
385+
386+ if (result == null || result .trim ().isEmpty ()) {
387+ response .setError (200 , "No beneficiaries found" );
388+ return response .toString ();
389+ }
390+
391+ return result ;
392+
393+ } catch (Exception e ) {
394+ logger .error ("Error in universal search: {}" , e .getMessage (), e );
395+ response .setError (400 , "Error searching beneficiaries: " + e .getMessage ());
396+ return response .toString ();
397+ }
398+ }
399+
347400 @ Operation (summary = "Provide the list of beneficiaries based on search criteria" )
348401 @ RequestMapping (value = "/searchBeneficiary" , method = RequestMethod .POST , headers = "Authorization" )
349402 public String searchBeneficiary (
@@ -366,6 +419,41 @@ public String searchBeneficiary(
366419 return output .toString ();
367420 }
368421
422+ /**
423+ * Elasticsearch-based advanced search endpoint
424+ */
425+ @ Operation (summary = "Advanced search beneficiaries using Elasticsearch" )
426+ @ RequestMapping (value = "/searchBeneficiaryES" , method = RequestMethod .POST , headers = "Authorization" )
427+ public String searchBeneficiaryES (
428+ @ RequestBody BeneficiaryModel request ,
429+ HttpServletRequest httpRequest ) {
430+
431+ logger .info ("searchBeneficiaryES request: {}" , request );
432+ OutputResponse output = new OutputResponse ();
433+
434+ try {
435+
436+ String auth = httpRequest .getHeader ("Authorization" );
437+
438+ Integer userID = jwtUtil .getUserIdFromRequest (httpRequest );
439+
440+ logger .info ("ES Advanced search for userId: {}" , userID );
441+
442+ String result = iemrSearchUserService .findBeneficiaryES (request , userID , auth );
443+
444+ return result ;
445+
446+ } catch (NumberFormatException ne ) {
447+ logger .error ("searchBeneficiaryES failed with number format error: {}" , ne .getMessage (), ne );
448+ output .setError (400 , "Invalid number format in search criteria" );
449+ return output .toString ();
450+ } catch (Exception e ) {
451+ logger .error ("searchBeneficiaryES failed with error: {}" , e .getMessage (), e );
452+ output .setError (500 , "Error searching beneficiaries: " + e .getMessage ());
453+ return output .toString ();
454+ }
455+ }
456+
369457 @ Operation (summary = "Provide all common data list needed for beneficiary registration" )
370458 @ RequestMapping (value = "/getRegistrationData" , method = RequestMethod .POST , produces = MediaType .APPLICATION_JSON , headers = "Authorization" )
371459 public String getRegistrationData () {
0 commit comments