From ed034f10690a0e2400280633451629e94fb24348 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Manciot?= Date: Fri, 21 Jul 2023 20:43:37 +0200 Subject: [PATCH] refactoring after upgrading endpoints api dependencies --- build.sbt | 2 +- .../payment/launch/PaymentEndpoints.scala | 15 +++--- .../service/BankAccountEndpoints.scala | 25 ++++----- .../payment/service/CardEndpoints.scala | 25 ++++----- .../service/CardPaymentEndpoints.scala | 33 +++++------- .../service/GenericPaymentEndpoints.scala | 10 ++-- .../service/KycDocumentEndpoints.scala | 20 +++---- .../payment/service/MandateEndpoints.scala | 23 ++++---- .../service/RecurringPaymentEndpoints.scala | 41 ++++++-------- .../service/RootPaymentEndpoints.scala | 54 +++++++------------ .../service/UboDeclarationEndpoints.scala | 26 ++++----- .../api/src/main/resources/reference.conf | 2 +- .../app/softnetwork/sbt/build/Versions.scala | 4 +- .../scalatest/PaymentEndpointsTestKit.scala | 24 +++------ 14 files changed, 118 insertions(+), 186 deletions(-) diff --git a/build.sbt b/build.sbt index f118710..6364d9e 100644 --- a/build.sbt +++ b/build.sbt @@ -31,7 +31,7 @@ ThisBuild / organization := "app.softnetwork" name := "payment" -ThisBuild / version := "0.4.0" +ThisBuild / version := "0.4.1" ThisBuild / scalaVersion := "2.12.15" diff --git a/core/src/main/scala/app/softnetwork/payment/launch/PaymentEndpoints.scala b/core/src/main/scala/app/softnetwork/payment/launch/PaymentEndpoints.scala index 1aceff8..c60bd0c 100644 --- a/core/src/main/scala/app/softnetwork/payment/launch/PaymentEndpoints.scala +++ b/core/src/main/scala/app/softnetwork/payment/launch/PaymentEndpoints.scala @@ -1,17 +1,12 @@ package app.softnetwork.payment.launch import akka.actor.typed.ActorSystem -import app.softnetwork.api.server.ApiEndpoints +import app.softnetwork.api.server.{ApiEndpoint, ApiEndpoints} import app.softnetwork.payment.serialization.paymentFormats import app.softnetwork.payment.service.GenericPaymentEndpoints import app.softnetwork.persistence.schema.SchemaProvider import app.softnetwork.session.service.SessionEndpoints import org.json4s.Formats -import sttp.capabilities -import sttp.capabilities.akka.AkkaStreams -import sttp.tapir.server.ServerEndpoint - -import scala.concurrent.Future trait PaymentEndpoints extends ApiEndpoints with PaymentGuardian { _: SchemaProvider => @@ -21,7 +16,9 @@ trait PaymentEndpoints extends ApiEndpoints with PaymentGuardian { _: SchemaProv def paymentEndpoints: ActorSystem[_] => GenericPaymentEndpoints - override def endpoints - : ActorSystem[_] => List[ServerEndpoint[AkkaStreams with capabilities.WebSockets, Future]] = - system => paymentEndpoints(system).endpoints + override def endpoints: ActorSystem[_] => List[ApiEndpoint] = + system => + List( + paymentEndpoints(system) + ) } diff --git a/core/src/main/scala/app/softnetwork/payment/service/BankAccountEndpoints.scala b/core/src/main/scala/app/softnetwork/payment/service/BankAccountEndpoints.scala index 17f98e5..c7317d3 100644 --- a/core/src/main/scala/app/softnetwork/payment/service/BankAccountEndpoints.scala +++ b/core/src/main/scala/app/softnetwork/payment/service/BankAccountEndpoints.scala @@ -7,9 +7,7 @@ import app.softnetwork.payment.model.{BankAccountView, PaymentAccount} import sttp.capabilities import sttp.capabilities.akka.AkkaStreams import sttp.model.StatusCode -import sttp.tapir.generic.auto._ import sttp.tapir.json.json4s.jsonBody -import sttp.tapir._ import sttp.tapir.server.ServerEndpoint import scala.concurrent.Future @@ -26,8 +24,7 @@ trait BankAccountEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler statusCode(StatusCode.Ok) .and(jsonBody[BankAccountCreatedOrUpdated].description("Bank account created or updated")) ) - .serverLogic(principal => { bank => - val session = principal._2 + .serverLogic(session => { bank => import bank._ var externalUuid: String = "" val updatedUser: Option[PaymentAccount.User] = { @@ -73,7 +70,7 @@ trait BankAccountEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler acceptedTermsOfPSP ) ).map { - case r: BankAccountCreatedOrUpdated => Right((principal._1._1, principal._1._2, r)) + case r: BankAccountCreatedOrUpdated => Right(r) case other => Left(error(other)) } }) @@ -88,14 +85,13 @@ trait BankAccountEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler .description("Authenticated user bank account") ) ) - .serverLogic(principal => + .serverLogic(session => _ => { run( - LoadBankAccount(externalUuidWithProfile(principal._2)) + LoadBankAccount(externalUuidWithProfile(session)) ).map { - case r: BankAccountLoaded => - Right((principal._1._1, principal._1._2, r.bankAccount.view)) - case other => Left(error(other)) + case r: BankAccountLoaded => Right(r.bankAccount.view) + case other => Left(error(other)) } } ) @@ -107,12 +103,11 @@ trait BankAccountEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler .out( statusCode(StatusCode.Ok).and(jsonBody[BankAccountDeleted.type]) ) - .serverLogic(principal => + .serverLogic(session => _ => - run(DeleteBankAccount(externalUuidWithProfile(principal._2), Some(false))).map { - case BankAccountDeleted => - Right((principal._1._1, principal._1._2, BankAccountDeleted)) - case other => Left(error(other)) + run(DeleteBankAccount(externalUuidWithProfile(session), Some(false))).map { + case BankAccountDeleted => Right(BankAccountDeleted) + case other => Left(error(other)) } ) .description("Delete authenticated user bank account") diff --git a/core/src/main/scala/app/softnetwork/payment/service/CardEndpoints.scala b/core/src/main/scala/app/softnetwork/payment/service/CardEndpoints.scala index b88fd72..0d112f0 100644 --- a/core/src/main/scala/app/softnetwork/payment/service/CardEndpoints.scala +++ b/core/src/main/scala/app/softnetwork/payment/service/CardEndpoints.scala @@ -7,9 +7,7 @@ import app.softnetwork.payment.model.{CardPreRegistration, CardView} import sttp.capabilities import sttp.capabilities.akka.AkkaStreams import sttp.model.StatusCode -import sttp.tapir.generic.auto._ import sttp.tapir.json.json4s.jsonBody -import sttp.tapir._ import sttp.tapir.server.ServerEndpoint import scala.concurrent.Future @@ -26,12 +24,11 @@ trait CardEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler => jsonBody[Seq[CardView]].description("Authenticated user cards") ) ) - .serverLogic(principal => + .serverLogic(session => _ => { - run(LoadCards(externalUuidWithProfile(principal._2))).map { - case r: CardsLoaded => - Right((principal._1._1, principal._1._2, r.cards.map(_.view))) - case other => Left(error(other)) + run(LoadCards(externalUuidWithProfile(session))).map { + case r: CardsLoaded => Right(r.cards.map(_.view)) + case other => Left(error(other)) } } ) @@ -47,9 +44,8 @@ trait CardEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler => .description("Card pre registration data") ) ) - .serverLogic(principal => + .serverLogic(session => cmd => { - val session = principal._2 var updatedUser = if (cmd.user.externalUuid.trim.isEmpty) { cmd.user.withExternalUuid(session.id) @@ -62,9 +58,8 @@ trait CardEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler => case _ => } run(cmd.copy(user = updatedUser)).map { - case r: CardPreRegistered => - Right((principal._1._1, principal._1._2, r.cardPreRegistration)) - case other => Left(error(other)) + case r: CardPreRegistered => Right(r.cardPreRegistration) + case other => Left(error(other)) } } ) @@ -77,10 +72,10 @@ trait CardEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler => .out( statusCode(StatusCode.Ok).and(jsonBody[CardDisabled.type]) ) - .serverLogic(principal => + .serverLogic(session => cardId => { - run(DisableCard(externalUuidWithProfile(principal._2), cardId)).map { - case CardDisabled => Right((principal._1._1, principal._1._2, CardDisabled)) + run(DisableCard(externalUuidWithProfile(session), cardId)).map { + case CardDisabled => Right(CardDisabled) case other => Left(error(other)) } } diff --git a/core/src/main/scala/app/softnetwork/payment/service/CardPaymentEndpoints.scala b/core/src/main/scala/app/softnetwork/payment/service/CardPaymentEndpoints.scala index a95fed4..4c7aa0c 100644 --- a/core/src/main/scala/app/softnetwork/payment/service/CardPaymentEndpoints.scala +++ b/core/src/main/scala/app/softnetwork/payment/service/CardPaymentEndpoints.scala @@ -1,6 +1,5 @@ package app.softnetwork.payment.service -import app.softnetwork.api.server.ApiErrors import app.softnetwork.payment.config.PaymentSettings import app.softnetwork.payment.handlers.GenericPaymentHandler import app.softnetwork.payment.message.PaymentMessages._ @@ -9,10 +8,8 @@ import sttp.capabilities import sttp.capabilities.akka.AkkaStreams import sttp.model.{HeaderNames, Method, StatusCode} import sttp.model.headers.CookieValueWithMeta -import sttp.tapir.generic.auto._ import sttp.tapir.json.json4s.jsonBody -import sttp.tapir._ -import sttp.tapir.server.{PartialServerEndpoint, ServerEndpoint} +import sttp.tapir.server.{PartialServerEndpointWithSecurityOutput, ServerEndpoint} import scala.concurrent.Future @@ -20,12 +17,13 @@ trait CardPaymentEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler import app.softnetwork.serialization._ - def payment(payment: Payment): PartialServerEndpoint[ + def payment(payment: Payment): PartialServerEndpointWithSecurityOutput[ (Seq[Option[String]], Option[String], Method, Option[String]), - ((Seq[Option[String]], Option[CookieValueWithMeta]), Session), + Session, (Option[String], Option[String], Option[String], Option[String], Payment), - ApiErrors.ErrorInfo, + Any, (Seq[Option[String]], Option[CookieValueWithMeta]), + Unit, Any, Future ] = @@ -69,9 +67,8 @@ trait CardPaymentEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler ) ) ) - .serverLogic(principal => { case (language, accept, userAgent, ipAddress, payment) => + .serverLogic(session => { case (language, accept, userAgent, ipAddress, payment) => val browserInfo = extractBrowserInfo(language, accept, userAgent, payment) - val session = principal._2 import payment._ run( PreAuthorizeCard( @@ -87,8 +84,8 @@ trait CardPaymentEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler printReceipt ) ).map { - case result: CardPreAuthorized => Right((principal._1._1, principal._1._2, result)) - case result: PaymentRedirection => Right((principal._1._1, principal._1._2, result)) + case result: CardPreAuthorized => Right(result) + case result: PaymentRedirection => Right(result) case other => Left(error(other)) } }) @@ -158,10 +155,9 @@ trait CardPaymentEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler ) ) ) - .serverLogic(principal => { + .serverLogic(session => { case (language, accept, userAgent, ipAddress, payment, creditedAccount) => val browserInfo = extractBrowserInfo(language, accept, userAgent, payment) - val session = principal._2 import payment._ run( PayIn( @@ -180,8 +176,8 @@ trait CardPaymentEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler printReceipt ) ).map { - case result: PaidIn => Right((principal._1._1, principal._1._2, result)) - case result: PaymentRedirection => Right((principal._1._1, principal._1._2, result)) + case result: PaidIn => Right(result) + case result: PaymentRedirection => Right(result) case other => Left(error(other)) } }) @@ -272,10 +268,9 @@ trait CardPaymentEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler ) ) ) - .serverLogic(principal => { + .serverLogic(session => { case (language, accept, userAgent, ipAddress, payment, recurringPaymentRegistrationId) => val browserInfo = extractBrowserInfo(language, accept, userAgent, payment) - val session = principal._2 import payment._ run( PayInFirstRecurring( @@ -286,8 +281,8 @@ trait CardPaymentEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler statementDescriptor ) ).map { - case result: FirstRecurringPaidIn => Right((principal._1._1, principal._1._2, result)) - case result: PaymentRedirection => Right((principal._1._1, principal._1._2, result)) + case result: FirstRecurringPaidIn => Right(result) + case result: PaymentRedirection => Right(result) case other => Left(error(other)) } }) diff --git a/core/src/main/scala/app/softnetwork/payment/service/GenericPaymentEndpoints.scala b/core/src/main/scala/app/softnetwork/payment/service/GenericPaymentEndpoints.scala index cf5d2f6..bb1a357 100644 --- a/core/src/main/scala/app/softnetwork/payment/service/GenericPaymentEndpoints.scala +++ b/core/src/main/scala/app/softnetwork/payment/service/GenericPaymentEndpoints.scala @@ -7,7 +7,6 @@ import app.softnetwork.payment.model._ import sttp.capabilities import sttp.capabilities.akka.AkkaStreams import sttp.model.Part -import sttp.tapir.generic.auto._ import sttp.tapir.json.json4s.jsonBody import sttp.tapir.server.ServerEndpoint import sttp.tapir.server.ServerEndpoint.Full @@ -35,12 +34,11 @@ trait GenericPaymentEndpoints val loadPaymentAccount: ServerEndpoint[Any with AkkaStreams, Future] = secureEndpoint.get .out(jsonBody[PaymentAccountView].description("Authenticated user payment account")) - .serverLogic(principal => + .serverLogic(session => _ => { - run(LoadPaymentAccount(externalUuidWithProfile(principal._2))).map { - case r: PaymentAccountLoaded => - Right((principal._1._1, principal._1._2, r.paymentAccount.view)) - case other => Left(error(other)) + run(LoadPaymentAccount(externalUuidWithProfile(session))).map { + case r: PaymentAccountLoaded => Right(r.paymentAccount.view) + case other => Left(error(other)) } } ) diff --git a/core/src/main/scala/app/softnetwork/payment/service/KycDocumentEndpoints.scala b/core/src/main/scala/app/softnetwork/payment/service/KycDocumentEndpoints.scala index 97ccc24..e55b605 100644 --- a/core/src/main/scala/app/softnetwork/payment/service/KycDocumentEndpoints.scala +++ b/core/src/main/scala/app/softnetwork/payment/service/KycDocumentEndpoints.scala @@ -8,9 +8,7 @@ import app.softnetwork.payment.model.{KycDocument, KycDocumentValidationReport} import sttp.capabilities import sttp.capabilities.akka.AkkaStreams import sttp.model.StatusCode -import sttp.tapir.generic.auto._ import sttp.tapir.json.json4s.jsonBody -import sttp.tapir._ import sttp.tapir.server.ServerEndpoint import scala.concurrent.Future @@ -36,17 +34,16 @@ trait KycDocumentEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler .description("Kyc document validation report") ) ) - .serverLogic(principal => { documentType => + .serverLogic(session => { documentType => val maybeKycDocumentType: Option[KycDocument.KycDocumentType] = KycDocument.KycDocumentType.enumCompanion.fromName(documentType) maybeKycDocumentType match { case None => Future.successful(Left(ApiErrors.BadRequest("wrong kyc document type"))) case Some(kycDocumentType) => - run(LoadKycDocumentStatus(externalUuidWithProfile(principal._2), kycDocumentType)).map { - case r: KycDocumentStatusLoaded => - Right((principal._1._1, principal._1._2, r.report)) - case other => Left(error(other)) + run(LoadKycDocumentStatus(externalUuidWithProfile(session), kycDocumentType)).map { + case r: KycDocumentStatusLoaded => Right(r.report) + case other => Left(error(other)) } } }) @@ -70,18 +67,17 @@ trait KycDocumentEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler ) ) ) - .serverLogic(principal => { case (documentType, pages) => + .serverLogic(session => { case (documentType, pages) => val maybeKycDocumentType: Option[KycDocument.KycDocumentType] = KycDocument.KycDocumentType.enumCompanion.fromName(documentType) maybeKycDocumentType match { case None => Future.successful(Left(ApiErrors.BadRequest("wrong kyc document type"))) case Some(kycDocumentType) => - run(AddKycDocument(externalUuidWithProfile(principal._2), pages.bytes, kycDocumentType)) + run(AddKycDocument(externalUuidWithProfile(session), pages.bytes, kycDocumentType)) .map { - case r: KycDocumentAdded => - Right((principal._1._1, principal._1._2, r)) - case other => Left(error(other)) + case r: KycDocumentAdded => Right(r) + case other => Left(error(other)) } } }) diff --git a/core/src/main/scala/app/softnetwork/payment/service/MandateEndpoints.scala b/core/src/main/scala/app/softnetwork/payment/service/MandateEndpoints.scala index cc37899..665468e 100644 --- a/core/src/main/scala/app/softnetwork/payment/service/MandateEndpoints.scala +++ b/core/src/main/scala/app/softnetwork/payment/service/MandateEndpoints.scala @@ -7,9 +7,7 @@ import app.softnetwork.payment.model.MandateResult import sttp.capabilities import sttp.capabilities.akka.AkkaStreams import sttp.model.StatusCode -import sttp.tapir.generic.auto._ import sttp.tapir.json.json4s.jsonBody -import sttp.tapir._ import sttp.tapir.server.ServerEndpoint import scala.concurrent.Future @@ -34,14 +32,12 @@ trait MandateEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler => ) ) ) - .serverLogic(principal => + .serverLogic(session => _ => - run(CreateMandate(externalUuidWithProfile(principal._2))).map { - case MandateCreated => - Right((principal._1._1, principal._1._2, MandateCreated)) - case r: MandateConfirmationRequired => - Right((principal._1._1, principal._1._2, r)) - case other => Left(error(other)) + run(CreateMandate(externalUuidWithProfile(session))).map { + case MandateCreated => Right(MandateCreated) + case r: MandateConfirmationRequired => Right(r) + case other => Left(error(other)) } ) .description("Create a mandate for the authenticated payment account") @@ -53,12 +49,11 @@ trait MandateEndpoints { _: RootPaymentEndpoints with GenericPaymentHandler => statusCode(StatusCode.Ok) .and(jsonBody[MandateCanceled.type].description("Mandate canceled")) ) - .serverLogic(principal => + .serverLogic(session => _ => - run(CancelMandate(externalUuidWithProfile(principal._2))).map { - case MandateCanceled => - Right((principal._1._1, principal._1._2, MandateCanceled)) - case other => Left(error(other)) + run(CancelMandate(externalUuidWithProfile(session))).map { + case MandateCanceled => Right(MandateCanceled) + case other => Left(error(other)) } ) .description("Create Mandate for the authenticated payment account") diff --git a/core/src/main/scala/app/softnetwork/payment/service/RecurringPaymentEndpoints.scala b/core/src/main/scala/app/softnetwork/payment/service/RecurringPaymentEndpoints.scala index 4823dc3..4a1e53f 100644 --- a/core/src/main/scala/app/softnetwork/payment/service/RecurringPaymentEndpoints.scala +++ b/core/src/main/scala/app/softnetwork/payment/service/RecurringPaymentEndpoints.scala @@ -7,9 +7,7 @@ import app.softnetwork.payment.model.{RecurringPayment, RecurringPaymentView} import sttp.capabilities import sttp.capabilities.akka.AkkaStreams import sttp.model.StatusCode -import sttp.tapir.generic.auto._ import sttp.tapir.json.json4s.jsonBody -import sttp.tapir._ import sttp.tapir.server.ServerEndpoint import scala.concurrent.Future @@ -40,14 +38,12 @@ trait RecurringPaymentEndpoints { _: RootPaymentEndpoints with GenericPaymentHan ) ) ) - .serverLogic(principal => + .serverLogic(session => cmd => - run(cmd.copy(debitedAccount = externalUuidWithProfile(principal._2))).map { - case r: RecurringPaymentRegistered => - Right((principal._1._1, principal._1._2, r)) - case r: MandateConfirmationRequired => - Right((principal._1._1, principal._1._2, r)) - case other => Left(error(other)) + run(cmd.copy(debitedAccount = externalUuidWithProfile(session))).map { + case r: RecurringPaymentRegistered => Right(r) + case r: MandateConfirmationRequired => Right(r) + case other => Left(error(other)) } ) .description("Register a recurring payment for the authenticated payment account") @@ -62,17 +58,16 @@ trait RecurringPaymentEndpoints { _: RootPaymentEndpoints with GenericPaymentHan .description("Recurring payment successfully loaded") ) ) - .serverLogic(principal => + .serverLogic(session => recurringPaymentRegistrationId => run( LoadRecurringPayment( - externalUuidWithProfile(principal._2), + externalUuidWithProfile(session), recurringPaymentRegistrationId ) ).map { - case r: RecurringPaymentLoaded => - Right((principal._1._1, principal._1._2, r.recurringPayment.view)) - case other => Left(error(other)) + case r: RecurringPaymentLoaded => Right(r.recurringPayment.view) + case other => Left(error(other)) } ) .description("Load the recurring payment of the authenticated payment account") @@ -92,12 +87,11 @@ trait RecurringPaymentEndpoints { _: RootPaymentEndpoints with GenericPaymentHan .description("Recurring card payment successfully updated") ) ) - .serverLogic(principal => + .serverLogic(session => cmd => - run(cmd.copy(debitedAccount = externalUuidWithProfile(principal._2))).map { - case r: RecurringCardPaymentRegistrationUpdated => - Right((principal._1._1, principal._1._2, r.result)) - case other => Left(error(other)) + run(cmd.copy(debitedAccount = externalUuidWithProfile(session))).map { + case r: RecurringCardPaymentRegistrationUpdated => Right(r.result) + case other => Left(error(other)) } ) .description( @@ -112,19 +106,18 @@ trait RecurringPaymentEndpoints { _: RootPaymentEndpoints with GenericPaymentHan statusCode(StatusCode.Ok) .and(jsonBody[RecurringPayment.RecurringCardPaymentResult]) ) - .serverLogic(principal => + .serverLogic(session => recurringPaymentRegistrationId => run( UpdateRecurringCardPaymentRegistration( - externalUuidWithProfile(principal._2), + externalUuidWithProfile(session), recurringPaymentRegistrationId, None, Some(RecurringPayment.RecurringCardPaymentStatus.ENDED) ) ).map { - case r: RecurringCardPaymentRegistrationUpdated => - Right((principal._1._1, principal._1._2, r.result)) - case other => Left(error(other)) + case r: RecurringCardPaymentRegistrationUpdated => Right(r.result) + case other => Left(error(other)) } ) diff --git a/core/src/main/scala/app/softnetwork/payment/service/RootPaymentEndpoints.scala b/core/src/main/scala/app/softnetwork/payment/service/RootPaymentEndpoints.scala index 85850c2..f4f6b93 100644 --- a/core/src/main/scala/app/softnetwork/payment/service/RootPaymentEndpoints.scala +++ b/core/src/main/scala/app/softnetwork/payment/service/RootPaymentEndpoints.scala @@ -1,41 +1,29 @@ package app.softnetwork.payment.service -import app.softnetwork.api.server.{ApiEndpoint, ApiErrors} +import app.softnetwork.api.server.ApiErrors import app.softnetwork.payment.config.PaymentSettings import app.softnetwork.payment.handlers.GenericPaymentHandler import app.softnetwork.payment.message.PaymentMessages._ import app.softnetwork.payment.serialization.paymentFormats -import app.softnetwork.session.service.SessionEndpoints -import com.softwaremill.session.{ - GetSessionTransport, - SetSessionTransport, - TapirCsrfCheckMode, - TapirSessionContinuity -} +import app.softnetwork.session.service.ServiceWithSessionEndpoints import org.json4s.Formats import org.softnetwork.session.model.Session import sttp.model.headers.CookieValueWithMeta import sttp.model.Method -import sttp.monad.FutureMonad -import sttp.tapir.server.PartialServerEndpoint -import sttp.tapir.{endpoint, extractFromRequest, Endpoint, EndpointInput} +import sttp.tapir.server.PartialServerEndpointWithSecurityOutput +import sttp.tapir.{Endpoint, EndpointInput} import scala.concurrent.Future +import scala.language.implicitConversions -trait RootPaymentEndpoints extends BasicPaymentService with ApiEndpoint { +trait RootPaymentEndpoints + extends BasicPaymentService + with ServiceWithSessionEndpoints[PaymentCommand, PaymentResult] { _: GenericPaymentHandler => override implicit def formats: Formats = paymentFormats - def sessionEndpoints: SessionEndpoints - - def sc: TapirSessionContinuity[Session] = sessionEndpoints.sc - - def st: SetSessionTransport = sessionEndpoints.st - - def gt: GetSessionTransport = sessionEndpoints.gt - - def checkMode: TapirCsrfCheckMode[Session] = sessionEndpoints.checkMode + override implicit def resultToApiError(result: PaymentResult): ApiErrors.ErrorInfo = error(result) def extractRemoteAddress: EndpointInput.ExtractFromRequest[Option[String]] = extractFromRequest[Option[String]](req => req.connectionInfo.remote.map(_.getHostName)) @@ -44,25 +32,19 @@ trait RootPaymentEndpoints extends BasicPaymentService with ApiEndpoint { endpoint .in(PaymentSettings.PaymentPath) - lazy val secureEndpoint: PartialServerEndpoint[ + lazy val secureEndpoint: PartialServerEndpointWithSecurityOutput[ (Seq[Option[String]], Option[String], Method, Option[String]), - ((Seq[Option[String]], Option[CookieValueWithMeta]), Session), + Session, Unit, - ApiErrors.ErrorInfo, + Any, (Seq[Option[String]], Option[CookieValueWithMeta]), + Unit, Any, Future - ] = { - val partial = sessionEndpoints.antiCsrfWithRequiredSession(sc, st, checkMode) - partial.endpoint + ] = + ApiErrors + .withApiErrorVariants( + antiCsrfWithRequiredSession(sc, gt, checkMode) + ) .in(PaymentSettings.PaymentPath) - .out(partial.securityOutput) - .errorOut(errors) - .serverSecurityLogic { inputs => - partial.securityLogic(new FutureMonad())(inputs).map { - case Left(_) => Left(ApiErrors.Unauthorized("Unauthorized")) - case Right(r) => Right((r._1, r._2)) - } - } - } } diff --git a/core/src/main/scala/app/softnetwork/payment/service/UboDeclarationEndpoints.scala b/core/src/main/scala/app/softnetwork/payment/service/UboDeclarationEndpoints.scala index 88cfa77..6f93906 100644 --- a/core/src/main/scala/app/softnetwork/payment/service/UboDeclarationEndpoints.scala +++ b/core/src/main/scala/app/softnetwork/payment/service/UboDeclarationEndpoints.scala @@ -7,9 +7,7 @@ import app.softnetwork.payment.model.{UboDeclaration, UboDeclarationView} import sttp.capabilities import sttp.capabilities.akka.AkkaStreams import sttp.model.StatusCode -import sttp.tapir.generic.auto._ import sttp.tapir.json.json4s.jsonBody -import sttp.tapir._ import sttp.tapir.server.ServerEndpoint import scala.concurrent.Future @@ -32,9 +30,9 @@ trait UboDeclarationEndpoints { _: RootPaymentEndpoints with GenericPaymentHandl .description("The UBO successfully recorded") ) ) - .serverLogic(principal => { ubo => - run(CreateOrUpdateUbo(externalUuidWithProfile(principal._2), ubo)).map { - case r: UboCreatedOrUpdated => Right((principal._1._1, principal._1._2, r.ubo)) + .serverLogic(session => { ubo => + run(CreateOrUpdateUbo(externalUuidWithProfile(session), ubo)).map { + case r: UboCreatedOrUpdated => Right(r.ubo) case other => Left(error(other)) } }) @@ -50,12 +48,11 @@ trait UboDeclarationEndpoints { _: RootPaymentEndpoints with GenericPaymentHandl .description("Ubo declaration of the authenticated legal payment account") ) ) - .serverLogic(principal => + .serverLogic(session => _ => - run(GetUboDeclaration(externalUuidWithProfile(principal._2))).map { - case r: UboDeclarationLoaded => - Right((principal._1._1, principal._1._2, r.declaration.view)) - case other => Left(error(other)) + run(GetUboDeclaration(externalUuidWithProfile(session))).map { + case r: UboDeclarationLoaded => Right(r.declaration.view) + case other => Left(error(other)) } ) .description("Load the Ubo declaration of the authenticated legal payment account") @@ -70,12 +67,11 @@ trait UboDeclarationEndpoints { _: RootPaymentEndpoints with GenericPaymentHandl ) ) ) - .serverLogic(principal => + .serverLogic(session => _ => - run(ValidateUboDeclaration(externalUuidWithProfile(principal._2))).map { - case UboDeclarationAskedForValidation => - Right((principal._1._1, principal._1._2, UboDeclarationAskedForValidation)) - case other => Left(error(other)) + run(ValidateUboDeclaration(externalUuidWithProfile(session))).map { + case UboDeclarationAskedForValidation => Right(UboDeclarationAskedForValidation) + case other => Left(error(other)) } ) .description("Validate the Ubo declaration of the authenticated legal payment account") diff --git a/mangopay/api/src/main/resources/reference.conf b/mangopay/api/src/main/resources/reference.conf index 80335e6..c4a4779 100644 --- a/mangopay/api/src/main/resources/reference.conf +++ b/mangopay/api/src/main/resources/reference.conf @@ -2,7 +2,7 @@ include "softnetwork-jdbc-persistence.conf" include "softnetwork-scheduler.conf" softnetwork.api.name = "softnetwork-mangopay" -softnetwork.api.version = "0.3.2.4" +softnetwork.api.version = "0.4.1" softnetwork.api.server.port = 9000 softnetwork.api.server.request-timeout = 120 s softnetwork.api.server.swagger-path-prefix = ["swagger", "payment"] diff --git a/project/src/main/scala/app/softnetwork/sbt/build/Versions.scala b/project/src/main/scala/app/softnetwork/sbt/build/Versions.scala index 0c058bf..c438c08 100644 --- a/project/src/main/scala/app/softnetwork/sbt/build/Versions.scala +++ b/project/src/main/scala/app/softnetwork/sbt/build/Versions.scala @@ -2,9 +2,9 @@ package app.softnetwork.sbt.build object Versions { - val genericPersistence = "0.3.2.4" + val genericPersistence = "0.3.4" - val scheduler = "0.3.2.2" + val scheduler = "0.3.4" val scalatest = "3.1.1" } diff --git a/testkit/src/main/scala/app/softnetwork/payment/scalatest/PaymentEndpointsTestKit.scala b/testkit/src/main/scala/app/softnetwork/payment/scalatest/PaymentEndpointsTestKit.scala index ab398c9..d9b9f89 100644 --- a/testkit/src/main/scala/app/softnetwork/payment/scalatest/PaymentEndpointsTestKit.scala +++ b/testkit/src/main/scala/app/softnetwork/payment/scalatest/PaymentEndpointsTestKit.scala @@ -1,32 +1,22 @@ package app.softnetwork.payment.scalatest import akka.actor.typed.ActorSystem -import akka.http.scaladsl.server.Route +import app.softnetwork.api.server.ApiEndpoint import app.softnetwork.payment.launch.PaymentEndpoints import app.softnetwork.payment.service.{GenericPaymentEndpoints, MockPaymentEndpoints} import app.softnetwork.persistence.schema.SchemaProvider -import app.softnetwork.session.scalatest.{ - SessionEndpointsRoute, - SessionEndpointsRoutes, - SessionServiceRoute -} -import com.softwaremill.session.CsrfCheck -import sttp.capabilities -import sttp.capabilities.akka.AkkaStreams -import sttp.tapir.server.ServerEndpoint - -import scala.concurrent.Future +import app.softnetwork.session.scalatest.SessionEndpointsRoute trait PaymentEndpointsTestKit extends PaymentEndpoints { _: SchemaProvider => override def paymentEndpoints: ActorSystem[_] => GenericPaymentEndpoints = system => MockPaymentEndpoints(system, sessionEndpoints(system)) - override def endpoints - : ActorSystem[_] => List[ServerEndpoint[AkkaStreams with capabilities.WebSockets, Future]] = + override def endpoints: ActorSystem[_] => List[ApiEndpoint] = system => - SessionEndpointsRoute(system, sessionEndpoints(system)).endpoints ++ paymentEndpoints( - system - ).endpoints + List( + SessionEndpointsRoute(system, sessionEndpoints(system)), + paymentEndpoints(system) + ) }