feat: #44 fatura de cartão com data futura fica pendente até confirmação

- Nova tabela pending_bill_imports (migration 016)
- ImportHandler.Confirm: quando is_credit_card=true e payment_date > hoje,
  salva como pending em vez de inserir transações
- Novos endpoints: GET /pending-bills, POST /pending-bills/:id/confirm,
  DELETE /pending-bills/:id
- Dashboard inclui pending_bill_imports no payload
- Frontend: resultado "fatura salva como pendente" no ImportView
- AccountsView exibe widget de faturas pendentes com ações de confirmar/descartar
- dashboard_test: mock de PendingBillRepo + TransactionRepository

Co-Authored-By: Claude Sonnet 4.6 <[email protected]>
This commit is contained in:
2026-05-28 22:26:42 -03:00
co-authored by Claude Sonnet 4.6
parent 9742ae7469
commit 2b8524dcde
16 changed files with 453 additions and 32 deletions
+28 -6
View File
@@ -11,12 +11,13 @@ import (
const maxUploadSize = 10 << 20 // 10 MB
type ImportHandler struct {
svc *service.ImportService
gameSvc *service.GameService
svc *service.ImportService
gameSvc *service.GameService
pendingSvc *service.PendingBillService
}
func NewImportHandler(svc *service.ImportService, gameSvc *service.GameService) *ImportHandler {
return &ImportHandler{svc: svc, gameSvc: gameSvc}
func NewImportHandler(svc *service.ImportService, gameSvc *service.GameService, pendingSvc *service.PendingBillService) *ImportHandler {
return &ImportHandler{svc: svc, gameSvc: gameSvc, pendingSvc: pendingSvc}
}
// Preview parses the uploaded file and returns rows with duplicate flags.
@@ -59,11 +60,14 @@ func (h *ImportHandler) Preview(w http.ResponseWriter, r *http.Request) {
// Confirm saves the rows provided in the request body (after user review).
// POST /api/imports/confirm
// When is_credit_card=true and payment_date is in the future, saves as pending instead of transactions.
func (h *ImportHandler) Confirm(w http.ResponseWriter, r *http.Request) {
var body struct {
Filename string `json:"filename"`
Filename string `json:"filename"`
Rows []model.ImportRow `json:"rows"`
ParseErrCount int `json:"parse_error_count"`
ParseErrCount int `json:"parse_error_count"`
IsCreditCard bool `json:"is_credit_card"`
PaymentDate string `json:"payment_date"` // YYYY-MM-DD
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
respondError(w, http.StatusBadRequest, "invalid JSON")
@@ -74,6 +78,24 @@ func (h *ImportHandler) Confirm(w http.ResponseWriter, r *http.Request) {
return
}
if body.IsCreditCard && body.PaymentDate != "" {
saved, pending, err := h.pendingSvc.MaybeSaveAsPending(r.Context(), body.Filename, body.PaymentDate, body.Rows)
if err != nil {
respondError(w, http.StatusInternalServerError, "failed to save pending bill")
return
}
if saved {
respondJSON(w, http.StatusOK, map[string]any{
"pending": true,
"pending_bill": pending,
"imported": 0,
"duplicates": len(body.Rows) - len(pending.Rows),
"errors": body.ParseErrCount,
})
return
}
}
result, err := h.svc.Confirm(r.Context(), body.Filename, body.Rows, body.ParseErrCount)
if err != nil {
respondError(w, http.StatusInternalServerError, "failed to save transactions")
+59
View File
@@ -0,0 +1,59 @@
package handler
import (
"net/http"
"strconv"
"github.com/go-chi/chi/v5"
"financeiro-carvalho/internal/service"
)
type PendingBillHandler struct {
svc *service.PendingBillService
gameSvc *service.GameService
}
func NewPendingBillHandler(svc *service.PendingBillService, gameSvc *service.GameService) *PendingBillHandler {
return &PendingBillHandler{svc: svc, gameSvc: gameSvc}
}
func (h *PendingBillHandler) List(w http.ResponseWriter, r *http.Request) {
items, err := h.svc.List(r.Context())
if err != nil {
respondError(w, http.StatusInternalServerError, "failed to list pending bills")
return
}
respondJSON(w, http.StatusOK, items)
}
func (h *PendingBillHandler) Confirm(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.Confirm(r.Context(), id); err != nil {
if err == service.ErrPendingBillNotFound {
respondError(w, http.StatusNotFound, "pending bill not found")
return
}
respondError(w, http.StatusInternalServerError, "failed to confirm pending bill")
return
}
h.gameSvc.NotifyAction(r.Context(), "import_confirmed", map[string]any{"count": 1})
w.WriteHeader(http.StatusNoContent)
}
func (h *PendingBillHandler) Discard(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.Discard(r.Context(), id); err != nil {
respondError(w, http.StatusInternalServerError, "failed to discard pending bill")
return
}
w.WriteHeader(http.StatusNoContent)
}