( changes: DriveChangeEntry[], config: GoogleDriveWebhookConfig, webhookData: PollWebhookContext['webhookData'], workflowData: PollWebhookContext['workflowData'], requestId: string, logger: Logger )
| 359 | } |
| 360 | |
| 361 | async function processChanges( |
| 362 | changes: DriveChangeEntry[], |
| 363 | config: GoogleDriveWebhookConfig, |
| 364 | webhookData: PollWebhookContext['webhookData'], |
| 365 | workflowData: PollWebhookContext['workflowData'], |
| 366 | requestId: string, |
| 367 | logger: Logger |
| 368 | ): Promise<{ processedCount: number; failedCount: number; newKnownFileIds: string[] }> { |
| 369 | let processedCount = 0 |
| 370 | let failedCount = 0 |
| 371 | const newKnownFileIds: string[] = [] |
| 372 | const knownFileIdsSet = new Set(config.knownFileIds || []) |
| 373 | |
| 374 | for (const change of changes) { |
| 375 | let eventType: 'created' | 'modified' | 'deleted' |
| 376 | if (change.removed) { |
| 377 | eventType = 'deleted' |
| 378 | } else if (!knownFileIdsSet.has(change.fileId)) { |
| 379 | eventType = 'created' |
| 380 | } else { |
| 381 | eventType = 'modified' |
| 382 | } |
| 383 | |
| 384 | // Track file as known regardless of filter so future changes are correctly classified |
| 385 | if (!change.removed) { |
| 386 | newKnownFileIds.push(change.fileId) |
| 387 | } |
| 388 | |
| 389 | // Apply event type filter before idempotency so filtered events aren't cached |
| 390 | const filter = config.eventTypeFilter |
| 391 | if (filter) { |
| 392 | const skip = filter === 'created_or_modified' ? eventType === 'deleted' : eventType !== filter |
| 393 | if (skip) continue |
| 394 | } |
| 395 | |
| 396 | try { |
| 397 | const idempotencyKey = `${webhookData.id}:${change.fileId}:${change.time || change.fileId}` |
| 398 | |
| 399 | await pollingIdempotency.executeWithIdempotency('google-drive', idempotencyKey, async () => { |
| 400 | const payload: GoogleDriveWebhookPayload = { |
| 401 | file: change.file || { id: change.fileId }, |
| 402 | eventType, |
| 403 | timestamp: new Date().toISOString(), |
| 404 | } |
| 405 | |
| 406 | const result = await processPolledWebhookEvent( |
| 407 | webhookData, |
| 408 | workflowData, |
| 409 | payload, |
| 410 | requestId |
| 411 | ) |
| 412 | |
| 413 | if (!result.success) { |
| 414 | logger.error( |
| 415 | `[${requestId}] Failed to process webhook for file ${change.fileId}:`, |
| 416 | result.statusCode, |
| 417 | result.error |
| 418 | ) |
no test coverage detected