From 7f60e0234037cf417bf5674651c05cd2b8a1651d Mon Sep 17 00:00:00 2001 From: dankito Date: Tue, 3 Sep 2024 22:24:31 +0200 Subject: [PATCH] Implemented BankingModelService to filter out duplicates in retrieved AccountTransactions --- .../client/service/BankingModelService.kt | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 BankingClient/src/commonMain/kotlin/net/codinux/banking/client/service/BankingModelService.kt diff --git a/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/service/BankingModelService.kt b/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/service/BankingModelService.kt new file mode 100644 index 00000000..d4a3f0e1 --- /dev/null +++ b/BankingClient/src/commonMain/kotlin/net/codinux/banking/client/service/BankingModelService.kt @@ -0,0 +1,24 @@ +package net.codinux.banking.client.service + +import net.codinux.banking.client.model.AccountTransaction + +open class BankingModelService { + + /** + * It's not possible to retrieve only new transactions from bank server (almost no bank implements HKKAN job). So + * for updating account transactions we start at the date of latest account transactions retrieval time (e.g. + * transactions have at last been fetched at 01. September 12:00, then there may have been some other transactions + * been booked on September 1st after 12:00 o'clock). + * + * Therefore retrieved account transactions may contain transactions that we already have locally. This method filters + * from [retrievedTransactions] those already in [existingTransactions] and returns only that ones, that are not in + * [existingTransactions]. + */ + open fun findNewTransactions(retrievedTransactions: List, existingTransactions: List): List { + val existingTransactionsByIdentifier = existingTransactions.associateBy { it.identifier } + val existingTransactionsIdentifiers = existingTransactionsByIdentifier.keys + + return retrievedTransactions.filter { transaction -> existingTransactionsIdentifiers.contains(transaction.identifier) == false } + } + +} \ No newline at end of file