getATMs written as New Style Endpoint

This commit is contained in:
constantine2nd 2017-12-22 07:28:57 +01:00
parent eb41c8f293
commit 1a87ca50c4
4 changed files with 46 additions and 24 deletions

View File

@ -272,6 +272,10 @@ val dateformat = new java.text.SimpleDateFormat("yyyy-MM-dd")
val BranchesNotFoundLicense = "OBP-32001: No branches available. License may not be set."
val BranchesNotFound = "OBP-32002: No branches available."
// ATM related messages
val atmsNotFoundLicense = "OBP-33001: No ATMs available. License may not be set."
val atmsNotFound = "OBP-33002: No ATMs available."
// General Resource related messages above here

View File

@ -1024,34 +1024,44 @@ trait APIMethods300 {
Catalogs(Core, notPSD2, OBWG),
List(apiTagATM)
)
// TODO Rewrite as New Style Endpoint
lazy val getAtms : PartialFunction[Req, Box[User] => Box[JsonResponse]] = {
case "banks" :: BankId(bankId) :: "atms" :: Nil JsonGet json => {
user => {
_ => {
val limit = S.param("limit")
val offset = S.param("offset")
for {
// Get atms from the active provider
u <- if(getAtmsIsPublic)
Box(Some(1))
else
user ?~! UserNotLoggedIn
_ <- Bank(bankId) ?~! {ErrorMessages.BankNotFound}
limit <- tryo(
S.param("limit") match {
case Full(l) if (l.toInt > 1000) => 1000
case Full(l) => l.toInt
case _ => 50
(user, sessioContext) <- extractCallContext()
_ <- Helper.booleanToFuture(failMsg = UserNotLoggedIn) {
canGetBranch(getBranchesIsPublic, user)
}
_ <- Helper.booleanToFuture(failMsg = s"${InvalidNumber } limit:${limit.getOrElse("")}") {
limit match {
case Full(i) => i.toList.forall(c => Character.isDigit(c) == true)
case _ => true
}
) ?~! s"${InvalidNumber } limit:${S.param("limit").get }"
// default0, start from page 0
offset <- tryo(S.param("offset").getOrElse("0").toInt) ?~!
s"${InvalidNumber } offset:${S.param("offset").get }"
atms <- Box(Atms.atmsProvider.vend.getAtms(bankId, OBPLimit(limit), OBPOffset(offset))) ~> APIFailure("No ATMs available. License may not be set.", 204)
}
_ <- Helper.booleanToFuture(failMsg = s"${InvalidNumber } offset:${offset.getOrElse("")}") {
offset match {
case Full(i) => i.toList.forall(c => Character.isDigit(c) == true)
case _ => true
}
}
_ <- Future { Bank(bankId) } map { x => fullBoxOrException(x ?~! BankNotFound) }
atms <- Connector.connector.vend.getAtmsFuture(bankId) map {
case Full(List()) | Empty =>
fullBoxOrException(Empty ?~! atmsNotFound)
case Full(list) =>
val branchesWithLicense = for { branch <- list if branch.meta.license.name.size > 3 } yield branch
if (branchesWithLicense.size == 0) fullBoxOrException(Empty ?~! atmsNotFoundLicense)
else Full(branchesWithLicense)
} map { unboxFull(_) } map {
// Before we slice we need to sort in order to keep consistent results
_.sortWith(_.atmId.value < _.atmId.value)
// Slice the result in next way: from=offset and until=offset + limit
.slice(offset.getOrElse("0").toInt, offset.getOrElse("0").toInt + limit.getOrElse("100").toInt)
}
} yield {
// Format the data as json
val json = JSONFactory300.createAtmsJsonV300(atms)
// Return
successJsonResponse(Extraction.decompose(json))
(JSONFactory300.createAtmsJsonV300(atms), getGatewayLoginHeader(sessioContext))
}
}
}

View File

@ -1121,6 +1121,10 @@ trait Connector extends MdcLoggable{
Failure(NotImplemented + currentMethodName)
}
def getAtmsFuture(bankId: BankId, queryParams: OBPQueryParam*): Future[Box[List[AtmT]]] = Future {
Failure(NotImplemented + currentMethodName)
}
//This method is only existing in mapper
def accountOwnerExists(user: ResourceUser, bankId: BankId, accountId: AccountId): Box[Boolean]= {
val res =

View File

@ -1447,7 +1447,11 @@ object LocalMappedConnector extends Connector with MdcLoggable {
}
}
override def getAtmsFuture(bankId: BankId, queryParams: OBPQueryParam*): Future[Box[List[MappedAtm]]] = {
Future {
Full(MappedAtm.findAll(By(MappedAtm.mBankId, bankId.value)))
}
}
/**