Skip to content

Commit 2ce3b3d

Browse files
committed
limit results to one page
1 parent 7e7e5df commit 2ce3b3d

File tree

1 file changed

+20
-46
lines changed

1 file changed

+20
-46
lines changed

components/hubspot/actions/search-crm/search-crm.mjs

Lines changed: 20 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ import {
44
DEFAULT_CONTACT_PROPERTIES,
55
DEFAULT_DEAL_PROPERTIES,
66
DEFAULT_LEAD_PROPERTIES,
7-
DEFAULT_LIMIT,
87
DEFAULT_LINE_ITEM_PROPERTIES,
98
DEFAULT_PRODUCT_PROPERTIES,
109
DEFAULT_TICKET_PROPERTIES,
1110
SEARCHABLE_OBJECT_TYPES,
1211
} from "../../common/constants.mjs";
1312
import hubspot from "../../hubspot.app.mjs";
1413
import common from "../common/common-create.mjs";
14+
const DEFAULT_LIMIT = 200;
1515

1616
export default {
1717
key: "hubspot-search-crm",
@@ -42,13 +42,6 @@ export default {
4242
"Set to `true` to search for an exact match of the search value. If `false`, partial matches will be returned. Default: `true`",
4343
default: true,
4444
optional: true,
45-
reloadProps: true,
46-
},
47-
createdAfter: {
48-
type: "string",
49-
label: "Created After",
50-
description: "Filter results to only include objects created after this date. Example: `2023-03-03T16:16:24.400Z`. Recommended when `Exact Match` is set to `false`.",
51-
optional: true,
5245
},
5346
createIfNotFound: {
5447
type: "boolean",
@@ -59,18 +52,17 @@ export default {
5952
optional: true,
6053
reloadProps: true,
6154
},
55+
offset: {
56+
type: "integer",
57+
label: "Offset",
58+
description: "The offset to start from. Used for pagination.",
59+
default: 0,
60+
optional: true,
61+
},
6262
},
6363
async additionalProps() {
6464
const props = {};
6565

66-
if (this.exactMatch === false && !this.createdAfter) {
67-
props.exactMatchAlert = {
68-
type: "alert",
69-
alertType: "info",
70-
content: "It is recommended to set `Created After` when `Exact Match` is set to `false` to avoid potential timeouts.",
71-
};
72-
}
73-
7466
if (this.objectType === "custom_object") {
7567
try {
7668
props.customObjectType = {
@@ -239,29 +231,6 @@ export default {
239231
})) || []
240232
);
241233
},
242-
async paginate(params) {
243-
let results, done = false;
244-
const items = [];
245-
while ((!results || params.after) && !done) {
246-
results = await this.hubspot.searchCRM(params);
247-
if (results.paging) {
248-
params.after = results.paging.next.after;
249-
} else {
250-
delete params.after;
251-
}
252-
results = results.results;
253-
for (const result of results) {
254-
if (this.createdAfter
255-
&& Date.parse(result.properties.createdate) <= Date.parse(this.createdAfter)
256-
) {
257-
done = true;
258-
break;
259-
}
260-
items.push(result);
261-
}
262-
}
263-
return items;
264-
},
265234
},
266235
async run({ $ }) {
267236
const {
@@ -272,9 +241,8 @@ export default {
272241
searchProperty,
273242
searchValue,
274243
exactMatch,
244+
offset,
275245
/* eslint-disable no-unused-vars */
276-
createdAfter,
277-
exactMatchAlert,
278246
info,
279247
createIfNotFound,
280248
creationProps,
@@ -313,7 +281,10 @@ export default {
313281
direction: "DESCENDING",
314282
},
315283
],
284+
limit: DEFAULT_LIMIT,
285+
after: offset,
316286
};
287+
317288
if (exactMatch) {
318289
data.filters = [
319290
{
@@ -322,17 +293,17 @@ export default {
322293
value: searchValue,
323294
},
324295
];
325-
} else {
326-
data.limit = DEFAULT_LIMIT;
327296
}
328297

329-
let results = await this.paginate({
298+
let {
299+
results, paging,
300+
} = await this.hubspot.searchCRM({
330301
object: actualObjectType,
331302
data,
332303
});
333304

334305
if (!exactMatch) {
335-
results = results.filter(
306+
results = results?.filter(
336307
(result) =>
337308
result.properties[searchProperty] &&
338309
result.properties[searchProperty]
@@ -358,6 +329,9 @@ export default {
358329
"$summary",
359330
`Successfully retrieved ${results?.length} object(s).`,
360331
);
361-
return results;
332+
return {
333+
results,
334+
paging,
335+
};
362336
},
363337
};

0 commit comments

Comments
 (0)