Primer commit

This commit is contained in:
2026-05-30 14:31:19 -06:00
commit a35d26fac0
277 changed files with 265240 additions and 0 deletions
+41
View File
@@ -0,0 +1,41 @@
// DECISION DETERMINISTICA ESTRICTA (Create vs Update)
// Llave: el ID de la opp de SUCURSAL (unico en GHL), guardado como custom field
// "ID Oportunidad Sucursal" en la opp de Marca. Buscamos entre las opps del
// contacto en Marca una cuyo custom field valga exactamente ese id.
// - Si hay match -> UPDATE (esa opp exacta).
// - Si NO hay match -> CREATE (sin fallback por nombre).
const result = $input.first().json;
const opportunities = result.opportunities || [];
const sucursalOppId = $('Datos de Lead').first().json.Oportunidad.opportunity_id;
let action = 'CREATE';
let opportunityId = null;
let matchReason = '';
const match = opportunities.find(o =>
(o.customFields || []).some(cf => {
const v = cf.fieldValueString ?? cf.fieldValue ?? '';
return String(v) === String(sucursalOppId);
})
);
if (match) {
action = 'UPDATE';
opportunityId = match.id;
matchReason = 'Match por ID Oportunidad Sucursal';
} else {
action = 'CREATE';
matchReason = 'Sin match por ID Oportunidad Sucursal -> CREATE (contacto tiene ' + opportunities.length + ' opps)';
}
return [{
json: {
action,
opportunityId,
matchReason,
sucursalOppId,
contactId: $('Set Contact ID Resuelto').first().json.contactId,
totalOppsContacto: opportunities.length
}
}];