diff options
author | ms <ms@taler.net> | 2023-03-22 14:27:06 +0100 |
---|---|---|
committer | ms <ms@taler.net> | 2023-03-22 14:27:06 +0100 |
commit | 10bc544c731291090683bc67e421c1740f6dc269 (patch) | |
tree | 8dd6f9048fe5450ef9f138cc17d4a04794b96297 /sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt | |
parent | b92a628982bde6582269734713183f3760e1f5e6 (diff) | |
download | libeufin-10bc544c731291090683bc67e421c1740f6dc269.tar.gz libeufin-10bc544c731291090683bc67e421c1740f6dc269.tar.bz2 libeufin-10bc544c731291090683bc67e421c1740f6dc269.zip |
pointing demobanks
Avoid relying on the default demobank along the
HTTP handlers, to honor the demobanks multitenancy.
This change aims to also make the code compatible
with DD38. Polishing (Access API) access control
to bank accounts too.
Diffstat (limited to 'sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt')
-rw-r--r-- | sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt | 35 |
1 files changed, 27 insertions, 8 deletions
diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt index d62f0b0f..2361b876 100644 --- a/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt +++ b/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt @@ -19,7 +19,7 @@ fun maybeDebit( "Demobank '${demobankName}' not found when trying to check the debit threshold" + " for user $accountLabel" ) - val balance = getBalance(accountLabel, withPending = true) + val balance = getBalance(accountLabel, demobankName, withPending = true) val maxDebt = if (accountLabel == "admin") { demobank.config.bankDebtLimit } else demobank.config.usersDebtLimit @@ -32,8 +32,13 @@ fun maybeDebit( return false } -fun getMaxDebitForUser(username: String): Int { - val bank = getDefaultDemobank() +fun getMaxDebitForUser( + username: String, + demobankName: String = "default" +): Int { + val bank = getDemobank(demobankName) ?: throw internalServerError( + "demobank $demobankName not found" + ) if (username == "admin") return bank.config.bankDebtLimit return bank.config.usersDebtLimit } @@ -50,6 +55,9 @@ fun getBalanceForJson(value: BigDecimal, currency: String): BalanceJson { * last statement. If the bank account does not have any statement * yet, then zero is returned. When 'withPending' is true, it adds * the pending transactions to it. + * + * Note: because transactions are searched after the bank accounts + * (numeric) id, the research in the database is not ambiguous. */ fun getBalance( bankAccount: BankAccountEntity, @@ -92,10 +100,16 @@ fun getBalance( return lastBalance } -// Wrapper offering to get bank accounts from a string. -fun getBalance(accountLabel: String, withPending: Boolean = true): BigDecimal { - val defaultDemobank = getDefaultDemobank() - val account = getBankAccountFromLabel(accountLabel, defaultDemobank) +// Gets the balance of 'accountLabel', which is hosted at 'demobankName'. +fun getBalance(accountLabel: String, + demobankName: String = "default", + withPending: Boolean = true +): BigDecimal { + val demobank = getDemobank(demobankName) ?: throw SandboxError( + HttpStatusCode.InternalServerError, + "Demobank '$demobankName' not found" + ) + val account = getBankAccountFromLabel(accountLabel, demobank) return getBalance(account, withPending) } @@ -150,7 +164,12 @@ fun wireTransfer( " Only ${demobank.config.currency} allowed." ) // Check funds are sufficient. - if (maybeDebit(debitAccount.label, amountAsNumber)) { + if ( + maybeDebit( + debitAccount.label, + amountAsNumber, + demobank.name + )) { logger.error("Account ${debitAccount.label} would surpass debit threshold. Rollback wire transfer") throw SandboxError(HttpStatusCode.PreconditionFailed, "Insufficient funds") } |