2022-03-04 22:47:06 +00:00
|
|
|
# Other library imports
|
|
|
|
# import requests
|
|
|
|
# from json import dumps
|
|
|
|
|
|
|
|
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
|
2023-02-09 07:20:00 +00:00
|
|
|
|
2022-07-15 10:09:54 +00:00
|
|
|
# Project imports
|
|
|
|
from settings import settings
|
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 = {}
|
2022-05-02 08:19:55 +00:00
|
|
|
self.currencies = []
|
2022-03-08 20:42:47 +00:00
|
|
|
|
|
|
|
def __irc_started__(self):
|
2022-05-02 15:32:19 +00:00
|
|
|
self.log.debug("IRC hook called.")
|
2022-03-08 20:42:47 +00:00
|
|
|
self.startup()
|
2022-05-02 15:32:19 +00:00
|
|
|
self.log.debug("Finished initialising subclasses.")
|
2022-03-08 20:42:47 +00:00
|
|
|
|
2022-05-05 08:18:35 +00:00
|
|
|
def all_sinks_authenticated(self): # TODO: fix
|
2022-05-05 17:16:56 +00:00
|
|
|
self.money.setup_loops()
|
2022-05-05 08:18:35 +00:00
|
|
|
|
2022-03-08 20:42:47 +00:00
|
|
|
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.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-07-15 10:09:54 +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
|
|
|
|
"""
|
2022-04-19 20:03:11 +00:00
|
|
|
if not account_infos:
|
|
|
|
self.log.error(f"No accounts provided for {subclass}") #
|
|
|
|
return
|
2022-03-08 20:42:47 +00:00
|
|
|
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:
|
2022-07-15 10:09:54 +00:00
|
|
|
account_infos[bank][index]["account_number"][
|
|
|
|
field
|
|
|
|
] = account[field]
|
2022-03-22 21:56:35 +00:00
|
|
|
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-07-15 10:09:54 +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.
|
|
|
|
"""
|
2022-04-12 21:06:28 +00:00
|
|
|
total = 0
|
|
|
|
if settings.Nordigen.enabled == "1":
|
|
|
|
total_nordigen = self.nordigen.get_total_map()
|
|
|
|
total_nordigen_usd = self.money.multiple_to_usd(total_nordigen)
|
|
|
|
total += total_nordigen_usd
|
|
|
|
if settings.TrueLayer.enabled == "1":
|
|
|
|
total_truelayer = self.truelayer.get_total_map()
|
|
|
|
total_truelayer_usd = self.money.multiple_to_usd(total_truelayer)
|
|
|
|
total += total_truelayer_usd
|
2022-04-09 18:59:22 +00:00
|
|
|
|
2022-04-12 21:06:28 +00:00
|
|
|
return total
|