diff options
author | Antoine A <> | 2024-01-23 20:45:54 +0100 |
---|---|---|
committer | Antoine A <> | 2024-01-23 20:45:54 +0100 |
commit | 1d27092ddb45ab63cee93a738e785f36f7039dbc (patch) | |
tree | 27f1ef86bbd603395de2139c4c04fd8407bf8bf7 | |
parent | 9210b01fb46380537afccb50b07fdba0108fbe33 (diff) | |
download | libeufin-1d27092ddb45ab63cee93a738e785f36f7039dbc.tar.gz libeufin-1d27092ddb45ab63cee93a738e785f36f7039dbc.tar.bz2 libeufin-1d27092ddb45ab63cee93a738e785f36f7039dbc.zip |
New RevenueApi
-rw-r--r-- | API_CHANGES.md | 1 | ||||
-rw-r--r-- | bank/src/main/kotlin/tech/libeufin/bank/RevenueApi.kt | 4 | ||||
-rw-r--r-- | bank/src/main/kotlin/tech/libeufin/bank/TalerMessage.kt | 9 | ||||
-rw-r--r-- | bank/src/main/kotlin/tech/libeufin/bank/db/Database.kt | 2 | ||||
-rw-r--r-- | bank/src/main/kotlin/tech/libeufin/bank/db/ExchangeDAO.kt | 30 | ||||
-rw-r--r-- | bank/src/main/kotlin/tech/libeufin/bank/db/NotificationWatcher.kt | 6 | ||||
-rw-r--r-- | bank/src/main/kotlin/tech/libeufin/bank/db/TransactionDAO.kt | 26 | ||||
-rw-r--r-- | bank/src/test/kotlin/RevenueApiTest.kt | 18 | ||||
-rw-r--r-- | bank/src/test/kotlin/helpers.kt | 10 |
9 files changed, 50 insertions, 56 deletions
diff --git a/API_CHANGES.md b/API_CHANGES.md index 14626148..b9e550ba 100644 --- a/API_CHANGES.md +++ b/API_CHANGES.md @@ -32,6 +32,7 @@ This files contains all the API changes for the current release: - GET /accounts/USERNAME: add tan_channel - Add GET /accounts/USERNAME/taler-revenue/config - Add GET /accounts/USERNAME/taler-wire-gateway/config +- Change GET /accounts/USERNAME/taler-revenue/history logic and body type ## bank cli diff --git a/bank/src/main/kotlin/tech/libeufin/bank/RevenueApi.kt b/bank/src/main/kotlin/tech/libeufin/bank/RevenueApi.kt index 22a44ca1..999503d2 100644 --- a/bank/src/main/kotlin/tech/libeufin/bank/RevenueApi.kt +++ b/bank/src/main/kotlin/tech/libeufin/bank/RevenueApi.kt @@ -38,12 +38,12 @@ fun Routing.revenueApi(db: Database, ctx: BankConfig) { get("/accounts/{USERNAME}/taler-revenue/history") { val params = HistoryParams.extract(context.request.queryParameters) val bankAccount = call.bankInfo(db) - val items = db.exchange.revenueHistory(params, bankAccount.bankAccountId); + val items = db.transaction.revenueHistory(params, bankAccount.bankAccountId); if (items.isEmpty()) { call.respond(HttpStatusCode.NoContent) } else { - call.respond(MerchantIncomingHistory(items, bankAccount.internalPaytoUri)) + call.respond(RevenueIncomingHistory(items, bankAccount.internalPaytoUri)) } } } diff --git a/bank/src/main/kotlin/tech/libeufin/bank/TalerMessage.kt b/bank/src/main/kotlin/tech/libeufin/bank/TalerMessage.kt index 41b83a34..1d7c5efa 100644 --- a/bank/src/main/kotlin/tech/libeufin/bank/TalerMessage.kt +++ b/bank/src/main/kotlin/tech/libeufin/bank/TalerMessage.kt @@ -596,19 +596,18 @@ data class OutgoingTransaction( ) @Serializable -data class MerchantIncomingHistory( - val incoming_transactions : List<MerchantIncomingBankTransaction>, +data class RevenueIncomingHistory( + val incoming_transactions : List<RevenueIncomingBankTransaction>, val credit_account: String ) @Serializable -data class MerchantIncomingBankTransaction( +data class RevenueIncomingBankTransaction( val row_id: Long, val date: TalerProtocolTimestamp, val amount: TalerAmount, val debit_account: String, - val exchange_url: String, - val wtid: ShortHashCode + val subject: String ) /** diff --git a/bank/src/main/kotlin/tech/libeufin/bank/db/Database.kt b/bank/src/main/kotlin/tech/libeufin/bank/db/Database.kt index a0edbdec..f652c3c5 100644 --- a/bank/src/main/kotlin/tech/libeufin/bank/db/Database.kt +++ b/bank/src/main/kotlin/tech/libeufin/bank/db/Database.kt @@ -160,7 +160,7 @@ class Database(dbConfig: String, internal val bankCurrency: String, internal val suspend fun load(): List<T> = page( params.page, "bank_transaction_id", - "$query WHERE $accountColumn=? AND", + "$query $accountColumn=? AND", { setLong(1, bankAccountId) 1 diff --git a/bank/src/main/kotlin/tech/libeufin/bank/db/ExchangeDAO.kt b/bank/src/main/kotlin/tech/libeufin/bank/db/ExchangeDAO.kt index 3339bae6..474205bc 100644 --- a/bank/src/main/kotlin/tech/libeufin/bank/db/ExchangeDAO.kt +++ b/bank/src/main/kotlin/tech/libeufin/bank/db/ExchangeDAO.kt @@ -44,6 +44,7 @@ class ExchangeDAO(private val db: Database) { FROM taler_exchange_incoming AS tfr JOIN bank_account_transactions AS txs ON bank_transaction=txs.bank_transaction_id + WHERE """) { IncomingReserveTransaction( row_id = it.getLong("bank_transaction_id"), @@ -71,6 +72,7 @@ class ExchangeDAO(private val db: Database) { FROM taler_exchange_outgoing AS tfr JOIN bank_account_transactions AS txs ON bank_transaction=txs.bank_transaction_id + WHERE """) { OutgoingTransaction( row_id = it.getLong("bank_transaction_id"), @@ -82,34 +84,6 @@ class ExchangeDAO(private val db: Database) { ) } - /** Query [merchantId] history of taler outgoing transactions to its account */ - suspend fun revenueHistory( - params: HistoryParams, - merchantId: Long - ): List<MerchantIncomingBankTransaction> - = db.poolHistory(params, merchantId, NotificationWatcher::listenRevenue, """ - SELECT - bank_transaction_id - ,transaction_date - ,(amount).val AS amount_val - ,(amount).frac AS amount_frac - ,debtor_payto_uri - ,wtid - ,exchange_base_url - FROM taler_exchange_outgoing AS tfr - JOIN bank_account_transactions AS txs - ON bank_transaction=txs.bank_transaction_id - """, "creditor_account_id") { - MerchantIncomingBankTransaction( - row_id = it.getLong("bank_transaction_id"), - date = it.getTalerTimestamp("transaction_date"), - amount = it.getAmount("amount", db.bankCurrency), - debit_account = it.getString("debtor_payto_uri"), - wtid = ShortHashCode(it.getBytes("wtid")), - exchange_url = it.getString("exchange_base_url") - ) - } - /** Result of taler transfer transaction creation */ sealed class TransferResult { /** Transaction [id] and wire transfer [timestamp] */ diff --git a/bank/src/main/kotlin/tech/libeufin/bank/db/NotificationWatcher.kt b/bank/src/main/kotlin/tech/libeufin/bank/db/NotificationWatcher.kt index 973423e1..eb6cd879 100644 --- a/bank/src/main/kotlin/tech/libeufin/bank/db/NotificationWatcher.kt +++ b/bank/src/main/kotlin/tech/libeufin/bank/db/NotificationWatcher.kt @@ -75,15 +75,15 @@ internal class NotificationWatcher(private val pgSource: PGSimpleDataSource) { bankTxFlows[creditor]?.run { flow.emit(creditRow) } + revenueTxFlows[creditor]?.run { + flow.emit(creditRow) + } } "outgoing_tx" -> { val (account, merchant, debitRow, creditRow) = it.parameter.split(' ', limit = 4).map { it.toLong() } outgoingTxFlows[account]?.run { flow.emit(debitRow) } - revenueTxFlows[merchant]?.run { - flow.emit(creditRow) - } } "incoming_tx" -> { val (account, row) = it.parameter.split(' ', limit = 2).map { it.toLong() } diff --git a/bank/src/main/kotlin/tech/libeufin/bank/db/TransactionDAO.kt b/bank/src/main/kotlin/tech/libeufin/bank/db/TransactionDAO.kt index b6038dc5..1b996f5a 100644 --- a/bank/src/main/kotlin/tech/libeufin/bank/db/TransactionDAO.kt +++ b/bank/src/main/kotlin/tech/libeufin/bank/db/TransactionDAO.kt @@ -187,7 +187,7 @@ class TransactionDAO(private val db: Database) { ,creditor_payto_uri ,subject ,direction - FROM bank_account_transactions + FROM bank_account_transactions WHERE """) { BankAccountTransactionInfo( row_id = it.getLong("bank_transaction_id"), @@ -200,4 +200,28 @@ class TransactionDAO(private val db: Database) { ) } } + + /** Query [accountId] history of incoming transactions to its account */ + suspend fun revenueHistory( + params: HistoryParams, + accountId: Long + ): List<RevenueIncomingBankTransaction> + = db.poolHistory(params, accountId, NotificationWatcher::listenRevenue, """ + SELECT + bank_transaction_id + ,transaction_date + ,(amount).val AS amount_val + ,(amount).frac AS amount_frac + ,debtor_payto_uri + ,subject + FROM bank_account_transactions WHERE direction='credit' AND + """) { + RevenueIncomingBankTransaction( + row_id = it.getLong("bank_transaction_id"), + date = it.getTalerTimestamp("transaction_date"), + amount = it.getAmount("amount", db.bankCurrency), + debit_account = it.getString("debtor_payto_uri"), + subject = it.getString("subject") + ) + } }
\ No newline at end of file diff --git a/bank/src/test/kotlin/RevenueApiTest.kt b/bank/src/test/kotlin/RevenueApiTest.kt index e6bc7651..dfa0e69c 100644 --- a/bank/src/test/kotlin/RevenueApiTest.kt +++ b/bank/src/test/kotlin/RevenueApiTest.kt @@ -42,27 +42,23 @@ class RevenueApiTest { fun history() = bankSetup { setMaxDebt("exchange", "KUDOS:1000000") authRoutine(HttpMethod.Get, "/accounts/merchant/taler-revenue/history") - historyRoutine<MerchantIncomingHistory>( + historyRoutine<RevenueIncomingHistory>( url = "/accounts/merchant/taler-revenue/history", ids = { it.incoming_transactions.map { it.row_id } }, registered = listOf( { // Transactions using clean transfer logic transfer("KUDOS:10") + }, + { + // Common credit transactions + tx("exchange", "KUDOS:10", "merchant", "ignored") } ), ignored = listOf( { - // Ignore manual incoming transaction - tx("exchange", "KUDOS:10", "merchant", "${randShortHashCode()} http://exchange.example.com/") - }, - { - // Ignore malformed incoming transaction - tx("merchant", "KUDOS:10", "exchange", "ignored") - }, - { - // Ignore malformed outgoing transaction - tx("exchange", "KUDOS:10", "merchant", "ignored") + // Ignore debit transactions + tx("merchant", "KUDOS:10", "customer") } ) ) diff --git a/bank/src/test/kotlin/helpers.kt b/bank/src/test/kotlin/helpers.kt index 69c0ede9..c02a7e77 100644 --- a/bank/src/test/kotlin/helpers.kt +++ b/bank/src/test/kotlin/helpers.kt @@ -397,19 +397,19 @@ inline suspend fun <reified B> HttpResponse.assertHistoryIds(size: Int, ids: (B) val params = PageParams.extract(call.request.url.parameters) // testing the size is like expected. - assertEquals(size, history.size) + assertEquals(size, history.size, "bad history lenght: $history") if (params.delta < 0) { // testing that the first id is at most the 'start' query param. - assert(history[0] <= params.start) + assert(history[0] <= params.start) { "bad history start: $params $history" } // testing that the id decreases. if (history.size > 1) - assert(history.windowed(2).all { (a, b) -> a > b }) + assert(history.windowed(2).all { (a, b) -> a > b }) { "bad history order: $history" } } else { // testing that the first id is at least the 'start' query param. - assert(history[0] >= params.start) + assert(history[0] >= params.start) { "bad history start: $params $history" } // testing that the id increases. if (history.size > 1) - assert(history.windowed(2).all { (a, b) -> a < b }) + assert(history.windowed(2).all { (a, b) -> a < b }) { "bad history order: $history" } } return body |