Backend: - Implementa API de Endereços (`POST /enderecos`) e migration da tabela `addresses`. - Adiciona bloqueio de login para usuários de empresas não verificadas (status `pending`). - Criação automática do usuário Master (`seedAdmin`) com empresa verificada. - Adiciona aliases de rota em PT-BR (`/api/v1/empresas` GET/PATCH, `/api/v1/usuarios` PATCH) para compatibilidade com o frontend. - Atualiza DTOs para suportar campos em português no registro de empresas e atualização de usuários. - Endpoint `/auth/me` agora retorna `company_name` e flag `superadmin`. - Ajusta filtro de repositório para listar empresas por status de verificação. Frontend: - Nova página `/usuarios-pendentes` com layout padrão e funcionalidade de aprovação. - Atualiza [Header](cci:1://file:///c:/Projetos/saveinmed/saveinmed-frontend/src/components/Header.tsx:29:0-337:2) para exibir o nome da empresa do usuário logado. - Serviço `empresaApiService`: correções de mapeamento (`corporate_name` -> `razao_social`) e novos métodos. - Tipagem atualizada para incluir campos de empresa no [UserData](cci:2://file:///c:/Projetos/saveinmed/saveinmed-frontend/src/types/auth.ts:15:0-30:1). Fixes: - Correção de erro 405 (Method Not Allowed) nas rotas de atualização. - Correção de erro 404 na listagem de pendentes. - Resolução de campos vazios na listagem de empresas.
135 lines
3.6 KiB
Go
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 := jsonAPI.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")
|
|
jsonAPI.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")
|
|
jsonAPI.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")
|
|
jsonAPI.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")
|
|
jsonAPI.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 := jsonAPI.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")
|
|
jsonAPI.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")
|
|
jsonAPI.NewEncoder(w).Encode(wds)
|
|
}
|