diff --git a/project/build.properties b/project/build.properties index e67343a..c8fcab5 100755 --- a/project/build.properties +++ b/project/build.properties @@ -1 +1 @@ -sbt.version=1.5.0 +sbt.version=1.6.2 diff --git a/util-play/src/main/scala/de/innfactory/play/controller/BaseController.scala b/util-play/src/main/scala/de/innfactory/play/controller/BaseController.scala index 140f9a1..cc8f508 100755 --- a/util-play/src/main/scala/de/innfactory/play/controller/BaseController.scala +++ b/util-play/src/main/scala/de/innfactory/play/controller/BaseController.scala @@ -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] = diff --git a/util-play/src/main/scala/de/innfactory/play/mail/Mail.scala b/util-play/src/main/scala/de/innfactory/play/mail/Mail.scala index 9db95f6..a5418a9 100644 --- a/util-play/src/main/scala/de/innfactory/play/mail/Mail.scala +++ b/util-play/src/main/scala/de/innfactory/play/mail/Mail.scala @@ -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 + ) + } } diff --git a/util-play/src/main/scala/de/innfactory/play/mail/MailConfig.scala b/util-play/src/main/scala/de/innfactory/play/mail/MailConfig.scala index 10053e9..3987cc0 100644 --- a/util-play/src/main/scala/de/innfactory/play/mail/MailConfig.scala +++ b/util-play/src/main/scala/de/innfactory/play/mail/MailConfig.scala @@ -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")) +} \ No newline at end of file diff --git a/util-play/src/main/scala/de/innfactory/play/mail/MailResponse.scala b/util-play/src/main/scala/de/innfactory/play/mail/MailResponse.scala index a12f500..4123bc7 100644 --- a/util-play/src/main/scala/de/innfactory/play/mail/MailResponse.scala +++ b/util-play/src/main/scala/de/innfactory/play/mail/MailResponse.scala @@ -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] } diff --git a/util-play/src/main/scala/de/innfactory/play/mail/MailSendError.scala b/util-play/src/main/scala/de/innfactory/play/mail/MailSendError.scala index 1479cf4..0626f24 100644 --- a/util-play/src/main/scala/de/innfactory/play/mail/MailSendError.scala +++ b/util-play/src/main/scala/de/innfactory/play/mail/MailSendError.scala @@ -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 \ No newline at end of file + with Showable + +object MailSendError { + def desc = "Mailservice had a problem while sending the mail." +} \ No newline at end of file diff --git a/util-play/src/main/scala/de/innfactory/play/mail/MailService.scala b/util-play/src/main/scala/de/innfactory/play/mail/MailService.scala index c2b26d4..2ea08ff 100644 --- a/util-play/src/main/scala/de/innfactory/play/mail/MailService.scala +++ b/util-play/src/main/scala/de/innfactory/play/mail/MailService.scala @@ -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] } diff --git a/util-play/src/main/scala/de/innfactory/play/mail/MailServiceImpl.scala b/util-play/src/main/scala/de/innfactory/play/mail/MailServiceImpl.scala index a75087a..a00aebf 100644 --- a/util-play/src/main/scala/de/innfactory/play/mail/MailServiceImpl.scala +++ b/util-play/src/main/scala/de/innfactory/play/mail/MailServiceImpl.scala @@ -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} @@ -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 } diff --git a/util-play/src/main/scala/de/innfactory/play/slick/enhanced/utils/filteroptions/BooleanFilterOptions.scala b/util-play/src/main/scala/de/innfactory/play/slick/enhanced/utils/filteroptions/BooleanFilterOptions.scala index 2e8f677..3d3965e 100755 --- a/util-play/src/main/scala/de/innfactory/play/slick/enhanced/utils/filteroptions/BooleanFilterOptions.scala +++ b/util-play/src/main/scala/de/innfactory/play/slick/enhanced/utils/filteroptions/BooleanFilterOptions.scala @@ -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(