init
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
package app
|
||||
|
||||
import (
|
||||
"slices"
|
||||
"time"
|
||||
|
||||
"finance-duck/internal/banking"
|
||||
"finance-duck/internal/domain"
|
||||
)
|
||||
|
||||
type authorization struct {
|
||||
Expires time.Time
|
||||
Institution string
|
||||
Country string
|
||||
}
|
||||
type Consent struct {
|
||||
Institution string `json:"institution"`
|
||||
Country string `json:"country"`
|
||||
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"`
|
||||
Status string `json:"status"`
|
||||
ValidUntil string `json:"valid_until"`
|
||||
Error string `json:"error"`
|
||||
}
|
||||
|
||||
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", 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]
|
||||
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
|
||||
}
|
||||
Reference in New Issue
Block a user