libeufin

Integration and sandbox testing for FinTech APIs and data formats
Log | Files | Refs | Submodules | README | LICENSE

commit 34855306d980eb6fede98328d5604dffeb3d565e
parent a5ad795d34402fe8e1f69c4c2edcaab670519853
Author: MS <ms@taler.net>
Date:   Mon, 23 Aug 2021 12:04:12 -1100

Calculate balance per bank account entity, and not per IBAN.

Diffstat:
Msandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt | 2+-
Msandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt | 3++-
Msandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt | 29+++++++++++++----------------
Msandbox/src/test/kotlin/BalanceTest.kt | 47++++++++++++++++-------------------------------
4 files changed, 32 insertions(+), 49 deletions(-)

diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt @@ -446,7 +446,7 @@ private fun constructCamtResponse(type: Int, subscriber: EbicsSubscriberEntity): */ val bankAccount = getBankAccountFromSubscriber(subscriber) logger.info("getting history for account with iban ${bankAccount.iban}") - val history = historyForAccount(bankAccount.iban) + val history = historyForAccount(bankAccount) return buildCamtString(type, bankAccount.iban, history) } diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt @@ -519,7 +519,8 @@ fun serverMain(dbName: String, port: Int) { // only reason for a post is to hide the iban (to some degree.) post("/admin/payments/camt") { val body = call.receiveJson<CamtParams>() - val history = historyForAccount(body.iban) + val bankAccount = getBankAccountFromIban(body.iban) + val history = historyForAccount(bankAccount) SandboxAssert(body.type == 53, "Only Camt.053 is implemented") val camt53 = buildCamtString(body.type, body.iban, history) call.respondText(camt53, ContentType.Text.Xml, HttpStatusCode.OK) diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt @@ -55,22 +55,19 @@ fun balanceForAccount(bankAccount: BankAccountEntity): BigDecimal { return balance } -fun historyForAccount(iban: String): List<RawPayment> { +fun historyForAccount(bankAccount: BankAccountEntity): List<RawPayment> { val history = mutableListOf<RawPayment>() - logger.debug("Querying transactions involving: ${iban}") transaction { - BankAccountTransactionsTable.select { - BankAccountTransactionsTable.creditorIban eq iban or - (BankAccountTransactionsTable.debtorIban eq iban) - /** - FIXME: add the following condition too: - and (BankAccountTransactionsTable.date.between(start.millis, end.millis)) - */ - /** - FIXME: add the following condition too: - and (BankAccountTransactionsTable.date.between(start.millis, end.millis)) - */ - }.forEach { + /** + FIXME: add the following condition too: + and (BankAccountTransactionsTable.date.between(start.millis, end.millis)) + */ + /** + FIXME: add the following condition too: + and (BankAccountTransactionsTable.date.between(start.millis, end.millis)) + */ + BankAccountTransactionsTable.select { BankAccountTransactionsTable.account eq bankAccount.id } + }.forEach { history.add( RawPayment( subject = it[BankAccountTransactionsTable.subject], @@ -92,6 +89,5 @@ fun historyForAccount(iban: String): List<RawPayment> { ) ) } - } return history -} +} +\ No newline at end of file diff --git a/sandbox/src/test/kotlin/BalanceTest.kt b/sandbox/src/test/kotlin/BalanceTest.kt @@ -3,6 +3,7 @@ import org.jetbrains.exposed.sql.SchemaUtils import org.jetbrains.exposed.sql.insert import org.jetbrains.exposed.sql.transactions.transaction import org.junit.Test +import tech.libeufin.sandbox.BankAccountEntity import tech.libeufin.sandbox.BankAccountTransactionsTable import tech.libeufin.sandbox.BankAccountsTable import tech.libeufin.sandbox.balanceForAccount @@ -17,8 +18,15 @@ class BalanceTest { withTestDatabase { transaction { SchemaUtils.create(BankAccountTransactionsTable) + val one = BankAccountEntity.new { + name = "Person 1" + iban = "IBAN 1" + bic = "BIC" + label = "label 1" + currency = "EUR" + } BankAccountTransactionsTable.insert { - it[account] = EntityID(0, BankAccountsTable) + it[account] = one.id it[creditorIban] = "earns" it[creditorBic] = "BIC" it[creditorName] = "Creditor Name" @@ -30,11 +38,11 @@ class BalanceTest { it[date] = LocalDateTime.now().millis() it[currency] = "EUR" it[pmtInfId] = "0" - it[direction] = "DBIT" + it[direction] = "CRDT" it[accountServicerReference] = "test-account-servicer-reference" } BankAccountTransactionsTable.insert { - it[account] = EntityID(0, BankAccountsTable) + it[account] = one.id it[creditorIban] = "earns" it[creditorBic] = "BIC" it[creditorName] = "Creditor Name" @@ -46,15 +54,15 @@ class BalanceTest { it[date] = LocalDateTime.now().millis() it[currency] = "EUR" it[pmtInfId] = "0" - it[direction] = "DBIT" + it[direction] = "CRDT" it[accountServicerReference] = "test-account-servicer-reference" } BankAccountTransactionsTable.insert { - it[account] = EntityID(0, BankAccountsTable) - it[creditorIban] = "other" + it[account] = one.id + it[creditorIban] = "earns" it[creditorBic] = "BIC" it[creditorName] = "Creditor Name" - it[debtorIban] = "earns" + it[debtorIban] = "spends" it[debtorBic] = "BIC" it[debtorName] = "Debitor Name" it[subject] = "deal" @@ -65,30 +73,7 @@ class BalanceTest { it[direction] = "DBIT" it[accountServicerReference] = "test-account-servicer-reference" } - BankAccountTransactionsTable.insert { - it[account] = EntityID(0, BankAccountsTable) - it[creditorIban] = "other" - it[creditorBic] = "BIC" - it[creditorName] = "Creditor Name" - it[debtorIban] = "earns-bad-amount" - it[debtorBic] = "BIC" - it[debtorName] = "Debitor Name" - it[subject] = "deal" - it[amount] = "not a number" - it[date] = LocalDateTime.now().millis() - it[currency] = "EUR" - it[pmtInfId] = "0" - it[direction] = "DBIT" - it[accountServicerReference] = "test-account-servicer-reference" - } - assert(java.math.BigDecimal.ONE == balanceForAccount("earns")) - try { - balanceForAccount("earns-bad-amount") - } catch (e: UtilError) { - return@transaction - } - // here the expected exception wasn't thrown. - assert(false) + assert(java.math.BigDecimal.ONE == balanceForAccount(one)) } } }