From 5a02612a35b25b79f06293b11e7d558314f4d37d Mon Sep 17 00:00:00 2001 From: Everett Sochowski Date: Fri, 20 Feb 2015 15:18:36 +0100 Subject: [PATCH] Refactor cash api to bring implementation specific work into the connector --- .../scala/code/bankconnectors/Connector.scala | 8 +++ .../code/bankconnectors/LocalConnector.scala | 68 +++++++++++++++++++ src/main/scala/code/tesobe/CashAPI.scala | 68 ++----------------- .../code/api/v1_3_0/PhysicalCardsTest.scala | 7 ++ 4 files changed, 87 insertions(+), 64 deletions(-) diff --git a/src/main/scala/code/bankconnectors/Connector.scala b/src/main/scala/code/bankconnectors/Connector.scala index 883d15553..d2284ad94 100644 --- a/src/main/scala/code/bankconnectors/Connector.scala +++ b/src/main/scala/code/bankconnectors/Connector.scala @@ -1,5 +1,6 @@ package code.bankconnectors +import code.tesobe.CashTransaction import code.util.Helper._ import com.tesobe.model.CreateBankAccount import net.liftweb.common.Box @@ -130,4 +131,11 @@ trait Connector { //for sandbox use -> allows us to check if we can generate a new test account with the given number def accountExists(bankId : BankId, accountNumber : String) : Boolean + + + //cash api requires getting an account via a uuid: for legacy reasons it does not use bankId + accountId + def getAccountByUUID(uuid : String) : Box[AccountType] + + //cash api requires a call to add a new transaction and update the account balance + def addCashTransactionAndUpdateBalance(account : AccountType, cashTransaction : CashTransaction) } \ No newline at end of file diff --git a/src/main/scala/code/bankconnectors/LocalConnector.scala b/src/main/scala/code/bankconnectors/LocalConnector.scala index 6182a511a..151e94860 100644 --- a/src/main/scala/code/bankconnectors/LocalConnector.scala +++ b/src/main/scala/code/bankconnectors/LocalConnector.scala @@ -2,6 +2,7 @@ package code.bankconnectors import java.text.SimpleDateFormat import java.util.{UUID, TimeZone} +import code.tesobe.CashTransaction import code.util.Helper import net.liftweb.common.{Failure, Box, Loggable, Full} import net.liftweb.json.JsonAST.JValue @@ -17,6 +18,8 @@ import com.mongodb.QueryBuilder import code.metadata.counterparties.{Counterparties, MongoCounterparties, Metadata} import com.tesobe.model.{CreateBankAccount, UpdateBankAccount} +import scala.math.BigDecimal.RoundingMode + private object LocalConnector extends Connector with Loggable { type AccountType = Account @@ -417,4 +420,69 @@ private object LocalConnector extends Connector with Loggable { } } + + //cash api requires getting an account via a uuid: for legacy reasons it does not use bankId + accountId + override def getAccountByUUID(uuid: String): Box[AccountType] = { + Account.find(uuid) + } + + //cash api requires a call to add a new transaction and update the account balance + override def addCashTransactionAndUpdateBalance(account: AccountType, cashTransaction: CashTransaction): Unit = { + val thisAccountBank = OBPBank.createRecord. + IBAN(account.iban.getOrElse("")). + national_identifier(account.nationalIdentifier). + name(account.bankName) + + val thisAccount = OBPAccount.createRecord. + holder(account.holder.get). + number(account.number). + kind(account.kind.get). + bank(thisAccountBank) + + val otherAccountBank = OBPBank.createRecord. + IBAN(""). + national_identifier(""). + name("") + + val otherAccount = OBPAccount.createRecord. + holder(cashTransaction.otherParty). + number(""). + kind(""). + bank(otherAccountBank) + + val amount : BigDecimal = { + if(cashTransaction.kind == "in") + BigDecimal(cashTransaction.amount).setScale(2,RoundingMode.HALF_UP).abs + else + BigDecimal((cashTransaction.amount * (-1) )).setScale(2,RoundingMode.HALF_UP) + } + + val newBalance : OBPBalance = OBPBalance.createRecord. + currency(account.currency). + amount(account.balance + amount) + + val newValue : OBPValue = OBPValue.createRecord. + currency(account.currency). + amount(amount) + + val details = OBPDetails.createRecord. + kind("cash"). + posted(cashTransaction.date). + other_data(cashTransaction.otherInformation). + new_balance(newBalance). + value(newValue). + completed(cashTransaction.date). + label(cashTransaction.label) + + val transaction = OBPTransaction.createRecord. + this_account(thisAccount). + other_account(otherAccount). + details(details) + + val env = OBPEnvelope.createRecord. + obp_transaction(transaction) + account.accountBalance(account.balance + amount).lastUpdate(now) + account.save + env.save + } } \ No newline at end of file diff --git a/src/main/scala/code/tesobe/CashAPI.scala b/src/main/scala/code/tesobe/CashAPI.scala index ee5b65ab1..1fd178939 100644 --- a/src/main/scala/code/tesobe/CashAPI.scala +++ b/src/main/scala/code/tesobe/CashAPI.scala @@ -1,6 +1,7 @@ package code.tesobe import java.util.Date +import code.bankconnectors.Connector import code.model.dataAccess._ import net.liftweb.common.{Full, Box, Loggable} import net.liftweb.http.{JsonResponse, S} @@ -62,75 +63,14 @@ object CashAccountAPI extends RestHelper with Loggable { case "cash-accounts" :: uuid :: "transactions" :: Nil JsonPost json -> _ => { - def getAccountByUUID(uuid : String) : Box[Account] = { - Account.find(uuid) - } - - def addCashTransaction(account : Account, cashTransaction : CashTransaction) = { - val thisAccountBank = OBPBank.createRecord. - IBAN(account.iban.getOrElse("")). - national_identifier(account.nationalIdentifier). - name(account.bankName) - - val thisAccount = OBPAccount.createRecord. - holder(account.holder.get). - number(account.number). - kind(account.kind.get). - bank(thisAccountBank) - - val otherAccountBank = OBPBank.createRecord. - IBAN(""). - national_identifier(""). - name("") - - val otherAccount = OBPAccount.createRecord. - holder(cashTransaction.otherParty). - number(""). - kind(""). - bank(otherAccountBank) - - val amount : BigDecimal = { - if(cashTransaction.kind == "in") - BigDecimal(cashTransaction.amount).setScale(2,RoundingMode.HALF_UP).abs - else - BigDecimal((cashTransaction.amount * (-1) )).setScale(2,RoundingMode.HALF_UP) - } - - val newBalance : OBPBalance = OBPBalance.createRecord. - currency(account.currency). - amount(account.balance + amount) - - val newValue : OBPValue = OBPValue.createRecord. - currency(account.currency). - amount(amount) - - val details = OBPDetails.createRecord. - kind("cash"). - posted(cashTransaction.date). - other_data(cashTransaction.otherInformation). - new_balance(newBalance). - value(newValue). - completed(cashTransaction.date). - label(cashTransaction.label) - - val transaction = OBPTransaction.createRecord. - this_account(thisAccount). - other_account(otherAccount). - details(details) - - val env = OBPEnvelope.createRecord. - obp_transaction(transaction) - account.accountBalance(account.balance + amount).lastUpdate(now) - account.save - env.save - } + val connector = Connector.connector.vend if(isValidKey) { - getAccountByUUID(uuid) match { + connector.getAccountByUUID(uuid) match { case Full(account) => tryo { json.extract[CashTransaction] } match { case Full(cashTransaction) => - addCashTransaction(account, cashTransaction) + connector.addCashTransactionAndUpdateBalance(account, cashTransaction) JsonResponse(SuccessMessage("transaction successfully added"), Nil, Nil, 200) case _ => invalidCashTransactionJsonFormatError diff --git a/src/test/scala/code/api/v1_3_0/PhysicalCardsTest.scala b/src/test/scala/code/api/v1_3_0/PhysicalCardsTest.scala index 8c966cf3e..f9d37a296 100644 --- a/src/test/scala/code/api/v1_3_0/PhysicalCardsTest.scala +++ b/src/test/scala/code/api/v1_3_0/PhysicalCardsTest.scala @@ -4,6 +4,7 @@ import code.api.DefaultUsers import code.api.test.ServerSetup import code.api.util.APIUtil import code.bankconnectors.{OBPQueryParam, Connector} +import code.tesobe.CashTransaction import com.tesobe.model.CreateBankAccount import net.liftweb.common.{Failure, Loggable, Empty, Box} import code.model._ @@ -107,6 +108,12 @@ class PhysicalCardsTest extends ServerSetup with DefaultUsers { override def createSandboxBankAccount(bankId: BankId, accountId: AccountId, accountNumber: String, currency: String, initialBalance: BigDecimal, accountHolderName: String): Box[AccountType] = ??? + + //cash api requires getting an account via a uuid: for legacy reasons it does not use bankId + accountId + override def getAccountByUUID(uuid: String): Box[PhysicalCardsTest.this.MockedCardConnector.AccountType] = ??? + + //cash api requires a call to add a new transaction and update the account balance + override def addCashTransactionAndUpdateBalance(account: PhysicalCardsTest.this.MockedCardConnector.AccountType, cashTransaction: CashTransaction): Unit = ??? } override def beforeAll() {