Implement viewing bank balances
This commit is contained in:
@@ -61,7 +61,7 @@ class NordigenClient(BaseClient, AggregatorClient):
|
||||
"""
|
||||
# This function is a stub.
|
||||
|
||||
return ["GB", "SE", "BG", "UA"]
|
||||
return ["GB", "SE", "BG"]
|
||||
|
||||
async def get_banks(self, country):
|
||||
"""
|
||||
@@ -129,7 +129,6 @@ class NordigenClient(BaseClient, AggregatorClient):
|
||||
|
||||
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"]
|
||||
@@ -151,9 +150,7 @@ class NordigenClient(BaseClient, AggregatorClient):
|
||||
async def get_all_account_info(self, requisition=None, store=False):
|
||||
to_return = {}
|
||||
if not requisition:
|
||||
print("NOT REQUISITION")
|
||||
requisitions = await self.get_requisitions()
|
||||
print("GOT REQS", requisitions)
|
||||
else:
|
||||
requisitions = [await self.get_requisition(requisition)]
|
||||
|
||||
@@ -169,6 +166,78 @@ class NordigenClient(BaseClient, AggregatorClient):
|
||||
to_return[req["institution_id"]] = [account_info]
|
||||
|
||||
if store:
|
||||
print("TO STORE IS TRUE")
|
||||
if requisition is not None:
|
||||
raise Exception("Cannot store partial data")
|
||||
self.store_account_info(to_return)
|
||||
return to_return
|
||||
|
||||
async def get_balance(self, account_id):
|
||||
"""
|
||||
Get the balance and currency of an account.
|
||||
:param account_id: the account ID
|
||||
:return: tuple of (currency, amount)
|
||||
:rtype: tuple
|
||||
"""
|
||||
|
||||
path = f"accounts/{account_id}/balances"
|
||||
response = await self.call(path, schema="AccountBalance")
|
||||
|
||||
total = 0
|
||||
currency = None
|
||||
if "balances" not in response:
|
||||
return (False, False)
|
||||
for entry in response["balances"]:
|
||||
if currency:
|
||||
if not currency == entry["balanceAmount"]["currency"]:
|
||||
return (False, False)
|
||||
total += float(entry["balanceAmount"]["amount"])
|
||||
currency = entry["balanceAmount"]["currency"]
|
||||
return (currency, total)
|
||||
|
||||
async def get_all_balances(self):
|
||||
"""
|
||||
Get all balances.
|
||||
Keyed by bank.
|
||||
"""
|
||||
if self.instance.account_info is None:
|
||||
await self.get_all_account_info(store=True)
|
||||
|
||||
account_balances = {}
|
||||
for bank, accounts in self.instance.account_info.items():
|
||||
if bank not in account_balances:
|
||||
account_balances[bank] = []
|
||||
for account in accounts:
|
||||
account_id = account["account_id"]
|
||||
currency, amount = await self.get_balance(account_id)
|
||||
account_balances[bank].append(
|
||||
{
|
||||
"currency": currency,
|
||||
"balance": amount,
|
||||
"account_id": account_id,
|
||||
}
|
||||
)
|
||||
return account_balances
|
||||
|
||||
async def get_total_map(self):
|
||||
"""
|
||||
Return a dictionary keyed by currencies with the amounts as values.
|
||||
:return: dict keyed by currency, values are amounts
|
||||
:rtype: dict
|
||||
"""
|
||||
if self.instance.account_info is None:
|
||||
await self.get_all_account_info(store=True)
|
||||
|
||||
totals = {}
|
||||
for bank, accounts in self.instance.account_info.items():
|
||||
for account in accounts:
|
||||
account_id = account["account_id"]
|
||||
currency, amount = await self.get_balance(account_id)
|
||||
if not amount:
|
||||
continue
|
||||
if not currency:
|
||||
continue
|
||||
if currency in totals:
|
||||
totals[currency] += amount
|
||||
else:
|
||||
totals[currency] = amount
|
||||
return totals
|
||||
|
||||
Reference in New Issue
Block a user