90 lines
2.4 KiB
Go
90 lines
2.4 KiB
Go
package app
|
|
|
|
import (
|
|
"slices"
|
|
"time"
|
|
|
|
"finance-duck/internal/banking"
|
|
"finance-duck/internal/domain"
|
|
)
|
|
|
|
const defaultHistoryMonths = 12
|
|
|
|
type authorization struct {
|
|
Expires time.Time
|
|
Institution string
|
|
Country string
|
|
HistoryMonths int
|
|
}
|
|
type Consent struct {
|
|
Institution string `json:"institution"`
|
|
Country string `json:"country"`
|
|
HistoryMonths int `json:"history_months"`
|
|
Error string `json:"error,omitempty"`
|
|
NeedsReconnect bool `json:"needs_reconnect"`
|
|
}
|
|
type Connection struct {
|
|
AccountID string `json:"account_id"`
|
|
Institution string `json:"institution"`
|
|
Country string `json:"country"`
|
|
HistoryMonths int `json:"history_months"`
|
|
Status string `json:"status"`
|
|
ValidUntil string `json:"valid_until"`
|
|
Error string `json:"error"`
|
|
}
|
|
|
|
func (c Consent) historyMonths() int {
|
|
if c.HistoryMonths == 0 {
|
|
return defaultHistoryMonths
|
|
}
|
|
return c.HistoryMonths
|
|
}
|
|
|
|
func (a *App) connections(d domain.Dataset) []Connection {
|
|
out := make([]Connection, 0, len(d.Accounts))
|
|
for _, account := range d.Accounts {
|
|
c := Connection{AccountID: account.ID, Institution: account.Institution, Country: "DE", HistoryMonths: defaultHistoryMonths, Status: "local"}
|
|
if account.ExternalAccountID != "" {
|
|
c.Status = "reconnect_required"
|
|
c.Error = "No saved bank consent; reconnect this account"
|
|
}
|
|
for _, session := range a.ops.Sessions {
|
|
for _, linked := range session.Accounts {
|
|
if linked.ID != account.ID {
|
|
continue
|
|
}
|
|
meta := a.ops.Consents[session.ID]
|
|
c.HistoryMonths = meta.historyMonths()
|
|
if meta.Institution != "" {
|
|
c.Institution = meta.Institution
|
|
}
|
|
if meta.Country != "" {
|
|
c.Country = meta.Country
|
|
}
|
|
c.ValidUntil = session.ValidUntil
|
|
c.Error = meta.Error
|
|
c.Status = "connected"
|
|
expiry, err := time.Parse(time.RFC3339, session.ValidUntil)
|
|
if meta.NeedsReconnect || err != nil || !expiry.After(time.Now()) {
|
|
c.Status = "reconnect_required"
|
|
if c.Error == "" {
|
|
c.Error = "Bank consent expired; reconnect to resume automatic imports"
|
|
}
|
|
} else if meta.Error != "" {
|
|
c.Status = "error"
|
|
}
|
|
}
|
|
}
|
|
out = append(out, c)
|
|
}
|
|
return out
|
|
}
|
|
|
|
func copySessions(sessions []banking.Session) []banking.Session {
|
|
out := append([]banking.Session{}, sessions...)
|
|
for i := range out {
|
|
out[i].Accounts = slices.Clone(out[i].Accounts)
|
|
}
|
|
return out
|
|
}
|