diff --git a/components/hubspot/actions/get-associated-emails/get-associated-emails.mjs b/components/hubspot/actions/get-associated-emails/get-associated-emails.mjs new file mode 100644 index 0000000000000..59adbab791ff1 --- /dev/null +++ b/components/hubspot/actions/get-associated-emails/get-associated-emails.mjs @@ -0,0 +1,102 @@ +import { DEFAULT_EMAIL_PROPERTIES } from "../../common/constants.mjs"; +import hubspot from "../../hubspot.app.mjs"; + +export default { + key: "hubspot-get-associated-emails", + name: "Get Associated Emails", + description: "Retrieves emails associated with a specific object (contact, company, or deal). [See the documentation](https://developers.hubspot.com/docs/reference/api/crm/search)", + version: "0.0.1", + type: "action", + props: { + hubspot, + objectType: { + propDefinition: [ + hubspot, + "objectType", + () => ({ + includeCustom: true, + }), + ], + label: "Associated Object Type", + description: "The type of the object the emails are associated with", + }, + objectId: { + type: "string", + label: "Object ID", + description: "The ID of the object to get associated emails for", + propDefinition: [ + hubspot, + "objectIds", + ({ objectType }) => ({ + objectType, + }), + ], + }, + additionalProperties: { + type: "string[]", + label: "Additional Properties", + description: "Additional properties to retrieve for the emails", + optional: true, + }, + limit: { + type: "integer", + label: "Limit", + description: "Maximum number of emails to retrieve", + optional: true, + default: 20, + min: 1, + max: 100, + }, + }, + async run({ $ }) { + const properties = [ + ...DEFAULT_EMAIL_PROPERTIES, + ...(this.additionalProperties || []), + ]; + + const { + objectType, objectId, limit, + } = this; + + const { results } = await this.hubspot.getAssociations({ + $, + objectType, + objectId, + toObjectType: "emails", + params: { + limit, + }, + }); + + if (!results?.length > 0) { + $.export("$summary", "No emails found with this association"); + return []; + } + + const emailIds = results.map((association) => ({ + id: association.toObjectId, + })); + + const { results: emails } = await this.hubspot.batchGetObjects({ + $, + objectType: "emails", + data: { + properties, + inputs: emailIds, + }, + }); + + // Sort emails by timestamp in descending order (most recent first) + emails?.sort((a, b) => { + const timestampA = new Date(a.properties?.hs_timestamp || 0).getTime(); + const timestampB = new Date(b.properties?.hs_timestamp || 0).getTime(); + return timestampB - timestampA; + }) || []; + + const summary = `Successfully retrieved ${emails.length} email(s)`; + + $.export("$summary", summary); + + return emails; + }, +}; diff --git a/components/hubspot/common/constants.mjs b/components/hubspot/common/constants.mjs index f06c89f81b369..156359d385d9e 100644 --- a/components/hubspot/common/constants.mjs +++ b/components/hubspot/common/constants.mjs @@ -172,6 +172,17 @@ const DEFAULT_LEAD_PROPERTIES = [ "hs_associated_company_name", ]; +const DEFAULT_EMAIL_PROPERTIES = [ + "hs_timestamp", + "hs_email_direction", + "hs_email_html", + "hs_email_status", + "hs_email_subject", + "hs_email_text", + "hs_attachment_ids", + "hs_email_headers", +]; + const DEFAULT_MEETING_PROPERTIES = [ "hs_timestamp", "hubspot_owner_id", @@ -225,5 +236,6 @@ export { DEFAULT_LINE_ITEM_PROPERTIES, DEFAULT_LEAD_PROPERTIES, ENGAGEMENT_TYPE_OPTIONS, + DEFAULT_EMAIL_PROPERTIES, DEFAULT_MEETING_PROPERTIES, }; diff --git a/components/hubspot/package.json b/components/hubspot/package.json index 0f2f0c1494063..435936d6ba838 100644 --- a/components/hubspot/package.json +++ b/components/hubspot/package.json @@ -1,6 +1,6 @@ { "name": "@pipedream/hubspot", - "version": "1.5.0", + "version": "1.6.0", "description": "Pipedream Hubspot Components", "main": "hubspot.app.mjs", "keywords": [ diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 957a06ef25bc6..f4c0876c8f99e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -10067,8 +10067,7 @@ importers: specifier: ^1.5.1 version: 1.6.6 - components/paazl: - specifiers: {} + components/paazl: {} components/paddle: {} @@ -11185,8 +11184,7 @@ importers: specifier: ^1.5.1 version: 1.6.6 - components/pulumi: - specifiers: {} + components/pulumi: {} components/pumble: dependencies: @@ -15989,8 +15987,7 @@ importers: components/zagomail: {} - components/zammad: - specifiers: {} + components/zammad: {} components/zamzar: dependencies: