- Validação cross-tenant no login e rotas protegidas
- File serving via /api/files/{bucket}/{path} (eliminação DNS)
- Mensagens de erro humanizadas inline (sem pop-ups)
- Middleware tenant detection via headers customizados
- Upload de logos retorna URLs via API
- README atualizado com changelog v1.4 completo
106 lines
2.8 KiB
Go
106 lines
2.8 KiB
Go
package handlers
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
|
|
"aggios-app/backend/internal/domain"
|
|
"aggios-app/backend/internal/service"
|
|
)
|
|
|
|
// TenantHandler handles tenant/agency listing endpoints
|
|
type TenantHandler struct {
|
|
tenantService *service.TenantService
|
|
}
|
|
|
|
// NewTenantHandler creates a new tenant handler
|
|
func NewTenantHandler(tenantService *service.TenantService) *TenantHandler {
|
|
return &TenantHandler{
|
|
tenantService: tenantService,
|
|
}
|
|
}
|
|
|
|
// ListAll lists all agencies/tenants (SUPERADMIN only)
|
|
func (h *TenantHandler) ListAll(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
tenants, err := h.tenantService.ListAll()
|
|
if err != nil {
|
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
if tenants == nil {
|
|
tenants = []*domain.Tenant{}
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
json.NewEncoder(w).Encode(tenants)
|
|
}
|
|
|
|
// CheckExists returns 200 if tenant exists by subdomain, otherwise 404
|
|
func (h *TenantHandler) CheckExists(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
subdomain := r.URL.Query().Get("subdomain")
|
|
if subdomain == "" {
|
|
http.Error(w, "subdomain is required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
_, err := h.tenantService.GetBySubdomain(subdomain)
|
|
if err != nil {
|
|
if err == service.ErrTenantNotFound {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
json.NewEncoder(w).Encode(map[string]string{"status": "ok"})
|
|
}
|
|
|
|
// GetPublicConfig returns public branding info for a tenant by subdomain
|
|
func (h *TenantHandler) GetPublicConfig(w http.ResponseWriter, r *http.Request) {
|
|
if r.Method != http.MethodGet {
|
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
|
return
|
|
}
|
|
|
|
subdomain := r.URL.Query().Get("subdomain")
|
|
if subdomain == "" {
|
|
http.Error(w, "subdomain is required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
tenant, err := h.tenantService.GetBySubdomain(subdomain)
|
|
if err != nil {
|
|
if err == service.ErrTenantNotFound {
|
|
http.NotFound(w, r)
|
|
return
|
|
}
|
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Return only public info
|
|
response := map[string]string{
|
|
"name": tenant.Name,
|
|
"primary_color": tenant.PrimaryColor,
|
|
"secondary_color": tenant.SecondaryColor,
|
|
"logo_url": tenant.LogoURL,
|
|
"logo_horizontal_url": tenant.LogoHorizontalURL,
|
|
}
|
|
|
|
w.Header().Set("Content-Type", "application/json; charset=utf-8")
|
|
json.NewEncoder(w).Encode(response)
|
|
}
|