Skip to content

Commit e56c1e0

Browse files
authored
Merging pull request #18143
* 'Get Associated Emails' action * package/pnpm bump * Adjusting action requests * Expanding objectType prop * Adjusting request limit * Syntax adjustments
1 parent cf9efd3 commit e56c1e0

File tree

3 files changed

+115
-1
lines changed

3 files changed

+115
-1
lines changed
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
import { DEFAULT_EMAIL_PROPERTIES } from "../../common/constants.mjs";
2+
import hubspot from "../../hubspot.app.mjs";
3+
4+
export default {
5+
key: "hubspot-get-associated-emails",
6+
name: "Get Associated Emails",
7+
description: "Retrieves emails associated with a specific object (contact, company, or deal). [See the documentation](https://developers.hubspot.com/docs/reference/api/crm/search)",
8+
version: "0.0.1",
9+
type: "action",
10+
props: {
11+
hubspot,
12+
objectType: {
13+
propDefinition: [
14+
hubspot,
15+
"objectType",
16+
() => ({
17+
includeCustom: true,
18+
}),
19+
],
20+
label: "Associated Object Type",
21+
description: "The type of the object the emails are associated with",
22+
},
23+
objectId: {
24+
type: "string",
25+
label: "Object ID",
26+
description: "The ID of the object to get associated emails for",
27+
propDefinition: [
28+
hubspot,
29+
"objectIds",
30+
({ objectType }) => ({
31+
objectType,
32+
}),
33+
],
34+
},
35+
additionalProperties: {
36+
type: "string[]",
37+
label: "Additional Properties",
38+
description: "Additional properties to retrieve for the emails",
39+
optional: true,
40+
},
41+
limit: {
42+
type: "integer",
43+
label: "Limit",
44+
description: "Maximum number of emails to retrieve",
45+
optional: true,
46+
default: 20,
47+
min: 1,
48+
max: 100,
49+
},
50+
},
51+
async run({ $ }) {
52+
const properties = [
53+
...DEFAULT_EMAIL_PROPERTIES,
54+
...(this.additionalProperties || []),
55+
];
56+
57+
const {
58+
objectType, objectId, limit,
59+
} = this;
60+
61+
const { results } = await this.hubspot.getAssociations({
62+
$,
63+
objectType,
64+
objectId,
65+
toObjectType: "emails",
66+
params: {
67+
limit,
68+
},
69+
});
70+
71+
if (!results?.length > 0) {
72+
$.export("$summary", "No emails found with this association");
73+
return [];
74+
}
75+
76+
const emailIds = results.map((association) => ({
77+
id: association.toObjectId,
78+
}));
79+
80+
const { results: emails } = await this.hubspot.batchGetObjects({
81+
$,
82+
objectType: "emails",
83+
data: {
84+
properties,
85+
inputs: emailIds,
86+
},
87+
});
88+
89+
// Sort emails by timestamp in descending order (most recent first)
90+
emails?.sort((a, b) => {
91+
const timestampA = new Date(a.properties?.hs_timestamp || 0).getTime();
92+
const timestampB = new Date(b.properties?.hs_timestamp || 0).getTime();
93+
return timestampB - timestampA;
94+
}) || [];
95+
96+
const summary = `Successfully retrieved ${emails.length} email(s)`;
97+
98+
$.export("$summary", summary);
99+
100+
return emails;
101+
},
102+
};

components/hubspot/common/constants.mjs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,17 @@ const DEFAULT_LEAD_PROPERTIES = [
172172
"hs_associated_company_name",
173173
];
174174

175+
const DEFAULT_EMAIL_PROPERTIES = [
176+
"hs_timestamp",
177+
"hs_email_direction",
178+
"hs_email_html",
179+
"hs_email_status",
180+
"hs_email_subject",
181+
"hs_email_text",
182+
"hs_attachment_ids",
183+
"hs_email_headers",
184+
];
185+
175186
const DEFAULT_MEETING_PROPERTIES = [
176187
"hs_timestamp",
177188
"hubspot_owner_id",
@@ -225,5 +236,6 @@ export {
225236
DEFAULT_LINE_ITEM_PROPERTIES,
226237
DEFAULT_LEAD_PROPERTIES,
227238
ENGAGEMENT_TYPE_OPTIONS,
239+
DEFAULT_EMAIL_PROPERTIES,
228240
DEFAULT_MEETING_PROPERTIES,
229241
};

components/hubspot/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@pipedream/hubspot",
3-
"version": "1.5.0",
3+
"version": "1.6.0",
44
"description": "Pipedream Hubspot Components",
55
"main": "hubspot.app.mjs",
66
"keywords": [

0 commit comments

Comments
 (0)