Skip to content

Commit

Permalink
Hubspot Improvements (#12469)
Browse files Browse the repository at this point in the history
* move common files

* refactoring

* create-associations - parse toObjectIds array

* bug fixes

* combine create-contact & create-or-update-contact

* versions

* versions

* new-contact & contact-updated -> new-or-updated-contact

* new-company & company-updated -> new-or-updated-company

* new-deal & new-deal-updated -> new-or-updated-deal

* ticket properties

* new-product & product-updated -> new-or-updated-product

* new-line-item & line-item-updated -> new-or-updated-line-item

* new-blog-article & updated-blog-article -> new-or-updated-blog-article

* deprecate new-contact-in-list & add list filtering to new-or-updated-contact

* filter engagements by type

* add email event types

* add test-event to new-email-event

* add test-events to sources

* add default props to create-deal

* improve date prop descriptions

* add default props to create-ticket

* improve batch-create-or-update-contact

* add default properties to search-crm

* add createIfNotFound to search-crm

* add default props for get-company, get-contact, & get-deal

* refactoring

* updates

* improve summary
  • Loading branch information
michelle0927 committed Jul 1, 2024
1 parent e7371bb commit f6d3a72
Show file tree
Hide file tree
Showing 82 changed files with 2,686 additions and 1,657 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ export default {
key: "hubspot-add-contact-to-list",
name: "Add Contact to List",
description: "Adds a contact to a specific static list. [See the documentation](https://legacydocs.hubspot.com/docs/methods/lists/add_contact_to_list)",
version: "0.0.8",
version: "0.0.9",
type: "action",
props: {
hubspot,
Expand Down Expand Up @@ -33,9 +33,15 @@ export default {
list,
contactEmail,
} = this;
const response = await this.hubspot.addContactsToList(list.value, [
contactEmail,
], $);
const response = await this.hubspot.addContactsToList({
$,
listId: list.value,
data: {
emails: [
contactEmail,
],
},
});
$.export("$summary", "Successfully added contact to list");
return response;
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,14 @@ import hubspot from "../../hubspot.app.mjs";
export default {
key: "hubspot-batch-create-or-update-contact",
name: "Batch Create or Update Contact",
description: "Create or update a batch of contacts by its ID. [See the documentation](https://developers.hubspot.com/docs/api/crm/contacts)",
version: "0.0.5",
description: "Create or update a batch of contacts by its ID or email. [See the documentation](https://developers.hubspot.com/docs/api/crm/contacts)",
version: "0.0.6",
type: "action",
props: {
hubspot,
contacts: {
label: "Contacts Array",
description: "Provide a **list of contacts** to be created or updated. If the provided contact has the prop ID, this action will attempt to update it.\n\n**Expected format for create:** `{ \"company\": \"Biglytics\", \"email\": \"bcooper@biglytics.net\", \"firstname\": \"Bryan\", \"lastname\": \"Cooper\", \"phone\": \"(877) 929-0687\", \"website\": \"biglytics.net\" }` \n\n**Expected format for update:** `{ \"id\": \"101\", \"company\": \"Biglytics\", \"email\": \"bcooper@biglytics.net\", \"firstname\": \"Bryan\", \"lastname\": \"Cooper\", \"phone\": \"(877) 929-0687\", \"website\": \"biglytics.net\" }`",
description: "Provide a **list of contacts** to be created or updated. If the provided contact has the prop ID or if the provided email already exists, this action will attempt to update it.\n\n**Expected format for create:** `{ \"company\": \"Biglytics\", \"email\": \"bcooper@biglytics.net\", \"firstname\": \"Bryan\", \"lastname\": \"Cooper\", \"phone\": \"(877) 929-0687\", \"website\": \"biglytics.net\" }` \n\n**Expected format for update:** `{ \"id\": \"101\", \"company\": \"Biglytics\", \"email\": \"bcooper@biglytics.net\", \"firstname\": \"Bryan\", \"lastname\": \"Cooper\", \"phone\": \"(877) 929-0687\", \"website\": \"biglytics.net\" }`",
type: "string[]",
},
},
Expand All @@ -24,23 +24,58 @@ export default {
}
return contacts;
},
async searchExistingContactProperties(contacts, $) {
const emails = contacts.map(({ email }) => email);
const { results } = await this.hubspot.searchCRM({
$,
object: "contact",
data: {
filters: [
{
propertyName: "email",
operator: "IN",
values: emails,
},
],
},
});
const updateEmails = results?.map(({ properties }) => properties.email);
const insertProperties = contacts.filter(({ email }) => !updateEmails.includes(email))
.map((properties) => ({
properties,
}));
const updateProperties = [];
for (const contact of results) {
updateProperties.push({
id: contact.id,
properties: contacts.find(({ email }) => contact.properties.email === email),
});
}
return {
insertProperties,
updateProperties,
};
},
},
async run({ $ }) {
const contacts = this.parseContactArray(this.contacts);

const insertProperties = contacts.filter((contact) => (!Object.prototype.hasOwnProperty.call(contact, "id")))
.map((properties) => ({
properties,
}));
const {
insertProperties, updateProperties,
} = await this.searchExistingContactProperties(contacts, $);

const updateProperties = contacts.filter((contact) => (Object.prototype.hasOwnProperty.call(contact, "id")))
const updatePropertiesWithId = contacts.filter((contact) => (Object.prototype.hasOwnProperty.call(contact, "id")))
.map(({
id, ...properties
}) => ({
id: id,
properties,
}));

if (updatePropertiesWithId?.length) {
updateProperties.push(...updatePropertiesWithId);
}

let response = {};
response.created = await this.hubspot.batchCreateContacts({
$,
Expand Down
51 changes: 0 additions & 51 deletions components/hubspot/actions/common-get-object.mjs

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ export default {
label: "Property Groups",
reloadProps: true,
async options() {
const { results: groups } = await this.hubspot.getPropertyGroups(this.getObjectType());
const { results: groups } = await this.hubspot.getPropertyGroups({
objectType: this.getObjectType(),
});
return groups.map((group) => ({
label: group.label,
value: group.name,
Expand All @@ -23,7 +25,13 @@ export default {
...common.methods,
isRelevantProperty(property) {
const isInPropertyGroups = this.propertyGroups?.includes(property.groupName);
return common.methods.isRelevantProperty(property) && isInPropertyGroups;
const isDefaultProperty = this.isDefaultProperty(property);
return common.methods.isRelevantProperty(property)
&& isInPropertyGroups
&& !isDefaultProperty;
},
isDefaultProperty() {
return false;
},
},
async run({ $ }) {
Expand All @@ -46,7 +54,13 @@ export default {
}
});
try {
const response = await hubspot.createObject(objectType, properties, $);
const response = await hubspot.createObject({
$,
objectType,
data: {
properties,
},
});
const objectName = hubspot.getObjectTypeName(objectType);
$.export("$summary", `Successfully created ${objectName}`);

Expand All @@ -56,7 +70,14 @@ export default {
const errorObj = JSON.parse(err?.message);
if (errorObj?.category === "CONFLICT" || errorObj?.category === "OBJECT_ALREADY_EXISTS") {
const objectId = parseInt(errorObj.message.replace(/[^\d]/g, ""));
const response = await hubspot.updateObject(objectType, properties, objectId, $);
const response = await hubspot.updateObject({
$,
objectType,
objectId,
data: {
properties,
},
});
const objectName = hubspot.getObjectTypeName(objectType);
$.export("$summary", `Successfully updated ${objectName}`);
return response;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import hubspot from "../hubspot.app.mjs";
import hubspot from "../../hubspot.app.mjs";
import {
OBJECT_TYPE, HUBSPOT_OWNER,
} from "../common/constants.mjs";
} from "../../common/constants.mjs";

/**
* Returns an options method for a CRM object type, intended to be used in
Expand Down Expand Up @@ -79,23 +79,28 @@ export default {
options = getOptionsMethod(objectTypeName);
}

if (property.fieldType === "checkbox") {
type = "string[]";
if (property.name === "hs_timestamp") {
property.description += ". Enter date in ISO-8601 format. Example: `2024-06-25T15:43:49.214Z`";
}

return {
type,
name: property.name,
label: property.label,
description: property.description,
optional: !requiredProperties.includes(property),
optional: !requiredProperties.includes(property.name),
options,
};
},
},
async additionalProps() {
const schema = await this.hubspot.getSchema(this.getObjectType());
const { results: properties } = await this.hubspot.getProperties(this.getObjectType());
const objectType = this.getObjectType();
const schema = await this.hubspot.getSchema({
objectType,
});
const { results: properties } = await this.hubspot.getProperties({
objectType,
});
return properties
.filter(this.isRelevantProperty)
.map((property) => this.makePropDefinition(property, schema.requiredProperties))
Expand Down
102 changes: 102 additions & 0 deletions components/hubspot/actions/common/common-get-object.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import hubspot from "../../hubspot.app.mjs";
import {
DEFAULT_CONTACT_PROPERTIES,
DEFAULT_COMPANY_PROPERTIES,
DEFAULT_DEAL_PROPERTIES,
DEFAULT_TICKET_PROPERTIES,
DEFAULT_PRODUCT_PROPERTIES,
DEFAULT_LINE_ITEM_PROPERTIES,
} from "../../common/constants.mjs";

export default {
props: {
hubspot,
objectId: {
type: "string",
label: "Object ID",
description: "Hubspot's internal ID for the object",
async options(opts) {
return this.hubspot.createOptions(this.getObjectType(), opts);
},
reloadProps: true,
},
info: {
type: "alert",
alertType: "info",
content: "",
hidden: true,
},
// eslint-disable-next-line pipedream/props-description
additionalProperties: {
type: "string[]",
label: "Additional properties to retrieve",
optional: true,
async options({ page }) {
if (page !== 0) {
return [];
}
const { results: properties } = await this.hubspot.getProperties({
objectType: this.getObjectType(),
});
const defaultProperties = this.getDefaultProperties(this.getObjectType());
return properties
.filter(({ name }) => !defaultProperties.includes(name))
.map((property) => ({
label: property.label,
value: property.name,
}));
},
},
},
async additionalProps(props) {
return {
info: {
...props.info,
content: `Properties:\n\`${this.getDefaultProperties(this.getObjectType()).join(", ")}\``,
hidden: false,
},
};
},
methods: {
getObjectType() {
throw new Error("getObjectType is not implemented");
},
getDefaultProperties(objectType) {
if (objectType === "contact") {
return DEFAULT_CONTACT_PROPERTIES;
} else if (objectType === "company") {
return DEFAULT_COMPANY_PROPERTIES;
} else if (objectType === "deal") {
return DEFAULT_DEAL_PROPERTIES;
} else if (objectType === "ticket") {
return DEFAULT_TICKET_PROPERTIES;
} else if (objectType === "product") {
return DEFAULT_PRODUCT_PROPERTIES;
} else if (objectType === "line_item") {
return DEFAULT_LINE_ITEM_PROPERTIES;
} else {
return [];
}
},
},
async run({ $ }) {
const objectType = this.getObjectType();
const { additionalProperties = [] } = this;
const defaultProperties = this.getDefaultProperties(this.getObjectType());

const object = await this.hubspot.getObject(
objectType,
this.objectId,
[
...defaultProperties,
...additionalProperties,
],
$,
);

const objectName = this.hubspot.getObjectTypeName(objectType);
$.export("$summary", `Successfully fetched ${objectName}`);

return object;
},
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ export default {
label: "Property Groups",
reloadProps: true,
async options() {
const { results: groups } = await this.hubspot.getPropertyGroups(this.getObjectType());
const { results: groups } = await this.hubspot.getPropertyGroups({
objectType: this.getObjectType(),
});
return groups.map((group) => ({
label: group.label,
value: group.name,
Expand Down Expand Up @@ -45,7 +47,13 @@ export default {
}
});

const response = await hubspot.updateObject(objectType, properties, objectId, $);
const response = await hubspot.updateObject({
objectType,
objectId,
data: {
properties,
},
});
const objectName = hubspot.getObjectTypeName(objectType);

$.export("$summary", `Successfully updated ${objectName}`);
Expand Down
Loading

0 comments on commit f6d3a72

Please sign in to comment.