saveinmed/backend/internal/http/handler/financial_handler.go
Tiago Yamamoto 36d6fa4ae0 feat: Implement Phase 4 features
Backend (Go):
- FCM Push Notifications (fcm.go, push_handler.go)
- Credit Lines (credit_line.go, credit_handler.go)
- Payment Config (admin_handler.go, seller_payment_handler.go)
- Team Management (team_handler.go)

Backoffice (NestJS):
- Dashboard module (KPIs, revenue charts)
- Audit module (tracking changes)
- Disputes module (CRUD, resolution)
- Reports module (CSV export)
- Performance module (seller scores)
- Fraud module (detection, alerts)

Frontend (Marketplace):
- ThemeContext for Dark Mode
- HelpCenter page with FAQ
- OrderDetails with timeline
- Team management page
- Persistent cart (Zustand)
2025-12-27 10:07:05 -03:00

135 lines
3.6 KiB
Go

package handler
import (
"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)
}