Seth commited on
Commit
12ebb02
·
1 Parent(s): f1bcbcc
backend/app/main.py CHANGED
@@ -516,9 +516,8 @@ async def push_to_smartlead(request: SmartleadPushRequest, db: Session = Depends
516
  'email': lead_email,
517
  'custom_vars': item['custom_vars']
518
  })
519
- # If response doesn't have lead_ids, try to match by email order
520
- if not email_to_lead_id and len(batch) == len(lead_batch):
521
- # Store with email for now - we'll try to get lead_id another way
522
  for item in batch:
523
  if item.get('custom_vars'):
524
  leads_to_update.append({
@@ -537,7 +536,21 @@ async def push_to_smartlead(request: SmartleadPushRequest, db: Session = Depends
537
  failed_count += 1
538
 
539
  # Step 2: Try to update each successfully added lead with custom variables
540
- # Use lead_id from the response
 
 
 
 
 
 
 
 
 
 
 
 
 
 
541
  for item in leads_to_update:
542
  if item.get('lead_id'):
543
  try:
@@ -556,10 +569,10 @@ async def push_to_smartlead(request: SmartleadPushRequest, db: Session = Depends
556
  "error": f"Lead added but custom variables could not be set: {str(e)}"
557
  })
558
  else:
559
- # If we don't have lead_id, log an error
560
  errors.append({
561
  "email": item.get('email', 'unknown'),
562
- "error": "Lead added but lead_id not found in response - cannot update custom variables"
563
  })
564
 
565
  # Update run record
 
516
  'email': lead_email,
517
  'custom_vars': item['custom_vars']
518
  })
519
+ # If response doesn't have lead_ids, store with email for later lookup
520
+ if not email_to_lead_id:
 
521
  for item in batch:
522
  if item.get('custom_vars'):
523
  leads_to_update.append({
 
536
  failed_count += 1
537
 
538
  # Step 2: Try to update each successfully added lead with custom variables
539
+ # First, fetch lead_id for any leads that don't have it
540
+ for item in leads_to_update:
541
+ if not item.get('lead_id'):
542
+ # Try to fetch lead_id by email
543
+ try:
544
+ lead_info = client.get_lead_by_email(campaign_id, item['email'])
545
+ if lead_info:
546
+ lead_id = lead_info.get('lead_id') or lead_info.get('id') or lead_info.get('leadId')
547
+ if lead_id:
548
+ item['lead_id'] = lead_id
549
+ except Exception as e:
550
+ # If we can't fetch lead_id, we'll skip updating this lead
551
+ pass
552
+
553
+ # Now update leads with custom variables
554
  for item in leads_to_update:
555
  if item.get('lead_id'):
556
  try:
 
569
  "error": f"Lead added but custom variables could not be set: {str(e)}"
570
  })
571
  else:
572
+ # If we still don't have lead_id after trying to fetch it, log an error
573
  errors.append({
574
  "email": item.get('email', 'unknown'),
575
+ "error": "Lead added but lead_id not found - cannot update custom variables. You may need to set them manually in Smartlead UI."
576
  })
577
 
578
  # Update run record
backend/app/smartlead_client.py CHANGED
@@ -161,6 +161,44 @@ class SmartleadClient:
161
  }
162
  return self._make_request("POST", f"/campaigns/{campaign_id}/leads", data)
163
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
164
  def update_campaign_settings(self, campaign_id: str, settings: Dict) -> Dict:
165
  """Update campaign settings"""
166
  return self._make_request("POST", f"/campaigns/{campaign_id}/settings", settings)
 
161
  }
162
  return self._make_request("POST", f"/campaigns/{campaign_id}/leads", data)
163
 
164
+ def get_lead_by_email(self, campaign_id: str, email: str) -> Optional[Dict]:
165
+ """Get lead information by email address
166
+
167
+ This may use different endpoint patterns:
168
+ - GET /campaigns/{campaign_id}/leads?email={email}
169
+ - GET /campaigns/{campaign_id}/leads/{email}
170
+ """
171
+ endpoints = [
172
+ f"/campaigns/{campaign_id}/leads?email={email}",
173
+ f"/campaigns/{campaign_id}/leads",
174
+ ]
175
+
176
+ for endpoint in endpoints:
177
+ try:
178
+ response = self._make_request("GET", endpoint)
179
+ # Response might be a list of leads or a single lead
180
+ if isinstance(response, list):
181
+ for lead in response:
182
+ if lead.get('email') == email or lead.get('email_address') == email:
183
+ return lead
184
+ elif isinstance(response, dict):
185
+ if response.get('email') == email or response.get('email_address') == email:
186
+ return response
187
+ # Check if response has a 'leads' or 'data' array
188
+ leads_list = response.get('leads') or response.get('data') or []
189
+ for lead in leads_list:
190
+ if lead.get('email') == email or lead.get('email_address') == email:
191
+ return lead
192
+ return None
193
+ except Exception as e:
194
+ # If 404 or other error, try next endpoint
195
+ if "404" in str(e):
196
+ continue
197
+ # For other errors, log and continue
198
+ continue
199
+
200
+ return None
201
+
202
  def update_campaign_settings(self, campaign_id: str, settings: Dict) -> Dict:
203
  """Update campaign settings"""
204
  return self._make_request("POST", f"/campaigns/{campaign_id}/settings", settings)