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 ;
7071import com .iemr .common .service .userbeneficiarydata .MaritalStatusService ;
7172import com .iemr .common .service .userbeneficiarydata .StatusService ;
7273import com .iemr .common .service .userbeneficiarydata .TitleService ;
74+ import com .iemr .common .utils .CookieUtil ;
75+ import com .iemr .common .utils .JwtUtil ;
7376import com .iemr .common .utils .mapper .InputMapper ;
7477import com .iemr .common .utils .mapper .OutputMapper ;
7578import com .iemr .common .utils .response .OutputResponse ;
@@ -103,6 +106,8 @@ public class BeneficiaryRegistrationController {
103106 private BeneficiaryOccupationService beneficiaryOccupationService ;
104107 private GovtIdentityTypeService govtIdentityTypeService ;
105108
109+ @ Autowired
110+ private JwtUtil jwtUtil ;
106111
107112 @ Autowired
108113 public void setBenRelationshipTypeService (BenRelationshipTypeService benRelationshipTypeService ) {
@@ -342,6 +347,54 @@ public String searchUserByPhone(
342347 return response .toString ();
343348 }
344349
350+ @ Operation (summary = "Provide the list of beneficiaries using Elasticsearch" )
351+ @ RequestMapping (value = "/searchUser" , method = RequestMethod .POST , headers = "Authorization" )
352+ public String searchUser (@ RequestBody String request , HttpServletRequest httpRequest ) {
353+ OutputResponse response = new OutputResponse ();
354+ try {
355+ logger .info ("Universal search request received" );
356+
357+ JsonParser parser = new JsonParser ();
358+ JsonObject requestObj = parser .parse (request ).getAsJsonObject ();
359+
360+ String searchQuery = null ;
361+ if (requestObj .has ("search" ) && !requestObj .get ("search" ).isJsonNull ()) {
362+ searchQuery = requestObj .get ("search" ).getAsString ();
363+ }
364+
365+ if (searchQuery == null || searchQuery .trim ().isEmpty ()) {
366+ response .setError (400 , "Search query is required" );
367+ return response .toString ();
368+ }
369+
370+ String auth = httpRequest .getHeader ("Authorization" );
371+
372+ Integer userID = jwtUtil .getUserIdFromRequest (httpRequest );
373+
374+ logger .info ("ES search for userId: {}" , userID );
375+
376+ Boolean is1097 = false ;
377+ if (requestObj .has ("is1097" ) && !requestObj .get ("is1097" ).isJsonNull ()) {
378+ is1097 = requestObj .get ("is1097" ).getAsBoolean ();
379+ }
380+
381+ logger .info ("Searching with query: {}, userId: {}, is1097: {}" , searchQuery , userID , is1097 );
382+ String result = iemrSearchUserService .searchUser (searchQuery , userID , auth , is1097 );
383+
384+ if (result == null || result .trim ().isEmpty ()) {
385+ response .setError (200 , "No beneficiaries found" );
386+ return response .toString ();
387+ }
388+
389+ return result ;
390+
391+ } catch (Exception e ) {
392+ logger .error ("Error in universal search: {}" , e .getMessage (), e );
393+ response .setError (400 , "Error searching beneficiaries: " + e .getMessage ());
394+ return response .toString ();
395+ }
396+ }
397+
345398 @ Operation (summary = "Provide the list of beneficiaries based on search criteria" )
346399 @ RequestMapping (value = "/searchBeneficiary" , method = RequestMethod .POST , headers = "Authorization" )
347400 public String searchBeneficiary (
@@ -364,6 +417,41 @@ public String searchBeneficiary(
364417 return output .toString ();
365418 }
366419
420+ /**
421+ * Elasticsearch-based advanced search endpoint
422+ */
423+ @ Operation (summary = "Advanced search beneficiaries using Elasticsearch" )
424+ @ RequestMapping (value = "/searchBeneficiaryES" , method = RequestMethod .POST , headers = "Authorization" )
425+ public String searchBeneficiaryES (
426+ @ RequestBody BeneficiaryModel request ,
427+ HttpServletRequest httpRequest ) {
428+
429+ logger .info ("searchBeneficiaryES request: {}" , request );
430+ OutputResponse output = new OutputResponse ();
431+
432+ try {
433+
434+ String auth = httpRequest .getHeader ("Authorization" );
435+
436+ Integer userID = jwtUtil .getUserIdFromRequest (httpRequest );
437+
438+ logger .info ("ES Advanced search for userId: {}" , userID );
439+
440+ String result = iemrSearchUserService .findBeneficiaryES (request , userID , auth );
441+
442+ return result ;
443+
444+ } catch (NumberFormatException ne ) {
445+ logger .error ("searchBeneficiaryES failed with number format error: {}" , ne .getMessage (), ne );
446+ output .setError (400 , "Invalid number format in search criteria" );
447+ return output .toString ();
448+ } catch (Exception e ) {
449+ logger .error ("searchBeneficiaryES failed with error: {}" , e .getMessage (), e );
450+ output .setError (500 , "Error searching beneficiaries: " + e .getMessage ());
451+ return output .toString ();
452+ }
453+ }
454+
367455 @ Operation (summary = "Provide all common data list needed for beneficiary registration" )
368456 @ RequestMapping (value = "/getRegistrationData" , method = RequestMethod .POST , produces = MediaType .APPLICATION_JSON , headers = "Authorization" )
369457 public String getRegistrationData () {
0 commit comments