feat(#19): registro manual de transações e recorrências fixas

Adiciona CRUD manual de transações (income/expense) com filtro mensal, CRUD de
recorrências fixas com verificação mensal de cobertura (±10% por categoria),
endpoint de ignore/unignore e telas Vue para Transações e Configurações.

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
2026-05-26 20:26:48 -03:00
co-authored by Claude Sonnet 4.6
parent 83dff2de78
commit 12e8500827
17 changed files with 1351 additions and 8 deletions
+21
View File
@@ -56,6 +56,14 @@ func main() {
importSvc := service.NewImportService(transactionRepo)
importHandler := handler.NewImportHandler(importSvc)
manualTxRepo := repository.NewManualTransactionRepository(pool)
txSvc := service.NewTransactionService(manualTxRepo)
txHandler := handler.NewTransactionHandler(txSvc)
recurringRepo := repository.NewRecurringRepository(pool)
recurringSvc := service.NewRecurringService(recurringRepo, manualTxRepo)
recurringHandler := handler.NewRecurringHandler(recurringSvc)
r.Get("/health", handler.Health)
r.Route("/api", func(r chi.Router) {
@@ -66,6 +74,19 @@ func main() {
r.Post("/imports/preview", importHandler.Preview)
r.Post("/imports/confirm", importHandler.Confirm)
r.Get("/transactions", txHandler.List)
r.Post("/transactions", txHandler.Create)
r.Put("/transactions/{id}", txHandler.Update)
r.Delete("/transactions/{id}", txHandler.Delete)
r.Get("/recurring", recurringHandler.List)
r.Post("/recurring", recurringHandler.Create)
r.Put("/recurring/{id}", recurringHandler.Update)
r.Delete("/recurring/{id}", recurringHandler.Delete)
r.Get("/recurring/status", recurringHandler.MonthlyStatus)
r.Post("/recurring/{id}/ignore", recurringHandler.Ignore)
r.Delete("/recurring/{id}/ignore", recurringHandler.Unignore)
})
// Serve Vue SPA — non-API routes fall through to index.html
+130
View File
@@ -0,0 +1,130 @@
package handler
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"financeiro-carvalho/internal/model"
"financeiro-carvalho/internal/repository"
"financeiro-carvalho/internal/service"
)
type RecurringHandler struct {
svc *service.RecurringService
}
func NewRecurringHandler(svc *service.RecurringService) *RecurringHandler {
return &RecurringHandler{svc: svc}
}
func (h *RecurringHandler) List(w http.ResponseWriter, r *http.Request) {
items, err := h.svc.List(r.Context())
if err != nil {
respondError(w, http.StatusInternalServerError, "failed to list recurring expenses")
return
}
respondJSON(w, http.StatusOK, items)
}
func (h *RecurringHandler) Create(w http.ResponseWriter, r *http.Request) {
var in model.RecurringInput
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
respondError(w, http.StatusBadRequest, "invalid JSON")
return
}
out, err := h.svc.Create(r.Context(), in)
if err != nil {
respondError(w, http.StatusBadRequest, err.Error())
return
}
respondJSON(w, http.StatusCreated, out)
}
func (h *RecurringHandler) Update(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 in model.RecurringInput
if err := json.NewDecoder(r.Body).Decode(&in); err != nil {
respondError(w, http.StatusBadRequest, "invalid JSON")
return
}
out, err := h.svc.Update(r.Context(), id, in)
if errors.Is(err, repository.ErrNotFound) {
respondError(w, http.StatusNotFound, "not found")
return
}
if err != nil {
respondError(w, http.StatusBadRequest, err.Error())
return
}
respondJSON(w, http.StatusOK, out)
}
func (h *RecurringHandler) Delete(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
respondError(w, http.StatusBadRequest, "invalid id")
return
}
if err := h.svc.Delete(r.Context(), id); err != nil {
respondError(w, http.StatusNotFound, "not found")
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *RecurringHandler) MonthlyStatus(w http.ResponseWriter, r *http.Request) {
month := r.URL.Query().Get("month")
if month == "" {
respondError(w, http.StatusBadRequest, "month param required (YYYY-MM)")
return
}
statuses, err := h.svc.MonthlyStatus(r.Context(), month)
if err != nil {
respondError(w, http.StatusInternalServerError, "failed to compute status")
return
}
respondJSON(w, http.StatusOK, statuses)
}
func (h *RecurringHandler) Ignore(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"`
Reason string `json:"reason"`
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
respondError(w, http.StatusBadRequest, "invalid JSON")
return
}
if err := h.svc.Ignore(r.Context(), id, body.Month, body.Reason); err != nil {
respondError(w, http.StatusInternalServerError, "failed to ignore")
return
}
w.WriteHeader(http.StatusNoContent)
}
func (h *RecurringHandler) Unignore(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
respondError(w, http.StatusBadRequest, "invalid id")
return
}
month := r.URL.Query().Get("month")
if err := h.svc.Unignore(r.Context(), id, month); err != nil {
respondError(w, http.StatusInternalServerError, "failed to unignore")
return
}
w.WriteHeader(http.StatusNoContent)
}
+83
View File
@@ -0,0 +1,83 @@
package handler
import (
"encoding/json"
"errors"
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"financeiro-carvalho/internal/model"
"financeiro-carvalho/internal/repository"
"financeiro-carvalho/internal/service"
)
type TransactionHandler struct {
svc *service.TransactionService
}
func NewTransactionHandler(svc *service.TransactionService) *TransactionHandler {
return &TransactionHandler{svc: svc}
}
func (h *TransactionHandler) List(w http.ResponseWriter, r *http.Request) {
month := r.URL.Query().Get("month")
txs, err := h.svc.List(r.Context(), month)
if err != nil {
respondError(w, http.StatusInternalServerError, "failed to list transactions")
return
}
respondJSON(w, http.StatusOK, txs)
}
func (h *TransactionHandler) Create(w http.ResponseWriter, r *http.Request) {
var t model.Transaction
if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
respondError(w, http.StatusBadRequest, "invalid JSON")
return
}
out, err := h.svc.Create(r.Context(), t)
if err != nil {
respondError(w, http.StatusBadRequest, err.Error())
return
}
respondJSON(w, http.StatusCreated, out)
}
func (h *TransactionHandler) Update(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 t model.Transaction
if err := json.NewDecoder(r.Body).Decode(&t); err != nil {
respondError(w, http.StatusBadRequest, "invalid JSON")
return
}
t.ID = id
out, err := h.svc.Update(r.Context(), t)
if errors.Is(err, repository.ErrNotFound) || errors.Is(err, service.ErrCannotEditImported) {
respondError(w, http.StatusNotFound, err.Error())
return
}
if err != nil {
respondError(w, http.StatusBadRequest, err.Error())
return
}
respondJSON(w, http.StatusOK, out)
}
func (h *TransactionHandler) Delete(w http.ResponseWriter, r *http.Request) {
id, err := strconv.Atoi(chi.URLParam(r, "id"))
if err != nil {
respondError(w, http.StatusBadRequest, "invalid id")
return
}
if err := h.svc.Delete(r.Context(), id); err != nil {
respondError(w, http.StatusNotFound, err.Error())
return
}
w.WriteHeader(http.StatusNoContent)
}
+6 -8
View File
@@ -14,15 +14,13 @@ var m001 string
//go:embed sql/002_add_external_id.sql
var m002 string
//go:embed sql/003_recurring_ignores.sql
var m003 string
func Run(ctx context.Context, pool *pgxpool.Pool) error {
for i, sql := range []struct {
version int
sql string
}{
{1, m001},
{2, m002},
} {
if _, err := pool.Exec(ctx, sql.sql); err != nil {
migrations := []string{m001, m002, m003}
for i, sql := range migrations {
if _, err := pool.Exec(ctx, sql); err != nil {
return fmt.Errorf("migration %03d: %w", i+1, err)
}
}
@@ -0,0 +1,10 @@
CREATE TABLE IF NOT EXISTS recurring_ignores (
id SERIAL PRIMARY KEY,
recurring_id INTEGER NOT NULL REFERENCES recurring_expenses (id) ON DELETE CASCADE,
month CHAR(7) NOT NULL, -- YYYY-MM
reason TEXT,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
UNIQUE (recurring_id, month)
);
INSERT INTO schema_migrations (version) VALUES (3) ON CONFLICT DO NOTHING;
+29
View File
@@ -0,0 +1,29 @@
package model
import "time"
type RecurringExpense struct {
ID int `json:"id"`
Name string `json:"name"`
ExpectedAmount float64 `json:"expected_amount"`
DayOfMonth int `json:"day_of_month"`
CategoryID *int `json:"category_id"`
Active bool `json:"active"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type RecurringInput struct {
Name string `json:"name"`
ExpectedAmount float64 `json:"expected_amount"`
DayOfMonth int `json:"day_of_month"`
CategoryID *int `json:"category_id"`
}
// RecurringStatus reports whether a recurring expense is covered for a month.
type RecurringStatus struct {
RecurringExpense
Covered bool `json:"covered"` // has a matching transaction
Ignored bool `json:"ignored"` // user explicitly ignored this month
Reason string `json:"reason,omitempty"`
}
+121
View File
@@ -0,0 +1,121 @@
package repository
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"financeiro-carvalho/internal/model"
)
type RecurringRepository interface {
List(ctx context.Context) ([]model.RecurringExpense, error)
GetByID(ctx context.Context, id int) (*model.RecurringExpense, error)
Create(ctx context.Context, in model.RecurringInput) (*model.RecurringExpense, error)
Update(ctx context.Context, id int, in model.RecurringInput) (*model.RecurringExpense, error)
Delete(ctx context.Context, id int) error
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
}
type recurringRepo struct{ db *pgxpool.Pool }
func NewRecurringRepository(db *pgxpool.Pool) RecurringRepository {
return &recurringRepo{db: db}
}
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
FROM recurring_expenses ORDER BY name ASC`)
if err != nil {
return nil, err
}
defer rows.Close()
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 {
return nil, err
}
out = append(out, re)
}
return out, rows.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
FROM recurring_expenses WHERE id = $1`, id).
Scan(&re.ID, &re.Name, &re.ExpectedAmount, &re.DayOfMonth, &re.CategoryID, &re.Active, &re.CreatedAt, &re.UpdatedAt)
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrNotFound
}
return &re, err
}
func (r *recurringRepo) Create(ctx context.Context, in model.RecurringInput) (*model.RecurringExpense, error) {
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)
return &re, err
}
func (r *recurringRepo) Update(ctx context.Context, id int, in model.RecurringInput) (*model.RecurringExpense, error) {
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)
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrNotFound
}
return &re, err
}
func (r *recurringRepo) Delete(ctx context.Context, id int) error {
tag, err := r.db.Exec(ctx, `DELETE FROM recurring_expenses WHERE id = $1`, id)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return ErrNotFound
}
return nil
}
func (r *recurringRepo) IsIgnored(ctx context.Context, id int, month string) (bool, string, error) {
var reason string
err := r.db.QueryRow(ctx,
`SELECT COALESCE(reason, '') FROM recurring_ignores WHERE recurring_id = $1 AND month = $2`,
id, month).Scan(&reason)
if errors.Is(err, pgx.ErrNoRows) {
return false, "", nil
}
return err == nil, reason, err
}
func (r *recurringRepo) Ignore(ctx context.Context, id int, month, reason string) error {
_, err := r.db.Exec(ctx, `
INSERT INTO recurring_ignores (recurring_id, month, reason)
VALUES ($1, $2, $3)
ON CONFLICT (recurring_id, month) DO UPDATE SET reason = $3`,
id, month, reason)
return err
}
func (r *recurringRepo) Unignore(ctx context.Context, id int, month string) error {
_, err := r.db.Exec(ctx, `DELETE FROM recurring_ignores WHERE recurring_id = $1 AND month = $2`, id, month)
return err
}
@@ -0,0 +1,122 @@
package repository
import (
"context"
"errors"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgxpool"
"financeiro-carvalho/internal/model"
)
type ManualTransactionRepository interface {
List(ctx context.Context, month string) ([]model.Transaction, error)
GetByID(ctx context.Context, id int) (*model.Transaction, error)
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)
}
type manualTxRepo struct{ db *pgxpool.Pool }
func NewManualTransactionRepository(db *pgxpool.Pool) ManualTransactionRepository {
return &manualTxRepo{db: db}
}
func (r *manualTxRepo) List(ctx context.Context, month string) ([]model.Transaction, error) {
query := `
SELECT id, date::text, amount, description, type, source, category_id, created_at, updated_at
FROM transactions`
args := []any{}
if month != "" {
query += ` WHERE TO_CHAR(date, 'YYYY-MM') = $1`
args = append(args, month)
}
query += ` ORDER BY date DESC, id DESC`
rows, err := r.db.Query(ctx, query, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var out []model.Transaction
for rows.Next() {
var t model.Transaction
if err := rows.Scan(&t.ID, &t.Date, &t.Amount, &t.Description, &t.Type, &t.Source, &t.CategoryID, &t.CreatedAt, &t.UpdatedAt); err != nil {
return nil, err
}
out = append(out, t)
}
return out, rows.Err()
}
func (r *manualTxRepo) GetByID(ctx context.Context, id int) (*model.Transaction, error) {
var t model.Transaction
err := r.db.QueryRow(ctx, `
SELECT id, date::text, amount, description, type, source, category_id, created_at, updated_at
FROM transactions WHERE id = $1`, id).
Scan(&t.ID, &t.Date, &t.Amount, &t.Description, &t.Type, &t.Source, &t.CategoryID, &t.CreatedAt, &t.UpdatedAt)
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrNotFound
}
return &t, err
}
func (r *manualTxRepo) Create(ctx context.Context, t model.Transaction) (*model.Transaction, error) {
var out model.Transaction
err := r.db.QueryRow(ctx, `
INSERT INTO transactions (date, amount, description, type, source, category_id)
VALUES ($1, $2, $3, $4, 'manual', $5)
RETURNING id, date::text, amount, description, type, source, category_id, created_at, updated_at`,
t.Date, t.Amount, t.Description, t.Type, t.CategoryID).
Scan(&out.ID, &out.Date, &out.Amount, &out.Description, &out.Type, &out.Source, &out.CategoryID, &out.CreatedAt, &out.UpdatedAt)
return &out, err
}
func (r *manualTxRepo) Update(ctx context.Context, t model.Transaction) (*model.Transaction, error) {
var out model.Transaction
err := r.db.QueryRow(ctx, `
UPDATE transactions
SET date = $1, amount = $2, description = $3, type = $4, category_id = $5, updated_at = NOW()
WHERE id = $6 AND source = 'manual'
RETURNING id, date::text, amount, description, type, source, category_id, created_at, updated_at`,
t.Date, t.Amount, t.Description, t.Type, t.CategoryID, t.ID).
Scan(&out.ID, &out.Date, &out.Amount, &out.Description, &out.Type, &out.Source, &out.CategoryID, &out.CreatedAt, &out.UpdatedAt)
if errors.Is(err, pgx.ErrNoRows) {
return nil, ErrNotFound
}
return &out, err
}
func (r *manualTxRepo) Delete(ctx context.Context, id int) error {
tag, err := r.db.Exec(ctx, `DELETE FROM transactions WHERE id = $1 AND source = 'manual'`, id)
if err != nil {
return err
}
if tag.RowsAffected() == 0 {
return ErrNotFound
}
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 {
// No category set — cannot auto-match, always report as uncovered
return false, nil
}
return count > 0, err
}
+107
View File
@@ -0,0 +1,107 @@
package service
import (
"context"
"errors"
"strings"
"financeiro-carvalho/internal/model"
"financeiro-carvalho/internal/repository"
)
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")
)
type RecurringService struct {
repo repository.RecurringRepository
txRepo repository.ManualTransactionRepository
}
func NewRecurringService(repo repository.RecurringRepository, txRepo repository.ManualTransactionRepository) *RecurringService {
return &RecurringService{repo: repo, txRepo: txRepo}
}
func (s *RecurringService) List(ctx context.Context) ([]model.RecurringExpense, error) {
items, err := s.repo.List(ctx)
if items == nil {
return []model.RecurringExpense{}, err
}
return items, err
}
func (s *RecurringService) Create(ctx context.Context, in model.RecurringInput) (*model.RecurringExpense, error) {
if err := validateRecurringInput(in); err != nil {
return nil, err
}
in.Name = strings.TrimSpace(in.Name)
return s.repo.Create(ctx, in)
}
func (s *RecurringService) Update(ctx context.Context, id int, in model.RecurringInput) (*model.RecurringExpense, error) {
if err := validateRecurringInput(in); err != nil {
return nil, err
}
in.Name = strings.TrimSpace(in.Name)
return s.repo.Update(ctx, id, in)
}
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).
func (s *RecurringService) MonthlyStatus(ctx context.Context, month string) ([]model.RecurringStatus, error) {
items, err := s.repo.List(ctx)
if err != nil {
return nil, err
}
var result []model.RecurringStatus
for _, re := range items {
if !re.Active {
continue
}
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)
if err != nil {
return nil, err
}
}
result = append(result, model.RecurringStatus{
RecurringExpense: re,
Covered: covered || ignored,
Ignored: ignored,
Reason: reason,
})
}
return result, nil
}
func (s *RecurringService) Ignore(ctx context.Context, id int, month, reason string) error {
return s.repo.Ignore(ctx, id, month, reason)
}
func (s *RecurringService) Unignore(ctx context.Context, id int, month string) error {
return s.repo.Unignore(ctx, id, month)
}
func validateRecurringInput(in model.RecurringInput) error {
if strings.TrimSpace(in.Name) == "" {
return ErrRecurringEmptyName
}
if in.ExpectedAmount <= 0 {
return ErrRecurringInvalidAmount
}
if in.DayOfMonth < 1 || in.DayOfMonth > 31 {
return ErrRecurringInvalidDayOfMonth
}
return nil
}
+153
View File
@@ -0,0 +1,153 @@
package service_test
import (
"context"
"testing"
"financeiro-carvalho/internal/model"
"financeiro-carvalho/internal/repository"
"financeiro-carvalho/internal/service"
)
// ── mock recurring repo ──────────────────────────────────────────────────────
type mockRecurringRepo struct {
items []model.RecurringExpense
ignores map[string]string // "id:month" → reason
}
func newMockRecurring(items []model.RecurringExpense) *mockRecurringRepo {
return &mockRecurringRepo{items: items, ignores: map[string]string{}}
}
func (m *mockRecurringRepo) List(_ context.Context) ([]model.RecurringExpense, error) {
return m.items, nil
}
func (m *mockRecurringRepo) GetByID(_ context.Context, id int) (*model.RecurringExpense, error) {
for _, r := range m.items {
if r.ID == id {
cp := r
return &cp, nil
}
}
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}
m.items = append(m.items, r)
return &r, nil
}
func (m *mockRecurringRepo) Update(_ context.Context, id int, in model.RecurringInput) (*model.RecurringExpense, error) {
for i, r := range m.items {
if r.ID == id {
m.items[i].Name = in.Name
m.items[i].ExpectedAmount = in.ExpectedAmount
cp := m.items[i]
return &cp, nil
}
}
return nil, repository.ErrNotFound
}
func (m *mockRecurringRepo) Delete(_ context.Context, id int) error { return nil }
func (m *mockRecurringRepo) IsIgnored(_ context.Context, id int, month string) (bool, string, error) {
key := string(rune(id)) + ":" + month
r, ok := m.ignores[key]
return ok, r, nil
}
func (m *mockRecurringRepo) Ignore(_ context.Context, id int, month, reason string) error {
m.ignores[string(rune(id))+":"+month] = reason
return nil
}
func (m *mockRecurringRepo) Unignore(_ context.Context, id int, month string) error {
delete(m.ignores, string(rune(id))+":"+month)
return nil
}
// ── mock tx repo ─────────────────────────────────────────────────────────────
type mockTxRepo struct {
matchResult bool
}
func (m *mockTxRepo) List(_ context.Context, _ string) ([]model.Transaction, error) { return nil, nil }
func (m *mockTxRepo) GetByID(_ context.Context, _ int) (*model.Transaction, error) { return nil, nil }
func (m *mockTxRepo) Create(_ context.Context, t model.Transaction) (*model.Transaction, error) {
return &t, nil
}
func (m *mockTxRepo) Update(_ context.Context, t model.Transaction) (*model.Transaction, error) {
return &t, nil
}
func (m *mockTxRepo) Delete(_ context.Context, _ int) error { return nil }
func (m *mockTxRepo) HasMatchingTransaction(_ context.Context, _ *int, _ string, _ float64) (bool, error) {
return m.matchResult, nil
}
// ── tests ─────────────────────────────────────────────────────────────────────
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}
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("expected Covered=true")
}
}
func TestMonthlyStatus_Uncovered(t *testing.T) {
re := model.RecurringExpense{ID: 2, Name: "Netflix", ExpectedAmount: 55, DayOfMonth: 10, CategoryID: &catID, Active: true}
svc := service.NewRecurringService(newMockRecurring([]model.RecurringExpense{re}), &mockTxRepo{matchResult: false})
statuses, err := svc.MonthlyStatus(context.Background(), "2024-03")
if err != nil {
t.Fatal(err)
}
if statuses[0].Covered {
t.Error("expected Covered=false")
}
}
func TestMonthlyStatus_Ignored_CountsAsCovered(t *testing.T) {
re := model.RecurringExpense{ID: 3, Name: "Seguro", ExpectedAmount: 200, DayOfMonth: 1, CategoryID: &catID, Active: true}
repo := newMockRecurring([]model.RecurringExpense{re})
_ = repo.Ignore(context.Background(), 3, "2024-03", "viagem")
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("ignored should count as covered")
}
if !statuses[0].Ignored {
t.Error("expected Ignored=true")
}
}
func TestCreateRecurring_Validation(t *testing.T) {
svc := service.NewRecurringService(newMockRecurring(nil), &mockTxRepo{})
_, err := svc.Create(context.Background(), model.RecurringInput{Name: "", ExpectedAmount: 100, DayOfMonth: 5})
if err != service.ErrRecurringEmptyName {
t.Fatalf("expected ErrRecurringEmptyName, got %v", err)
}
_, err = svc.Create(context.Background(), model.RecurringInput{Name: "X", ExpectedAmount: -1, DayOfMonth: 5})
if err != service.ErrRecurringInvalidAmount {
t.Fatalf("expected ErrRecurringInvalidAmount, got %v", err)
}
_, err = svc.Create(context.Background(), model.RecurringInput{Name: "X", ExpectedAmount: 100, DayOfMonth: 0})
if err != service.ErrRecurringInvalidDayOfMonth {
t.Fatalf("expected ErrRecurringInvalidDayOfMonth, got %v", err)
}
}
+74
View File
@@ -0,0 +1,74 @@
package service
import (
"context"
"errors"
"strings"
"financeiro-carvalho/internal/model"
"financeiro-carvalho/internal/repository"
)
var (
ErrInvalidTransactionType = errors.New("type must be 'income' or 'expense'")
ErrCannotEditImported = errors.New("imported transactions cannot be edited or deleted")
ErrTransactionEmptyDesc = errors.New("description is required")
ErrTransactionInvalidAmount = errors.New("amount must be greater than zero")
)
type TransactionService struct {
repo repository.ManualTransactionRepository
}
func NewTransactionService(repo repository.ManualTransactionRepository) *TransactionService {
return &TransactionService{repo: repo}
}
func (s *TransactionService) List(ctx context.Context, month string) ([]model.Transaction, error) {
txs, err := s.repo.List(ctx, month)
if txs == nil {
return []model.Transaction{}, err
}
return txs, err
}
func (s *TransactionService) Create(ctx context.Context, t model.Transaction) (*model.Transaction, error) {
if err := validateTransaction(t); err != nil {
return nil, err
}
t.Description = strings.TrimSpace(t.Description)
return s.repo.Create(ctx, t)
}
func (s *TransactionService) Update(ctx context.Context, t model.Transaction) (*model.Transaction, error) {
if err := validateTransaction(t); err != nil {
return nil, err
}
t.Description = strings.TrimSpace(t.Description)
out, err := s.repo.Update(ctx, t)
if errors.Is(err, repository.ErrNotFound) {
return nil, ErrCannotEditImported
}
return out, err
}
func (s *TransactionService) Delete(ctx context.Context, id int) error {
err := s.repo.Delete(ctx, id)
if errors.Is(err, repository.ErrNotFound) {
return ErrCannotEditImported
}
return err
}
func validateTransaction(t model.Transaction) error {
if strings.TrimSpace(t.Description) == "" {
return ErrTransactionEmptyDesc
}
if t.Amount <= 0 {
return ErrTransactionInvalidAmount
}
if t.Type != "income" && t.Type != "expense" {
return ErrInvalidTransactionType
}
return nil
}