diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt index a000e85..24c986e 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/EmailsFetcher.kt @@ -34,21 +34,18 @@ open class EmailsFetcher( protected val log by logger() - open fun listenForNewEmails(account: EmailAccount, downloadMessageBody: Boolean = false, emailFolderName: String = "INBOX", - onError: ((FetchEmailsError) -> Unit)? = null, emailReceived: (EmailWithInvoice) -> Unit) = runBlocking { + open fun listenForNewEmails(account: EmailAccount, options: FetchEmailsOptions) = runBlocking { try { connect(account) { store -> - val folder = store.getFolder(emailFolderName) + val folder = store.getFolder(options.emailFolderName) folder.open(Folder.READ_ONLY) - val status = FetchEmailsStatus(FetchEmailsOptions(downloadMessageBody), onError = onError) + val status = FetchEmailsStatus(options) folder.addMessageCountListener(object : MessageCountAdapter() { override fun messagesAdded(event: MessageCountEvent) { event.messages.forEach { message -> - findEInvoice(message, status)?.let { - emailReceived(it) - } + findEInvoice(message, status) } } }) @@ -59,7 +56,7 @@ open class EmailsFetcher( } } catch (e: Throwable) { log.error(e) { "Listening to new emails of '${account.username}' failed" } - onError?.invoke(FetchEmailsError(FetchEmailsErrorType.ListenForNewEmails, null, e)) + options.onError?.invoke(FetchEmailsError(FetchEmailsErrorType.ListenForNewEmails, null, e)) } log.info { "Stopped listening to new emails of '${account.username}'" } @@ -82,13 +79,13 @@ open class EmailsFetcher( } - open fun fetchAllEmails(account: EmailAccount, downloadMessageBody: Boolean = false, emailFolderName: String = "INBOX"): FetchEmailsResult { + open fun fetchAllEmails(account: EmailAccount, options: FetchEmailsOptions = FetchEmailsOptions()): FetchEmailsResult { try { return connect(account) { store -> - val inbox = store.getFolder(emailFolderName) + val inbox = store.getFolder(options.emailFolderName) inbox.open(Folder.READ_ONLY) - val status = FetchEmailsStatus(FetchEmailsOptions(downloadMessageBody)) + val status = FetchEmailsStatus(options) val emails = fetchAllEmailsInFolder(inbox, status).also { inbox.close(false) @@ -132,13 +129,17 @@ open class EmailsFetcher( } if (attachmentsWithEInvoice.isNotEmpty()) { - return EmailWithInvoice( + val email = EmailWithInvoice( message.from?.joinToString(), message.subject ?: "", message.sentDate?.let { map(it) }, map(message.receivedDate), message.messageNumber, parts.any { it.mediaType == "application/pgp-encrypted" }, getPlainTextBody(parts, status), getHtmlBody(parts, status), attachmentsWithEInvoice ) + + status.options.emailReceived(email) + + return email } return null diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt index 161dab0..1c007a7 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsOptions.kt @@ -1,5 +1,13 @@ package net.codinux.invoicing.email data class FetchEmailsOptions( - val downloadMessageBody: Boolean = false -) \ No newline at end of file + val downloadMessageBody: Boolean = false, + val emailFolderName: String = "INBOX", + + val onError: ((FetchEmailsError) -> Unit)? = null, + val onEmailReceived: ((EmailWithInvoice) -> Unit)? = null +) { + fun emailReceived(email: EmailWithInvoice) { + onEmailReceived?.invoke(email) + } +} \ No newline at end of file diff --git a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsStatus.kt b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsStatus.kt index 5f40b72..e27049b 100644 --- a/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsStatus.kt +++ b/e-invoice-domain/src/main/kotlin/net/codinux/invoicing/email/FetchEmailsStatus.kt @@ -6,8 +6,7 @@ import jakarta.mail.Part data class FetchEmailsStatus( val options: FetchEmailsOptions, - val messageSpecificErrors: MutableList = mutableListOf(), - val onError: ((FetchEmailsError) -> Unit)? = null + val messageSpecificErrors: MutableList = mutableListOf() ) { fun addError(type: FetchEmailsErrorType, parts: Collection, error: Throwable) = @@ -22,7 +21,7 @@ data class FetchEmailsStatus( fun addError(error: FetchEmailsError) { messageSpecificErrors.add(error) - onError?.invoke(error) + options.onError?.invoke(error) } private fun getMessage(part: Part): Message? { diff --git a/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/email/EmailsFetcherTest.kt b/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/email/EmailsFetcherTest.kt index f3e984a..63f9c52 100644 --- a/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/email/EmailsFetcherTest.kt +++ b/e-invoice-domain/src/test/kotlin/net/codinux/invoicing/email/EmailsFetcherTest.kt @@ -25,7 +25,7 @@ class EmailsFetcherTest { @Test fun fetchAllEmails() { - val result = underTest.fetchAllEmails(emailAccount, true) + val result = underTest.fetchAllEmails(emailAccount, FetchEmailsOptions(true)) assertThat(result.emails).isNotEmpty()