Fix transaction IDs and make Nordigen validation stricter

This commit is contained in:
Mark Veidemanis 2023-03-11 18:30:56 +00:00
parent af65433c55
commit 6d6b370327
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 21 additions and 21 deletions

View File

@ -1,9 +1,9 @@
from datetime import timedelta from datetime import timedelta
from hashlib import sha256 from hashlib import sha256
import orjson
from django.conf import settings from django.conf import settings
from django.utils import timezone from django.utils import timezone
from orjson import dumps
from core.clients.aggregator import AggregatorClient from core.clients.aggregator import AggregatorClient
from core.clients.base import BaseClient from core.clients.base import BaseClient
@ -250,17 +250,16 @@ class NordigenClient(BaseClient, AggregatorClient):
def normalise_transactions(self, transactions, state=None): def normalise_transactions(self, transactions, state=None):
for transaction in transactions: for transaction in transactions:
# Rename ID # Rename ID
if "transactionId" in transaction: if transaction["transactionId"]:
transaction["transaction_id"] = transaction["transactionId"] transaction["transaction_id"] = transaction["transactionId"]
del transaction["transactionId"] del transaction["transactionId"]
elif "internalTransactionId" in transaction: elif transaction["internalTransactionId"]:
transaction["transaction_id"] = transaction["internalTransactionId"] transaction["transaction_id"] = transaction["internalTransactionId"]
del transaction["internalTransactionId"] del transaction["internalTransactionId"]
else: else:
# No transaction ID. This is a problem for our implementation # No transaction ID. This is a problem for our implementation
tx_hash = sha256( tx_hash = sha256(
dumps(transaction, sort_keys=True).encode("utf8") orjson.dumps(transaction, option=orjson.OPT_SORT_KEYS)
).hexdigest() ).hexdigest()
transaction["transaction_id"] = tx_hash transaction["transaction_id"] = tx_hash

View File

@ -9,7 +9,7 @@ class MyModel(BaseModel):
# TODO: inherit from MyModel # TODO: inherit from MyModel
class TokenNew(BaseModel): class TokenNew(MyModel):
access: str access: str
access_expires: int access_expires: int
refresh: str refresh: str
@ -24,7 +24,7 @@ TokenNewSchema = {
} }
class RequisitionResult(BaseModel): class RequisitionResult(MyModel):
id: str id: str
created: str created: str
redirect: str redirect: str
@ -39,7 +39,7 @@ class RequisitionResult(BaseModel):
redirect_immediate: bool redirect_immediate: bool
class Requisitions(BaseModel): class Requisitions(MyModel):
count: int count: int
next: str | None next: str | None
previous: str | None previous: str | None
@ -54,7 +54,7 @@ RequisitionsSchema = {
} }
class RequisitionsPost(BaseModel): class RequisitionsPost(MyModel):
id: str id: str
created: str created: str
redirect: str redirect: str
@ -85,7 +85,7 @@ RequisitionsPostSchema = {
} }
class Requisition(BaseModel): class Requisition(MyModel):
id: str id: str
created: str created: str
redirect: str redirect: str
@ -116,7 +116,7 @@ RequisitionSchema = {
} }
class AccountDetailsNested(BaseModel): class AccountDetailsNested(MyModel):
resourceId: str resourceId: str
currency: str currency: str
ownerName: str ownerName: str
@ -128,9 +128,10 @@ class AccountDetailsNested(BaseModel):
bban: str | None bban: str | None
name: str | None name: str | None
product: str | None product: str | None
bic: str | None
class AccountDetails(BaseModel): class AccountDetails(MyModel):
account: AccountDetailsNested account: AccountDetailsNested
@ -139,13 +140,13 @@ AccountDetailsSchema = {
} }
class AccountBalance(BaseModel): class AccountBalance(MyModel):
balanceAmount: dict[str, str] balanceAmount: dict[str, str]
balanceType: str | None balanceType: str | None
referenceDate: str | None referenceDate: str | None
class AccountBalances(BaseModel): class AccountBalances(MyModel):
balances: list[AccountBalance] balances: list[AccountBalance]
summary: str | None summary: str | None
@ -156,12 +157,12 @@ AccountBalancesSchema = {
} }
class TXCurrencyAmount(BaseModel): class TXCurrencyAmount(MyModel):
amount: str amount: str
currency: str currency: str
class TransactionsCurrencyExchange(BaseModel): class TransactionsCurrencyExchange(MyModel):
instructedAmount: TXCurrencyAmount instructedAmount: TXCurrencyAmount
sourceCurrency: str sourceCurrency: str
exchangeRate: str exchangeRate: str
@ -169,15 +170,15 @@ class TransactionsCurrencyExchange(BaseModel):
targetCurrency: str targetCurrency: str
class TXAccount(BaseModel): class TXAccount(MyModel):
iban: str iban: str
bban: str | None bban: str | None
class TransactionsNested(BaseModel): class TransactionsNested(MyModel):
transactionId: str | None transactionId: str | None
bookingDate: str | None bookingDate: str | None
valueDate: str valueDate: str | None
bookingDateTime: str | None bookingDateTime: str | None
valueDateTime: str | None valueDateTime: str | None
transactionAmount: TXCurrencyAmount transactionAmount: TXCurrencyAmount
@ -192,12 +193,12 @@ class TransactionsNested(BaseModel):
currencyExchange: TransactionsCurrencyExchange | None currencyExchange: TransactionsCurrencyExchange | None
class TransactionsBookedPending(BaseModel): class TransactionsBookedPending(MyModel):
booked: list[TransactionsNested] booked: list[TransactionsNested]
pending: list[TransactionsNested] pending: list[TransactionsNested]
class Transactions(BaseModel): class Transactions(MyModel):
transactions: TransactionsBookedPending transactions: TransactionsBookedPending