(ctx: PollWebhookContext)
| 76 | label: 'IMAP', |
| 77 | |
| 78 | async pollWebhook(ctx: PollWebhookContext): Promise<'success' | 'failure'> { |
| 79 | const { webhookData, workflowData, requestId, logger } = ctx |
| 80 | const webhookId = webhookData.id |
| 81 | |
| 82 | try { |
| 83 | const config = getProviderConfig<ImapWebhookConfig>(webhookData.providerConfig) |
| 84 | |
| 85 | if (!config.host || !config.username || !config.password) { |
| 86 | logger.error(`[${requestId}] Missing IMAP credentials for webhook ${webhookId}`) |
| 87 | await markWebhookFailed(webhookId, logger) |
| 88 | return 'failure' |
| 89 | } |
| 90 | |
| 91 | const hostValidation = await validateDatabaseHost(config.host, 'host') |
| 92 | if (!hostValidation.isValid) { |
| 93 | logger.error( |
| 94 | `[${requestId}] IMAP host validation failed for webhook ${webhookId}: ${hostValidation.error}` |
| 95 | ) |
| 96 | await markWebhookFailed(webhookId, logger) |
| 97 | return 'failure' |
| 98 | } |
| 99 | |
| 100 | const client = new ImapFlow({ |
| 101 | host: hostValidation.resolvedIP!, |
| 102 | servername: config.host, |
| 103 | port: config.port || 993, |
| 104 | secure: config.secure ?? true, |
| 105 | auth: { |
| 106 | user: config.username, |
| 107 | pass: config.password, |
| 108 | }, |
| 109 | tls: { rejectUnauthorized: true }, |
| 110 | logger: false, |
| 111 | }) |
| 112 | |
| 113 | let emails: Awaited<ReturnType<typeof fetchNewEmails>>['emails'] = [] |
| 114 | let latestUidByMailbox: Record<string, number> = {} |
| 115 | let uidValidityByMailbox: Record<string, string> = {} |
| 116 | |
| 117 | try { |
| 118 | await client.connect() |
| 119 | |
| 120 | const result = await fetchNewEmails(client, config, requestId, logger) |
| 121 | emails = result.emails |
| 122 | latestUidByMailbox = result.latestUidByMailbox |
| 123 | uidValidityByMailbox = result.uidValidityByMailbox |
| 124 | |
| 125 | const pollTimestamp = new Date().toISOString() |
| 126 | |
| 127 | if (!emails.length) { |
| 128 | await updateImapState( |
| 129 | webhookId, |
| 130 | latestUidByMailbox, |
| 131 | pollTimestamp, |
| 132 | config, |
| 133 | logger, |
| 134 | uidValidityByMailbox |
| 135 | ) |
nothing calls this directly
no test coverage detected