From 83dcf278a51b42edbbf2688600d8966118160e98 Mon Sep 17 00:00:00 2001 From: Cursor Agent Date: Wed, 25 Nov 2026 00:51:59 +0000 Subject: [PATCH 1/2] fix(admin): CSV de prospectos legible en Excel MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit El export de /admin/leads salía sin BOM, con encabezados en inglés, fecha en UTC y el JSON de metadata en una sola columna. Excel en Windows mostraba acentos rotos y estados sin traducir. El archivo ahora lleva BOM y CRLF, columnas del nicho, fecha y hora en la zona del negocio, estado traducido y el nombre prospectos-AAAA-MM-DD.csv. Co-authored-by: santmun --- src/admin/routes.ts | 6 +- src/admin/views/leads.ts | 155 +++++++++++++++++++++++++++--- src/env.ts | 2 + test/admin/routes.test.ts | 192 +++++++++++++++++++++++++++++++++++++- 4 files changed, 339 insertions(+), 16 deletions(-) diff --git a/src/admin/routes.ts b/src/admin/routes.ts index 05219fb4..4aa2a4c1 100644 --- a/src/admin/routes.ts +++ b/src/admin/routes.ts @@ -42,7 +42,7 @@ import { KbDocsRepo, indexDoc, removeDocVectors, reindexAll, MAX_DOC_CHARS } fro import { renderMejoras } from "./views/mejoras"; import { runFlywheel, getLessons, saveLessons } from "../flywheel/detect"; import { applySuggestion, dismissSuggestion } from "../flywheel/apply"; -import { renderLeads, exportLeadsCsv } from "./views/leads"; +import { renderLeads, exportLeadsCsv, leadsExportFilename } from "./views/leads"; import { renderTickets } from "./views/tickets"; import { renderConfig } from "./views/config"; import { renderConexiones } from "./views/conexiones"; @@ -518,11 +518,13 @@ adminApp.post("/config", async (c) => { // --- CSV export ------------------------------------------------------------- adminApp.get("/leads/export.csv", async (c) => { + const now = new Date(); const csv = await exportLeadsCsv(c.env); + const filename = leadsExportFilename(c.env, now); return new Response(csv, { headers: { "Content-Type": "text/csv; charset=utf-8", - "Content-Disposition": `attachment; filename="leads-${Date.now()}.csv"`, + "Content-Disposition": `attachment; filename="${filename}"`, }, }); }); diff --git a/src/admin/views/leads.ts b/src/admin/views/leads.ts index 42be2cbf..59761bda 100644 --- a/src/admin/views/leads.ts +++ b/src/admin/views/leads.ts @@ -1,10 +1,22 @@ import type { Env } from "../../env"; import { Db } from "../../db/client"; import { LeadsRepo, leadMetadata, type Lead } from "../../db/leads"; -import { getNiche } from "../../niches"; +import { getNiche, type NichePack } from "../../niches"; +import { todayInTz } from "../../time/resolveDate"; +import { memberConfig } from "../../../member/config.local"; import { layout } from "./layout"; import { fmtDate, fmtDateTime } from "../format"; +// Mismas etiquetas que la tabla de Prospectos. El CSV las reutiliza para que +// Excel no muestre "intent" / "status" cuando el panel está en español. +const COL_FECHA = "Fecha"; +const COL_HORA = "Hora"; +const COL_NOMBRE = "Nombre"; +const COL_CONTACTO = "Contacto"; +const COL_RESUMEN = "Resumen"; +const COL_NOTAS = "Notas"; +const COL_ESTADO = "Estado"; + // Escapa texto del LLM/cliente antes de meterlo en HTML (el intent y las notas // pueden traer <, &, links pegados por el cliente, etc.). function esc(v: string | null | undefined): string { @@ -27,19 +39,19 @@ export async function renderLeads(env: Env): Promise { // Columnas: núcleo (fecha, nombre, contacto) + o bien las columnas del nicho // (leídas de metadata) o bien el "Resumen" genérico + estado (re-etiquetado). const cols: Col[] = [ - { h: "Fecha", w: "94px", cell: (l) => `${fmtDate(l.created_at)}` }, - { h: "Nombre", w: "minmax(120px,1.1fr)", cell: (l) => `${esc(l.name) || "(sin nombre)"}` }, - { h: "Contacto", w: "minmax(110px,1fr)", cell: (l) => `${esc(l.contact) || "—"}` }, + { h: COL_FECHA, w: "94px", cell: (l) => `${fmtDate(l.created_at)}` }, + { h: COL_NOMBRE, w: "minmax(120px,1.1fr)", cell: (l) => `${esc(l.name) || "(sin nombre)"}` }, + { h: COL_CONTACTO, w: "minmax(110px,1fr)", cell: (l) => `${esc(l.contact) || "—"}` }, ]; if (niche.columns.length) { for (const c of niche.columns) { cols.push({ h: c.label, w: "minmax(78px,.85fr)", cell: (_l, meta) => `${esc(meta[c.key]) || "—"}` }); } } else { - cols.push({ h: "Resumen · click para ver detalle", w: "minmax(200px,1.8fr)", cell: (l) => `${esc(l.intent)}` }); + cols.push({ h: `${COL_RESUMEN} · click para ver detalle`, w: "minmax(200px,1.8fr)", cell: (l) => `${esc(l.intent)}` }); } cols.push({ - h: "Estado", + h: COL_ESTADO, w: "132px", cell: (l) => `