Files
carvalho-finances/apps/api/internal/repository/transaction.go
T
Mlcavalho1andClaude Sonnet 4.6 00356971c7 feat: #46 auto-categorização no import por histórico de descrições
- TransactionRepository.CategoryByDescription: busca a categoria mais
  recente para cada descrição normalizada no histórico do perfil
- ImportService.Preview: pré-preenche category_id em linhas não-duplicatas
  cuja descrição já foi categorizada antes (case-insensitive)
- Duplicatas não recebem sugestão
- 2 novos testes: auto-categorize + duplicate-not-categorized

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
2026-05-28 22:49:44 -03:00

130 lines
3.8 KiB
Go

package repository
import (
"context"
"strings"
"github.com/jackc/pgx/v5/pgxpool"
"financeiro-carvalho/internal/middleware"
"financeiro-carvalho/internal/model"
)
type TransactionRepository interface {
IsDuplicate(ctx context.Context, date, description string, amount float64) (bool, error)
IsExternalIDKnown(ctx context.Context, externalID string) (bool, error)
BulkInsert(ctx context.Context, rows []model.ImportRow) (int, error)
SaveImportLog(ctx context.Context, filename, format string, imported, duplicates, errors int) error
// CategoryByDescription returns the most recently used category_id for each normalized description.
CategoryByDescription(ctx context.Context, descriptions []string) (map[string]int, error)
}
type transactionRepo struct{ db *pgxpool.Pool }
func NewTransactionRepository(db *pgxpool.Pool) TransactionRepository {
return &transactionRepo{db: db}
}
func normalizeDesc(s string) string {
return strings.ToLower(strings.Join(strings.Fields(s), " "))
}
func (r *transactionRepo) IsDuplicate(ctx context.Context, date, description string, amount float64) (bool, error) {
pid := middleware.ProfileIDFromCtx(ctx)
var count int
err := r.db.QueryRow(ctx, `
SELECT COUNT(*) FROM transactions
WHERE date = $1 AND amount = $2 AND LOWER(description) = $3 AND profile_id = $4`,
date, amount, normalizeDesc(description), pid).Scan(&count)
return count > 0, err
}
func (r *transactionRepo) IsExternalIDKnown(ctx context.Context, externalID string) (bool, error) {
if externalID == "" {
return false, nil
}
pid := middleware.ProfileIDFromCtx(ctx)
var count int
err := r.db.QueryRow(ctx, `
SELECT COUNT(*) FROM transactions WHERE external_id = $1 AND profile_id = $2`,
externalID, pid).Scan(&count)
return count > 0, err
}
func (r *transactionRepo) BulkInsert(ctx context.Context, rows []model.ImportRow) (int, error) {
pid := middleware.ProfileIDFromCtx(ctx)
tx, err := r.db.Begin(ctx)
if err != nil {
return 0, err
}
defer tx.Rollback(ctx)
count := 0
for _, row := range rows {
if row.IsDuplicate {
continue
}
var extID *string
if row.ExternalID != "" {
extID = &row.ExternalID
}
var origDate *string
if row.OriginalDate != "" {
origDate = &row.OriginalDate
}
_, err := tx.Exec(ctx, `
INSERT INTO transactions (date, original_date, amount, description, type, source, external_id, category_id, profile_id)
VALUES ($1, $2, $3, $4, $5, 'import', $6, $7, $8)`,
row.Date, origDate, row.Amount, row.Description, row.Type, extID, row.CategoryID, pid)
if err != nil {
return count, err
}
count++
}
return count, tx.Commit(ctx)
}
func (r *transactionRepo) CategoryByDescription(ctx context.Context, descriptions []string) (map[string]int, error) {
if len(descriptions) == 0 {
return map[string]int{}, nil
}
pid := middleware.ProfileIDFromCtx(ctx)
normed := make([]string, len(descriptions))
for i, d := range descriptions {
normed[i] = normalizeDesc(d)
}
rows, err := r.db.Query(ctx, `
SELECT DISTINCT ON (LOWER(description)) LOWER(description), category_id
FROM transactions
WHERE profile_id = $1
AND category_id IS NOT NULL
AND LOWER(description) = ANY($2)
ORDER BY LOWER(description), date DESC, id DESC
`, pid, normed)
if err != nil {
return nil, err
}
defer rows.Close()
out := make(map[string]int)
for rows.Next() {
var desc string
var catID int
if err := rows.Scan(&desc, &catID); err != nil {
return nil, err
}
out[desc] = catID
}
return out, rows.Err()
}
func (r *transactionRepo) SaveImportLog(ctx context.Context, filename, format string, imported, duplicates, errors int) error {
_, err := r.db.Exec(ctx, `
INSERT INTO import_logs (filename, format, imported_count, duplicate_count, error_count)
VALUES ($1, $2, $3, $4, $5)`,
filename, format, imported, duplicates, errors)
return err
}