From c0dc41a63a5fadfb18c4a89f40b5542a09aeb43d Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Wed, 8 Mar 2023 17:04:47 +0000 Subject: [PATCH] Implement getting bank account details --- app/urls.py | 6 ++ core/clients/aggregators/nordigen.py | 75 +++++++++++++++++++ core/templates/partials/aggregator-info.html | 4 +- .../partials/aggregator-req-info.html | 28 +++++++ core/views/aggregators.py | 38 +++++++++- 5 files changed, 145 insertions(+), 6 deletions(-) create mode 100644 core/templates/partials/aggregator-req-info.html diff --git a/app/urls.py b/app/urls.py index 2575b4f..5f003ab 100644 --- a/app/urls.py +++ b/app/urls.py @@ -87,4 +87,10 @@ urlpatterns = [ aggregators.ReqDelete.as_view(), name="req_delete", ), + # Requisition info + path( + "aggs//info///", + aggregators.ReqInfo.as_view(), + name="req_info", + ), ] + static(settings.STATIC_URL, document_root=settings.STATIC_ROOT) diff --git a/core/clients/aggregators/nordigen.py b/core/clients/aggregators/nordigen.py index 06f95ac..c8b3bd8 100644 --- a/core/clients/aggregators/nordigen.py +++ b/core/clients/aggregators/nordigen.py @@ -104,3 +104,78 @@ class NordigenClient(BaseClient): path, schema="RequisitionDelete", http_method="delete" ) return response + + async def get_requisition(self, requisition): + """ + Get a list of accounts for a requisition. + :param requisition: requisition ID""" + path = f"requisitions/{requisition}" + response = await self.call(path, schema="Requisition") + + return response + + # def get_ownernames(self): + # """ + # Get list of supplementary owner names. + # """ + # ownernames = loads(settings.Nordigen.OwnerNames) + # return ownernames + + async def get_account(self, account_id): + """ + Get details of an account. + :param requisition: requisition ID""" + + path = f"accounts/{account_id}/details" + response = await self.call(path, schema="AccountDetails") + 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:] + # if "ownerName" not in parsed: + # ownernames = self.get_ownernames() + # if account_id in ownernames: + # parsed["ownerName"] = ownernames[account_id] + + # else: + # return False + # recipient = parsed["ownerName"] + del parsed["bban"] + 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 + print("PARSED", parsed) + return parsed + + async def get_all_account_info(self, requisition=None): + print("GET ALL ACCOU T INFO", requisition) + to_return = {} + if not requisition: + raise NotImplementedError + # requisitions = await self.get_requisitions() + else: + requisitions = [await self.get_requisition(requisition)] + print("REQUISITIONS", requisitions) + + print("REQUISITIONS", requisitions) + + for req in requisitions: + print("REQ ITER", req) + accounts = req["accounts"] + print("ACCOUNTS", accounts) + for account_id in accounts: + account_info = await self.get_account(account_id) + if not account_info: + continue + if req["institution_id"] in to_return: + to_return[req["institution_id"]].append(account_info) + else: + to_return[req["institution_id"]] = [account_info] + return to_return diff --git a/core/templates/partials/aggregator-info.html b/core/templates/partials/aggregator-info.html index abce5bf..1669b7e 100644 --- a/core/templates/partials/aggregator-info.html +++ b/core/templates/partials/aggregator-info.html @@ -43,7 +43,7 @@ {% if type == 'page' %} -