From 94c027939345c9de2455f6f5b31660528cfa9990 Mon Sep 17 00:00:00 2001 From: David Tarrant Date: Wed, 9 Oct 2024 10:07:50 +0100 Subject: [PATCH] Fixing hubspot get user code --- controllers/hubspot.js | 52 ++++++++++++++++++++++++++-------------- package.json | 2 +- views/pages/glossary.ejs | 1 - 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/controllers/hubspot.js b/controllers/hubspot.js index 588c87d..dd9b3b8 100644 --- a/controllers/hubspot.js +++ b/controllers/hubspot.js @@ -24,34 +24,50 @@ async function getHubspotUser(userId, email) { } ], properties: ["email", "firstname", "lastname", "associatedcompanyid", "odi_membership__active_or_lapsed__", "odi_member_partner_type"] - } + }; + const contactResponse = await hubspotClient.crm.contacts.searchApi.doSearch(contactSearchRequest); + if (!contactResponse || !contactResponse.results || contactResponse.results.length === 0) { + throw new Error("No contact found with the provided email."); + } + const companyId = contactResponse.results[0].properties.associatedcompanyid; const hubSpotId = contactResponse.results[0].id; let membershipStatus = contactResponse.results[0].properties.odi_membership__active_or_lapsed__; let membershipType = contactResponse.results[0].properties.odi_member_partner_type; + let companyMembership = false; - const companySearchRequest = { - filterGroups: [ - { - filters: [ + // If companyId is valid, fetch company details + if (companyId) { + try { + const companySearchRequest = { + filterGroups: [ { - propertyName: "hs_object_id", - operator: "EQ", - value: companyId + filters: [ + { + propertyName: "hs_object_id", + operator: "EQ", + value: companyId + } + ] } - ] + ], + properties: ["name", "odi_membership_status__active_or_lapsed__", "member_partner_type_org_"] + }; + const companyResponse = await hubspotClient.crm.companies.searchApi.doSearch(companySearchRequest); + + if (companyResponse && companyResponse.results && companyResponse.results.length > 0) { + if (companyResponse.results[0].properties.odi_membership_status__active_or_lapsed__ === "Active") { + companyMembership = true; + membershipStatus = "Active"; + membershipType = companyResponse.results[0].properties.member_partner_type_org_; + } } - ], - properties: ["name", "odi_membership_status__active_or_lapsed__", "member_partner_type_org_"] - } - const companyResponse = await hubspotClient.crm.companies.searchApi.doSearch(companySearchRequest); - let companyMembership = false; - if (companyResponse.results[0].properties.odi_membership_status__active_or_lapsed__ == "Active") { - companyMembership = true; - membershipStatus = "Active"; - membershipType = companyResponse.results[0].properties.member_partner_type_org_; + } catch (companyError) { + console.warn("Error fetching company details:", companyError); + // Continue without company details if there's an error fetching them + } } // Check if a record with the hubSpotId already exists diff --git a/package.json b/package.json index 323f695..8281358 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "care.theodi.org", - "version": "2.8.1", + "version": "2.8.2", "description": "The ODI Care tool (AI enabled)", "main": "index.js", "scripts": { diff --git a/views/pages/glossary.ejs b/views/pages/glossary.ejs index dc8352d..8c4cf5c 100644 --- a/views/pages/glossary.ejs +++ b/views/pages/glossary.ejs @@ -1,4 +1,3 @@ -