11package integrations .telex .salesagent .lead .service ;
22
3+ import com .fasterxml .jackson .core .JsonProcessingException ;
34import com .fasterxml .jackson .databind .JsonNode ;
45import com .fasterxml .jackson .databind .ObjectMapper ;
6+ import integrations .telex .salesagent .lead .dto .PeopleLeadDto ;
57import integrations .telex .salesagent .lead .dto .PeopleSearchRequest ;
6- import integrations .telex .salesagent .lead .dto .RapidLeadDto ;
78import integrations .telex .salesagent .telex .service .TelexClient ;
89import lombok .RequiredArgsConstructor ;
910import org .springframework .beans .factory .annotation .Value ;
1617import java .util .HashMap ;
1718import java .util .List ;
1819import java .util .Map ;
20+ import java .util .stream .Collectors ;
1921
2022@ Slf4j
2123@ Service
@@ -34,65 +36,94 @@ public class LeadPeopleResearchService {
3436 private final ObjectMapper objectMapper ;
3537 private final TelexClient telexClient ;
3638
37- public List <RapidLeadDto > queryLeads (String channelID , PeopleSearchRequest request ) {
39+ public List <PeopleLeadDto > queryLeads (String channelID , PeopleSearchRequest request ) {
3840 try {
39- // Prepare headers
40- HttpHeaders headers = new HttpHeaders ();
41- headers .set ("X-RapidAPI-Key" , rapidApiKey );
42- headers .set ("X-RapidAPI-Host" , rapidApiHost );
43- headers .setContentType (MediaType .APPLICATION_JSON );
44-
45- // Build the request body (LinkedIn search URL)
46- Map <String , String > payload = new HashMap <>();
47- payload .put ("url" , request .getUrl ());
48-
49- HttpEntity <Map <String , String >> entity = new HttpEntity <>(payload , headers );
50-
51- // Call RapidAPI
52- log .info ("Searching for people with URL: {}" , request .getUrl ());
53- ResponseEntity <String > response = restTemplate .exchange (
54- rapidApiUrl , HttpMethod .POST , entity , String .class
55- );
56-
57- // Process the response
58- if (response .getStatusCode ().is2xxSuccessful ()) {
59- List <RapidLeadDto > leads = formatPeopleResponse (response .getBody ());
60-
61- if (leads .isEmpty ()) {
62- String report = "🔍 No LinkedIn profiles found for the given search URL." ;
63- telexClient .sendInstruction (channelID , report );
64- } else {
65- for (RapidLeadDto lead : leads ) {
66- telexClient .processTelexPayload (channelID , lead );
67- }
68- }
69- return leads ;
70- } else {
71- log .error ("API Error: {}" , response .getStatusCode ());
72- return new ArrayList <>();
41+ // Build URL with location filter
42+ String searchUrl = request .buildLinkedInSearchUrl ();
43+ log .info ("Constructed LinkedIn search URL: {}" , searchUrl );
44+
45+ // Make API call
46+ List <PeopleLeadDto > leads = fetchLeadsFromApi (searchUrl );
47+
48+ // Apply keyword filtering
49+ if (request .getKeyword () != null && !request .getKeyword ().isEmpty ()) {
50+ leads = filterByKeyword (leads , request .getKeyword ());
51+ log .info ("Filtered {} leads by keyword: {}" , leads .size (), request .getKeyword ());
7352 }
53+
54+ // Process results
55+ processResults (channelID , leads );
56+
57+ return leads ;
58+
7459 } catch (Exception e ) {
7560 log .error ("Error calling RapidAPI: {}" , e .getMessage (), e );
61+ }
62+ return new ArrayList <>();
63+ }
64+
65+ private List <PeopleLeadDto > filterByKeyword (List <PeopleLeadDto > leads , String keyword ) {
66+ String lowerKeyword = keyword .toLowerCase ();
67+ return leads .stream ()
68+ .filter (lead ->
69+ (lead .getHeadline () != null &&
70+ lead .getHeadline ().toLowerCase ().contains (lowerKeyword )) ||
71+ (lead .getFullName () != null &&
72+ lead .getFullName ().toLowerCase ().contains (lowerKeyword )))
73+ .collect (Collectors .toList ());
74+ }
75+
76+ private void processResults (String channelID , List <PeopleLeadDto > leads ) throws JsonProcessingException {
77+ if (leads .isEmpty ()) {
78+ telexClient .sendInstruction (channelID , "🔍 No matching profiles found." );
79+ } else {
80+ leads .forEach (lead -> {
81+ try {
82+ telexClient .processTelexPayload (channelID , lead );
83+ } catch (JsonProcessingException e ) {
84+ throw new RuntimeException (e );
85+ }
86+ });
87+ }
88+ }
89+
90+ private List <PeopleLeadDto > fetchLeadsFromApi (String searchUrl ) {
91+ HttpHeaders headers = new HttpHeaders ();
92+ headers .set ("X-RapidAPI-Key" , rapidApiKey );
93+ headers .set ("X-RapidAPI-Host" , rapidApiHost );
94+ headers .setContentType (MediaType .APPLICATION_JSON );
95+
96+ Map <String , String > payload = new HashMap <>();
97+ payload .put ("url" , searchUrl );
98+
99+ HttpEntity <Map <String , String >> entity = new HttpEntity <>(payload , headers );
100+
101+ ResponseEntity <String > response = restTemplate .exchange (
102+ rapidApiUrl , HttpMethod .POST , entity , String .class
103+ );
104+
105+ if (!response .getStatusCode ().is2xxSuccessful ()) {
106+ log .error ("API request failed with status: {}" , response .getStatusCode ());
76107 return new ArrayList <>();
77108 }
109+
110+ return formatPeopleResponse (response .getBody ());
78111 }
79112
80- // Parse API response into Lead objects
81- private List <RapidLeadDto > formatPeopleResponse (String responseBody ) {
82- List <RapidLeadDto > leads = new ArrayList <>();
113+ private List <PeopleLeadDto > formatPeopleResponse (String responseBody ) {
114+ List <PeopleLeadDto > leads = new ArrayList <>();
83115 try {
84116 JsonNode root = objectMapper .readTree (responseBody );
85117 JsonNode items = root .path ("data" ).path ("items" );
86118
87119 if (items .isArray ()) {
88120 for (JsonNode item : items ) {
89- RapidLeadDto lead = new RapidLeadDto (
90- null ,
121+ PeopleLeadDto lead = new PeopleLeadDto (
91122 item .path ("fullName" ).asText (),
123+ item .path ("headline" ).asText (),
124+ item .path ("location" ).asText (),
92125 item .path ("profileURL" ).asText (),
93- String .format ("%s | %s" ,
94- item .path ("headline" ).asText (),
95- item .path ("location" ).asText ())
126+ item .path ("username" ).asText ()
96127 );
97128 leads .add (lead );
98129 }
0 commit comments