Skip to content

Commit

Permalink
feat(#3): mail api
Browse files Browse the repository at this point in the history
  • Loading branch information
jona7o committed Feb 13, 2022
1 parent 4142357 commit 3456f01
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 28 deletions.
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.5.0
sbt.version=1.6.2
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ abstract class BaseController(implicit cc: ControllerComponents, ec: ExecutionCo
def completeResult(statusCode: Int = 200)(implicit writes: Writes[T]): Future[play.api.mvc.Result] =
value.value.map {
case Left(error: ResultStatus) => mapToResult(error)
case Right(value: T) => MvcResults.Status(statusCode)(Json.toJson(value))
case Right(value) => MvcResults.Status(statusCode)(Json.toJson(value))
}

def completeResultWithoutBody(statusCode: Int = 200): Future[play.api.mvc.Result] =
Expand Down
29 changes: 20 additions & 9 deletions util-play/src/main/scala/de/innfactory/play/mail/Mail.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,28 @@ import play.api.libs.json.{JsObject, Json}


case class Mail(
recipients: Seq[String],
toCC: Option[Seq[String]],
toBCC: Option[Seq[String]],
replyTo: Option[String],
body: String,
fromName: String,
subject: String,
fromEmail: String
to: Seq[String],
cc: Option[Seq[String]] = None,
bcc: Option[Seq[String]] = None,
replyTo: Option[String] = None,
body: String,
fromName: String = "innFactory Mail Gateway",
subject: String,
from: String = "mail@innfactory.cloud"
) {

def toRequestJson: JsObject = ???
def toRequestJson: JsObject = {
Json.obj(
"recipients" -> to,
"toCC" -> cc,
"toBCC" -> bcc,
"replyTo" -> replyTo,
"body" -> body,
"fromName" -> fromName,
"subject" -> subject,
"fromEmail" -> from
)
}

}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
package de.innfactory.play.mail

import com.typesafe.config.Config

case class MailConfig(endpoint: String, apiKey: String)

object MailConfig {
def default()(implicit config : Config) = MailConfig(config.getString("mail.endpoint"), config.getString("mail.apiKey"))
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@ import play.api.libs.json.Json
case class MailResponse(id: String)

object MailResponse {
implicit val format = Json.format[MailResponse]
val empty: MailResponse = MailResponse("")

implicit val format = Json.format[MailResponse]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@ import de.innfactory.implicits.Showable
import de.innfactory.play.controller.ErrorResult

case class MailSendError(
message: String = "Mailservice had a problem while sending the mail",
message: String = "Mailservice had a problem while sending the mail.",
additionalInfoToLog: Option[String] = None,
additionalInfoErrorCode: Option[String] = None,
statusCode: Int = 503
) extends ErrorResult()
with Showable
with Showable

object MailSendError {
def desc = "Mailservice had a problem while sending the mail."
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ trait MailService {

val logger = Logger("mail").logger

def sendF(mail: Mail): Future[Option[MailResponse]]
def sendOpt(mail: Mail): Future[Option[MailResponse]]

def sendE(mail: Mail): Future[Result[MailResponse]]
def send(mail: Mail): Future[Result[MailResponse]]

def sendET(mail: Mail): EitherT[Future, Result, MailResponse] = EitherT(sendE(mail))
def sendET(mail: Mail): EitherT[Future, MailSendError, MailResponse]

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package de.innfactory.play.mail
import de.innfactory.implicits.OptionUtils.EnhancedOption
import de.innfactory.play.controller.ResultStatus
import de.innfactory.play.results.Results
import cats.data.EitherT
import cats.implicits.catsSyntaxEitherId
import de.innfactory.play.results.Results.Result
import play.api.libs.ws.{WSClient, WSRequest}

Expand All @@ -10,18 +9,27 @@ import scala.concurrent.{ExecutionContext, Future}

class MailServiceImpl @Inject()(mailConfig: MailConfig, wsClient: WSClient)(implicit ec: ExecutionContext) extends MailService {

override def sendOpt(mail: Mail): Future[Option[MailResponse]] = {
send(mail).map {
case Right(mailResponse) => Some(mailResponse)
case Left(_) => None
}
}

override def send(mail: Mail): Future[Result[MailResponse]] = sendET(mail).value

override def sendF(mail: Mail): Future[Option[MailResponse]] = {
override def sendET(mail: Mail): EitherT[Future, MailSendError, MailResponse] = {
val request: WSRequest = wsClient.url(mailConfig.endpoint + "/v1/sendmail")
request.post(mail.toRequestJson)
.map(response => {
.addHttpHeaders(("Authorization", mailConfig.apiKey))

EitherT(
request.post(mail.toRequestJson)
.map(response => {
if (response.status == 200) {
response.json.asOpt[MailResponse]
MailResponse(response.body.replace("\"", "")).asRight
} else {
None
Left(MailSendError(MailSendError.desc + s" (${response.status})"))
}
})
}))
}

override def sendE(mail: Mail): Future[Result[MailResponse]] = sendF(mail).map(_.toResult(MailSendError())) // TODO: implement error handling
}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ abstract class OptionalBooleanFilterOptions[E, V] extends FilterOptions[E, V] {
params.get(equalsQueryParam) match {
case Some(value) if value.nonEmpty => equalsOption = EqualsOptionBoolean(Some(value.headOption.get.toBoolean))
case None => equalsOption = EqualsOptionBoolean(None)
case _ => equalsOption = EqualsOptionBoolean(None)
}
if (equalsOption.value.isDefined)
Some(
Expand Down

0 comments on commit 3456f01

Please sign in to comment.