From 79f4a62a1b1d476bf98128f433d02770319668b2 Mon Sep 17 00:00:00 2001 From: Mlcarvalho1 Date: Wed, 27 May 2026 15:00:28 -0300 Subject: [PATCH 1/3] =?UTF-8?q?feat(#36):=20receitas=20recorrentes=20com?= =?UTF-8?q?=20confirma=C3=A7=C3=A3o=20de=20sal=C3=A1rio=20at=C3=A9=20dia?= =?UTF-8?q?=205?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 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 --- apps/api/cmd/server/main.go | 2 + apps/api/internal/handler/recurring.go | 46 +++ .../migration/sql/009_recurring_income.sql | 13 + apps/api/internal/model/dashboard.go | 19 +- apps/api/internal/model/recurring.go | 18 +- apps/api/internal/repository/recurring.go | 64 +++- .../internal/repository/transaction_manual.go | 26 +- apps/api/internal/service/dashboard.go | 41 ++- apps/api/internal/service/recurring.go | 83 ++++- apps/api/internal/service/recurring_test.go | 78 ++++- apps/web/src/components/AppHud.vue | 2 +- apps/web/src/router/index.ts | 8 +- apps/web/src/stores/dashboard.ts | 9 + apps/web/src/stores/recurring.ts | 30 +- apps/web/src/views/HomeView.vue | 70 ++++- apps/web/src/views/RecurringView.vue | 293 ++++++++++++++++++ 16 files changed, 725 insertions(+), 77 deletions(-) create mode 100644 apps/api/internal/migration/sql/009_recurring_income.sql create mode 100644 apps/web/src/views/RecurringView.vue diff --git a/apps/api/cmd/server/main.go b/apps/api/cmd/server/main.go index 3953c6d..1da175d 100644 --- a/apps/api/cmd/server/main.go +++ b/apps/api/cmd/server/main.go @@ -105,6 +105,8 @@ func main() { r.Get("/recurring/status", recurringHandler.MonthlyStatus) r.Post("/recurring/{id}/ignore", recurringHandler.Ignore) r.Delete("/recurring/{id}/ignore", recurringHandler.Unignore) + r.Post("/recurring/{id}/confirm", recurringHandler.ConfirmIncome) + r.Post("/recurring/{id}/late", recurringHandler.MarkLate) r.Get("/accounts", accountHandler.List) r.Post("/accounts", accountHandler.Create) diff --git a/apps/api/internal/handler/recurring.go b/apps/api/internal/handler/recurring.go index 578bc1d..933da0a 100644 --- a/apps/api/internal/handler/recurring.go +++ b/apps/api/internal/handler/recurring.go @@ -128,3 +128,49 @@ func (h *RecurringHandler) Unignore(w http.ResponseWriter, r *http.Request) { } w.WriteHeader(http.StatusNoContent) } + +func (h *RecurringHandler) ConfirmIncome(w http.ResponseWriter, r *http.Request) { + id, err := strconv.Atoi(chi.URLParam(r, "id")) + if err != nil { + respondError(w, http.StatusBadRequest, "invalid id") + return + } + var body struct { + Month string `json:"month"` + } + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + respondError(w, http.StatusBadRequest, "invalid JSON") + return + } + if err := h.svc.ConfirmIncome(r.Context(), id, body.Month); errors.Is(err, repository.ErrNotFound) { + respondError(w, http.StatusNotFound, "not found") + return + } else if err != nil { + respondError(w, http.StatusBadRequest, err.Error()) + return + } + w.WriteHeader(http.StatusNoContent) +} + +func (h *RecurringHandler) MarkLate(w http.ResponseWriter, r *http.Request) { + id, err := strconv.Atoi(chi.URLParam(r, "id")) + if err != nil { + respondError(w, http.StatusBadRequest, "invalid id") + return + } + var body struct { + Month string `json:"month"` + } + if err := json.NewDecoder(r.Body).Decode(&body); err != nil { + respondError(w, http.StatusBadRequest, "invalid JSON") + return + } + if err := h.svc.MarkLate(r.Context(), id, body.Month); errors.Is(err, repository.ErrNotFound) { + respondError(w, http.StatusNotFound, "not found") + return + } else if err != nil { + respondError(w, http.StatusBadRequest, err.Error()) + return + } + w.WriteHeader(http.StatusNoContent) +} diff --git a/apps/api/internal/migration/sql/009_recurring_income.sql b/apps/api/internal/migration/sql/009_recurring_income.sql new file mode 100644 index 0000000..790f9a9 --- /dev/null +++ b/apps/api/internal/migration/sql/009_recurring_income.sql @@ -0,0 +1,13 @@ +-- Add type to recurring_expenses (income | expense, default expense — retrocompatível) +ALTER TABLE recurring_expenses + ADD COLUMN IF NOT EXISTS type VARCHAR(10) NOT NULL DEFAULT 'expense' + CHECK (type IN ('income', 'expense')); + +-- Track income recurrings marked as "late" for a given month +CREATE TABLE IF NOT EXISTS recurring_late ( + id SERIAL PRIMARY KEY, + recurring_id INTEGER NOT NULL REFERENCES recurring_expenses(id) ON DELETE CASCADE, + month VARCHAR(7) NOT NULL, + created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(), + UNIQUE(recurring_id, month) +); diff --git a/apps/api/internal/model/dashboard.go b/apps/api/internal/model/dashboard.go index 889e712..a0fbd93 100644 --- a/apps/api/internal/model/dashboard.go +++ b/apps/api/internal/model/dashboard.go @@ -24,13 +24,14 @@ type RecentTransaction struct { } type DashboardData struct { - Month string `json:"month"` - TotalIncome float64 `json:"total_income"` - TotalExpenses float64 `json:"total_expenses"` - SavingsPct float64 `json:"savings_pct"` - TotalPatrimony float64 `json:"total_patrimony"` - ByCategory []CategoryTotal `json:"by_category"` - MonthlyEvolution []MonthEvolution `json:"monthly_evolution"` - RecentTransactions []RecentTransaction `json:"recent_transactions"` - PendingRecurring int `json:"pending_recurring"` + Month string `json:"month"` + TotalIncome float64 `json:"total_income"` + TotalExpenses float64 `json:"total_expenses"` + SavingsPct float64 `json:"savings_pct"` + TotalPatrimony float64 `json:"total_patrimony"` + ByCategory []CategoryTotal `json:"by_category"` + MonthlyEvolution []MonthEvolution `json:"monthly_evolution"` + RecentTransactions []RecentTransaction `json:"recent_transactions"` + PendingRecurring int `json:"pending_recurring"` + PendingIncomeRecurrings []PendingIncome `json:"pending_income_recurrings"` } diff --git a/apps/api/internal/model/recurring.go b/apps/api/internal/model/recurring.go index 2827ed2..37168bf 100644 --- a/apps/api/internal/model/recurring.go +++ b/apps/api/internal/model/recurring.go @@ -8,6 +8,7 @@ type RecurringExpense struct { ExpectedAmount float64 `json:"expected_amount"` DayOfMonth int `json:"day_of_month"` CategoryID *int `json:"category_id"` + Type string `json:"type"` // "income" | "expense" Active bool `json:"active"` CreatedAt time.Time `json:"created_at"` UpdatedAt time.Time `json:"updated_at"` @@ -18,12 +19,23 @@ type RecurringInput struct { ExpectedAmount float64 `json:"expected_amount"` DayOfMonth int `json:"day_of_month"` CategoryID *int `json:"category_id"` + Type string `json:"type"` // "income" | "expense" } -// RecurringStatus reports whether a recurring expense is covered for a month. +// RecurringStatus reports whether a recurring item is covered/ignored/late for a month. type RecurringStatus struct { RecurringExpense - Covered bool `json:"covered"` // has a matching transaction - Ignored bool `json:"ignored"` // user explicitly ignored this month + Covered bool `json:"covered"` // has a matching transaction or was ignored + Ignored bool `json:"ignored"` // user explicitly ignored this month (expense) + Late bool `json:"late"` // user marked income as late this month Reason string `json:"reason,omitempty"` } + +// PendingIncome is a lightweight summary of an income recurring pending confirmation. +type PendingIncome struct { + ID int `json:"id"` + Name string `json:"name"` + ExpectedAmount float64 `json:"expected_amount"` + DayOfMonth int `json:"day_of_month"` + Late bool `json:"late"` +} diff --git a/apps/api/internal/repository/recurring.go b/apps/api/internal/repository/recurring.go index 590658a..9a39310 100644 --- a/apps/api/internal/repository/recurring.go +++ b/apps/api/internal/repository/recurring.go @@ -19,6 +19,9 @@ type RecurringRepository interface { IsIgnored(ctx context.Context, id int, month string) (bool, string, error) Ignore(ctx context.Context, id int, month, reason string) error Unignore(ctx context.Context, id int, month string) error + IsLate(ctx context.Context, id int, month string) (bool, error) + MarkLate(ctx context.Context, id int, month string) error + UnmarkLate(ctx context.Context, id int, month string) error } type recurringRepo struct{ db *pgxpool.Pool } @@ -29,7 +32,7 @@ func NewRecurringRepository(db *pgxpool.Pool) RecurringRepository { func (r *recurringRepo) List(ctx context.Context) ([]model.RecurringExpense, error) { rows, err := r.db.Query(ctx, ` - SELECT id, name, expected_amount, day_of_month, category_id, active, created_at, updated_at + SELECT id, name, expected_amount, day_of_month, category_id, type, active, created_at, updated_at FROM recurring_expenses ORDER BY name ASC`) if err != nil { return nil, err @@ -38,7 +41,7 @@ func (r *recurringRepo) List(ctx context.Context) ([]model.RecurringExpense, err var out []model.RecurringExpense for rows.Next() { var re model.RecurringExpense - if err := rows.Scan(&re.ID, &re.Name, &re.ExpectedAmount, &re.DayOfMonth, &re.CategoryID, &re.Active, &re.CreatedAt, &re.UpdatedAt); err != nil { + if err := rows.Scan(&re.ID, &re.Name, &re.ExpectedAmount, &re.DayOfMonth, &re.CategoryID, &re.Type, &re.Active, &re.CreatedAt, &re.UpdatedAt); err != nil { return nil, err } out = append(out, re) @@ -49,9 +52,9 @@ func (r *recurringRepo) List(ctx context.Context) ([]model.RecurringExpense, err func (r *recurringRepo) GetByID(ctx context.Context, id int) (*model.RecurringExpense, error) { var re model.RecurringExpense err := r.db.QueryRow(ctx, ` - SELECT id, name, expected_amount, day_of_month, category_id, active, created_at, updated_at + SELECT id, name, expected_amount, day_of_month, category_id, type, active, created_at, updated_at FROM recurring_expenses WHERE id = $1`, id). - Scan(&re.ID, &re.Name, &re.ExpectedAmount, &re.DayOfMonth, &re.CategoryID, &re.Active, &re.CreatedAt, &re.UpdatedAt) + Scan(&re.ID, &re.Name, &re.ExpectedAmount, &re.DayOfMonth, &re.CategoryID, &re.Type, &re.Active, &re.CreatedAt, &re.UpdatedAt) if errors.Is(err, pgx.ErrNoRows) { return nil, ErrNotFound } @@ -59,25 +62,33 @@ func (r *recurringRepo) GetByID(ctx context.Context, id int) (*model.RecurringEx } func (r *recurringRepo) Create(ctx context.Context, in model.RecurringInput) (*model.RecurringExpense, error) { + t := in.Type + if t == "" { + t = "expense" + } var re model.RecurringExpense err := r.db.QueryRow(ctx, ` - INSERT INTO recurring_expenses (name, expected_amount, day_of_month, category_id) - VALUES ($1, $2, $3, $4) - RETURNING id, name, expected_amount, day_of_month, category_id, active, created_at, updated_at`, - in.Name, in.ExpectedAmount, in.DayOfMonth, in.CategoryID). - Scan(&re.ID, &re.Name, &re.ExpectedAmount, &re.DayOfMonth, &re.CategoryID, &re.Active, &re.CreatedAt, &re.UpdatedAt) + INSERT INTO recurring_expenses (name, expected_amount, day_of_month, category_id, type) + VALUES ($1, $2, $3, $4, $5) + RETURNING id, name, expected_amount, day_of_month, category_id, type, active, created_at, updated_at`, + in.Name, in.ExpectedAmount, in.DayOfMonth, in.CategoryID, t). + Scan(&re.ID, &re.Name, &re.ExpectedAmount, &re.DayOfMonth, &re.CategoryID, &re.Type, &re.Active, &re.CreatedAt, &re.UpdatedAt) return &re, err } func (r *recurringRepo) Update(ctx context.Context, id int, in model.RecurringInput) (*model.RecurringExpense, error) { + t := in.Type + if t == "" { + t = "expense" + } var re model.RecurringExpense err := r.db.QueryRow(ctx, ` UPDATE recurring_expenses - SET name = $1, expected_amount = $2, day_of_month = $3, category_id = $4, updated_at = NOW() - WHERE id = $5 - RETURNING id, name, expected_amount, day_of_month, category_id, active, created_at, updated_at`, - in.Name, in.ExpectedAmount, in.DayOfMonth, in.CategoryID, id). - Scan(&re.ID, &re.Name, &re.ExpectedAmount, &re.DayOfMonth, &re.CategoryID, &re.Active, &re.CreatedAt, &re.UpdatedAt) + SET name = $1, expected_amount = $2, day_of_month = $3, category_id = $4, type = $5, updated_at = NOW() + WHERE id = $6 + RETURNING id, name, expected_amount, day_of_month, category_id, type, active, created_at, updated_at`, + in.Name, in.ExpectedAmount, in.DayOfMonth, in.CategoryID, t, id). + Scan(&re.ID, &re.Name, &re.ExpectedAmount, &re.DayOfMonth, &re.CategoryID, &re.Type, &re.Active, &re.CreatedAt, &re.UpdatedAt) if errors.Is(err, pgx.ErrNoRows) { return nil, ErrNotFound } @@ -119,3 +130,28 @@ func (r *recurringRepo) Unignore(ctx context.Context, id int, month string) erro _, err := r.db.Exec(ctx, `DELETE FROM recurring_ignores WHERE recurring_id = $1 AND month = $2`, id, month) return err } + +func (r *recurringRepo) IsLate(ctx context.Context, id int, month string) (bool, error) { + var count int + err := r.db.QueryRow(ctx, + `SELECT COUNT(*) FROM recurring_late WHERE recurring_id = $1 AND month = $2`, + id, month).Scan(&count) + if err != nil { + return false, err + } + return count > 0, nil +} + +func (r *recurringRepo) MarkLate(ctx context.Context, id int, month string) error { + _, err := r.db.Exec(ctx, ` + INSERT INTO recurring_late (recurring_id, month) + VALUES ($1, $2) + ON CONFLICT (recurring_id, month) DO NOTHING`, + id, month) + return err +} + +func (r *recurringRepo) UnmarkLate(ctx context.Context, id int, month string) error { + _, err := r.db.Exec(ctx, `DELETE FROM recurring_late WHERE recurring_id = $1 AND month = $2`, id, month) + return err +} diff --git a/apps/api/internal/repository/transaction_manual.go b/apps/api/internal/repository/transaction_manual.go index 36d55aa..0f54150 100644 --- a/apps/api/internal/repository/transaction_manual.go +++ b/apps/api/internal/repository/transaction_manual.go @@ -16,8 +16,8 @@ type ManualTransactionRepository interface { Create(ctx context.Context, t model.Transaction) (*model.Transaction, error) Update(ctx context.Context, t model.Transaction) (*model.Transaction, error) Delete(ctx context.Context, id int) error - // HasMatchingTransaction checks if a category has an expense in the given month. - HasMatchingTransaction(ctx context.Context, categoryID *int, month string, amount float64) (bool, error) + // HasMatchingTransaction checks if a category has a transaction of the given type in the given month. + HasMatchingTransaction(ctx context.Context, categoryID *int, month string, amount float64, txType string) (bool, error) } type manualTxRepo struct{ db *pgxpool.Pool } @@ -103,20 +103,18 @@ func (r *manualTxRepo) Delete(ctx context.Context, id int) error { return nil } -func (r *manualTxRepo) HasMatchingTransaction(ctx context.Context, categoryID *int, month string, amount float64) (bool, error) { - var count int - var err error - if categoryID != nil { - err = r.db.QueryRow(ctx, ` - SELECT COUNT(*) FROM transactions - WHERE category_id = $1 - AND TO_CHAR(date, 'YYYY-MM') = $2 - AND type = 'expense' - AND amount BETWEEN $3 * 0.9 AND $3 * 1.1`, - *categoryID, month, amount).Scan(&count) - } else { +func (r *manualTxRepo) HasMatchingTransaction(ctx context.Context, categoryID *int, month string, amount float64, txType string) (bool, error) { + if categoryID == nil { // No category set — cannot auto-match, always report as uncovered return false, nil } + var count int + err := r.db.QueryRow(ctx, ` + SELECT COUNT(*) FROM transactions + WHERE category_id = $1 + AND TO_CHAR(date, 'YYYY-MM') = $2 + AND type = $3 + AND amount BETWEEN $4 * 0.9 AND $4 * 1.1`, + *categoryID, month, txType, amount).Scan(&count) return count > 0, err } diff --git a/apps/api/internal/service/dashboard.go b/apps/api/internal/service/dashboard.go index 0028f45..41e578f 100644 --- a/apps/api/internal/service/dashboard.go +++ b/apps/api/internal/service/dashboard.go @@ -58,11 +58,27 @@ func (s *DashboardService) Get(ctx context.Context, month string) (*model.Dashbo return nil, err } pending := 0 - for _, s := range statuses { - if !s.Covered { - pending++ + var pendingIncome []model.PendingIncome + for _, st := range statuses { + if st.Type == "income" { + if !st.Covered { + pendingIncome = append(pendingIncome, model.PendingIncome{ + ID: st.ID, + Name: st.Name, + ExpectedAmount: st.ExpectedAmount, + DayOfMonth: st.DayOfMonth, + Late: st.Late, + }) + } + } else { + if !st.Covered { + pending++ + } } } + if pendingIncome == nil { + pendingIncome = []model.PendingIncome{} + } if byCategory == nil { byCategory = []model.CategoryTotal{} @@ -80,14 +96,15 @@ func (s *DashboardService) Get(ctx context.Context, month string) (*model.Dashbo } return &model.DashboardData{ - Month: month, - TotalIncome: income, - TotalExpenses: expenses, - SavingsPct: savingsPct, - TotalPatrimony: patrimony, - ByCategory: byCategory, - MonthlyEvolution: evolution, - RecentTransactions: recent, - PendingRecurring: pending, + Month: month, + TotalIncome: income, + TotalExpenses: expenses, + SavingsPct: savingsPct, + TotalPatrimony: patrimony, + ByCategory: byCategory, + MonthlyEvolution: evolution, + RecentTransactions: recent, + PendingRecurring: pending, + PendingIncomeRecurrings: pendingIncome, }, nil } diff --git a/apps/api/internal/service/recurring.go b/apps/api/internal/service/recurring.go index 71d9932..93d0186 100644 --- a/apps/api/internal/service/recurring.go +++ b/apps/api/internal/service/recurring.go @@ -4,6 +4,7 @@ import ( "context" "errors" "strings" + "time" "financeiro-carvalho/internal/model" "financeiro-carvalho/internal/repository" @@ -13,6 +14,7 @@ var ( ErrRecurringEmptyName = errors.New("name is required") ErrRecurringInvalidAmount = errors.New("expected_amount must be greater than zero") ErrRecurringInvalidDayOfMonth = errors.New("day_of_month must be between 1 and 31") + ErrRecurringNotIncome = errors.New("recurring item is not of type income") ) type RecurringService struct { @@ -52,7 +54,7 @@ func (s *RecurringService) Delete(ctx context.Context, id int) error { return s.repo.Delete(ctx, id) } -// MonthlyStatus checks which active recurring expenses are covered/ignored for the given month (YYYY-MM). +// MonthlyStatus checks which active recurring items are covered/ignored/late for the given month (YYYY-MM). func (s *RecurringService) MonthlyStatus(ctx context.Context, month string) ([]model.RecurringStatus, error) { items, err := s.repo.List(ctx) if err != nil { @@ -64,27 +66,82 @@ func (s *RecurringService) MonthlyStatus(ctx context.Context, month string) ([]m if !re.Active { continue } - ignored, reason, err := s.repo.IsIgnored(ctx, re.ID, month) - if err != nil { - return nil, err + + txType := re.Type + if txType == "" { + txType = "expense" } - covered := false - if !ignored { - covered, err = s.txRepo.HasMatchingTransaction(ctx, re.CategoryID, month, re.ExpectedAmount) + + if txType == "expense" { + ignored, reason, err := s.repo.IsIgnored(ctx, re.ID, month) if err != nil { return nil, err } + covered := false + if !ignored { + covered, err = s.txRepo.HasMatchingTransaction(ctx, re.CategoryID, month, re.ExpectedAmount, "expense") + if err != nil { + return nil, err + } + } + result = append(result, model.RecurringStatus{ + RecurringExpense: re, + Covered: covered || ignored, + Ignored: ignored, + Reason: reason, + }) + } else { + // income type + late, err := s.repo.IsLate(ctx, re.ID, month) + if err != nil { + return nil, err + } + covered, err := s.txRepo.HasMatchingTransaction(ctx, re.CategoryID, month, re.ExpectedAmount, "income") + if err != nil { + return nil, err + } + result = append(result, model.RecurringStatus{ + RecurringExpense: re, + Covered: covered, + Late: late, + }) } - result = append(result, model.RecurringStatus{ - RecurringExpense: re, - Covered: covered || ignored, - Ignored: ignored, - Reason: reason, - }) } return result, nil } +// ConfirmIncome creates an income transaction confirming receipt of this recurring income. +func (s *RecurringService) ConfirmIncome(ctx context.Context, id int, month string) error { + re, err := s.repo.GetByID(ctx, id) + if err != nil { + return err + } + if re.Type != "income" { + return ErrRecurringNotIncome + } + today := time.Now().Format("2006-01-02") + tx := model.Transaction{ + Date: today, + Amount: re.ExpectedAmount, + Description: re.Name, + Type: "income", + CategoryID: re.CategoryID, + } + _, err = s.txRepo.Create(ctx, tx) + return err +} + +func (s *RecurringService) MarkLate(ctx context.Context, id int, month string) error { + re, err := s.repo.GetByID(ctx, id) + if err != nil { + return err + } + if re.Type != "income" { + return ErrRecurringNotIncome + } + return s.repo.MarkLate(ctx, id, month) +} + func (s *RecurringService) Ignore(ctx context.Context, id int, month, reason string) error { return s.repo.Ignore(ctx, id, month, reason) } diff --git a/apps/api/internal/service/recurring_test.go b/apps/api/internal/service/recurring_test.go index 5e0d814..c2009c1 100644 --- a/apps/api/internal/service/recurring_test.go +++ b/apps/api/internal/service/recurring_test.go @@ -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{}) diff --git a/apps/web/src/components/AppHud.vue b/apps/web/src/components/AppHud.vue index 2be94e0..6a27b7d 100644 --- a/apps/web/src/components/AppHud.vue +++ b/apps/web/src/components/AppHud.vue @@ -49,7 +49,7 @@ const navItems = [ { to: '/importar', label: 'IMPORT' }, { to: '/contas', label: 'CONTAS' }, { to: '/personagem', label: 'PERS.' }, - { to: '/configuracoes', label: 'CFG' }, + { to: '/recorrencias', label: 'REC.' }, ] diff --git a/apps/web/src/router/index.ts b/apps/web/src/router/index.ts index 169087f..c861736 100644 --- a/apps/web/src/router/index.ts +++ b/apps/web/src/router/index.ts @@ -41,10 +41,14 @@ const router = createRouter({ name: 'character', component: () => import('../views/CharacterView.vue'), }, + { + path: '/recorrencias', + name: 'recurring', + component: () => import('../views/RecurringView.vue'), + }, { path: '/configuracoes', - name: 'settings', - component: () => import('../views/SettingsView.vue'), + redirect: '/recorrencias', }, ], }) diff --git a/apps/web/src/stores/dashboard.ts b/apps/web/src/stores/dashboard.ts index c6f8bbe..97a11c4 100644 --- a/apps/web/src/stores/dashboard.ts +++ b/apps/web/src/stores/dashboard.ts @@ -25,6 +25,14 @@ export interface RecentTransaction { type: 'income' | 'expense' } +export interface PendingIncome { + id: number + name: string + expected_amount: number + day_of_month: number + late: boolean +} + export interface DashboardData { month: string total_income: number @@ -35,6 +43,7 @@ export interface DashboardData { monthly_evolution: MonthEvolution[] recent_transactions: RecentTransaction[] pending_recurring: number + pending_income_recurrings: PendingIncome[] } export const useDashboardStore = defineStore('dashboard', () => { diff --git a/apps/web/src/stores/recurring.ts b/apps/web/src/stores/recurring.ts index 45cc722..48a1b80 100644 --- a/apps/web/src/stores/recurring.ts +++ b/apps/web/src/stores/recurring.ts @@ -1,7 +1,6 @@ import { defineStore } from 'pinia' import { ref } from 'vue' import { api } from '@/services/api' -import type { Category } from './categories' export interface RecurringExpense { id: number @@ -9,6 +8,7 @@ export interface RecurringExpense { expected_amount: number day_of_month: number category_id: number | null + type: 'income' | 'expense' active: boolean created_at: string updated_at: string @@ -17,6 +17,7 @@ export interface RecurringExpense { export interface RecurringStatus extends RecurringExpense { covered: boolean ignored: boolean + late: boolean reason?: string } @@ -74,8 +75,33 @@ export const useRecurringStore = defineStore('recurring', () => { await fetchMonthlyStatus(month) } + async function confirmIncome(id: number, month: string) { + await api.post(`/recurring/${id}/confirm`, { month }) + await fetchMonthlyStatus(month) + } + + async function markLate(id: number, month: string) { + await api.post(`/recurring/${id}/late`, { month }) + await fetchMonthlyStatus(month) + } + const pendingCount = (month: string) => monthlyStatus.value.filter((s) => !s.covered).length - return { items, monthlyStatus, loading, error, fetchAll, fetchMonthlyStatus, create, update, remove, ignore, unignore, pendingCount } + return { + items, + monthlyStatus, + loading, + error, + fetchAll, + fetchMonthlyStatus, + create, + update, + remove, + ignore, + unignore, + confirmIncome, + markLate, + pendingCount, + } }) diff --git a/apps/web/src/views/HomeView.vue b/apps/web/src/views/HomeView.vue index c8e6de0..a2a882f 100644 --- a/apps/web/src/views/HomeView.vue +++ b/apps/web/src/views/HomeView.vue @@ -2,6 +2,7 @@ import { ref, computed, onMounted } from 'vue' import { useDashboardStore } from '@/stores/dashboard' import { useGameStore } from '@/stores/game' +import { useRecurringStore } from '@/stores/recurring' import NeonPanel from '@/components/NeonPanel.vue' import XPBar from '@/components/XPBar.vue' import CharacterSprite from '@/components/CharacterSprite.vue' @@ -9,6 +10,7 @@ import MonthSwitcher from '@/components/MonthSwitcher.vue' const dash = useDashboardStore() const game = useGameStore() +const recurring = useRecurringStore() const currentMonth = ref(new Date().toISOString().slice(0, 7)) onMounted(() => dash.fetch(currentMonth.value)) @@ -51,6 +53,22 @@ const xpPct = computed(() => { }) const savingsPct = computed(() => dash.data?.savings_pct ?? 0) const savingsOk = computed(() => savingsPct.value >= 40) + +const today = new Date() +const isCurrentMonth = computed(() => currentMonth.value === today.toISOString().slice(0, 7)) +const dayOfMonth = today.getDate() +const pendingIncome = computed(() => dash.data?.pending_income_recurrings ?? []) +const showIncomeWidget = computed(() => isCurrentMonth.value && pendingIncome.value.length > 0) +const showIncomeConfirmButtons = computed(() => dayOfMonth <= 5) + +async function confirmIncome(id: number) { + await recurring.confirmIncome(id, currentMonth.value) + await dash.fetch(currentMonth.value) +} +async function markLate(id: number) { + await recurring.markLate(id, currentMonth.value) + await dash.fetch(currentMonth.value) +} @@ -338,6 +362,15 @@ async function markLate(id: number) { .empty { font-size: 8px; color: var(--fc-text-dim); padding: 8px 0; } +/* Bill widget */ +.bill-list { display: flex; flex-direction: column; gap: 10px; } +.bill-item { display: flex; justify-content: space-between; align-items: center; gap: var(--fc-space-2); flex-wrap: wrap; } +.bill-item__info { display: flex; flex-direction: column; gap: 3px; } +.bill-item__name { font-size: 13px; font-weight: 500; } +.bill-item__due { font-size: 10px; } +.bill-item__right { display: flex; align-items: center; gap: var(--fc-space-2); } +.bill-item__total { font-size: 15px; } + /* Income widget */ .dash-income-panel { margin-bottom: 0; } .dash-alert--salary { background: rgba(255,180,0,.08); border-color: var(--fc-gold); }