fix: ventas KPI = Online Clientify (89) y atribución por fuente real

Excluye Málaga del cohort; enriquece source desde lead/Clientify para que el cruce ads vs CRM deje de volcar todo en sin-atribuir.

Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
4Ventures
2026-09-28 12:19:57 +02:00
parent 48fb31d247
commit 062c43325d
6 changed files with 206 additions and 27 deletions

View File

@@ -530,20 +530,26 @@ export type DealWonRow = {
source?: string | null;
pipeline_id?: string | null;
pipeline_name?: string | null;
lead_id?: string | null;
name?: string | null;
};
/**
* Match Clientify board «Online» (paid won). If the tenant has an Online
* pipeline, prefer it so totals match the board; otherwise keep all paid won.
* Match Clientify board «Online» (paid won).
* When an Online pipeline exists in the set, exclude other pipelines (e.g. Málaga).
* If no pipeline metadata at all, keep all paid won.
*/
export function preferOnlinePaidDeals(deals: DealWonRow[]): DealWonRow[] {
const paid = deals.filter((d) => n(d.amount) > 0);
const online = paid.filter((d) => {
const withPipe = paid.filter((d) => d.pipeline_id || d.pipeline_name);
if (!withPipe.length) return paid;
const online = withPipe.filter((d) => {
const id = String(d.pipeline_id || '');
const name = d.pipeline_name || '';
return id === '77547' || /online/i.test(name);
const name = (d.pipeline_name || '').trim();
return id === '77547' || /^online$/i.test(name) || /^online\b/i.test(name);
});
return online.length ? online : paid;
if (online.length) return online;
return paid;
}
export function filterDealsForRange(
@@ -653,12 +659,57 @@ export function normalizeLeadChannel(source?: string | null): string {
) {
return 'google';
}
if (
s === 'ecommerce' ||
s === 'woocommerce' ||
s === 'shopify' ||
s.includes('woocommerce') ||
s.includes('ecommerce') ||
s.includes('blue parrot')
) {
return 'ecommerce';
}
if (s.includes('recomend') || s.includes('amigo') || s === 'referral') {
return 'referral';
}
if (
s.includes('nivel') ||
s.includes('prueba') ||
s.startsWith('test') ||
s === 'general' ||
s === 'organic' ||
s === 'unknown' ||
s === 'import' ||
s === 'manual'
) {
return 'organic';
}
if (s === 'inbox_messenger' || s === 'messenger') return 'messenger';
if (s.startsWith('integrations')) return 'integrations-api';
if (s === 'whatsapp') return 'whatsapp';
return s;
}
/** Infer channel source for a won deal (Clientify deal_source / lead / order name). */
export function inferDealSource(d: {
source?: string | null;
name?: string | null;
tags?: string[] | null;
}): string {
const raw = (d.source || '').trim();
if (raw) return normalizeLeadChannel(raw);
const tags = (d.tags || []).join(' ').toLowerCase();
if (/facebook|instagram|\bmeta\b|\bfb\b/.test(tags)) return 'meta';
if (/google/.test(tags)) return 'google';
if (
/woocommerce|ecommerce/.test(tags) ||
/^order\s+\d+/i.test(d.name || '')
) {
return 'ecommerce';
}
return '';
}
export function normalizeLeadStatus(status?: string | null): string {
const s = (status || '').toLowerCase().trim();
if (!s) return 'sin-estado';
@@ -706,7 +757,9 @@ export function resolveWonCohort(
deals: DealWonRow[],
leads: CrmLeadRow[]
): { deals: DealWonRow[]; source: 'deals' | 'cliente_leads' | 'none' } {
const won = deals.filter((d) => !d.stage || d.stage === 'won');
const won = preferOnlinePaidDeals(
deals.filter((d) => !d.stage || d.stage === 'won')
);
if (won.length) return { deals: won, source: 'deals' };
const fromLeads = clienteLeadsAsWonDeals(leads);
if (fromLeads.length) return { deals: fromLeads, source: 'cliente_leads' };
@@ -924,8 +977,10 @@ export function attributionCross(
for (const d of preferOnlinePaidDeals(deals)) {
if (d.stage && d.stage !== 'won') continue;
const raw = (d.source || '').trim();
const channel = raw ? normalizeLeadChannel(raw) : 'sin-atribuir';
const channel =
inferDealSource(d) ||
(d.source ? normalizeLeadChannel(d.source) : '') ||
'sin-atribuir';
const cur = ensure(channel);
cur.ventas += 1;
cur.importe += n(d.amount);