fix(crm): include missing columns in CSV/XML lead exports and fetch only latest execution per lead

Abierto
#68 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
52/100
Tipo de issue
Error
Claridad
Bien especificado
Estado de actividad
Activo
Stack tecnológico
nextjs, postgresql, typescript

Línea de trabajo

Start with getLeads in apps/api/src/crm/crm.service.ts and the export field definitions and CSV/XML methods in apps/api/src/crm/services/crm-export.service.ts. Trace how lead enrichment supplies the five named export columns and how automation executions are selected. Done means CSV/XML exports include those values with batched, capped processing, and the leads page fetches only the latest execution per lead.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

backend
Problem Description

In apps/api/src/crm/crm.service.ts (getLeads), there are two issues:

  1. CSV / XML exports are missing 5 columns and have no row limit (lines 128-189):

    • When exporting leads (?export=csv or ?export=xml), getLeads only loads basic lead profiles and contactCapture records.
    • It skips loading conversation, automationExecution, and audienceListMembers - so Origin Automation, Origin Folder, Audience List, In Sales Chat, and Conversation Stage all come out blank in the downloaded file.
    • It also loads every lead at once with no take limit, which can crash or time out on large accounts (10k+ leads).
  2. CRM Leads page loads too much old automation history (lines 244-270):

    • For the leads shown on the page, automationExecution.findMany loads every past automation run (including the full automation.nodes flow JSON), even though the code only uses the latest 1 run per lead.

Expected Behavior
  1. CSV and XML exports include Origin Automation, Origin Folder, Audience List, In Sales Chat, and Conversation Stage, and process/cap large exports safely so the server doesn't run out of memory.
  2. The CRM Leads page only fetches the latest 1 automationExecution per lead instead of all historical runs.

Where to Look
  • apps/api/src/crm/crm.service.ts (getLeads, lines 128-270)
  • apps/api/src/crm/services/crm-export.service.ts (getExportFieldDefinitions, exportAsCSV, exportAsXML)

Solution / Implementation Guide
  1. Fetch only the latest run per lead (crm.service.ts):
    • Add distinct: ['instagramUserId'] to prisma.automationExecution.findMany (it is already sorted by startedAt: 'desc', so Postgres will return only the newest execution per user).
  2. Reuse the enrichment logic for CSV/XML exports:
    • Extract a small helper to enrich leads with contactCapture, conversation, latest automationExecution, and audienceListMembers.
    • Use it in the export path too, fetching in batches (e.g., 500-1,000 leads at a time, with a safe max export cap like 10,000 rows).
Lenguaje dominante
TypeScript
Estrellas
2
Forks
1
Merge medio
6 min
PR fusionados (30 d)
27

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de sumamakhan761/tryinstance

Todos los issues de sumamakhan761/tryinstance

Issues similares

Más issues de TypeScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.