From 29f9f3a9059858642bf83afd8941b9e2c7c39018 Mon Sep 17 00:00:00 2001 From: Gustavo Date: Fri, 31 Jul 2026 20:59:42 -0300 Subject: [PATCH] Close two minor gaps flagged by round-2 re-review of listLeads split Drop any row whose id disappeared between the id-pagination query and the hydrate query (e.g. a lead deleted by a base reset mid-request) instead of leaking an undefined hole into the response. Document that SORTS may only reference leads columns, since the id-pagination query no longer joins segments. --- src/treatment/queries.mjs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/treatment/queries.mjs b/src/treatment/queries.mjs index 882ccf2..2d9d05b 100644 --- a/src/treatment/queries.mjs +++ b/src/treatment/queries.mjs @@ -24,6 +24,10 @@ function sharedNumberColumnsSql(column) { ) END AS numero_compartilhado_override_raw`; } +// So colunas de `leads`: a paginacao de listLeads roda uma consulta so-id sem +// o LEFT JOIN de segments antes de hidratar a pagina, entao um sort aqui que +// referencie uma coluna de outra tabela (ex.: segments.affinity) quebraria +// essa consulta. const SORTS = { 'prioridade:asc': 'prioridade ASC, score DESC, cidade ASC, empresa ASC', 'score:desc': 'score DESC, empresa ASC', @@ -125,7 +129,10 @@ export function listLeads(db, filtro = {}) { ).all(...pageIds) .map((lead) => [lead.id, withSharedNumberState(withWhatsAppState(db, lead))])); - const rows = pageIds.map((id) => rowsById.get(id)); + // filter(Boolean): as duas consultas nao rodam na mesma transacao, entao um + // lead da pagina pode ser removido (reset da base) entre uma e outra — sem + // isso, esse id sobraria como buraco `undefined` na resposta. + const rows = pageIds.map((id) => rowsById.get(id)).filter(Boolean); return { total, page, pageSize, rows }; }