libeufin

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

commit 1cd854805405631afcfb404e2761cd64bb1605eb
parent 3b102b5382b8a4b7b86c9db6cf140724e6085ac5
Author: MS <ms@taler.net>
Date:   Thu, 25 Jun 2020 06:26:59 +0200

refactoring bank account import

Diffstat:
Mnexus/src/main/kotlin/tech/libeufin/nexus/DB.kt | 10++++++++++
Mnexus/src/main/kotlin/tech/libeufin/nexus/bankaccount/BankAccount.kt | 43+++++++++++++++++++++++++++++++++++++++++++
Mnexus/src/main/kotlin/tech/libeufin/nexus/server/NexusServer.kt | 21++-------------------
3 files changed, 55 insertions(+), 19 deletions(-)

diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/DB.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/DB.kt @@ -228,6 +228,16 @@ class OfferedBankAccountEntity(id: EntityID<String>) : Entity<String>(id) { var imported by NexusBankAccountEntity optionalReferencedOn OfferedBankAccountsTable.imported } +object AvailableConnectionsForAccountsTable : IntIdTable() { + val bankAccount = reference("bankAccount", NexusBankAccountsTable) + val bankConnection = reference("bankConnection", NexusBankConnectionsTable) +} +class AvailableConnectionForAccountEntity(id: EntityID<Int>) : IntEntity(id) { + companion object : IntEntityClass<AvailableConnectionForAccountEntity>(AvailableConnectionsForAccountsTable) + var bankAccount by NexusBankAccountEntity referencedOn AvailableConnectionsForAccountsTable.bankAccount + var bankConnection by NexusBankConnectionEntity referencedOn AvailableConnectionsForAccountsTable.bankConnection +} + /** * This table holds triples of <iban, bic, holder name>. * FIXME(dold): Allow other account and bank identifications than IBAN and BIC diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/bankaccount/BankAccount.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/bankaccount/BankAccount.kt @@ -20,6 +20,8 @@ package tech.libeufin.nexus.bankaccount import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper +import io.ktor.application.ApplicationCall +import io.ktor.application.call import io.ktor.client.HttpClient import io.ktor.http.HttpStatusCode import org.jetbrains.exposed.sql.SortOrder @@ -31,6 +33,8 @@ import tech.libeufin.nexus.ebics.fetchEbicsBySpec import tech.libeufin.nexus.ebics.submitEbicsPaymentInitiation import tech.libeufin.nexus.server.FetchSpecJson import tech.libeufin.nexus.server.Pain001Data +import tech.libeufin.nexus.server.requireBankConnection +import tech.libeufin.nexus.server.requireBankConnectionInternal import tech.libeufin.util.XMLUtil import java.time.Instant import java.time.ZonedDateTime @@ -297,3 +301,41 @@ suspend fun fetchBankAccountTransactions( ingestBankMessagesIntoAccount(res.connectionName, accountId) ingestTalerTransactions() } + +fun importBankAccount(call: ApplicationCall, offeredBankAccountId: String, nexusBankAccountId: String) { + transaction { + val conn = requireBankConnection(call, "connid") + val offeredAccount = OfferedBankAccountEntity.findById(offeredBankAccountId) ?: throw NexusError( + HttpStatusCode.NotFound, "Could not found raw bank account '${offeredBankAccountId}'" + ) + // detect name collisions first. + NexusBankAccountEntity.findById(nexusBankAccountId).run { + val importedAccount = when(this) { + is NexusBankAccountEntity -> { + if (this.iban != offeredAccount.iban) { + throw NexusError( + HttpStatusCode.Conflict, + "Cannot import two different accounts under one label: ${nexusBankAccountId}" + ) + } + this + } + else -> { + val newImportedAccount = NexusBankAccountEntity.new(nexusBankAccountId) { + iban = offeredAccount.iban + bankCode = offeredAccount.bankCode + defaultBankConnection = conn + highestSeenBankMessageId = 0 + accountHolder = offeredAccount.accountHolder + } + offeredAccount.imported = newImportedAccount + newImportedAccount + } + } + AvailableConnectionForAccountEntity.new { + bankAccount = importedAccount + bankConnection = conn + } + } + } +} +\ No newline at end of file diff --git a/nexus/src/main/kotlin/tech/libeufin/nexus/server/NexusServer.kt b/nexus/src/main/kotlin/tech/libeufin/nexus/server/NexusServer.kt @@ -52,10 +52,7 @@ import org.jetbrains.exposed.sql.and import org.jetbrains.exposed.sql.transactions.transaction import org.slf4j.event.Level import tech.libeufin.nexus.* -import tech.libeufin.nexus.bankaccount.addPaymentInitiation -import tech.libeufin.nexus.bankaccount.fetchBankAccountTransactions -import tech.libeufin.nexus.bankaccount.getPaymentInitiation -import tech.libeufin.nexus.bankaccount.submitPaymentInitiation +import tech.libeufin.nexus.bankaccount.* import tech.libeufin.nexus.ebics.* import tech.libeufin.util.* import tech.libeufin.nexus.logger @@ -810,24 +807,10 @@ fun serverMain(dbName: String, host: String) { // import one account into libeufin. post("/import-account") { val body = call.receive<ImportBankAccount>() - transaction { - val conn = requireBankConnection(call, "connid") - val account = OfferedBankAccountEntity.findById(body.offeredAccountId) ?: throw NexusError( - HttpStatusCode.NotFound, "Could not found raw bank account '${body.offeredAccountId}'" - ) - val importedBankAccount = NexusBankAccountEntity.new(body.nexusBankAccountId) { - iban = account.iban - bankCode = account.bankCode - defaultBankConnection = conn - highestSeenBankMessageId = 0 - accountHolder = account.accountHolder - } - account.imported = importedBankAccount - } + importBankAccount(call, body.offeredAccountId, body.nexusBankAccountId) call.respond(object {}) } } - route("/facades/{fcid}/taler") { talerFacadeRoutes(this, client) }