feat(#36): receitas recorrentes com confirmação de salário até dia 5
- Migration 009: coluna `type` em recurring_expenses + tabela recurring_late - API Go: tipo income/expense em CRUD; endpoints POST /confirm e /late - Dashboard: campo pending_income_recurrings na resposta - Frontend: RecurringView com seções RECEITAS e DESPESAS separadas - HomeView: widget de confirmação (dia 1-5) e badge SALÁRIO PENDENTE (pós dia 5) - Testes unitários: 4 novos casos (income covered, late, confirm, reject expense) Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
@@ -14,10 +14,11 @@ import (
|
||||
type mockRecurringRepo struct {
|
||||
items []model.RecurringExpense
|
||||
ignores map[string]string // "id:month" → reason
|
||||
lates map[string]bool // "id:month" → true
|
||||
}
|
||||
|
||||
func newMockRecurring(items []model.RecurringExpense) *mockRecurringRepo {
|
||||
return &mockRecurringRepo{items: items, ignores: map[string]string{}}
|
||||
return &mockRecurringRepo{items: items, ignores: map[string]string{}, lates: map[string]bool{}}
|
||||
}
|
||||
|
||||
func (m *mockRecurringRepo) List(_ context.Context) ([]model.RecurringExpense, error) {
|
||||
@@ -33,7 +34,7 @@ func (m *mockRecurringRepo) GetByID(_ context.Context, id int) (*model.Recurring
|
||||
return nil, repository.ErrNotFound
|
||||
}
|
||||
func (m *mockRecurringRepo) Create(_ context.Context, in model.RecurringInput) (*model.RecurringExpense, error) {
|
||||
r := model.RecurringExpense{ID: len(m.items) + 1, Name: in.Name, ExpectedAmount: in.ExpectedAmount, DayOfMonth: in.DayOfMonth, Active: true}
|
||||
r := model.RecurringExpense{ID: len(m.items) + 1, Name: in.Name, ExpectedAmount: in.ExpectedAmount, DayOfMonth: in.DayOfMonth, Type: in.Type, Active: true}
|
||||
m.items = append(m.items, r)
|
||||
return &r, nil
|
||||
}
|
||||
@@ -62,6 +63,17 @@ func (m *mockRecurringRepo) Unignore(_ context.Context, id int, month string) er
|
||||
delete(m.ignores, string(rune(id))+":"+month)
|
||||
return nil
|
||||
}
|
||||
func (m *mockRecurringRepo) IsLate(_ context.Context, id int, month string) (bool, error) {
|
||||
return m.lates[string(rune(id))+":"+month], nil
|
||||
}
|
||||
func (m *mockRecurringRepo) MarkLate(_ context.Context, id int, month string) error {
|
||||
m.lates[string(rune(id))+":"+month] = true
|
||||
return nil
|
||||
}
|
||||
func (m *mockRecurringRepo) UnmarkLate(_ context.Context, id int, month string) error {
|
||||
delete(m.lates, string(rune(id))+":"+month)
|
||||
return nil
|
||||
}
|
||||
|
||||
// ── mock tx repo ─────────────────────────────────────────────────────────────
|
||||
|
||||
@@ -78,7 +90,7 @@ func (m *mockTxRepo) Update(_ context.Context, t model.Transaction) (*model.Tran
|
||||
return &t, nil
|
||||
}
|
||||
func (m *mockTxRepo) Delete(_ context.Context, _ int) error { return nil }
|
||||
func (m *mockTxRepo) HasMatchingTransaction(_ context.Context, _ *int, _ string, _ float64) (bool, error) {
|
||||
func (m *mockTxRepo) HasMatchingTransaction(_ context.Context, _ *int, _ string, _ float64, _ string) (bool, error) {
|
||||
return m.matchResult, nil
|
||||
}
|
||||
|
||||
@@ -87,7 +99,7 @@ func (m *mockTxRepo) HasMatchingTransaction(_ context.Context, _ *int, _ string,
|
||||
var catID = 1
|
||||
|
||||
func TestMonthlyStatus_Covered(t *testing.T) {
|
||||
re := model.RecurringExpense{ID: 1, Name: "Plano Saúde", ExpectedAmount: 300, DayOfMonth: 5, CategoryID: &catID, Active: true}
|
||||
re := model.RecurringExpense{ID: 1, Name: "Plano Saúde", ExpectedAmount: 300, DayOfMonth: 5, CategoryID: &catID, Type: "expense", Active: true}
|
||||
svc := service.NewRecurringService(newMockRecurring([]model.RecurringExpense{re}), &mockTxRepo{matchResult: true})
|
||||
|
||||
statuses, err := svc.MonthlyStatus(context.Background(), "2024-03")
|
||||
@@ -103,7 +115,7 @@ func TestMonthlyStatus_Covered(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMonthlyStatus_Uncovered(t *testing.T) {
|
||||
re := model.RecurringExpense{ID: 2, Name: "Netflix", ExpectedAmount: 55, DayOfMonth: 10, CategoryID: &catID, Active: true}
|
||||
re := model.RecurringExpense{ID: 2, Name: "Netflix", ExpectedAmount: 55, DayOfMonth: 10, CategoryID: &catID, Type: "expense", Active: true}
|
||||
svc := service.NewRecurringService(newMockRecurring([]model.RecurringExpense{re}), &mockTxRepo{matchResult: false})
|
||||
|
||||
statuses, err := svc.MonthlyStatus(context.Background(), "2024-03")
|
||||
@@ -116,7 +128,7 @@ func TestMonthlyStatus_Uncovered(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestMonthlyStatus_Ignored_CountsAsCovered(t *testing.T) {
|
||||
re := model.RecurringExpense{ID: 3, Name: "Seguro", ExpectedAmount: 200, DayOfMonth: 1, CategoryID: &catID, Active: true}
|
||||
re := model.RecurringExpense{ID: 3, Name: "Seguro", ExpectedAmount: 200, DayOfMonth: 1, CategoryID: &catID, Type: "expense", Active: true}
|
||||
repo := newMockRecurring([]model.RecurringExpense{re})
|
||||
_ = repo.Ignore(context.Background(), 3, "2024-03", "viagem")
|
||||
svc := service.NewRecurringService(repo, &mockTxRepo{matchResult: false})
|
||||
@@ -133,6 +145,60 @@ func TestMonthlyStatus_Ignored_CountsAsCovered(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestMonthlyStatus_IncomeConfirmed(t *testing.T) {
|
||||
re := model.RecurringExpense{ID: 4, Name: "Salário", ExpectedAmount: 3200, DayOfMonth: 5, CategoryID: &catID, Type: "income", Active: true}
|
||||
svc := service.NewRecurringService(newMockRecurring([]model.RecurringExpense{re}), &mockTxRepo{matchResult: true})
|
||||
|
||||
statuses, err := svc.MonthlyStatus(context.Background(), "2024-03")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if len(statuses) != 1 {
|
||||
t.Fatalf("expected 1, got %d", len(statuses))
|
||||
}
|
||||
if !statuses[0].Covered {
|
||||
t.Error("income with matching transaction should be covered")
|
||||
}
|
||||
}
|
||||
|
||||
func TestMonthlyStatus_IncomeLate(t *testing.T) {
|
||||
re := model.RecurringExpense{ID: 5, Name: "Salário", ExpectedAmount: 3200, DayOfMonth: 5, CategoryID: &catID, Type: "income", Active: true}
|
||||
repo := newMockRecurring([]model.RecurringExpense{re})
|
||||
_ = repo.MarkLate(context.Background(), 5, "2024-03")
|
||||
svc := service.NewRecurringService(repo, &mockTxRepo{matchResult: false})
|
||||
|
||||
statuses, err := svc.MonthlyStatus(context.Background(), "2024-03")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if statuses[0].Covered {
|
||||
t.Error("late income should NOT be covered")
|
||||
}
|
||||
if !statuses[0].Late {
|
||||
t.Error("expected Late=true")
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfirmIncome_CreatesTransaction(t *testing.T) {
|
||||
re := model.RecurringExpense{ID: 6, Name: "Salário", ExpectedAmount: 3200, DayOfMonth: 5, CategoryID: &catID, Type: "income", Active: true}
|
||||
svc := service.NewRecurringService(newMockRecurring([]model.RecurringExpense{re}), &mockTxRepo{})
|
||||
|
||||
err := svc.ConfirmIncome(context.Background(), 6, "2024-03")
|
||||
if err != nil {
|
||||
t.Fatalf("unexpected error: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestConfirmIncome_RejectsExpenseType(t *testing.T) {
|
||||
re := model.RecurringExpense{ID: 7, Name: "Netflix", ExpectedAmount: 55, DayOfMonth: 10, CategoryID: &catID, Type: "expense", Active: true}
|
||||
svc := service.NewRecurringService(newMockRecurring([]model.RecurringExpense{re}), &mockTxRepo{})
|
||||
|
||||
err := svc.ConfirmIncome(context.Background(), 7, "2024-03")
|
||||
if err != service.ErrRecurringNotIncome {
|
||||
t.Fatalf("expected ErrRecurringNotIncome, got %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCreateRecurring_Validation(t *testing.T) {
|
||||
svc := service.NewRecurringService(newMockRecurring(nil), &mockTxRepo{})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user