libeufin

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

commit 090bc0e537d120e0996aeefc9fa82c1929bd676c
parent 1b9fbb02ac2d2eb402c18877079d1bdbae03a620
Author: MS <ms@taler.net>
Date:   Tue, 11 Apr 2023 13:16:43 +0200

Helpers.

Adding helper to extract the transactions history in Sandbox'
Access API.

Diffstat:
Msandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt | 41+++++++++--------------------------------
Msandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt | 44++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 53 insertions(+), 32 deletions(-)

diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/Main.kt @@ -1530,39 +1530,16 @@ val sandboxApp: Application.() -> Unit = { if (fromMs < 0) throw badRequest("'from_ms' param is less than 0") val untilMs = expectLong(call.request.queryParameters["until_ms"] ?: Long.MAX_VALUE.toString()) if (untilMs < 0) throw badRequest("'until_ms' param is less than 0") - val ret = mutableListOf<XLibeufinBankTransaction>() - /** - * Case where page number wasn't given, - * therefore the results starts from the last transaction. - */ - transaction { - /** - * Get a history page - from the calling bank account - having - * 'firstElementId' as the latest transaction in it. */ - fun getPage(firstElementId: Long): Iterable<BankAccountTransactionEntity> { - logger.debug("Trying to build pageBuf from ID: $firstElementId," + - " including $size txs in the past." + val ret: List<XLibeufinBankTransaction> = transaction { + extractTxHistory( + HistoryParams( + pageNumber = page, + pageSize = size, + bankAccount = bankAccount, + fromMs = fromMs, + untilMs = untilMs ) - return BankAccountTransactionEntity.find { - (BankAccountTransactionsTable.id lessEq firstElementId) and - (BankAccountTransactionsTable.account eq bankAccount.id) and - (BankAccountTransactionsTable.date.between(fromMs, untilMs)) - }.sortedByDescending { it.id.value }.take(size) - } - val lt: BankAccountTransactionEntity? = bankAccount.lastTransaction - if (lt == null) return@transaction - var nextPageIdUpperLimit: Long = lt.id.value - // This loop fetches (and discards) pages until the desired one is found. - for (i in 1..(page)) { - val pageBuf = getPage(nextPageIdUpperLimit) - logger.debug("pageBuf #$i follows. Request wants #$page:") - pageBuf.forEach { logger.debug("ID: ${it.id}, subject: ${it.subject}, amount: ${it.currency}:${it.amount}") } - if (pageBuf.none()) return@transaction - nextPageIdUpperLimit = pageBuf.last().id.value - 1 - if (i == page) pageBuf.forEach { - ret.add(getHistoryElementFromTransactionRow(it)) - } - } + ) } call.respond(object {val transactions = ret}) return@get diff --git a/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt b/sandbox/src/main/kotlin/tech/libeufin/sandbox/bankAccount.kt @@ -226,4 +226,48 @@ fun wireTransfer( } } return transactionRef +} + +/** + * Helper that constructs a transactions history page + * according to the URI parameters passed to Access API's + * GET /transactions. + */ +data class HistoryParams( + val pageNumber: Int, + val pageSize: Int, + val fromMs: Long, + val untilMs: Long, + val bankAccount: BankAccountEntity +) +fun extractTxHistory(params: HistoryParams): List<XLibeufinBankTransaction> { + val ret = mutableListOf<XLibeufinBankTransaction>() + /** + * Helper that gets transactions earlier than the 'firstElementId' + * transaction AND that match the URI parameters. + */ + fun getPage(firstElementId: Long): Iterable<BankAccountTransactionEntity> { + return BankAccountTransactionEntity.find { + (BankAccountTransactionsTable.id lessEq firstElementId) and + (BankAccountTransactionsTable.account eq params.bankAccount.id) and + (BankAccountTransactionsTable.date.between(params.fromMs, params.untilMs)) + }.sortedByDescending { it.id.value }.take(params.pageSize) + } + // Gets a pointer to the last transaction of this bank account. + val lastTransaction: BankAccountTransactionEntity? = params.bankAccount.lastTransaction + if (lastTransaction == null) return ret + var nextPageIdUpperLimit: Long = lastTransaction.id.value + + // This loop fetches (and discards) pages until the desired one is found. + for (i in 1..(params.pageNumber)) { + val pageBuf = getPage(nextPageIdUpperLimit) + logger.debug("pageBuf #$i follows. Request wants #${params.pageNumber}:") + pageBuf.forEach { logger.debug("ID: ${it.id}, subject: ${it.subject}, amount: ${it.currency}:${it.amount}") } + if (pageBuf.none()) return ret + nextPageIdUpperLimit = pageBuf.last().id.value - 1 + if (i == params.pageNumber) pageBuf.forEach { + ret.add(getHistoryElementFromTransactionRow(it)) + } + } + return ret } \ No newline at end of file