summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMS <ms@taler.net>2021-05-27 15:34:20 +0200
committerMS <ms@taler.net>2021-05-27 15:34:20 +0200
commitd527df40d0553bbe6c20bfac0754e8a0253d02d4 (patch)
tree2eedfae56786f50058cfc5335edb37179b434ae9
parent4c44480327c5ffc7cb925f2264dd401769d48ddb (diff)
downloadlibeufin-d527df40d0553bbe6c20bfac0754e8a0253d02d4.tar.gz
libeufin-d527df40d0553bbe6c20bfac0754e8a0253d02d4.tar.bz2
libeufin-d527df40d0553bbe6c20bfac0754e8a0253d02d4.zip
Calculating balances in the Sandbox.
At this point, the Sandbox goes through the whole history of one account (= one IBAN) and calculates the balance for it. Later, a mechanism to calculate the balance only based on a subset of such transactions should be provided.
-rw-r--r--sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt2
-rw-r--r--sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt24
-rw-r--r--sandbox/src/test/kotlin/BalanceTest.kt72
3 files changed, 96 insertions, 2 deletions
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt
index 4df1853f..0cc34380 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/EbicsProtocolBackend.kt
@@ -307,7 +307,7 @@ fun buildCamtString(type: Int, subscriberIban: String, history: List<RawPayment>
}
element("Amt") {
attribute("Ccy", "EUR")
- text(Amount(0).toPlainString())
+ text(balanceForAccount(subscriberIban).toString())
}
element("CdtDbtInd") {
// a temporary value to get the camt to validate.
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt
index a0c0d16b..d55f0912 100644
--- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt
+++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt
@@ -5,16 +5,38 @@ import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.transactions.transaction
import org.slf4j.Logger
import org.slf4j.LoggerFactory
+import tech.libeufin.sandbox.BankAccountTransactionsTable.amount
import tech.libeufin.util.RawPayment
import tech.libeufin.util.importDateFromMillis
import tech.libeufin.util.toDashedDate
+import java.math.BigInteger
private val logger: Logger = LoggerFactory.getLogger("tech.libeufin.sandbox")
+fun balanceForAccount(iban: String): BigInteger {
+ logger.debug("Calculating balance for account: ${iban}")
+ var balance = BigInteger.ZERO
+ transaction {
+ BankAccountTransactionsTable.select {
+ BankAccountTransactionsTable.creditorIban eq iban
+ }.forEach {
+ val amount = BigInteger(it[amount])
+ balance += amount
+ }
+ BankAccountTransactionsTable.select {
+ BankAccountTransactionsTable.debtorIban eq iban
+ }.forEach {
+ val amount = BigInteger(it[amount])
+ balance -= amount
+ }
+ }
+ return balance
+}
+
fun historyForAccount(iban: String): List<RawPayment> {
val history = mutableListOf<RawPayment>()
+ logger.debug("Querying transactions involving: ${iban}")
transaction {
- logger.debug("Querying transactions involving: ${iban}")
BankAccountTransactionsTable.select {
BankAccountTransactionsTable.creditorIban eq iban or
(BankAccountTransactionsTable.debtorIban eq iban)
diff --git a/sandbox/src/test/kotlin/BalanceTest.kt b/sandbox/src/test/kotlin/BalanceTest.kt
new file mode 100644
index 00000000..a8c4d9e9
--- /dev/null
+++ b/sandbox/src/test/kotlin/BalanceTest.kt
@@ -0,0 +1,72 @@
+import org.jetbrains.exposed.dao.id.EntityID
+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.BankAccountTransactionsTable
+import tech.libeufin.sandbox.BankAccountsTable
+import tech.libeufin.sandbox.balanceForAccount
+import tech.libeufin.util.millis
+import java.math.BigInteger
+import java.time.LocalDateTime
+
+class BalanceTest {
+
+ @Test
+ fun balanceTest() {
+ withTestDatabase {
+ transaction {
+ SchemaUtils.create(BankAccountTransactionsTable)
+ BankAccountTransactionsTable.insert {
+ it[account] = EntityID(0, BankAccountsTable)
+ it[creditorIban] = "earns"
+ it[creditorBic] = "BIC"
+ it[creditorName] = "Creditor Name"
+ it[debtorIban] = "spends"
+ it[debtorBic] = "BIC"
+ it[debtorName] = "Debitor Name"
+ it[subject] = "deal"
+ it[amount] = "1"
+ it[date] = LocalDateTime.now().millis()
+ it[currency] = "EUR"
+ it[pmtInfId] = "0"
+ it[direction] = "DBIT"
+ it[accountServicerReference] = "test-account-servicer-reference"
+ }
+ BankAccountTransactionsTable.insert {
+ it[account] = EntityID(0, BankAccountsTable)
+ it[creditorIban] = "earns"
+ it[creditorBic] = "BIC"
+ it[creditorName] = "Creditor Name"
+ it[debtorIban] = "spends"
+ it[debtorBic] = "BIC"
+ it[debtorName] = "Debitor Name"
+ it[subject] = "deal"
+ it[amount] = "1"
+ it[date] = LocalDateTime.now().millis()
+ it[currency] = "EUR"
+ it[pmtInfId] = "0"
+ 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"
+ it[debtorBic] = "BIC"
+ it[debtorName] = "Debitor Name"
+ it[subject] = "deal"
+ it[amount] = "1"
+ it[date] = LocalDateTime.now().millis()
+ it[currency] = "EUR"
+ it[pmtInfId] = "0"
+ it[direction] = "DBIT"
+ it[accountServicerReference] = "test-account-servicer-reference"
+ }
+ assert(BigInteger.ONE == balanceForAccount("earns"))
+ }
+ }
+ }
+} \ No newline at end of file