Skip to content

Commit

Permalink
Fixing hubspot user fetch to not break if a company is not associated…
Browse files Browse the repository at this point in the history
… with a person
  • Loading branch information
davetaz committed Oct 9, 2024
1 parent 96e647f commit a19a9a5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 19 deletions.
52 changes: 34 additions & 18 deletions controllers/hubspot.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,34 +48,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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "maturity.theodi.org",
"version": "2.0.0",
"version": "2.0.1",
"description": "The ODI Maturity Assessment Tool",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit a19a9a5

Please sign in to comment.