From 6d6b370327d94105ba4b951be18459f7bb655bd2 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sat, 11 Mar 2023 18:30:56 +0000 Subject: [PATCH] Fix transaction IDs and make Nordigen validation stricter --- core/clients/aggregators/nordigen.py | 9 ++++---- core/lib/schemas/nordigen_s.py | 33 ++++++++++++++-------------- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/core/clients/aggregators/nordigen.py b/core/clients/aggregators/nordigen.py index bccf7d6..296eb59 100644 --- a/core/clients/aggregators/nordigen.py +++ b/core/clients/aggregators/nordigen.py @@ -1,9 +1,9 @@ from datetime import timedelta from hashlib import sha256 +import orjson from django.conf import settings from django.utils import timezone -from orjson import dumps from core.clients.aggregator import AggregatorClient from core.clients.base import BaseClient @@ -250,17 +250,16 @@ class NordigenClient(BaseClient, AggregatorClient): def normalise_transactions(self, transactions, state=None): for transaction in transactions: # Rename ID - if "transactionId" in transaction: + if transaction["transactionId"]: transaction["transaction_id"] = transaction["transactionId"] del transaction["transactionId"] - elif "internalTransactionId" in transaction: + elif transaction["internalTransactionId"]: transaction["transaction_id"] = transaction["internalTransactionId"] del transaction["internalTransactionId"] else: # No transaction ID. This is a problem for our implementation - tx_hash = sha256( - dumps(transaction, sort_keys=True).encode("utf8") + orjson.dumps(transaction, option=orjson.OPT_SORT_KEYS) ).hexdigest() transaction["transaction_id"] = tx_hash diff --git a/core/lib/schemas/nordigen_s.py b/core/lib/schemas/nordigen_s.py index ee26cc5..ceb2d2f 100644 --- a/core/lib/schemas/nordigen_s.py +++ b/core/lib/schemas/nordigen_s.py @@ -9,7 +9,7 @@ class MyModel(BaseModel): # TODO: inherit from MyModel -class TokenNew(BaseModel): +class TokenNew(MyModel): access: str access_expires: int refresh: str @@ -24,7 +24,7 @@ TokenNewSchema = { } -class RequisitionResult(BaseModel): +class RequisitionResult(MyModel): id: str created: str redirect: str @@ -39,7 +39,7 @@ class RequisitionResult(BaseModel): redirect_immediate: bool -class Requisitions(BaseModel): +class Requisitions(MyModel): count: int next: str | None previous: str | None @@ -54,7 +54,7 @@ RequisitionsSchema = { } -class RequisitionsPost(BaseModel): +class RequisitionsPost(MyModel): id: str created: str redirect: str @@ -85,7 +85,7 @@ RequisitionsPostSchema = { } -class Requisition(BaseModel): +class Requisition(MyModel): id: str created: str redirect: str @@ -116,7 +116,7 @@ RequisitionSchema = { } -class AccountDetailsNested(BaseModel): +class AccountDetailsNested(MyModel): resourceId: str currency: str ownerName: str @@ -128,9 +128,10 @@ class AccountDetailsNested(BaseModel): bban: str | None name: str | None product: str | None + bic: str | None -class AccountDetails(BaseModel): +class AccountDetails(MyModel): account: AccountDetailsNested @@ -139,13 +140,13 @@ AccountDetailsSchema = { } -class AccountBalance(BaseModel): +class AccountBalance(MyModel): balanceAmount: dict[str, str] balanceType: str | None referenceDate: str | None -class AccountBalances(BaseModel): +class AccountBalances(MyModel): balances: list[AccountBalance] summary: str | None @@ -156,12 +157,12 @@ AccountBalancesSchema = { } -class TXCurrencyAmount(BaseModel): +class TXCurrencyAmount(MyModel): amount: str currency: str -class TransactionsCurrencyExchange(BaseModel): +class TransactionsCurrencyExchange(MyModel): instructedAmount: TXCurrencyAmount sourceCurrency: str exchangeRate: str @@ -169,15 +170,15 @@ class TransactionsCurrencyExchange(BaseModel): targetCurrency: str -class TXAccount(BaseModel): +class TXAccount(MyModel): iban: str bban: str | None -class TransactionsNested(BaseModel): +class TransactionsNested(MyModel): transactionId: str | None bookingDate: str | None - valueDate: str + valueDate: str | None bookingDateTime: str | None valueDateTime: str | None transactionAmount: TXCurrencyAmount @@ -192,12 +193,12 @@ class TransactionsNested(BaseModel): currencyExchange: TransactionsCurrencyExchange | None -class TransactionsBookedPending(BaseModel): +class TransactionsBookedPending(MyModel): booked: list[TransactionsNested] pending: list[TransactionsNested] -class Transactions(BaseModel): +class Transactions(MyModel): transactions: TransactionsBookedPending