feat: CDI % configurável por conta + fix mocks de teste

- Adiciona campo cdi_percentage (default 100%) na tabela accounts
- Cálculo CDI aplica proporção: balance × (compound-1) × (pct/100)
- Frontend exibe input de % quando yield_type=cdi e badge mostra valor
- Corrige mocks de teste desatualizados (DeleteByMonth, isTax)
- Corrige ConfirmIncome para rejeitar tipo expense (ErrRecurringNotIncome)

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
2026-05-28 22:20:32 -03:00
co-authored by Claude Sonnet 4.6
parent 433c4f42a5
commit 6292858970
10 changed files with 73 additions and 37 deletions
+4 -1
View File
@@ -50,6 +50,9 @@ var m013 string
//go:embed sql/014_profiles.sql //go:embed sql/014_profiles.sql
var m014 string var m014 string
//go:embed sql/015_accounts_cdi_percentage.sql
var m015 string
func Run(ctx context.Context, pool *pgxpool.Pool) error { func Run(ctx context.Context, pool *pgxpool.Pool) error {
// Bootstrap: ensure schema_migrations table exists before checking versions. // Bootstrap: ensure schema_migrations table exists before checking versions.
if _, err := pool.Exec(ctx, ` if _, err := pool.Exec(ctx, `
@@ -61,7 +64,7 @@ func Run(ctx context.Context, pool *pgxpool.Pool) error {
return fmt.Errorf("bootstrap schema_migrations: %w", err) return fmt.Errorf("bootstrap schema_migrations: %w", err)
} }
migrations := []string{m001, m002, m003, m004, m005, m006, m007, m008, m009, m010, m011, m012, m013, m014} migrations := []string{m001, m002, m003, m004, m005, m006, m007, m008, m009, m010, m011, m012, m013, m014, m015}
for i, sql := range migrations { for i, sql := range migrations {
version := i + 1 version := i + 1
var applied bool var applied bool
@@ -0,0 +1,2 @@
ALTER TABLE accounts
ADD COLUMN IF NOT EXISTS cdi_percentage NUMERIC(5,2) NOT NULL DEFAULT 100;
+2
View File
@@ -7,6 +7,7 @@ type Account struct {
InitialBalance float64 `json:"initial_balance"` InitialBalance float64 `json:"initial_balance"`
Balance float64 `json:"balance"` Balance float64 `json:"balance"`
YieldType string `json:"yield_type"` // "none" | "cdi" | "variable" YieldType string `json:"yield_type"` // "none" | "cdi" | "variable"
CDIPercentage float64 `json:"cdi_percentage"` // % do CDI que a conta rende (default 100)
LastYieldDate *string `json:"last_yield_date,omitempty"` LastYieldDate *string `json:"last_yield_date,omitempty"`
ClosingDay *int `json:"closing_day,omitempty"` ClosingDay *int `json:"closing_day,omitempty"`
DueDay *int `json:"due_day,omitempty"` DueDay *int `json:"due_day,omitempty"`
@@ -20,6 +21,7 @@ type AccountInput struct {
Type string `json:"type"` Type string `json:"type"`
InitialBalance float64 `json:"initial_balance"` InitialBalance float64 `json:"initial_balance"`
YieldType string `json:"yield_type"` // "none" | "cdi" | "variable" YieldType string `json:"yield_type"` // "none" | "cdi" | "variable"
CDIPercentage float64 `json:"cdi_percentage"` // % do CDI (default 100)
ClosingDay *int `json:"closing_day,omitempty"` ClosingDay *int `json:"closing_day,omitempty"`
DueDay *int `json:"due_day,omitempty"` DueDay *int `json:"due_day,omitempty"`
} }
+22 -14
View File
@@ -33,7 +33,7 @@ func (r *AccountRepository) List(ctx context.Context) ([]model.Account, error) {
rows, err := r.pool.Query(ctx, ` rows, err := r.pool.Query(ctx, `
SELECT SELECT
a.id, a.name, a.type, a.initial_balance, a.id, a.name, a.type, a.initial_balance,
a.yield_type, a.last_yield_date::text, a.yield_type, a.cdi_percentage, a.last_yield_date::text,
a.closing_day, a.due_day, a.closing_day, a.due_day,
a.created_at::text, a.updated_at::text, a.created_at::text, a.updated_at::text,
a.initial_balance a.initial_balance
@@ -54,7 +54,7 @@ func (r *AccountRepository) List(ctx context.Context) ([]model.Account, error) {
var out []model.Account var out []model.Account
for rows.Next() { for rows.Next() {
var a model.Account var a model.Account
if err := rows.Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.ClosingDay, &a.DueDay, &a.CreatedAt, &a.UpdatedAt, &a.Balance); err != nil { if err := rows.Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.CDIPercentage, &a.LastYieldDate, &a.ClosingDay, &a.DueDay, &a.CreatedAt, &a.UpdatedAt, &a.Balance); err != nil {
return nil, err return nil, err
} }
out = append(out, a) out = append(out, a)
@@ -68,7 +68,7 @@ func (r *AccountRepository) GetByID(ctx context.Context, id int) (*model.Account
err := r.pool.QueryRow(ctx, ` err := r.pool.QueryRow(ctx, `
SELECT SELECT
a.id, a.name, a.type, a.initial_balance, a.id, a.name, a.type, a.initial_balance,
a.yield_type, a.last_yield_date::text, a.yield_type, a.cdi_percentage, a.last_yield_date::text,
a.closing_day, a.due_day, a.closing_day, a.due_day,
a.created_at::text, a.updated_at::text, a.created_at::text, a.updated_at::text,
a.initial_balance a.initial_balance
@@ -79,7 +79,7 @@ func (r *AccountRepository) GetByID(ctx context.Context, id int) (*model.Account
LEFT JOIN transactions t ON t.account_id = a.id LEFT JOIN transactions t ON t.account_id = a.id
WHERE a.id = $1 AND a.profile_id = $2 WHERE a.id = $1 AND a.profile_id = $2
GROUP BY a.id GROUP BY a.id
`, id, pid).Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.ClosingDay, &a.DueDay, &a.CreatedAt, &a.UpdatedAt, &a.Balance) `, id, pid).Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.CDIPercentage, &a.LastYieldDate, &a.ClosingDay, &a.DueDay, &a.CreatedAt, &a.UpdatedAt, &a.Balance)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -92,13 +92,17 @@ func (r *AccountRepository) Create(ctx context.Context, in model.AccountInput) (
if yt == "" { if yt == "" {
yt = "none" yt = "none"
} }
cdiPct := in.CDIPercentage
if cdiPct <= 0 {
cdiPct = 100
}
var a model.Account var a model.Account
err := r.pool.QueryRow(ctx, ` err := r.pool.QueryRow(ctx, `
INSERT INTO accounts (name, type, initial_balance, yield_type, closing_day, due_day, profile_id) INSERT INTO accounts (name, type, initial_balance, yield_type, cdi_percentage, closing_day, due_day, profile_id)
VALUES ($1, $2, $3, $4, $5, $6, $7) VALUES ($1, $2, $3, $4, $5, $6, $7, $8)
RETURNING id, name, type, initial_balance, yield_type, last_yield_date::text, closing_day, due_day, created_at::text, updated_at::text RETURNING id, name, type, initial_balance, yield_type, cdi_percentage, last_yield_date::text, closing_day, due_day, created_at::text, updated_at::text
`, in.Name, in.Type, in.InitialBalance, yt, in.ClosingDay, in.DueDay, pid). `, in.Name, in.Type, in.InitialBalance, yt, cdiPct, in.ClosingDay, in.DueDay, pid).
Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.ClosingDay, &a.DueDay, &a.CreatedAt, &a.UpdatedAt) Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.CDIPercentage, &a.LastYieldDate, &a.ClosingDay, &a.DueDay, &a.CreatedAt, &a.UpdatedAt)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@@ -112,13 +116,17 @@ func (r *AccountRepository) Update(ctx context.Context, id int, in model.Account
if yt == "" { if yt == "" {
yt = "none" yt = "none"
} }
cdiPct := in.CDIPercentage
if cdiPct <= 0 {
cdiPct = 100
}
row := r.pool.QueryRow(ctx, ` row := r.pool.QueryRow(ctx, `
UPDATE accounts SET name=$1, type=$2, initial_balance=$3, yield_type=$4, closing_day=$5, due_day=$6, updated_at=NOW() UPDATE accounts SET name=$1, type=$2, initial_balance=$3, yield_type=$4, cdi_percentage=$5, closing_day=$6, due_day=$7, updated_at=NOW()
WHERE id=$7 AND profile_id=$8 WHERE id=$8 AND profile_id=$9
RETURNING id, name, type, initial_balance, yield_type, last_yield_date::text, closing_day, due_day, created_at::text, updated_at::text RETURNING id, name, type, initial_balance, yield_type, cdi_percentage, last_yield_date::text, closing_day, due_day, created_at::text, updated_at::text
`, in.Name, in.Type, in.InitialBalance, yt, in.ClosingDay, in.DueDay, id, pid) `, in.Name, in.Type, in.InitialBalance, yt, cdiPct, in.ClosingDay, in.DueDay, id, pid)
var a model.Account var a model.Account
if err := row.Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.LastYieldDate, &a.ClosingDay, &a.DueDay, &a.CreatedAt, &a.UpdatedAt); err != nil { if err := row.Scan(&a.ID, &a.Name, &a.Type, &a.InitialBalance, &a.YieldType, &a.CDIPercentage, &a.LastYieldDate, &a.ClosingDay, &a.DueDay, &a.CreatedAt, &a.UpdatedAt); err != nil {
return nil, err return nil, err
} }
full, err := r.GetByID(ctx, a.ID) full, err := r.GetByID(ctx, a.ID)
+2 -2
View File
@@ -33,13 +33,13 @@ func (m *mockCategoryRepo) GetByID(_ context.Context, id int) (*model.Category,
return nil, repository.ErrNotFound return nil, repository.ErrNotFound
} }
func (m *mockCategoryRepo) Create(_ context.Context, name, color string) (*model.Category, error) { func (m *mockCategoryRepo) Create(_ context.Context, name, color string, _ bool) (*model.Category, error) {
c := model.Category{ID: len(m.categories) + 1, Name: name, Color: color} c := model.Category{ID: len(m.categories) + 1, Name: name, Color: color}
m.categories = append(m.categories, c) m.categories = append(m.categories, c)
return &c, nil return &c, nil
} }
func (m *mockCategoryRepo) Update(_ context.Context, id int, name, color string) (*model.Category, error) { func (m *mockCategoryRepo) Update(_ context.Context, id int, name, color string, _ bool) (*model.Category, error) {
for i, c := range m.categories { for i, c := range m.categories {
if c.ID == id { if c.ID == id {
m.categories[i].Name = name m.categories[i].Name = name
+5 -1
View File
@@ -78,7 +78,11 @@ func (s *CDIYieldService) ApplyYield(ctx context.Context, a *model.Account) erro
compound *= 1 + lastRate/100 compound *= 1 + lastRate/100
} }
yieldAmount := a.Balance * (compound - 1) cdiPct := a.CDIPercentage
if cdiPct <= 0 {
cdiPct = 100
}
yieldAmount := a.Balance * (compound - 1) * (cdiPct / 100)
if yieldAmount <= 0 { if yieldAmount <= 0 {
return nil return nil
} }
+4 -4
View File
@@ -110,16 +110,16 @@ func (s *RecurringService) MonthlyStatus(ctx context.Context, month string) ([]m
return result, nil return result, nil
} }
// ConfirmIncome creates a transaction (income or expense) confirming this recurring item for the month. // ConfirmIncome creates a transaction confirming an income recurring item for the month.
func (s *RecurringService) ConfirmIncome(ctx context.Context, id int, month string) error { func (s *RecurringService) ConfirmIncome(ctx context.Context, id int, month string) error {
re, err := s.repo.GetByID(ctx, id) re, err := s.repo.GetByID(ctx, id)
if err != nil { if err != nil {
return err return err
} }
txType := re.Type if re.Type != "income" {
if txType == "" { return ErrRecurringNotIncome
txType = "expense"
} }
txType := re.Type
today := time.Now().Format("2006-01-02") today := time.Now().Format("2006-01-02")
tx := model.Transaction{ tx := model.Transaction{
Date: today, Date: today,
@@ -90,6 +90,7 @@ func (m *mockTxRepo) Update(_ context.Context, t model.Transaction) (*model.Tran
return &t, nil return &t, nil
} }
func (m *mockTxRepo) Delete(_ context.Context, _ int) error { return nil } func (m *mockTxRepo) Delete(_ context.Context, _ int) error { return nil }
func (m *mockTxRepo) DeleteByMonth(_ context.Context, _ string) (int, error) { return 0, nil }
func (m *mockTxRepo) HasMatchingTransaction(_ context.Context, _ *int, _ string, _ float64, _ string) (bool, error) { func (m *mockTxRepo) HasMatchingTransaction(_ context.Context, _ *int, _ string, _ float64, _ string) (bool, error) {
return m.matchResult, nil return m.matchResult, nil
} }
+2
View File
@@ -22,6 +22,7 @@ export interface Account {
initial_balance: number initial_balance: number
balance: number balance: number
yield_type: 'none' | 'cdi' | 'variable' yield_type: 'none' | 'cdi' | 'variable'
cdi_percentage: number
last_yield_date?: string last_yield_date?: string
closing_day?: number closing_day?: number
due_day?: number due_day?: number
@@ -35,6 +36,7 @@ export interface AccountInput {
type: string type: string
initial_balance: number initial_balance: number
yield_type: string yield_type: string
cdi_percentage: number
closing_day?: number | null closing_day?: number | null
due_day?: number | null due_day?: number | null
} }
+16 -2
View File
@@ -14,7 +14,7 @@ const typeLabels: Record<string, string> = {
credit: 'Cartão de Crédito', credit: 'Cartão de Crédito',
} }
const blank = (): AccountInput => ({ name: '', type: 'checking', initial_balance: 0, yield_type: 'none', closing_day: null, due_day: null }) const blank = (): AccountInput => ({ name: '', type: 'checking', initial_balance: 0, yield_type: 'none', cdi_percentage: 100, closing_day: null, due_day: null })
const form = ref(blank()) const form = ref(blank())
const editId = ref<number | null>(null) const editId = ref<number | null>(null)
const formError = ref<string | null>(null) const formError = ref<string | null>(null)
@@ -26,6 +26,7 @@ function startEdit(id: number) {
form.value = { form.value = {
name: a.name, type: a.type, initial_balance: a.initial_balance, name: a.name, type: a.type, initial_balance: a.initial_balance,
yield_type: a.yield_type ?? 'none', yield_type: a.yield_type ?? 'none',
cdi_percentage: a.cdi_percentage ?? 100,
closing_day: a.closing_day ?? null, closing_day: a.closing_day ?? null,
due_day: a.due_day ?? null, due_day: a.due_day ?? null,
} }
@@ -92,6 +93,17 @@ function totalBalance() {
<option value="cdi">CDI automático</option> <option value="cdi">CDI automático</option>
<option value="variable">Renda variável</option> <option value="variable">Renda variável</option>
</select> </select>
<div v-if="form.yield_type === 'cdi'" class="fc-label fc-acc-form__day-wrap">
% do CDI
<input
type="number"
v-model.number="form.cdi_percentage"
min="1"
max="200"
step="0.5"
class="fc-input fc-acc-form__day"
/>
</div>
<template v-if="form.type === 'credit'"> <template v-if="form.type === 'credit'">
<div class="fc-label fc-acc-form__day-wrap"> <div class="fc-label fc-acc-form__day-wrap">
Fechamento Fechamento
@@ -138,7 +150,9 @@ function totalBalance() {
<div class="fc-acc-item__info"> <div class="fc-acc-item__info">
<div class="fc-acc-item__name-row"> <div class="fc-acc-item__name-row">
<span class="fc-body fc-acc-item__name">{{ a.name }}</span> <span class="fc-body fc-acc-item__name">{{ a.name }}</span>
<span v-if="a.yield_type === 'cdi'" class="fc-acc-badge fc-acc-badge--cdi fc-pixel">CDI</span> <span v-if="a.yield_type === 'cdi'" class="fc-acc-badge fc-acc-badge--cdi fc-pixel">
{{ a.cdi_percentage != null && a.cdi_percentage !== 100 ? a.cdi_percentage + '% CDI' : 'CDI' }}
</span>
<span v-else-if="a.yield_type === 'variable'" class="fc-acc-badge fc-acc-badge--var fc-pixel">VAR</span> <span v-else-if="a.yield_type === 'variable'" class="fc-acc-badge fc-acc-badge--var fc-pixel">VAR</span>
</div> </div>
<span class="fc-mono fc-acc-item__meta"> <span class="fc-mono fc-acc-item__meta">