Skip to content

Commit de4c691

Browse files
committed
Adjusting action requests
1 parent f40d52a commit de4c691

File tree

1 file changed

+42
-48
lines changed

1 file changed

+42
-48
lines changed

components/hubspot/actions/get-associated-emails/get-associated-emails.mjs

Lines changed: 42 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -44,66 +44,60 @@ export default {
4444
label: "Limit",
4545
description: "Maximum number of emails to retrieve",
4646
optional: true,
47-
default: 100,
47+
default: 20,
4848
min: 1,
4949
max: 200,
5050
},
5151
},
52-
methods: {
53-
async getAssociatedEmails({
52+
async run({ $ }) {
53+
const properties = [
54+
...DEFAULT_EMAIL_PROPERTIES,
55+
...(this.additionalProperties || []),
56+
];
57+
58+
const {
59+
objectType, objectId, limit,
60+
} = this;
61+
62+
const { results } = await this.hubspot.getAssociations({
63+
$,
5464
objectType,
5565
objectId,
56-
additionalProperties = [],
57-
limit = 100,
58-
}) {
59-
const properties = [
60-
...DEFAULT_EMAIL_PROPERTIES,
61-
...additionalProperties,
62-
];
66+
toObjectType: "emails",
67+
params: {
68+
limit,
69+
},
70+
});
6371

64-
const { results } = await this.hubspot.searchCRM({
65-
object: "emails",
66-
data: {
67-
filterGroups: [
68-
{
69-
filters: [
70-
{
71-
propertyName: `associations.${objectType}`,
72-
operator: "EQ",
73-
value: objectId,
74-
},
75-
],
76-
},
77-
],
78-
properties,
79-
associations: [
80-
objectType,
81-
],
82-
sorts: [
83-
{
84-
propertyName: "hs_timestamp",
85-
direction: "DESCENDING",
86-
},
87-
],
88-
limit,
89-
},
90-
});
72+
if (!results?.length > 0) {
73+
$.export("$summary", "No emails found");
74+
return [];
75+
}
9176

92-
return results;
93-
},
94-
},
95-
async run({ $ }) {
96-
const emails = await this.getAssociatedEmails({
97-
objectType: this.objectType,
98-
objectId: this.objectId,
99-
additionalProperties: this.additionalProperties,
100-
limit: this.limit,
77+
const emailIds = results.map((association) => ({
78+
id: association.toObjectId,
79+
}));
80+
81+
const { results: emails } = await this.hubspot.batchGetObjects({
82+
$,
83+
objectType: "emails",
84+
data: {
85+
properties,
86+
inputs: emailIds,
87+
},
10188
});
10289

103-
const summary = `Successfully retrieved ${emails?.length || 0} email(s)`;
90+
// Sort emails by timestamp in descending order (most recent first)
91+
const sortedEmails = emails?.sort((a, b) => {
92+
const timestampA = new Date(a.properties?.hs_timestamp || 0).getTime();
93+
const timestampB = new Date(b.properties?.hs_timestamp || 0).getTime();
94+
return timestampB - timestampA;
95+
}) || [];
96+
97+
const summary = `Successfully retrieved ${sortedEmails?.length || 0} email(s)`;
10498

10599
$.export("$summary", summary);
106100

107-
return emails || [];
101+
return sortedEmails;
108102
},
109103
};

0 commit comments

Comments
 (0)