feat: multi-account Fio sync + switch QR default to 2502035405/2010
All checks were successful
Deploy to K8s / deploy (push) Successful in 24s
All checks were successful
Deploy to K8s / deploy (push) Successful in 24s
Add second Fio account (CZ0820100000002502035405 / 2502035405/2010). Both accounts are fetched on every sync run and combined before dedup, so the payments sheet accumulates transactions from either account. QR codes now default to the new account. Go: - config.go: hardcoded Accounts/LoadedAccount slice replaces scalar BankAccount + FioAPIToken; Config.BankAccount renamed QRAccount; per-account tokens via FIO_API_TOKEN_NEW / FIO_API_TOKEN_OLD - banksync.SyncToSheets: accepts []fio.Client, loops to combine txns - cmd/fuj/main.go: buildFioClients helper; both sync call sites updated - html_handler + build_adults/juniors: use Config.QRAccount - New TestSyncToSheets_MultiAccount covers cross-account dedup Python: - config.py: ACCOUNTS list + LOADED_ACCOUNTS (tokens from env) - fio_utils.py: fetch_transactions_for (per-account) + fetch_transactions_all (loops all accounts) - sync_fio_to_sheets.py: uses fetch_transactions_all Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
This commit is contained in:
@@ -7,6 +7,28 @@ import (
|
||||
"time"
|
||||
)
|
||||
|
||||
// Account describes a Fio bank account.
|
||||
type Account struct {
|
||||
IBAN string // e.g. "CZ0820100000002502035405"
|
||||
AcctNum string // bare account number, e.g. "2502035405"
|
||||
TokenEnv string // env var name holding the optional Fio API token
|
||||
Primary bool // true for the account QR codes default to
|
||||
}
|
||||
|
||||
// LoadedAccount is an Account with its token resolved from the environment.
|
||||
type LoadedAccount struct {
|
||||
Account
|
||||
Token string // value of os.Getenv(Account.TokenEnv); empty → transparent-scraper path
|
||||
}
|
||||
|
||||
// Accounts is the hardcoded list of Fio bank accounts to sync from.
|
||||
// The first entry with Primary=true is used for QR codes.
|
||||
// Tokens are loaded at runtime from each account's TokenEnv.
|
||||
var Accounts = []Account{
|
||||
{IBAN: "CZ0820100000002502035405", AcctNum: "2502035405", TokenEnv: "FIO_API_TOKEN_NEW", Primary: true},
|
||||
{IBAN: "CZ8520100000002800359168", AcctNum: "2800359168", TokenEnv: "FIO_API_TOKEN_OLD"},
|
||||
}
|
||||
|
||||
// Google Sheets IDs — change in code if sheets change (not from env).
|
||||
const (
|
||||
AttendanceSheetID = "1E2e_gT_K5AwSRCDLDTa2UetZTkHmBOcz0kFbBUNUNBA"
|
||||
@@ -34,28 +56,36 @@ var CacheSheetMap = map[string]string{
|
||||
// Mirrors scripts/config.py.
|
||||
type Config struct {
|
||||
CredentialsPath string
|
||||
BankAccount string
|
||||
QRAccount string // IBAN of the primary account used for QR codes
|
||||
LoadedAccounts []LoadedAccount // all accounts to sync, tokens resolved from env
|
||||
CacheDir string
|
||||
CacheTTL time.Duration
|
||||
CacheAPICheckTTL time.Duration
|
||||
DriveTimeout time.Duration
|
||||
LogLevel string
|
||||
FioAPIToken string
|
||||
ServerAddr string
|
||||
}
|
||||
|
||||
// Load reads configuration from the environment, applying defaults that
|
||||
// match the Python side.
|
||||
func Load() Config {
|
||||
loaded := make([]LoadedAccount, len(Accounts))
|
||||
var qrAccount string
|
||||
for i, a := range Accounts {
|
||||
loaded[i] = LoadedAccount{Account: a, Token: os.Getenv(a.TokenEnv)}
|
||||
if a.Primary {
|
||||
qrAccount = a.IBAN
|
||||
}
|
||||
}
|
||||
return Config{
|
||||
CredentialsPath: env("CREDENTIALS_PATH", ".secret/fuj-management-bot-credentials.json"),
|
||||
BankAccount: env("BANK_ACCOUNT", "CZ8520100000002800359168"),
|
||||
QRAccount: qrAccount,
|
||||
LoadedAccounts: loaded,
|
||||
CacheDir: env("CACHE_DIR", "tmp/go"),
|
||||
CacheTTL: envDuration("CACHE_TTL_SECONDS", 300),
|
||||
CacheAPICheckTTL: envDuration("CACHE_API_CHECK_TTL_SECONDS", 300),
|
||||
DriveTimeout: envDuration("DRIVE_TIMEOUT_SECONDS", 10),
|
||||
LogLevel: env("LOG_LEVEL", "INFO"),
|
||||
FioAPIToken: env("FIO_API_TOKEN", ""),
|
||||
ServerAddr: env("SERVER_ADDR", ":8080"),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -35,13 +35,13 @@ type SyncOpts struct {
|
||||
PrintFioTable bool // with DryRun: print every fetched Fio txn with NEW/DUP status
|
||||
}
|
||||
|
||||
// SyncToSheets fetches Fio transactions and appends new ones to the payments sheet.
|
||||
// Returns the number of rows appended.
|
||||
// SyncToSheets fetches Fio transactions from all provided clients and appends
|
||||
// new ones to the payments sheet. Returns the number of rows appended.
|
||||
// Ports scripts/sync_fio_to_sheets.py sync_to_sheets.
|
||||
func SyncToSheets(
|
||||
ctx context.Context,
|
||||
spreadsheetID string,
|
||||
fioClient fio.Client,
|
||||
fioClients []fio.Client,
|
||||
sh sheetsWriter,
|
||||
opts SyncOpts,
|
||||
) (int, error) {
|
||||
@@ -84,10 +84,14 @@ func SyncToSheets(
|
||||
from = to.AddDate(0, 0, -days)
|
||||
}
|
||||
|
||||
// 3. Fetch Fio transactions.
|
||||
txns, err := fioClient.FetchTransactions(ctx, from, to)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("sync: fetch fio: %w", err)
|
||||
// 3. Fetch Fio transactions from each account and combine.
|
||||
var txns []fio.Transaction
|
||||
for _, client := range fioClients {
|
||||
got, err := client.FetchTransactions(ctx, from, to)
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("sync: fetch fio: %w", err)
|
||||
}
|
||||
txns = append(txns, got...)
|
||||
}
|
||||
if opts.DryRun {
|
||||
fmt.Printf("Dry run: window %s to %s, fetched %d transaction(s) from Fio\n",
|
||||
|
||||
@@ -20,7 +20,7 @@ func TestSyncToSheets_EmptySheet(t *testing.T) {
|
||||
}}
|
||||
fioFake := &fio.Fake{Transactions: testFioTxns}
|
||||
|
||||
n, err := SyncToSheets(context.Background(), "SHEETID", fioFake, sh, SyncOpts{Days: 30})
|
||||
n, err := SyncToSheets(context.Background(), "SHEETID", []fio.Client{fioFake}, sh, SyncOpts{Days: 30})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -51,7 +51,7 @@ func TestSyncToSheets_Dedup(t *testing.T) {
|
||||
}}
|
||||
fioFake := &fio.Fake{Transactions: testFioTxns}
|
||||
|
||||
n, err := SyncToSheets(context.Background(), "SHEETID", fioFake, sh, SyncOpts{Days: 30})
|
||||
n, err := SyncToSheets(context.Background(), "SHEETID", []fio.Client{fioFake}, sh, SyncOpts{Days: 30})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -72,7 +72,7 @@ func TestSyncToSheets_NoNewTxns(t *testing.T) {
|
||||
}}
|
||||
fioFake := &fio.Fake{Transactions: testFioTxns}
|
||||
|
||||
n, err := SyncToSheets(context.Background(), "SHEETID", fioFake, sh, SyncOpts{Days: 30})
|
||||
n, err := SyncToSheets(context.Background(), "SHEETID", []fio.Client{fioFake}, sh, SyncOpts{Days: 30})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -92,7 +92,7 @@ func TestSyncToSheets_MissingHeader(t *testing.T) {
|
||||
}}
|
||||
fioFake := &fio.Fake{Transactions: testFioTxns[:1]}
|
||||
|
||||
n, err := SyncToSheets(context.Background(), "SHEETID", fioFake, sh, SyncOpts{Days: 30})
|
||||
n, err := SyncToSheets(context.Background(), "SHEETID", []fio.Client{fioFake}, sh, SyncOpts{Days: 30})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -105,7 +105,7 @@ func TestSyncToSheets_Sort(t *testing.T) {
|
||||
sh := &sheets.Fake{Values: map[string][][]any{"SHEETID/A1:K": {}}}
|
||||
fioFake := &fio.Fake{Transactions: testFioTxns[:1]}
|
||||
|
||||
_, err := SyncToSheets(context.Background(), "SHEETID", fioFake, sh, SyncOpts{Days: 30, Sort: true})
|
||||
_, err := SyncToSheets(context.Background(), "SHEETID", []fio.Client{fioFake}, sh, SyncOpts{Days: 30, Sort: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -118,7 +118,7 @@ func TestSyncToSheets_ExplicitDateWindow(t *testing.T) {
|
||||
|
||||
from := time.Date(2026, 4, 1, 0, 0, 0, 0, time.UTC)
|
||||
to := time.Date(2026, 4, 30, 0, 0, 0, 0, time.UTC)
|
||||
n, err := SyncToSheets(context.Background(), "SHEETID", fioFake, sh, SyncOpts{From: from, To: to})
|
||||
n, err := SyncToSheets(context.Background(), "SHEETID", []fio.Client{fioFake}, sh, SyncOpts{From: from, To: to})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
@@ -131,7 +131,7 @@ func TestSyncToSheets_DryRun(t *testing.T) {
|
||||
sh := &sheets.Fake{Values: map[string][][]any{"SHEETID/A1:K": {}}}
|
||||
fioFake := &fio.Fake{Transactions: testFioTxns}
|
||||
|
||||
n, err := SyncToSheets(context.Background(), "SHEETID", fioFake, sh,
|
||||
n, err := SyncToSheets(context.Background(), "SHEETID", []fio.Client{fioFake}, sh,
|
||||
SyncOpts{Days: 30, Sort: true, DryRun: true})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
@@ -144,6 +144,40 @@ func TestSyncToSheets_DryRun(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncToSheets_MultiAccount(t *testing.T) {
|
||||
txnsA := []fio.Transaction{
|
||||
{Date: "2026-04-10", Amount: 700, Sender: "Alice", Message: "april", VS: "1", BankID: "A1"},
|
||||
}
|
||||
txnsB := []fio.Transaction{
|
||||
{Date: "2026-04-11", Amount: 500, Sender: "Bob", Message: "duben", VS: "2", BankID: "B1"},
|
||||
}
|
||||
// One transaction that duplicates the first one from account A (same sync_id).
|
||||
dupID := syncIDFor(txnsA[0])
|
||||
sh := &sheets.Fake{Values: map[string][][]any{
|
||||
"SHEETID/A1:K": {
|
||||
{"Date", "Amount", "manual fix", "Person", "Purpose", "Inferred Amount", "Sender", "VS", "Message", "Bank ID", "Sync ID"},
|
||||
{"2026-04-10", 700.0, "", "", "", "", "Alice", "1", "april", "A1", dupID},
|
||||
},
|
||||
}}
|
||||
fakeA := &fio.Fake{Transactions: txnsA}
|
||||
fakeB := &fio.Fake{Transactions: txnsB}
|
||||
|
||||
n, err := SyncToSheets(context.Background(), "SHEETID", []fio.Client{fakeA, fakeB}, sh, SyncOpts{Days: 30})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if n != 1 {
|
||||
t.Errorf("want 1 new row (B1 from account B; A1 is duplicate), got %d", n)
|
||||
}
|
||||
if len(sh.Appended) != 1 || len(sh.Appended[0].Rows) != 1 {
|
||||
t.Fatalf("want exactly 1 row appended, got %v", sh.Appended)
|
||||
}
|
||||
row := sh.Appended[0].Rows[0]
|
||||
if row[6] != "Bob" {
|
||||
t.Errorf("expected Bob's row, got sender=%v", row[6])
|
||||
}
|
||||
}
|
||||
|
||||
// syncIDFor mirrors what SyncToSheets computes for a given fio.Transaction.
|
||||
func syncIDFor(tx fio.Transaction) string {
|
||||
currency := tx.Currency
|
||||
|
||||
@@ -138,7 +138,7 @@ func buildAdultsResponse(
|
||||
Unmatched: unmatched,
|
||||
AttendanceURL: "https://docs.google.com/spreadsheets/d/" + config.AttendanceSheetID + "/edit",
|
||||
PaymentsURL: "https://docs.google.com/spreadsheets/d/" + config.PaymentsSheetID + "/edit",
|
||||
BankAccount: cfg.BankAccount,
|
||||
BankAccount: cfg.QRAccount,
|
||||
CurrentMonth: currentMonth,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -134,7 +134,7 @@ func buildJuniorsResponse(
|
||||
Unmatched: unmatched,
|
||||
AttendanceURL: juniorURL,
|
||||
PaymentsURL: "https://docs.google.com/spreadsheets/d/" + config.PaymentsSheetID + "/edit",
|
||||
BankAccount: cfg.BankAccount,
|
||||
BankAccount: cfg.QRAccount,
|
||||
CurrentMonth: currentMonth,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -126,13 +126,13 @@ func (h *HTMLHandler) ServeQR(w http.ResponseWriter, r *http.Request) {
|
||||
amount := q.Get("amount")
|
||||
message := q.Get("message")
|
||||
if account == "" {
|
||||
account = h.apiHandler.Config.BankAccount
|
||||
account = h.apiHandler.Config.QRAccount
|
||||
}
|
||||
if amount == "" {
|
||||
amount = "0"
|
||||
}
|
||||
|
||||
payload := BuildSPD(account, amount, message, h.apiHandler.Config.BankAccount)
|
||||
payload := BuildSPD(account, amount, message, h.apiHandler.Config.QRAccount)
|
||||
png, err := RenderQRCode(payload)
|
||||
if err != nil {
|
||||
http.Error(w, "qr encode: "+err.Error(), http.StatusInternalServerError)
|
||||
|
||||
@@ -50,7 +50,7 @@ func fixtureHandler(t *testing.T) *api.Handler {
|
||||
t.Helper()
|
||||
return &api.Handler{
|
||||
Sources: fixtureSources{},
|
||||
Config: config.Config{BankAccount: "CZ0000000000000000000000"},
|
||||
Config: config.Config{QRAccount: "CZ0000000000000000000000"},
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user