Skip to content

Commit d0923d8

Browse files
committed
cr 2
1 parent fa48fdb commit d0923d8

File tree

2 files changed

+101
-42
lines changed

2 files changed

+101
-42
lines changed

components/hubspot/hubspot.app.mjs

Lines changed: 47 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1264,24 +1264,58 @@ export default {
12641264
...opts,
12651265
});
12661266
},
1267-
getListMembershipsByJoinOrder({
1267+
async getListMembershipsByJoinOrder({
12681268
listId, ...opts
12691269
}) {
1270-
return this.makeRequest({
1271-
api: API_PATH.CRMV3,
1272-
endpoint: `/lists/${listId}/memberships/join-order`,
1273-
...opts,
1274-
});
1270+
const MAX_RETRIES = 5;
1271+
const BASE_RETRY_DELAY = 500;
1272+
let success = false;
1273+
let retries = 0;
1274+
while (!success) {
1275+
try {
1276+
const response = await this.makeRequest({
1277+
api: API_PATH.CRMV3,
1278+
endpoint: `/lists/${listId}/memberships/join-order`,
1279+
...opts,
1280+
});
1281+
return response;
1282+
} catch (error) {
1283+
if (error.status === 429 && ++retries < MAX_RETRIES) {
1284+
const randomDelay = Math.floor(Math.random() * BASE_RETRY_DELAY);
1285+
const delay = BASE_RETRY_DELAY * (2 ** retries) + randomDelay;
1286+
await new Promise((resolve) => setTimeout(resolve, delay));
1287+
} else {
1288+
throw error;
1289+
}
1290+
}
1291+
}
12751292
},
1276-
batchGetObjects({
1293+
async batchGetObjects({
12771294
objectType, ...opts
12781295
}) {
1279-
return this.makeRequest({
1280-
api: API_PATH.CRMV3,
1281-
endpoint: `/objects/${objectType}/batch/read`,
1282-
method: "POST",
1283-
...opts,
1284-
});
1296+
const MAX_RETRIES = 5;
1297+
const BASE_RETRY_DELAY = 500;
1298+
let success = false;
1299+
let retries = 0;
1300+
while (!success) {
1301+
try {
1302+
const response = await this.makeRequest({
1303+
api: API_PATH.CRMV3,
1304+
endpoint: `/objects/${objectType}/batch/read`,
1305+
method: "POST",
1306+
...opts,
1307+
});
1308+
return response;
1309+
} catch (error) {
1310+
if (error.status === 429 && ++retries < MAX_RETRIES) {
1311+
const randomDelay = Math.floor(Math.random() * BASE_RETRY_DELAY);
1312+
const delay = BASE_RETRY_DELAY * (2 ** retries) + randomDelay;
1313+
await new Promise((resolve) => setTimeout(resolve, delay));
1314+
} else {
1315+
throw error;
1316+
}
1317+
}
1318+
}
12851319
},
12861320
listNotes(opts = {}) {
12871321
return this.makeRequest({

components/hubspot/sources/new-contact-added-to-list/new-contact-added-to-list.mjs

Lines changed: 54 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export default {
1111
name: "New Contact Added to List",
1212
description:
1313
"Emit new event when a contact is added to a HubSpot list. [See the documentation](https://developers.hubspot.com/docs/reference/api/crm/lists#get-%2Fcrm%2Fv3%2Flists%2F%7Blistid%7D%2Fmemberships%2Fjoin-order)",
14-
version: "0.0.10",
14+
version: "0.0.1",
1515
type: "source",
1616
dedupe: "unique",
1717
props: {
@@ -44,13 +44,14 @@ export default {
4444
},
4545
methods: {
4646
...common.methods,
47+
_getKey(listId) {
48+
return `list_${listId}_last_timestamp`;
49+
},
4750
_getLastMembershipTimestamp(listId) {
48-
const key = `list_${listId}_last_timestamp`;
49-
return this.db.get(key);
51+
return this.db.get(this._getKey(listId));
5052
},
5153
_setLastMembershipTimestamp(listId, timestamp) {
52-
const key = `list_${listId}_last_timestamp`;
53-
this.db.set(key, timestamp);
54+
this.db.set(this._getKey(listId), timestamp);
5455
},
5556
getTs() {
5657
return Date.now();
@@ -78,24 +79,41 @@ export default {
7879
...properties,
7980
];
8081

82+
const chunks = [];
83+
const chunkSize = 100;
84+
for (let i = 0; i < contactIds.length; i += chunkSize) {
85+
chunks.push(contactIds.slice(i, i + chunkSize));
86+
}
87+
88+
const contactMap = {};
89+
8190
try {
82-
const { results } = await this.hubspot.batchGetObjects({
83-
objectType: "contacts",
84-
data: {
85-
inputs: contactIds.map((id) => ({
86-
id,
87-
})),
88-
properties: allProperties,
89-
},
90-
});
91-
92-
const contactMap = {};
93-
results.forEach((contact) => {
94-
contactMap[contact.id] = contact;
95-
});
91+
for (const chunk of chunks) {
92+
try {
93+
const { results } = await this.hubspot.batchGetObjects({
94+
objectType: "contacts",
95+
data: {
96+
inputs: chunk.map((id) => ({
97+
id,
98+
})),
99+
properties: allProperties,
100+
},
101+
});
102+
103+
results.forEach((contact) => {
104+
contactMap[contact.id] = contact;
105+
});
106+
} catch (error) {
107+
console.warn(
108+
`Error fetching contact details for chunk of ${chunk.length} contacts:`,
109+
error,
110+
);
111+
}
112+
}
113+
96114
return contactMap;
97115
} catch (error) {
98-
console.warn("Error fetching contact details:", error);
116+
console.warn("Error processing contact details:", error);
99117
return {};
100118
}
101119
},
@@ -126,25 +144,29 @@ export default {
126144
params,
127145
});
128146

129-
if (!results || results.length === 0) {
147+
if (!results) {
148+
console.warn(
149+
`No results returned from API for list ${listId} - possible API issue`,
150+
);
151+
break;
152+
}
153+
154+
if (results.length === 0) {
130155
break;
131156
}
132157

133158
for (const membership of results) {
134159
const { membershipTimestamp } = membership;
135160

136-
if (
137-
new Date(membershipTimestamp) > new Date(lastMembershipTimestamp)
138-
) {
161+
if (membershipTimestamp > lastMembershipTimestamp) {
139162
newMemberships.push({
140163
membership,
141164
listInfo,
142165
});
143166

144167
if (
145168
!latestMembershipTimestamp ||
146-
new Date(membershipTimestamp) >
147-
new Date(latestMembershipTimestamp)
169+
membershipTimestamp > latestMembershipTimestamp
148170
) {
149171
latestMembershipTimestamp = membershipTimestamp;
150172
}
@@ -168,7 +190,10 @@ export default {
168190
return newMemberships;
169191
},
170192
async processResults() {
171-
const { listId } = this;
193+
const {
194+
listId,
195+
listInfo: { name },
196+
} = this;
172197

173198
if (!listId) {
174199
console.warn("No list selected to monitor");
@@ -177,7 +202,7 @@ export default {
177202

178203
const listInfo = {
179204
listId,
180-
name: `List ${listId}`,
205+
name: `List ${name}`,
181206
};
182207

183208
try {
@@ -203,7 +228,7 @@ export default {
203228
contactId: membership.recordId,
204229
contact: contactDetail,
205230
membership,
206-
addedAt: new Date().toISOString(),
231+
addedAt: membership.membershipTimestamp,
207232
};
208233

209234
const meta = this.generateMeta(membership, listInfo);

0 commit comments

Comments
 (0)