( emails: GmailEmail[], webhookData: PollWebhookContext['webhookData'], workflowData: PollWebhookContext['workflowData'], config: GmailWebhookConfig, accessToken: string, requestId: string, logger: Logger )
| 437 | } |
| 438 | |
| 439 | async function processEmails( |
| 440 | emails: GmailEmail[], |
| 441 | webhookData: PollWebhookContext['webhookData'], |
| 442 | workflowData: PollWebhookContext['workflowData'], |
| 443 | config: GmailWebhookConfig, |
| 444 | accessToken: string, |
| 445 | requestId: string, |
| 446 | logger: Logger |
| 447 | ) { |
| 448 | let processedCount = 0 |
| 449 | let failedCount = 0 |
| 450 | |
| 451 | for (const email of emails) { |
| 452 | try { |
| 453 | await pollingIdempotency.executeWithIdempotency( |
| 454 | 'gmail', |
| 455 | `${webhookData.id}:${email.id}`, |
| 456 | async () => { |
| 457 | const headers: Record<string, string> = {} |
| 458 | const payload = email.payload as Record<string, unknown> | undefined |
| 459 | if (payload?.headers && Array.isArray(payload.headers)) { |
| 460 | for (const header of payload.headers as { name: string; value: string }[]) { |
| 461 | headers[header.name.toLowerCase()] = header.value |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | let textContent = '' |
| 466 | let htmlContent = '' |
| 467 | |
| 468 | const extractContent = (part: Record<string, unknown>) => { |
| 469 | if (!part) return |
| 470 | |
| 471 | if (part.mimeType === 'text/plain') { |
| 472 | const body = part.body as { data?: string } | undefined |
| 473 | if (body?.data) { |
| 474 | textContent = Buffer.from(body.data, 'base64').toString('utf-8') |
| 475 | } |
| 476 | } else if (part.mimeType === 'text/html') { |
| 477 | const body = part.body as { data?: string } | undefined |
| 478 | if (body?.data) { |
| 479 | htmlContent = Buffer.from(body.data, 'base64').toString('utf-8') |
| 480 | } |
| 481 | } |
| 482 | |
| 483 | if (part.parts && Array.isArray(part.parts)) { |
| 484 | for (const subPart of part.parts) { |
| 485 | extractContent(subPart as Record<string, unknown>) |
| 486 | } |
| 487 | } |
| 488 | } |
| 489 | |
| 490 | if (payload) { |
| 491 | extractContent(payload) |
| 492 | } |
| 493 | |
| 494 | let date: string | null = null |
| 495 | if (headers.date) { |
| 496 | try { |
no test coverage detected