| 383 | } |
| 384 | |
| 385 | async function getGmailProfileHistoryId( |
| 386 | accessToken: string, |
| 387 | requestId: string, |
| 388 | logger: Logger |
| 389 | ): Promise<string | null> { |
| 390 | try { |
| 391 | const response = await fetch('https://gmail.googleapis.com/gmail/v1/users/me/profile', { |
| 392 | headers: { Authorization: `Bearer ${accessToken}` }, |
| 393 | }) |
| 394 | if (!response.ok) { |
| 395 | logger.warn( |
| 396 | `[${requestId}] Failed to fetch Gmail profile for fresh historyId: ${response.status}` |
| 397 | ) |
| 398 | return null |
| 399 | } |
| 400 | const profile = await response.json() |
| 401 | return (profile.historyId as string | undefined) ?? null |
| 402 | } catch (error) { |
| 403 | logger.warn(`[${requestId}] Error fetching Gmail profile:`, error) |
| 404 | return null |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | async function getEmailDetails(accessToken: string, messageId: string): Promise<GmailEmail> { |
| 409 | const messageUrl = `https://gmail.googleapis.com/gmail/v1/users/me/messages/${messageId}?format=full` |