Files
WYGIWYH/app/apps/bank_sync/providers/base.py
T
Herculino Trotta 9238c48bb5 feat(bank-sync): add SimpleFIN provider
Users paste a one-time setup token. On save it's claimed and the access
URL it returns is stored; credentials go in a header, never the URL, so
errors don't leak them.

- Providers can validate or transform their settings (clean_settings),
  run by the service form once every other field is valid, so a token
  isn't spent on a form that fails anyway.
- Providers can declare a minimum sync interval. SimpleFIN Bridge
  disables tokens that go much past ~24 requests a day, so it's 6 hours.
- Tokens and access URLs are user input pointing the server at a host:
  only SimpleFIN Bridge is allowed unless SIMPLEFIN_ALLOWED_HOSTS says
  otherwise. Checked again on every request, for restored settings.
- Transactions are fetched per account in 90-day windows, Bridge's
  limit per request. Pending ones are skipped.
2026-11-08 16:08:37 -03:00

60 lines
1.7 KiB
Python

from abc import ABC, abstractmethod
from dataclasses import dataclass
from datetime import date
from decimal import Decimal
@dataclass(frozen=True)
class RemoteAccount:
id: str
name: str
# Currency codes as the provider spells them
currencies: tuple = ()
# Why the account can't be synced right now (expired connection...), empty if fine
error: str = ""
@dataclass(frozen=True)
class RemoteTransaction:
id: str
date: date
# Signed: negative is money leaving the account
amount: Decimal
description: str = ""
notes: str = ""
currency: str = ""
class BankSyncProvider(ABC):
"""A bank data aggregator.
``settings_fields`` declares the provider's own configuration as Django form
fields. The service form renders them when the provider is selected and the
values end up in ``BankSyncService.settings`` under the same keys.
"""
settings_fields: dict = {}
# In hours, for services that limit how often they can be called
min_sync_interval: int = 1
def __init__(self, settings: dict):
self.settings = settings or {}
@classmethod
def clean_settings(cls, settings: dict) -> dict:
"""Validate or transform the settings before they're saved.
Runs once every other form field is valid. Raise ValidationError to reject.
"""
return settings
@abstractmethod
def list_accounts(self) -> list[RemoteAccount]:
"""Every account the credentials can see."""
@abstractmethod
def get_transactions(
self, account_id: str, start_date: date
) -> list[RemoteTransaction]:
"""Posted transactions of ``account_id`` dated ``start_date`` or later."""