Skip to content

Commit

Permalink
Fixed batch email 500
Browse files Browse the repository at this point in the history
  • Loading branch information
LotuxPunk committed May 24, 2024
1 parent 1d63057 commit 2550584
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 32 deletions.
60 changes: 31 additions & 29 deletions src/main/kotlin/com/vandeas/logic/impl/MailLogicImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ class MailLogicImpl(

private val mailers: MutableMap<String, Mailer> = mutableMapOf() //TODO: Update mailers on config change/deletion

private fun MailConfig.getMailerOrCreate(): Mailer = mailers[id] ?: this.toMailer().also { mailers[id] = it }

override suspend fun sendContactForm(form: ContactForm): SendOperationResult {
val config = contactFormConfigHandler.get(form.id)

Expand Down Expand Up @@ -77,43 +79,43 @@ class MailLogicImpl(
mailConfigHandler.get(mailInput.id)
}

val mailInputsByConfigByIdentifier = mailInputsByConfig.entries.map {
it.toPair()
}.groupBy { (config, _) ->
val mailerByIdentifier = mailInputsByConfig.keys.groupBy { config ->
config.identifierFromCredentials()
}.mapValues { (_, configs) ->
configs.first().getMailerOrCreate()
}

if (
mailInputsByConfigByIdentifier.entries.any { (_, mails) ->
mails.sumOf { (_, mailInputs) -> mailInputs.size } > RESEND_BATCH_LIMIT
}
) {
throw IllegalArgumentException("Resend Batch limit exceeded")
val mailInputsByIdentifier = mailInputsByConfig.entries.groupBy { (config, _) ->
config.identifierFromCredentials()
}.mapValues { (_, mails) ->
mails.flatMap { (_, mailInputs) -> mailInputs }
}

require(mailInputsByIdentifier.values.all { mails -> mails.size <= RESEND_BATCH_LIMIT }) {
"Resend Batch limit exceeded"
}

mailInputsByConfigByIdentifier.map { (identifier, mailInputsByConfig) ->
val sendResults = mailInputsByIdentifier.map { (identifier, mailInputs) ->
async {
(mailers[identifier] ?: mailConfigHandler.get(identifier).toMailer().also { mailers[identifier] = it }).sendEmails(
mails = mailInputsByConfig.flatMap { (config, mailInputs) ->
val contentTemplate = Template.parse(mailConfigHandler.getTemplate(config.id))
val subjectTemplate = Template.parse(config.subjectTemplate)

mailInputs.map { mailInput ->
Mail(
from = config.sender,
to = mailInput.email,
subject = subjectTemplate.processToString(mailInput.attributes),
content = contentTemplate.processToString(mailInput.attributes)
)
}
mailerByIdentifier[identifier]!!.sendEmails(
mails = mailInputs.map { mailInput ->
val contentTemplate = Template.parse(mailConfigHandler.getTemplate(mailInput.id))
val subjectTemplate = Template.parse(mailConfigHandler.get(mailInput.id).subjectTemplate)

Mail(
from = mailConfigHandler.get(mailInput.id).sender,
to = mailInput.email,
subject = subjectTemplate.processToString(mailInput.attributes),
content = contentTemplate.processToString(mailInput.attributes)
)
}
)
}
}.awaitAll().let { sendResults ->
SendOperationResult(
sent = sendResults.flatMap { it.sent },
failed = sendResults.flatMap { it.failed }
)
}
}.awaitAll()

SendOperationResult(
sent = sendResults.flatMap { it.sent },
failed = sendResults.flatMap { it.failed }
)
}
}
11 changes: 9 additions & 2 deletions src/main/kotlin/com/vandeas/plugins/Routing.kt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ fun Application.configureRouting() {
response
)
} catch (e: Exception) {
application.log.error("Failed to send contact form: ${e.message}")
when (e) {
is DailyLimitExceededException -> call.respond(HttpStatusCode.TooManyRequests, e.message)
is RecaptchaFailedException -> call.respond(HttpStatusCode.Forbidden, e.message)
Expand All @@ -55,7 +56,10 @@ fun Application.configureRouting() {
} catch (e: Exception) {
when (e) {
is IllegalArgumentException -> call.respond(HttpStatusCode.BadRequest, e.message ?: "")
else -> call.respond(HttpStatusCode.InternalServerError)
else -> {
application.log.error("Failed to send batch of mails: ${e.message}")
call.respond(HttpStatusCode.InternalServerError)
}
}
}
}
Expand All @@ -68,7 +72,10 @@ fun Application.configureRouting() {
} catch (e: Exception) {
when (e) {
is IllegalArgumentException -> call.respond(HttpStatusCode.BadRequest, e.message ?: "")
else -> call.respond(HttpStatusCode.InternalServerError)
else -> {
application.log.error("Failed to send mail: ${e.message}")
call.respond(HttpStatusCode.InternalServerError)
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class ConfigDirectoryImpl<T: Config>(
}

override fun getTemplate(id: String): String {
return templateHandler.getFileContent(configs[id]?.second?.id ?: throw NoSuchElementException("Config with id $id not found"))
return templateHandler.getFileContent(configs[id]?.second?.id ?: throw NoSuchElementException("Template with id $id not found"))
}

override fun onFileCreate(fileName: String, content: String) {
Expand Down

0 comments on commit 2550584

Please sign in to comment.