gnunet-svn
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[libeufin] branch master updated: refactoring bank account import


From: gnunet
Subject: [libeufin] branch master updated: refactoring bank account import
Date: Thu, 25 Jun 2020 06:28:02 +0200

This is an automated email from the git hooks/post-receive script.

ms pushed a commit to branch master
in repository libeufin.

The following commit(s) were added to refs/heads/master by this push:
     new 1cd8548  refactoring bank account import
1cd8548 is described below

commit 1cd854805405631afcfb404e2761cd64bb1605eb
Author: MS <ms@taler.net>
AuthorDate: Thu Jun 25 06:26:59 2020 +0200

    refactoring bank account import
---
 nexus/src/main/kotlin/tech/libeufin/nexus/DB.kt    | 10 ++++++
 .../tech/libeufin/nexus/bankaccount/BankAccount.kt | 42 ++++++++++++++++++++++
 .../tech/libeufin/nexus/server/NexusServer.kt      | 21 ++---------
 3 files changed, 54 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
index b6cd2e8..b8d04d1 100644
--- 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
index 373e9a7..21954f8 100644
--- 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
index 2e7c922..ebf6255 100644
--- 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)
             }

-- 
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]