mirror of
https://github.com/OpenBankProject/OBP-API.git
synced 2026-02-06 15:56:57 +00:00
Refactor cash api to bring implementation specific work into the connector
This commit is contained in:
parent
2cd755f1e5
commit
5a02612a35
@ -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)
|
||||
}
|
||||
@ -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
|
||||
}
|
||||
}
|
||||
@ -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
|
||||
|
||||
@ -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() {
|
||||
|
||||
Loading…
Reference in New Issue
Block a user