mirror of
https://github.com/eitchtee/WYGIWYH.git
synced 2026-11-22 00:21:50 +01:00
Provider registry modeled after automatic exchange rates: each provider declares its own settings fields, rendered dynamically in the service form. Lunch Flow is the first provider. - Map provider accounts to WYGIWYH accounts with a start date and an optional currency filter - Hourly task syncs due services; manual sync from the list - Deduplicates through Transaction.internal_id, optionally matching transactions already entered by hand - Per account errors and provider reported connection problems Refs #498
102 lines
3.8 KiB
Python
102 lines
3.8 KiB
Python
import os
|
|
from datetime import date
|
|
from decimal import Decimal
|
|
|
|
import requests
|
|
from django import forms
|
|
from django.utils.translation import gettext as _
|
|
from django.utils.translation import gettext_lazy
|
|
|
|
from apps.bank_sync.providers.base import (
|
|
BankSyncProvider,
|
|
RemoteAccount,
|
|
RemoteTransaction,
|
|
)
|
|
|
|
DEFAULT_BASE_URL = "https://www.lunchflow.app/api/v1"
|
|
|
|
|
|
class LunchFlowProvider(BankSyncProvider):
|
|
"""https://www.lunchflow.app/docs/api/personal-api-overview"""
|
|
|
|
settings_fields = {
|
|
"api_key": forms.CharField(
|
|
label=gettext_lazy("API Key"),
|
|
# Not a PasswordInput: browsers autofill those with saved logins
|
|
widget=forms.TextInput(attrs={"autocomplete": "off"}),
|
|
help_text=gettext_lazy(
|
|
"Create a REST API destination on Lunch Flow and give it access to "
|
|
"the accounts you want to sync."
|
|
),
|
|
),
|
|
}
|
|
|
|
def __init__(self, settings: dict):
|
|
super().__init__(settings)
|
|
# Server side only, users must not point the server at arbitrary hosts
|
|
self.base_url = os.getenv("LUNCHFLOW_API_URL", DEFAULT_BASE_URL).rstrip("/")
|
|
self.session = requests.Session()
|
|
self.session.headers["x-api-key"] = self.settings.get("api_key", "")
|
|
|
|
def _get(self, path: str, **params) -> dict:
|
|
response = self.session.get(f"{self.base_url}{path}", params=params, timeout=30)
|
|
response.raise_for_status()
|
|
return response.json()
|
|
|
|
def _balance_currency(self, account_id) -> str:
|
|
# The account list has no currency, the balance does
|
|
try:
|
|
return self._get(f"/accounts/{account_id}/balance")["balance"]["currency"]
|
|
except (requests.RequestException, ValueError, KeyError, TypeError):
|
|
return ""
|
|
|
|
def list_accounts(self) -> list[RemoteAccount]:
|
|
accounts = []
|
|
for account in self._get("/accounts")["accounts"]:
|
|
currency = account.get("currency") or self._balance_currency(account["id"])
|
|
status = account.get("status") or "ACTIVE"
|
|
accounts.append(
|
|
RemoteAccount(
|
|
id=str(account["id"]),
|
|
name=" - ".join(
|
|
filter(None, [account.get("institution_name"), account["name"]])
|
|
),
|
|
currencies=(currency.upper(),) if currency else (),
|
|
error=(
|
|
""
|
|
if status == "ACTIVE"
|
|
else _(
|
|
"Lunch Flow reports this account as %(status)s, "
|
|
"check the connection on Lunch Flow."
|
|
)
|
|
% {"status": status}
|
|
),
|
|
)
|
|
)
|
|
return accounts
|
|
|
|
def get_transactions(
|
|
self, account_id: str, start_date: date
|
|
) -> list[RemoteTransaction]:
|
|
data = self._get(
|
|
f"/accounts/{account_id}/transactions", **{"from": start_date.isoformat()}
|
|
)
|
|
transactions = []
|
|
for tx in data["transactions"]:
|
|
# Pending ones are excluded by default, but they change ids when posted
|
|
if tx.get("isPending"):
|
|
continue
|
|
merchant = (tx.get("merchant") or "").strip()
|
|
description = (tx.get("description") or "").strip()
|
|
transactions.append(
|
|
RemoteTransaction(
|
|
id=str(tx["id"]),
|
|
date=date.fromisoformat(tx["date"][:10]),
|
|
amount=Decimal(str(tx["amount"])),
|
|
description=merchant or description,
|
|
notes=description if merchant and description != merchant else "",
|
|
currency=(tx.get("currency") or "").upper(),
|
|
)
|
|
)
|
|
return transactions
|