2022-03-04 22:47:06 +00:00
|
|
|
# Other library imports
|
|
|
|
# import requests
|
|
|
|
# from json import dumps
|
|
|
|
|
|
|
|
# Project imports
|
2022-04-10 12:53:28 +00:00
|
|
|
from settings import settings
|
2022-03-04 22:47:06 +00:00
|
|
|
import sinks.fidor
|
|
|
|
import sinks.nordigen
|
|
|
|
import sinks.truelayer
|
2022-03-05 21:52:31 +00:00
|
|
|
import util
|
2022-03-07 20:10:59 +00:00
|
|
|
from db import r
|
2022-03-04 22:47:06 +00:00
|
|
|
|
|
|
|
|
2022-03-05 21:52:31 +00:00
|
|
|
class Sinks(util.Base):
|
2022-03-04 22:47:06 +00:00
|
|
|
"""
|
|
|
|
Class to manage calls to various sinks.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self):
|
2022-03-05 21:52:31 +00:00
|
|
|
super().__init__()
|
2022-03-08 20:42:47 +00:00
|
|
|
self.account_info = {}
|
|
|
|
|
|
|
|
def __irc_started__(self):
|
|
|
|
self.startup()
|
|
|
|
|
|
|
|
def startup(self):
|
|
|
|
"""
|
|
|
|
We NEED the other libraries, and we initialise fast, so don't make
|
|
|
|
any race conditions by relying on something that might not be there.
|
|
|
|
"""
|
2022-04-10 12:53:28 +00:00
|
|
|
if settings.Fidor.enabled == "1":
|
|
|
|
self.fidor = sinks.fidor.Fidor()
|
|
|
|
if settings.Nordigen.enabled == "1":
|
|
|
|
self.nordigen = sinks.nordigen.Nordigen(self)
|
|
|
|
if settings.TrueLayer.enabled == "1":
|
|
|
|
self.truelayer = sinks.truelayer.TrueLayer(self)
|
2022-03-07 19:59:30 +00:00
|
|
|
# setattr(self.truelayer, "sinks", self)
|
|
|
|
|
2022-03-25 18:46:08 +00:00
|
|
|
def got_transactions(self, subclass, account_id, transactions):
|
2022-03-08 20:42:47 +00:00
|
|
|
if not transactions:
|
|
|
|
return False
|
2022-03-07 20:10:59 +00:00
|
|
|
transaction_ids = [x["transaction_id"] for x in transactions]
|
2022-03-25 18:46:08 +00:00
|
|
|
new_key_name = f"new.transactions.{subclass}.{account_id}"
|
|
|
|
old_key_name = f"transactions.{subclass}.{account_id}"
|
2022-03-08 20:42:47 +00:00
|
|
|
# for transaction_id in transaction_ids:
|
|
|
|
if not transaction_ids:
|
|
|
|
return
|
|
|
|
r.sadd(new_key_name, *transaction_ids)
|
|
|
|
|
|
|
|
difference = list(r.sdiff(new_key_name, old_key_name))
|
|
|
|
|
|
|
|
difference = util.convert(difference)
|
2022-03-07 20:10:59 +00:00
|
|
|
|
2022-03-08 20:42:47 +00:00
|
|
|
new_transactions = [x for x in transactions if x["transaction_id"] in difference]
|
2022-03-07 20:10:59 +00:00
|
|
|
|
|
|
|
# Rename the new key to the old key so we can run the diff again
|
|
|
|
r.rename(new_key_name, old_key_name)
|
2022-03-08 20:42:47 +00:00
|
|
|
for transaction in new_transactions:
|
2022-03-25 18:46:08 +00:00
|
|
|
transaction["subclass"] = subclass
|
2022-03-08 20:42:47 +00:00
|
|
|
self.tx.transaction(transaction)
|
|
|
|
|
|
|
|
def got_account_info(self, subclass, account_infos):
|
|
|
|
"""
|
|
|
|
Called when we get account information from an API provider.
|
|
|
|
:param subclass: class name that called it, truelayer, fidor, etc
|
|
|
|
:param account_infos: dict of dicts of account information
|
|
|
|
:param account_infos: dict
|
|
|
|
"""
|
|
|
|
for bank, accounts in account_infos.items():
|
2022-03-22 21:56:35 +00:00
|
|
|
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)
|
|
|
|
self.log.warning(f"Potentially useless bank account: {account}")
|
2022-03-08 20:42:47 +00:00
|
|
|
currencies = [account["currency"] for bank, accounts in account_infos.items() for account in accounts]
|
2022-03-22 21:56:35 +00:00
|
|
|
for bank, accounts in account_infos.items():
|
|
|
|
self.account_info[bank] = []
|
|
|
|
for account in accounts:
|
|
|
|
self.account_info[bank].append(account)
|
|
|
|
# self.account_info = account_infos
|
2022-03-08 20:42:47 +00:00
|
|
|
self.currencies = currencies
|
2022-03-07 20:10:59 +00:00
|
|
|
|
2022-03-08 20:42:47 +00:00
|
|
|
# parsed_details =
|
|
|
|
# {"EUR": {"IBAN": "xxx", "BIC": "xxx"},
|
|
|
|
# "GBP": {"SORT": "04-04-04", "ACCOUNT": "1922-2993"}}
|
|
|
|
# self.markets.distribute_account_details(currencies, account_infos)
|
2022-04-09 18:59:22 +00:00
|
|
|
|
|
|
|
def get_total_usd(self):
|
|
|
|
"""
|
|
|
|
Get the total balance of our accounts in USD.
|
|
|
|
"""
|
|
|
|
total_nordigen = self.nordigen.get_total_map()
|
|
|
|
total_truelayer = self.truelayer.get_total_map()
|
|
|
|
|
|
|
|
# Yes, we can save an API call by merging but I think this is clearer
|
|
|
|
total_nordigen_usd = self.money.multiple_to_usd(total_nordigen)
|
|
|
|
total_truelayer_usd = self.money.multiple_to_usd(total_truelayer)
|
|
|
|
|
|
|
|
return total_truelayer_usd + total_nordigen_usd
|