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:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
@@ -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;
|
||||
@@ -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"`
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
@@ -7,7 +7,9 @@
|
||||
<nav class="app-nav">
|
||||
<RouterLink to="/">Início</RouterLink>
|
||||
<RouterLink to="/categorias">Categorias</RouterLink>
|
||||
<RouterLink to="/transacoes">Transações</RouterLink>
|
||||
<RouterLink to="/importar">Importar</RouterLink>
|
||||
<RouterLink to="/configuracoes">Configurações</RouterLink>
|
||||
</nav>
|
||||
</header>
|
||||
<RouterView />
|
||||
|
||||
@@ -19,6 +19,16 @@ const router = createRouter({
|
||||
name: 'import',
|
||||
component: () => import('../views/ImportView.vue'),
|
||||
},
|
||||
{
|
||||
path: '/transacoes',
|
||||
name: 'transactions',
|
||||
component: () => import('../views/TransactionsView.vue'),
|
||||
},
|
||||
{
|
||||
path: '/configuracoes',
|
||||
name: 'settings',
|
||||
component: () => import('../views/SettingsView.vue'),
|
||||
},
|
||||
],
|
||||
})
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import { api } from '@/services/api'
|
||||
import type { Category } from './categories'
|
||||
|
||||
export interface RecurringExpense {
|
||||
id: number
|
||||
name: string
|
||||
expected_amount: number
|
||||
day_of_month: number
|
||||
category_id: number | null
|
||||
active: boolean
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export interface RecurringStatus extends RecurringExpense {
|
||||
covered: boolean
|
||||
ignored: boolean
|
||||
reason?: string
|
||||
}
|
||||
|
||||
export const useRecurringStore = defineStore('recurring', () => {
|
||||
const items = ref<RecurringExpense[]>([])
|
||||
const monthlyStatus = ref<RecurringStatus[]>([])
|
||||
const loading = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
|
||||
async function fetchAll() {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
items.value = await api.get<RecurringExpense[]>('/recurring')
|
||||
} catch (e: any) {
|
||||
error.value = e.message
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchMonthlyStatus(month: string) {
|
||||
try {
|
||||
monthlyStatus.value = await api.get<RecurringStatus[]>(`/recurring/status?month=${month}`)
|
||||
} catch {
|
||||
monthlyStatus.value = []
|
||||
}
|
||||
}
|
||||
|
||||
async function create(input: Omit<RecurringExpense, 'id' | 'active' | 'created_at' | 'updated_at'>) {
|
||||
const item = await api.post<RecurringExpense>('/recurring', input)
|
||||
items.value.push(item)
|
||||
return item
|
||||
}
|
||||
|
||||
async function update(id: number, input: Omit<RecurringExpense, 'id' | 'active' | 'created_at' | 'updated_at'>) {
|
||||
const item = await api.put<RecurringExpense>(`/recurring/${id}`, input)
|
||||
const idx = items.value.findIndex((x) => x.id === id)
|
||||
if (idx !== -1) items.value[idx] = item
|
||||
return item
|
||||
}
|
||||
|
||||
async function remove(id: number) {
|
||||
await api.delete(`/recurring/${id}`)
|
||||
items.value = items.value.filter((x) => x.id !== id)
|
||||
}
|
||||
|
||||
async function ignore(id: number, month: string, reason: string) {
|
||||
await api.post(`/recurring/${id}/ignore`, { month, reason })
|
||||
await fetchMonthlyStatus(month)
|
||||
}
|
||||
|
||||
async function unignore(id: number, month: string) {
|
||||
await api.delete(`/recurring/${id}/ignore?month=${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 }
|
||||
})
|
||||
@@ -0,0 +1,62 @@
|
||||
import { defineStore } from 'pinia'
|
||||
import { ref } from 'vue'
|
||||
import { api } from '@/services/api'
|
||||
|
||||
export interface Transaction {
|
||||
id: number
|
||||
date: string
|
||||
amount: number
|
||||
description: string
|
||||
type: 'income' | 'expense'
|
||||
source: 'manual' | 'import'
|
||||
category_id: number | null
|
||||
created_at: string
|
||||
updated_at: string
|
||||
}
|
||||
|
||||
export interface TransactionInput {
|
||||
date: string
|
||||
amount: number
|
||||
description: string
|
||||
type: 'income' | 'expense'
|
||||
category_id: number | null
|
||||
}
|
||||
|
||||
export const useTransactionsStore = defineStore('transactions', () => {
|
||||
const transactions = ref<Transaction[]>([])
|
||||
const loading = ref(false)
|
||||
const error = ref<string | null>(null)
|
||||
|
||||
async function fetchAll(month?: string) {
|
||||
loading.value = true
|
||||
error.value = null
|
||||
try {
|
||||
const q = month ? `?month=${month}` : ''
|
||||
transactions.value = await api.get<Transaction[]>(`/transactions${q}`)
|
||||
} catch (e: any) {
|
||||
error.value = e.message
|
||||
} finally {
|
||||
loading.value = false
|
||||
}
|
||||
}
|
||||
|
||||
async function create(input: TransactionInput) {
|
||||
const t = await api.post<Transaction>('/transactions', input)
|
||||
transactions.value.unshift(t)
|
||||
return t
|
||||
}
|
||||
|
||||
async function update(id: number, input: TransactionInput) {
|
||||
const t = await api.put<Transaction>(`/transactions/${id}`, input)
|
||||
const idx = transactions.value.findIndex((x) => x.id === id)
|
||||
if (idx !== -1) transactions.value[idx] = t
|
||||
return t
|
||||
}
|
||||
|
||||
async function remove(id: number) {
|
||||
await api.delete(`/transactions/${id}`)
|
||||
transactions.value = transactions.value.filter((x) => x.id !== id)
|
||||
}
|
||||
|
||||
return { transactions, loading, error, fetchAll, create, update, remove }
|
||||
})
|
||||
@@ -0,0 +1,138 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, onMounted } from 'vue'
|
||||
import { useRecurringStore } from '@/stores/recurring'
|
||||
import { useCategoriesStore } from '@/stores/categories'
|
||||
|
||||
const store = useRecurringStore()
|
||||
const catStore = useCategoriesStore()
|
||||
|
||||
onMounted(() => {
|
||||
store.fetchAll()
|
||||
catStore.fetchAll()
|
||||
})
|
||||
|
||||
const blank = () => ({ name: '', expected_amount: 0, day_of_month: 1, category_id: null as number | null })
|
||||
const form = ref(blank())
|
||||
const editId = ref<number | null>(null)
|
||||
const amountRaw = ref('')
|
||||
const formError = ref<string | null>(null)
|
||||
|
||||
function parseAmount(s: string) {
|
||||
return parseFloat(s.replace(/\./g, '').replace(',', '.')) || 0
|
||||
}
|
||||
|
||||
function startEdit(id: number) {
|
||||
const item = store.items.find((x) => x.id === id)
|
||||
if (!item) return
|
||||
editId.value = id
|
||||
form.value = { name: item.name, expected_amount: item.expected_amount, day_of_month: item.day_of_month, category_id: item.category_id }
|
||||
amountRaw.value = item.expected_amount.toLocaleString('pt-BR', { minimumFractionDigits: 2 })
|
||||
formError.value = null
|
||||
}
|
||||
|
||||
function cancelEdit() {
|
||||
editId.value = null
|
||||
form.value = blank()
|
||||
amountRaw.value = ''
|
||||
formError.value = null
|
||||
}
|
||||
|
||||
async function submit() {
|
||||
formError.value = null
|
||||
form.value.expected_amount = parseAmount(amountRaw.value)
|
||||
try {
|
||||
if (editId.value !== null) {
|
||||
await store.update(editId.value, form.value)
|
||||
cancelEdit()
|
||||
} else {
|
||||
await store.create(form.value)
|
||||
form.value = blank()
|
||||
amountRaw.value = ''
|
||||
}
|
||||
} catch (e: any) {
|
||||
formError.value = e.message
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(id: number, name: string) {
|
||||
if (!confirm(`Excluir recorrência "${name}"?`)) return
|
||||
await store.remove(id)
|
||||
}
|
||||
|
||||
function catName(id: number | null) {
|
||||
if (!id) return '—'
|
||||
return catStore.categories.find((c) => c.id === id)?.name ?? '—'
|
||||
}
|
||||
|
||||
function fmt(v: number) {
|
||||
return v.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' })
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="page">
|
||||
<h1>Configurações — Recorrências</h1>
|
||||
|
||||
<form class="form-card" @submit.prevent="submit">
|
||||
<h2>{{ editId !== null ? 'Editar recorrência' : 'Nova recorrência' }}</h2>
|
||||
<div class="form-row">
|
||||
<input v-model="form.name" placeholder="Nome (ex: Netflix)" required class="input-name" />
|
||||
<input v-model="amountRaw" placeholder="55,90" required class="input-sm input-amount" />
|
||||
<input type="number" v-model.number="form.day_of_month" min="1" max="31" class="input-sm input-day" placeholder="Dia" />
|
||||
<select v-model="form.category_id" class="input-sm">
|
||||
<option :value="null">Sem categoria</option>
|
||||
<option v-for="c in catStore.categories" :key="c.id" :value="c.id">{{ c.name }}</option>
|
||||
</select>
|
||||
</div>
|
||||
<p v-if="formError" class="form-error">{{ formError }}</p>
|
||||
<div class="form-actions">
|
||||
<button type="submit" class="btn btn-primary">{{ editId !== null ? 'Salvar' : 'Adicionar' }}</button>
|
||||
<button v-if="editId !== null" type="button" class="btn btn-ghost" @click="cancelEdit">Cancelar</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<p v-if="store.loading">Carregando…</p>
|
||||
|
||||
<ul v-else class="recurring-list">
|
||||
<li v-for="item in store.items" :key="item.id" class="recurring-item" :class="{ editing: editId === item.id }">
|
||||
<div class="item-info">
|
||||
<strong>{{ item.name }}</strong>
|
||||
<span class="item-meta">
|
||||
Todo dia {{ item.day_of_month }} · {{ fmt(item.expected_amount) }} · {{ catName(item.category_id) }}
|
||||
</span>
|
||||
</div>
|
||||
<div class="item-actions">
|
||||
<button class="btn btn-sm" @click="startEdit(item.id)">Editar</button>
|
||||
<button class="btn btn-sm btn-danger" @click="remove(item.id, item.name)">Excluir</button>
|
||||
</div>
|
||||
</li>
|
||||
<li v-if="store.items.length === 0" class="empty">Nenhuma recorrência cadastrada</li>
|
||||
</ul>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.page { max-width: 640px; margin: 0 auto; padding: 1.5rem 1rem; font-family: sans-serif; }
|
||||
h1 { font-size: 1.5rem; margin-bottom: 1.25rem; }
|
||||
h2 { font-size: 0.95rem; font-weight: 600; margin: 0 0 0.75rem; }
|
||||
.form-card { background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 8px; padding: 1rem; margin-bottom: 1.5rem; }
|
||||
.form-row { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }
|
||||
.input-name { flex: 1; min-width: 150px; padding: 0.45rem 0.6rem; border: 1px solid #d1d5db; border-radius: 6px; font-size: 0.875rem; }
|
||||
.input-sm { padding: 0.45rem 0.6rem; border: 1px solid #d1d5db; border-radius: 6px; font-size: 0.875rem; }
|
||||
.input-amount { width: 90px; }
|
||||
.input-day { width: 60px; }
|
||||
.form-error { color: #dc2626; font-size: 0.875rem; margin: 0.5rem 0 0; }
|
||||
.form-actions { margin-top: 0.75rem; display: flex; gap: 0.5rem; }
|
||||
.btn { padding: 0.4rem 1rem; border: 1px solid #d1d5db; border-radius: 6px; cursor: pointer; font-size: 0.875rem; background: #fff; }
|
||||
.btn-primary { background: #4f46e5; color: #fff; border-color: #4f46e5; }
|
||||
.btn-ghost { background: transparent; }
|
||||
.btn-sm { padding: 0.25rem 0.6rem; }
|
||||
.btn-danger { color: #dc2626; border-color: #fca5a5; }
|
||||
.recurring-list { list-style: none; padding: 0; margin: 0; display: flex; flex-direction: column; gap: 0.5rem; }
|
||||
.recurring-item { display: flex; align-items: center; justify-content: space-between; padding: 0.75rem; border: 1px solid #e5e7eb; border-radius: 8px; background: #fff; flex-wrap: wrap; gap: 0.5rem; }
|
||||
.recurring-item.editing { border-color: #4f46e5; box-shadow: 0 0 0 2px #e0e7ff; }
|
||||
.item-info { display: flex; flex-direction: column; gap: 0.2rem; }
|
||||
.item-meta { font-size: 0.8rem; color: #6b7280; }
|
||||
.item-actions { display: flex; gap: 0.4rem; }
|
||||
.empty { color: #9ca3af; text-align: center; padding: 2rem; }
|
||||
</style>
|
||||
@@ -0,0 +1,202 @@
|
||||
<script setup lang="ts">
|
||||
import { ref, computed, onMounted } from 'vue'
|
||||
import { useTransactionsStore, type TransactionInput } from '@/stores/transactions'
|
||||
import { useCategoriesStore } from '@/stores/categories'
|
||||
|
||||
const store = useTransactionsStore()
|
||||
const catStore = useCategoriesStore()
|
||||
|
||||
const currentMonth = ref(new Date().toISOString().slice(0, 7))
|
||||
|
||||
onMounted(() => {
|
||||
store.fetchAll(currentMonth.value)
|
||||
catStore.fetchAll()
|
||||
})
|
||||
|
||||
function changeMonth(delta: number) {
|
||||
const [y, m] = currentMonth.value.split('-').map(Number)
|
||||
const d = new Date(y, m - 1 + delta, 1)
|
||||
currentMonth.value = d.toISOString().slice(0, 7)
|
||||
store.fetchAll(currentMonth.value)
|
||||
}
|
||||
|
||||
const blank = (): TransactionInput => ({
|
||||
date: new Date().toISOString().slice(0, 10),
|
||||
amount: 0,
|
||||
description: '',
|
||||
type: 'expense',
|
||||
category_id: null,
|
||||
})
|
||||
|
||||
const form = ref(blank())
|
||||
const editId = ref<number | null>(null)
|
||||
const formError = ref<string | null>(null)
|
||||
const amountRaw = ref('')
|
||||
|
||||
function parseAmount(s: string): number {
|
||||
return parseFloat(s.replace(/\./g, '').replace(',', '.')) || 0
|
||||
}
|
||||
|
||||
function startEdit(id: number) {
|
||||
const t = store.transactions.find((x) => x.id === id)
|
||||
if (!t || t.source !== 'manual') return
|
||||
editId.value = id
|
||||
form.value = { date: t.date, amount: t.amount, description: t.description, type: t.type as any, category_id: t.category_id }
|
||||
amountRaw.value = t.amount.toLocaleString('pt-BR', { minimumFractionDigits: 2 })
|
||||
formError.value = null
|
||||
}
|
||||
|
||||
function cancelEdit() {
|
||||
editId.value = null
|
||||
form.value = blank()
|
||||
amountRaw.value = ''
|
||||
formError.value = null
|
||||
}
|
||||
|
||||
async function submit() {
|
||||
formError.value = null
|
||||
form.value.amount = parseAmount(amountRaw.value)
|
||||
try {
|
||||
if (editId.value !== null) {
|
||||
await store.update(editId.value, form.value)
|
||||
cancelEdit()
|
||||
} else {
|
||||
await store.create(form.value)
|
||||
form.value = blank()
|
||||
amountRaw.value = ''
|
||||
}
|
||||
} catch (e: any) {
|
||||
formError.value = e.message
|
||||
}
|
||||
}
|
||||
|
||||
async function remove(id: number) {
|
||||
if (!confirm('Excluir esta transação?')) return
|
||||
try {
|
||||
await store.remove(id)
|
||||
} catch (e: any) {
|
||||
alert(e.message)
|
||||
}
|
||||
}
|
||||
|
||||
function fmt(v: number) {
|
||||
return v.toLocaleString('pt-BR', { style: 'currency', currency: 'BRL' })
|
||||
}
|
||||
|
||||
function catName(id: number | null) {
|
||||
if (!id) return '—'
|
||||
return catStore.categories.find((c) => c.id === id)?.name ?? '—'
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="page">
|
||||
<div class="page-header">
|
||||
<h1>Transações</h1>
|
||||
<div class="month-nav">
|
||||
<button class="btn" @click="changeMonth(-1)">‹</button>
|
||||
<span class="month-label">{{ currentMonth }}</span>
|
||||
<button class="btn" @click="changeMonth(1)">›</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<form class="form-card" @submit.prevent="submit">
|
||||
<h2>{{ editId !== null ? 'Editar transação' : 'Nova transação' }}</h2>
|
||||
<div class="form-row">
|
||||
<input type="date" v-model="form.date" required class="input-sm" />
|
||||
<select v-model="form.type" class="input-sm">
|
||||
<option value="expense">Gasto</option>
|
||||
<option value="income">Receita</option>
|
||||
</select>
|
||||
<input
|
||||
v-model="amountRaw"
|
||||
placeholder="150,90"
|
||||
required
|
||||
class="input-sm input-amount"
|
||||
/>
|
||||
<select v-model="form.category_id" class="input-sm">
|
||||
<option :value="null">Sem categoria</option>
|
||||
<option v-for="c in catStore.categories" :key="c.id" :value="c.id">{{ c.name }}</option>
|
||||
</select>
|
||||
<input v-model="form.description" placeholder="Descrição" required class="input-desc" />
|
||||
</div>
|
||||
<p v-if="formError" class="form-error">{{ formError }}</p>
|
||||
<div class="form-actions">
|
||||
<button type="submit" class="btn btn-primary">{{ editId !== null ? 'Salvar' : 'Adicionar' }}</button>
|
||||
<button v-if="editId !== null" type="button" class="btn btn-ghost" @click="cancelEdit">Cancelar</button>
|
||||
</div>
|
||||
</form>
|
||||
|
||||
<p v-if="store.loading">Carregando…</p>
|
||||
|
||||
<div v-else class="table-wrap">
|
||||
<table class="tx-table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Data</th>
|
||||
<th>Descrição</th>
|
||||
<th>Categoria</th>
|
||||
<th>Valor</th>
|
||||
<th>Origem</th>
|
||||
<th></th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
<tr v-for="t in store.transactions" :key="t.id" :class="{ 'row-editing': editId === t.id }">
|
||||
<td>{{ t.date }}</td>
|
||||
<td>{{ t.description }}</td>
|
||||
<td>{{ catName(t.category_id) }}</td>
|
||||
<td :class="t.type === 'income' ? 'amt-income' : 'amt-expense'">{{ fmt(t.amount) }}</td>
|
||||
<td>
|
||||
<span class="badge" :class="t.source === 'manual' ? 'badge-manual' : 'badge-import'">
|
||||
{{ t.source === 'manual' ? 'manual' : 'extrato' }}
|
||||
</span>
|
||||
</td>
|
||||
<td class="actions">
|
||||
<button v-if="t.source === 'manual'" class="btn btn-sm" @click="startEdit(t.id)">Editar</button>
|
||||
<button v-if="t.source === 'manual'" class="btn btn-sm btn-danger" @click="remove(t.id)">Excluir</button>
|
||||
</td>
|
||||
</tr>
|
||||
<tr v-if="store.transactions.length === 0">
|
||||
<td colspan="6" style="text-align:center;color:#9ca3af;padding:2rem">Nenhuma transação</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.page { max-width: 960px; margin: 0 auto; padding: 1.5rem 1rem; font-family: sans-serif; }
|
||||
.page-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 1.25rem; flex-wrap: wrap; gap: 0.75rem; }
|
||||
h1 { font-size: 1.5rem; margin: 0; }
|
||||
h2 { font-size: 0.95rem; font-weight: 600; margin: 0 0 0.75rem; }
|
||||
.month-nav { display: flex; align-items: center; gap: 0.5rem; }
|
||||
.month-label { font-size: 0.9rem; font-weight: 600; min-width: 6rem; text-align: center; }
|
||||
|
||||
.form-card { background: #f9fafb; border: 1px solid #e5e7eb; border-radius: 8px; padding: 1rem; margin-bottom: 1.5rem; }
|
||||
.form-row { display: flex; gap: 0.5rem; flex-wrap: wrap; align-items: center; }
|
||||
.input-sm { padding: 0.45rem 0.6rem; border: 1px solid #d1d5db; border-radius: 6px; font-size: 0.875rem; }
|
||||
.input-amount { width: 100px; }
|
||||
.input-desc { flex: 1; min-width: 160px; padding: 0.45rem 0.6rem; border: 1px solid #d1d5db; border-radius: 6px; font-size: 0.875rem; }
|
||||
.form-error { color: #dc2626; font-size: 0.875rem; margin: 0.5rem 0 0; }
|
||||
.form-actions { margin-top: 0.75rem; display: flex; gap: 0.5rem; }
|
||||
|
||||
.btn { padding: 0.4rem 1rem; border: 1px solid #d1d5db; border-radius: 6px; cursor: pointer; font-size: 0.875rem; background: #fff; }
|
||||
.btn-primary { background: #4f46e5; color: #fff; border-color: #4f46e5; }
|
||||
.btn-ghost { background: transparent; }
|
||||
.btn-sm { padding: 0.25rem 0.6rem; }
|
||||
.btn-danger { color: #dc2626; border-color: #fca5a5; }
|
||||
|
||||
.table-wrap { overflow-x: auto; }
|
||||
.tx-table { width: 100%; border-collapse: collapse; font-size: 0.875rem; }
|
||||
.tx-table th { text-align: left; padding: 0.5rem 0.75rem; background: #f9fafb; border-bottom: 1px solid #e5e7eb; white-space: nowrap; }
|
||||
.tx-table td { padding: 0.45rem 0.75rem; border-bottom: 1px solid #f3f4f6; }
|
||||
.row-editing { background: #eef2ff; }
|
||||
.amt-income { color: #059669; font-weight: 500; }
|
||||
.amt-expense { color: #dc2626; font-weight: 500; }
|
||||
.badge { font-size: 0.7rem; padding: 0.15rem 0.4rem; border-radius: 4px; }
|
||||
.badge-manual { background: #ede9fe; color: #5b21b6; }
|
||||
.badge-import { background: #dbeafe; color: #1e40af; }
|
||||
.actions { display: flex; gap: 0.4rem; white-space: nowrap; }
|
||||
</style>
|
||||
Reference in New Issue
Block a user