Skip to content
// HubSpot deals → the common deal shape used by the rest of the workflow.
// The HubSpot node may return properties as { value: … } (legacy API) or plain values; handle both.
const s = $('Settings (edit me)').first().json;
const val = (p, k) => (p?.[k] && typeof p[k] === 'object' ? p[k].value : p?.[k]) ?? null;

return $input.all()
  .filter((i) => i.json.dealId || i.json.id)
  .map(({ json: d }) => {
    const p = d.properties ?? {};
    const lastActivity = val(p, 'notes_last_updated') ?? val(p, 'hs_lastmodifieddate');
    const ownerId = String(val(p, 'hubspot_owner_id') ?? '');
    return {
      json: {
        id: String(d.dealId ?? d.id),
        name: val(p, 'dealname') ?? '(unnamed deal)',
        stage: String(val(p, 'dealstage') ?? 'unknown').toLowerCase(),
        amount: Number(val(p, 'amount') ?? 0),
        owner: s.owners[ownerId]?.name ?? (ownerId ? `Owner ${ownerId}` : 'Unassigned'),
        contact: '',
        last_activity: lastActivity ? new Date(Number(lastActivity) || lastActivity).toISOString() : null,
        url: s.crm_deal_url ? s.crm_deal_url.replace('{id}', String(d.dealId ?? d.id)) : '',
      },
    };
  });