Seth commited on
Commit
3a01a54
·
1 Parent(s): 9c4a359
Files changed (1) hide show
  1. backend/app/main.py +8 -4
backend/app/main.py CHANGED
@@ -227,6 +227,7 @@ async def download_sequences(file_id: str = Query(...), db: Session = Depends(ge
227
 
228
  # Group sequences by contact
229
  contacts = {}
 
230
  for seq in sequences:
231
  contact_key = f"{seq.sequence_id}"
232
  if contact_key not in contacts:
@@ -242,19 +243,22 @@ async def download_sequences(file_id: str = Query(...), db: Session = Depends(ge
242
  }
243
  contacts[contact_key]['subjects'][seq.email_number] = seq.subject
244
  contacts[contact_key]['bodies'][seq.email_number] = seq.email_content
 
 
 
245
 
246
  # Create CSV in memory
247
  # Use QUOTE_MINIMAL to properly quote fields with newlines for Smartlead
248
  output = io.StringIO()
249
  writer = csv.writer(output, quoting=csv.QUOTE_MINIMAL)
250
 
251
- # Write header with all subject/body columns
252
  header = ['First Name', 'Last Name', 'Email', 'Company', 'Title', 'Product']
253
- for i in range(1, 11):
254
  header.extend([f'Subject {i}', f'Body {i}'])
255
  writer.writerow(header)
256
 
257
- # Write rows (one row per contact with all subjects/bodies)
258
  # Preserve newlines in email bodies for proper formatting in Smartlead
259
  for contact in contacts.values():
260
  row = [
@@ -265,7 +269,7 @@ async def download_sequences(file_id: str = Query(...), db: Session = Depends(ge
265
  contact['title'],
266
  contact['product']
267
  ]
268
- for i in range(1, 11):
269
  subject = contact['subjects'].get(i, '') or ''
270
  body = contact['bodies'].get(i, '') or ''
271
 
 
227
 
228
  # Group sequences by contact
229
  contacts = {}
230
+ max_email_number = 0 # Track the maximum email number across all contacts
231
  for seq in sequences:
232
  contact_key = f"{seq.sequence_id}"
233
  if contact_key not in contacts:
 
243
  }
244
  contacts[contact_key]['subjects'][seq.email_number] = seq.subject
245
  contacts[contact_key]['bodies'][seq.email_number] = seq.email_content
246
+ # Track the maximum email number
247
+ if seq.email_number > max_email_number:
248
+ max_email_number = seq.email_number
249
 
250
  # Create CSV in memory
251
  # Use QUOTE_MINIMAL to properly quote fields with newlines for Smartlead
252
  output = io.StringIO()
253
  writer = csv.writer(output, quoting=csv.QUOTE_MINIMAL)
254
 
255
+ # Write header - only include columns up to the maximum email number found
256
  header = ['First Name', 'Last Name', 'Email', 'Company', 'Title', 'Product']
257
+ for i in range(1, max_email_number + 1):
258
  header.extend([f'Subject {i}', f'Body {i}'])
259
  writer.writerow(header)
260
 
261
+ # Write rows (one row per contact with subjects/bodies up to max_email_number)
262
  # Preserve newlines in email bodies for proper formatting in Smartlead
263
  for contact in contacts.values():
264
  row = [
 
269
  contact['title'],
270
  contact['product']
271
  ]
272
+ for i in range(1, max_email_number + 1):
273
  subject = contact['subjects'].get(i, '') or ''
274
  body = contact['bodies'].get(i, '') or ''
275