Skip to content

Commit

Permalink
refactoring after upgrading endpoints api dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
fupelaqu committed Jul 21, 2023
1 parent 02790da commit ed034f1
Show file tree
Hide file tree
Showing 14 changed files with 118 additions and 186 deletions.
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
Original file line number Diff line number Diff line change
@@ -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 =>

Expand All @@ -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)
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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] = {
Expand Down Expand Up @@ -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))
}
})
Expand All @@ -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))
}
}
)
Expand All @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}
)
Expand All @@ -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)
Expand All @@ -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))
}
}
)
Expand All @@ -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))
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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._
Expand All @@ -9,23 +8,22 @@ 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

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
] =
Expand Down Expand Up @@ -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(
Expand All @@ -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))
}
})
Expand Down Expand Up @@ -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(
Expand All @@ -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))
}
})
Expand Down Expand Up @@ -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(
Expand All @@ -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))
}
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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))
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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))
}
}
})
Expand All @@ -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))
}
}
})
Expand Down
Loading

0 comments on commit ed034f1

Please sign in to comment.