Fetch account details and display
This commit is contained in:
46
core/clients/aggregator.py
Normal file
46
core/clients/aggregator.py
Normal file
@@ -0,0 +1,46 @@
|
||||
from abc import ABC
|
||||
|
||||
|
||||
class AggregatorClient(ABC):
|
||||
def store_account_info(self, account_infos):
|
||||
print("STORE ACCOUNT INFO CALLED")
|
||||
# account_infos = {
|
||||
# bank: accounts
|
||||
# for bank, accounts in account_info.items()
|
||||
# for account in accounts
|
||||
# #if account["account_id"] in self.banks
|
||||
# }
|
||||
# For each bank
|
||||
for bank, accounts in account_infos.items():
|
||||
# Iterate the accounts
|
||||
for index, account in enumerate(list(accounts)):
|
||||
if "account_number" not in account:
|
||||
account_infos[bank][index]["account_number"] = {}
|
||||
fields = ["sort_code", "number", "iban"]
|
||||
for field in fields:
|
||||
if field in account:
|
||||
account_infos[bank][index]["account_number"][
|
||||
field
|
||||
] = account[field]
|
||||
del account_infos[bank][index][field]
|
||||
# if len(account["account_number"]) == 1:
|
||||
# account_infos[bank].remove(account)
|
||||
currencies = [
|
||||
account["currency"]
|
||||
for bank, accounts in account_infos.items()
|
||||
for account in accounts
|
||||
]
|
||||
for bank, accounts in account_infos.items():
|
||||
if not self.instance.account_info:
|
||||
self.instance.account_info = {}
|
||||
self.instance.account_info[bank] = []
|
||||
for account in accounts:
|
||||
self.instance.account_info[bank].append(account)
|
||||
# self.account_info = account_infos
|
||||
self.currencies = currencies
|
||||
|
||||
self.instance.currencies = currencies
|
||||
self.instance.save()
|
||||
|
||||
print("CURRENCIES", self.instance.currencies)
|
||||
print("ACCOUNT INFO", self.instance.account_info)
|
||||
@@ -3,13 +3,14 @@ from datetime import timedelta
|
||||
from django.conf import settings
|
||||
from django.utils import timezone
|
||||
|
||||
from core.clients.aggregator import AggregatorClient
|
||||
from core.clients.base import BaseClient
|
||||
from core.util import logs
|
||||
|
||||
log = logs.get_logger("nordigen")
|
||||
|
||||
|
||||
class NordigenClient(BaseClient):
|
||||
class NordigenClient(BaseClient, AggregatorClient):
|
||||
url = "https://ob.nordigen.com/api/v2"
|
||||
|
||||
async def connect(self):
|
||||
@@ -60,7 +61,7 @@ class NordigenClient(BaseClient):
|
||||
"""
|
||||
# This function is a stub.
|
||||
|
||||
return ["GB", "SE"]
|
||||
return ["GB", "SE", "BG", "UA"]
|
||||
|
||||
async def get_banks(self, country):
|
||||
"""
|
||||
@@ -128,28 +129,31 @@ class NordigenClient(BaseClient):
|
||||
|
||||
path = f"accounts/{account_id}/details"
|
||||
response = await self.call(path, schema="AccountDetails")
|
||||
print("RESPONSE", response)
|
||||
if "account" not in response:
|
||||
return False
|
||||
parsed = response["account"]
|
||||
if "bban" in parsed and parsed["currency"] == "GBP":
|
||||
sort_code = parsed["bban"][0:6]
|
||||
account_number = parsed["bban"][6:]
|
||||
del parsed["bban"]
|
||||
if "iban" in parsed:
|
||||
if "iban" in parsed and parsed["currency"] == "GBP":
|
||||
if parsed["iban"]:
|
||||
sort_code = parsed["iban"][-14:-8]
|
||||
account_number = parsed["iban"][-8:]
|
||||
del parsed["iban"]
|
||||
sort_code = "-".join(list(map("".join, zip(*[iter(sort_code)] * 2))))
|
||||
parsed["sort_code"] = sort_code
|
||||
parsed["number"] = account_number
|
||||
parsed["recipient"] = "TODO"
|
||||
# if "iban" in parsed:
|
||||
# del parsed["iban"]
|
||||
sort_code = "-".join(list(map("".join, zip(*[iter(sort_code)] * 2))))
|
||||
parsed["sort_code"] = sort_code
|
||||
parsed["number"] = account_number
|
||||
parsed["recipient"] = "TODO"
|
||||
# Let's add the account ID so we can reference it later
|
||||
parsed["account_id"] = account_id
|
||||
return parsed
|
||||
|
||||
async def get_all_account_info(self, requisition=None):
|
||||
async def get_all_account_info(self, requisition=None, store=False):
|
||||
to_return = {}
|
||||
if not requisition:
|
||||
raise NotImplementedError
|
||||
# requisitions = await self.get_requisitions()
|
||||
print("NOT REQUISITION")
|
||||
requisitions = await self.get_requisitions()
|
||||
print("GOT REQS", requisitions)
|
||||
else:
|
||||
requisitions = [await self.get_requisition(requisition)]
|
||||
|
||||
@@ -163,4 +167,8 @@ class NordigenClient(BaseClient):
|
||||
to_return[req["institution_id"]].append(account_info)
|
||||
else:
|
||||
to_return[req["institution_id"]] = [account_info]
|
||||
|
||||
if store:
|
||||
print("TO STORE IS TRUE")
|
||||
self.store_account_info(to_return)
|
||||
return to_return
|
||||
|
||||
Reference in New Issue
Block a user