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

@@ -268,11 +268,28 @@ async function upsertDeal(tenantId: string, d: Json) {
}
}
const closed = str(d.actual_closed_date, 40);
const pipeUrl = str(d.pipeline, 200);
const pipeUrl = str(d.pipeline, 200) || '';
let pipelineId: string | null = null;
if (pipeUrl.includes('/pipelines/')) {
pipelineId = pipeUrl.replace(/\/$/, '').split('/').pop() || null;
}
const tags = Array.isArray(d.tags) ? d.tags.map(String) : [];
const tagBlob = tags.join(' ').toLowerCase();
const dealName = str(d.name, 200) || '';
let source =
str(d.deal_source, 80) ||
str(d.contact_source, 80) ||
'';
if (!source) {
if (/facebook|instagram|\bmeta\b|\bfb\b/.test(tagBlob)) source = 'meta';
else if (/google/.test(tagBlob)) source = 'google';
else if (
/woocommerce|ecommerce/.test(tagBlob) ||
/^order\s+\d+/i.test(dealName)
) {
source = 'ecommerce';
}
}
const row = {
id,
tenant_id: tenantId,
@@ -284,6 +301,8 @@ async function upsertDeal(tenantId: string, d: Json) {
lost_at: stage === 'lost' && closed ? closed : null,
pipeline_id: pipelineId,
pipeline_name: str(d.pipeline_desc, 120) || null,
source: source ? source.toLowerCase() : null,
name: dealName || null,
};
const base = supabaseUrl();
await fetch(