MCPcopy Index your code
hub / github.com/simstudioai/sim / pollSearchBased

Function pollSearchBased

apps/sim/lib/webhooks/polling/hubspot.ts:208–348  ·  view source on GitHub ↗
(
  ctx: PollWebhookContext,
  config: HubSpotWebhookConfig,
  accessToken: string
)

Source from the content-addressed store, hash-verified

206}
207
208async function pollSearchBased(
209 ctx: PollWebhookContext,
210 config: HubSpotWebhookConfig,
211 accessToken: string
212): Promise<'success' | 'failure'> {
213 const { webhookData, workflowData, requestId, logger } = ctx
214 const webhookId = webhookData.id
215
216 const objectType = resolveObjectType(config)
217 const eventType = config.eventType
218 if (!objectType) {
219 throw new Error(`HubSpot webhook ${webhookId} is missing objectType`)
220 }
221 if (eventType !== 'created' && eventType !== 'updated' && eventType !== 'property_changed') {
222 throw new Error(`HubSpot webhook ${webhookId} is missing or has invalid eventType`)
223 }
224 if (eventType === 'property_changed' && !config.targetPropertyName?.trim()) {
225 throw new Error(
226 `HubSpot webhook ${webhookId} uses property_changed event but has no targetPropertyName`
227 )
228 }
229
230 // property_changed walks the modified-date stream and diffs the watched property locally.
231 const filterProperty =
232 eventType === 'created' ? 'createdate' : resolveModifiedDateProperty(objectType)
233 const nowMs = Date.now()
234
235 if (!config.lastSeenTimestampMs) {
236 await updateWebhookProviderConfig(
237 webhookId,
238 {
239 lastSeenTimestampMs: String(nowMs),
240 lastCheckedTimestamp: new Date(nowMs).toISOString(),
241 ...(eventType === 'property_changed'
242 ? {
243 propertySnapshot: {
244 property: config.targetPropertyName?.trim() ?? '',
245 entries: [],
246 },
247 }
248 : {}),
249 },
250 logger
251 )
252 await markWebhookSuccess(webhookId, logger)
253 logger.info(
254 `[${requestId}] Seeded HubSpot webhook ${webhookId} watermark to ${nowMs} (${objectType}/${eventType}/${filterProperty})`
255 )
256 return 'success'
257 }
258
259 const watermarkMs = Number(config.lastSeenTimestampMs)
260 if (!Number.isFinite(watermarkMs)) {
261 throw new Error(
262 `HubSpot webhook ${webhookId} has corrupt watermark ${config.lastSeenTimestampMs}`
263 )
264 }
265

Callers 1

pollWebhookFunction · 0.85

Calls 13

markWebhookSuccessFunction · 0.90
markWebhookFailedFunction · 0.90
resolveObjectTypeFunction · 0.85
buildUserFiltersFunction · 0.85
fetchHubSpotChangesFunction · 0.85
resolvePropertySnapshotFunction · 0.85
processRecordsFunction · 0.85
serializeSnapshotFunction · 0.85
infoMethod · 0.80

Tested by

no test coverage detected