feat(#35): rendimento CDI automático em contas de investimento

- Migration 010: colunas yield_type e last_yield_date em accounts + source='yield' nas transações
- CDIYieldService: busca taxas BCB (serie 12) e cria transação source=yield com rendimento acumulado
- Weekends/feriados sem taxa usam última taxa disponível (AC4)
- GET /api/accounts dispara cálculo CDI on-demand antes de retornar saldos
- AccountsView: seletor yield_type (none/cdi/variable) + badge CDI/VAR na listagem
- Transações source=yield aparecem no histórico com descrição 'Rendimento CDI — {período}'

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
2026-05-27 15:06:05 -03:00
co-authored by Claude Sonnet 4.6
parent 79f4a62a1b
commit ee6cd9f37e
9 changed files with 249 additions and 27 deletions
+38 -13
View File
@@ -8,6 +8,17 @@ import (
"financeiro-carvalho/internal/model"
)
// AccountRepoWithYield extends the basic account repo with CDI-related operations.
type AccountRepoWithYield interface {
List(ctx context.Context) ([]model.Account, error)
GetByID(ctx context.Context, id int) (*model.Account, error)
Create(ctx context.Context, in model.AccountInput) (*model.Account, error)
Update(ctx context.Context, id int, in model.AccountInput) (*model.Account, error)
Delete(ctx context.Context, id int) error
TotalPatrimony(ctx context.Context) (float64, error)
UpdateLastYieldDate(ctx context.Context, id int, date string) error
}
type AccountRepository struct {
pool *pgxpool.Pool
}
@@ -20,6 +31,7 @@ func (r *AccountRepository) List(ctx context.Context) ([]model.Account, error) {
rows, err := r.pool.Query(ctx, `
SELECT
a.id, a.name, a.type, a.initial_balance,
a.yield_type, a.last_yield_date::text,
a.created_at::text, a.updated_at::text,
a.initial_balance
+ COALESCE(SUM(CASE WHEN t.type = 'income' THEN t.amount ELSE 0 END), 0)
@@ -38,7 +50,7 @@ func (r *AccountRepository) List(ctx context.Context) ([]model.Account, error) {
var out []model.Account
for rows.Next() {
var a model.Account
if err := rows.Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.CreatedAt, &a.UpdatedAt, &a.Balance); err != nil {
if err := rows.Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.CreatedAt, &a.UpdatedAt, &a.Balance); err != nil {
return nil, err
}
out = append(out, a)
@@ -51,6 +63,7 @@ func (r *AccountRepository) GetByID(ctx context.Context, id int) (*model.Account
err := r.pool.QueryRow(ctx, `
SELECT
a.id, a.name, a.type, a.initial_balance,
a.yield_type, a.last_yield_date::text,
a.created_at::text, a.updated_at::text,
a.initial_balance
+ COALESCE(SUM(CASE WHEN t.type = 'income' THEN t.amount ELSE 0 END), 0)
@@ -60,7 +73,7 @@ func (r *AccountRepository) GetByID(ctx context.Context, id int) (*model.Account
LEFT JOIN transactions t ON t.account_id = a.id
WHERE a.id = $1
GROUP BY a.id
`, id).Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.CreatedAt, &a.UpdatedAt, &a.Balance)
`, id).Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.CreatedAt, &a.UpdatedAt, &a.Balance)
if err != nil {
return nil, err
}
@@ -68,13 +81,17 @@ func (r *AccountRepository) GetByID(ctx context.Context, id int) (*model.Account
}
func (r *AccountRepository) Create(ctx context.Context, in model.AccountInput) (*model.Account, error) {
yt := in.YieldType
if yt == "" {
yt = "none"
}
var a model.Account
err := r.pool.QueryRow(ctx, `
INSERT INTO accounts (name, type, initial_balance)
VALUES ($1, $2, $3)
RETURNING id, name, type, initial_balance, created_at::text, updated_at::text
`, in.Name, in.Type, in.InitialBalance).
Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.CreatedAt, &a.UpdatedAt)
INSERT INTO accounts (name, type, initial_balance, yield_type)
VALUES ($1, $2, $3, $4)
RETURNING id, name, type, initial_balance, yield_type, last_yield_date::text, created_at::text, updated_at::text
`, in.Name, in.Type, in.InitialBalance, yt).
Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.CreatedAt, &a.UpdatedAt)
if err != nil {
return nil, err
}
@@ -83,16 +100,19 @@ func (r *AccountRepository) Create(ctx context.Context, in model.AccountInput) (
}
func (r *AccountRepository) Update(ctx context.Context, id int, in model.AccountInput) (*model.Account, error) {
yt := in.YieldType
if yt == "" {
yt = "none"
}
row := r.pool.QueryRow(ctx, `
UPDATE accounts SET name=$1, type=$2, initial_balance=$3, updated_at=NOW()
WHERE id=$4
RETURNING id, name, type, initial_balance, created_at::text, updated_at::text
`, in.Name, in.Type, in.InitialBalance, id)
UPDATE accounts SET name=$1, type=$2, initial_balance=$3, yield_type=$4, updated_at=NOW()
WHERE id=$5
RETURNING id, name, type, initial_balance, yield_type, last_yield_date::text, created_at::text, updated_at::text
`, in.Name, in.Type, in.InitialBalance, yt, id)
var a model.Account
if err := row.Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.CreatedAt, &a.UpdatedAt); err != nil {
if err := row.Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.CreatedAt, &a.UpdatedAt); err != nil {
return nil, err
}
// recalculate balance
full, err := r.GetByID(ctx, a.ID)
if err != nil {
return nil, err
@@ -119,3 +139,8 @@ func (r *AccountRepository) TotalPatrimony(ctx context.Context) (float64, error)
`).Scan(&total)
return total, err
}
func (r *AccountRepository) UpdateLastYieldDate(ctx context.Context, id int, date string) error {
_, err := r.pool.Exec(ctx, `UPDATE accounts SET last_yield_date = $1 WHERE id = $2`, date, id)
return err
}