saveinmed/backend/internal/http/handler/financial_handler.go
Gabbriiel 90467db1ec refactor: substitui backend Medusa por backend Go e corrige testes do marketplace
- Remove backend Medusa.js (TypeScript) e substitui pelo backend Go (saveinmed-performance-core)
- Corrige testes auth.test.ts: alinha paths de API (v1/ sem barra inicial) e campo access_token
- Corrige GroupedProductCard.test.tsx: ajusta distância formatada (toFixed) e troca userEvent por fireEvent com fakeTimers
- Corrige AuthContext.test.tsx: usa vi.hoisted() para mocks e corrige parênteses no waitFor

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
2026-02-17 04:56:37 -06:00

136 lines
3.6 KiB
Go

package handler
import (
"encoding/json"
"net/http"
"strconv"
)
// UploadDocument handles KYC doc upload.
func (h *Handler) UploadDocument(w http.ResponseWriter, r *http.Request) {
usr, err := h.getUserFromContext(r.Context())
if err != nil {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
var req struct {
Type string `json:"type"`
URL string `json:"url"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
doc, err := h.svc.UploadDocument(r.Context(), usr.CompanyID, req.Type, req.URL)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(doc)
}
// GetDocuments lists company KYC docs.
func (h *Handler) GetDocuments(w http.ResponseWriter, r *http.Request) {
usr, err := h.getUserFromContext(r.Context())
if err != nil {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
docs, err := h.svc.GetCompanyDocuments(r.Context(), usr.CompanyID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(docs)
}
// GetLedger returns financial history.
func (h *Handler) GetLedger(w http.ResponseWriter, r *http.Request) {
usr, err := h.getUserFromContext(r.Context())
if err != nil {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
page, _ := strconv.Atoi(r.URL.Query().Get("page"))
pageSize, _ := strconv.Atoi(r.URL.Query().Get("page_size"))
res, err := h.svc.GetFormattedLedger(r.Context(), usr.CompanyID, page, pageSize)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(res)
}
// GetBalance returns current wallet balance.
func (h *Handler) GetBalance(w http.ResponseWriter, r *http.Request) {
usr, err := h.getUserFromContext(r.Context())
if err != nil {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
bal, err := h.svc.GetBalance(r.Context(), usr.CompanyID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(map[string]int64{"balance_cents": bal})
}
// RequestWithdrawal initiates a payout.
func (h *Handler) RequestWithdrawal(w http.ResponseWriter, r *http.Request) {
usr, err := h.getUserFromContext(r.Context())
if err != nil {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
var req struct {
AmountCents int64 `json:"amount_cents"`
BankInfo string `json:"bank_info"`
}
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
http.Error(w, err.Error(), http.StatusBadRequest)
return
}
wd, err := h.svc.RequestWithdrawal(r.Context(), usr.CompanyID, req.AmountCents, req.BankInfo)
if err != nil {
http.Error(w, err.Error(), http.StatusBadRequest) // User error mostly (insufficient funds)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(wd)
}
// ListWithdrawals shows history of payouts.
func (h *Handler) ListWithdrawals(w http.ResponseWriter, r *http.Request) {
usr, err := h.getUserFromContext(r.Context())
if err != nil {
http.Error(w, "unauthorized", http.StatusUnauthorized)
return
}
wds, err := h.svc.ListWithdrawals(r.Context(), usr.CompanyID)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(wds)
}