feat: #45 meta de poupança configurável + conquistas genéricas

- Nova tabela user_settings com savings_goal_pct (default 40%)
- API GET /api/settings, PUT /api/settings
- DashboardService inclui savings_goal_pct no payload
- DashboardHandler dispara savings_checked com goal configurado
- GameService.NotifyAction: checa pct >= goal (sem hardcode de 40%)
- SettingsView: painel META DE POUPANÇA com input configurável
- HomeView: widget mostra meta dinâmica (META: X%) e usa savingsGoal
- stores/settings.ts: store com fetch e update

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
2026-05-28 22:32:08 -03:00
co-authored by Claude Sonnet 4.6
parent 9fada4997a
commit 1d59cb2a0e
15 changed files with 296 additions and 15 deletions
+10 -2
View File
@@ -24,10 +24,11 @@ type DashboardService struct {
patrimony PatrimonySource
billSvc *CreditBillService
pendingBillSvc *PendingBillService
settingsSvc *SettingsService
}
func NewDashboardService(repo DashboardRepo, recurrSvc *RecurringService, patrimony PatrimonySource, billSvc *CreditBillService, pendingBillSvc *PendingBillService) *DashboardService {
return &DashboardService{repo: repo, recurrSvc: recurrSvc, patrimony: patrimony, billSvc: billSvc, pendingBillSvc: pendingBillSvc}
func NewDashboardService(repo DashboardRepo, recurrSvc *RecurringService, patrimony PatrimonySource, billSvc *CreditBillService, pendingBillSvc *PendingBillService, settingsSvc *SettingsService) *DashboardService {
return &DashboardService{repo: repo, recurrSvc: recurrSvc, patrimony: patrimony, billSvc: billSvc, pendingBillSvc: pendingBillSvc, settingsSvc: settingsSvc}
}
func (s *DashboardService) Get(ctx context.Context, month string) (*model.DashboardData, error) {
@@ -115,11 +116,18 @@ func (s *DashboardService) Get(ctx context.Context, month string) (*model.Dashbo
pendingBillImports = []model.PendingBillImport{}
}
settings, _ := s.settingsSvc.Get(ctx)
goalPct := 40.0
if settings != nil {
goalPct = settings.SavingsGoalPct
}
return &model.DashboardData{
Month: month,
TotalIncome: income,
TotalExpenses: expenses,
SavingsPct: savingsPct,
SavingsGoalPct: goalPct,
TotalPatrimony: patrimony,
ByCategory: byCategory,
MonthlyEvolution: evolution,
+11 -1
View File
@@ -61,12 +61,22 @@ func (m *mockPendingBillRepo) GetByID(_ context.Context, _ int) (*model.PendingB
}
func (m *mockPendingBillRepo) Delete(_ context.Context, _ int) error { return nil }
type mockSettingsRepo struct{}
func (m *mockSettingsRepo) Get(_ context.Context) (*model.UserSettings, error) {
return &model.UserSettings{SavingsGoalPct: 40}, nil
}
func (m *mockSettingsRepo) Upsert(_ context.Context, s model.UserSettings) (*model.UserSettings, error) {
return &s, nil
}
func newDashboardSvc(income, expenses float64) *service.DashboardService {
repo := &mockAccountRepo{}
recurrSvc := service.NewRecurringService(newMockRecurring(nil), &mockTxRepo{})
billSvc := service.NewCreditBillService(&mockCreditBillRepo{}, repo)
pendingBillSvc := service.NewPendingBillService(&mockPendingBillRepo{}, &mockImportTxRepo{})
return service.NewDashboardService(&mockDashboardRepo{income: income, expenses: expenses}, recurrSvc, &mockPatrimony{}, billSvc, pendingBillSvc)
settingsSvc := service.NewSettingsService(&mockSettingsRepo{})
return service.NewDashboardService(&mockDashboardRepo{income: income, expenses: expenses}, recurrSvc, &mockPatrimony{}, billSvc, pendingBillSvc, settingsSvc)
}
func TestDashboard_SavingsPct_40(t *testing.T) {
+6 -2
View File
@@ -165,9 +165,13 @@ func (s *GameService) NotifyAction(ctx context.Context, action string, extra map
case "savings_checked":
pct, _ := extra["pct"].(float64)
goal, _ := extra["goal"].(float64)
if goal <= 0 {
goal = 40
}
_ = s.repo.UpdateQuestPct(ctx, monthlyPeriod, pct)
if pct >= 40 {
_, _ = s.AwardXP(ctx, "savings_goal_met", 200, "Meta de 40% atingida!")
if pct >= goal {
_, _ = s.AwardXP(ctx, "savings_goal_met", 200, "Meta de poupança atingida!")
s.tryUnlockAchievement(ctx, "first_40pct")
}
}
+30
View File
@@ -0,0 +1,30 @@
package service
import (
"context"
"errors"
"financeiro-carvalho/internal/model"
"financeiro-carvalho/internal/repository"
)
var ErrInvalidSavingsGoal = errors.New("savings_goal_pct must be between 1 and 100")
type SettingsService struct {
repo repository.SettingsRepository
}
func NewSettingsService(repo repository.SettingsRepository) *SettingsService {
return &SettingsService{repo: repo}
}
func (s *SettingsService) Get(ctx context.Context) (*model.UserSettings, error) {
return s.repo.Get(ctx)
}
func (s *SettingsService) Update(ctx context.Context, in model.UserSettings) (*model.UserSettings, error) {
if in.SavingsGoalPct < 1 || in.SavingsGoalPct > 100 {
return nil, ErrInvalidSavingsGoal
}
return s.repo.Upsert(ctx, in)
}