import assert from "assert"; import moment from "moment"; import { RawEvent, NormalizedEvent } from "../../types"; export default (event: RawEvent): NormalizedEvent<"hubspot"> => { assert(event, `Hubspot:contacts null event: ${event}`); assert( event.vid, `Hubspot:contacts event is missing the required field vid ${event}` ); // These aren't really events, so no timestamp const properties = event.properties || {}; const created = properties.createdate && properties.createdate.value && Date.parse(properties.createdate.value); const name = `${(properties.firstname && properties.firstname.value) || ""} ${(properties.lastname && properties.lastname.value) || ""}`.trim(); const avatar = properties.hs_avatar_filemanager_key && `https://cdn2.hubspot.net/${properties.hs_avatar_filemanager_key.value}`; const phone = (properties.hs_calculated_phone_number && properties.hs_calculated_phone_number.value) || (properties.phone && properties.phone.value) || (properties.mobilephone && properties.mobilephone.value); const company = { name: (properties.company && properties.company.value) || null, id: null, industry: (properties.industry && properties.industry.value) || null, employee_count: parseInt( (properties.company_size && properties.company_size.value) || (properties.numemployees && properties.numemployees.value) ) || null, plan: null }; let email = properties.email && properties.email.value; let birthday: string | null = null; if (properties.date_of_birth) { try { birthday = moment(properties.date_of_birth.value) .valueOf() .toString(); } catch (e) { console.log( "WARNING Hubspot:contacts Could not parse date_of_birth", properties.date_of_birth ); } } let userId; event["identity-profiles"].forEach(profile => { const emailSource = profile.identities.find( identity => identity.type === "EMAIL" ); const idSource = profile.identities.find( identity => identity.type === "ID" ); if (!email && emailSource) { email = emailSource.value; } if (idSource) { userId = idSource.value; } }); return { id: `hubspot|${event.vid}`, ts: created || 0, user_info: { source_user_id: String(event.vid), user_id: userId || null, name: name || null, email: email || null, title: (properties.jobtitle && properties.jobtitle.value) || null, birthday, company, created_at: created || null, age: null, gender: (properties.gender && properties.gender.value) || null, website: (properties.website && properties.website.value) || null, ip: null, avatar: avatar || null, nickname: null, phone: phone || null, city: (properties.city && properties.city.value) || null, country: (properties.country && properties.country.value) || null, postal_code: (properties.zip && properties.zip.value) || null, region: (properties.state && properties.state.value) || null, address_line1: (properties.address && properties.address.value) || null, continent_code: null, address_line2: null, country_code: null, timezone: null, user_agent: null, is_employee: null, custom_attributes: Object.keys(properties).length ? JSON.stringify({ "hubspot|contacts": { _ts: created || 0, ...Object.assign( {}, ...Object.keys(properties).map(key => ({ [key]: properties[key].value !== undefined ? properties[key].value : properties[key] })) ) } }) : null }, text: "Created Contact", href: event["profile-url"], is_bad: false, direction: null, source: "hubspot", stream: "contacts" }; };