( accessToken: string, messageId: string, requestId: string, logger: Logger )
| 486 | } |
| 487 | |
| 488 | async function downloadOutlookAttachments( |
| 489 | accessToken: string, |
| 490 | messageId: string, |
| 491 | requestId: string, |
| 492 | logger: Logger |
| 493 | ): Promise<OutlookAttachment[]> { |
| 494 | const attachments: OutlookAttachment[] = [] |
| 495 | |
| 496 | try { |
| 497 | const response = await fetchWithRetry( |
| 498 | `https://graph.microsoft.com/v1.0/me/messages/${messageId}/attachments`, |
| 499 | { |
| 500 | headers: { |
| 501 | Authorization: `Bearer ${accessToken}`, |
| 502 | }, |
| 503 | } |
| 504 | ) |
| 505 | |
| 506 | if (!response.ok) { |
| 507 | logger.error(`[${requestId}] Failed to fetch attachments for message ${messageId}`) |
| 508 | return attachments |
| 509 | } |
| 510 | |
| 511 | const data = await response.json() |
| 512 | const attachmentsList = data.value || [] |
| 513 | |
| 514 | for (const attachment of attachmentsList) { |
| 515 | try { |
| 516 | if (attachment['@odata.type'] === '#microsoft.graph.fileAttachment') { |
| 517 | const contentBytes = attachment.contentBytes |
| 518 | if (contentBytes) { |
| 519 | const buffer = Buffer.from(contentBytes, 'base64') |
| 520 | attachments.push({ |
| 521 | name: attachment.name, |
| 522 | data: buffer, |
| 523 | contentType: attachment.contentType, |
| 524 | size: attachment.size, |
| 525 | }) |
| 526 | } |
| 527 | } |
| 528 | } catch (error) { |
| 529 | logger.error( |
| 530 | `[${requestId}] Error processing attachment ${attachment.id} for message ${messageId}:`, |
| 531 | error |
| 532 | ) |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | logger.info( |
| 537 | `[${requestId}] Downloaded ${attachments.length} attachments for message ${messageId}` |
| 538 | ) |
| 539 | } catch (error) { |
| 540 | logger.error(`[${requestId}] Error downloading attachments for message ${messageId}:`, error) |
| 541 | } |
| 542 | |
| 543 | return attachments |
| 544 | } |
| 545 |
no test coverage detected