feat: redesign superadmin agencies list, implement flat design, add date filters, and fix UI bugs
This commit is contained in:
@@ -5,7 +5,6 @@ import (
|
||||
"errors"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"aggios-app/backend/internal/config"
|
||||
@@ -13,6 +12,7 @@ import (
|
||||
"aggios-app/backend/internal/service"
|
||||
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
@@ -45,6 +45,8 @@ func (h *AgencyRegistrationHandler) RegisterAgency(w http.ResponseWriter, r *htt
|
||||
}
|
||||
|
||||
log.Printf("📥 Registering agency: %s (subdomain: %s)", req.AgencyName, req.Subdomain)
|
||||
log.Printf("📊 Payload received: RazaoSocial=%s, Phone=%s, City=%s, State=%s, Neighborhood=%s, TeamSize=%s, PrimaryColor=%s, SecondaryColor=%s",
|
||||
req.RazaoSocial, req.Phone, req.City, req.State, req.Neighborhood, req.TeamSize, req.PrimaryColor, req.SecondaryColor)
|
||||
|
||||
tenant, admin, err := h.agencyService.RegisterAgency(req)
|
||||
if err != nil {
|
||||
@@ -104,6 +106,112 @@ func (h *AgencyRegistrationHandler) RegisterAgency(w http.ResponseWriter, r *htt
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// PublicRegister handles public agency registration
|
||||
func (h *AgencyRegistrationHandler) PublicRegister(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var req domain.PublicRegisterAgencyRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
log.Printf("❌ Error decoding request: %v", err)
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("📥 Public Registering agency: %s (subdomain: %s)", req.CompanyName, req.Subdomain)
|
||||
log.Printf("📦 Full Payload: %+v", req)
|
||||
|
||||
// Map to internal request
|
||||
phone := ""
|
||||
if len(req.Contacts) > 0 {
|
||||
phone = req.Contacts[0].Whatsapp
|
||||
}
|
||||
|
||||
internalReq := domain.RegisterAgencyRequest{
|
||||
AgencyName: req.CompanyName,
|
||||
Subdomain: req.Subdomain,
|
||||
CNPJ: req.CNPJ,
|
||||
RazaoSocial: req.RazaoSocial,
|
||||
Description: req.Description,
|
||||
Website: req.Website,
|
||||
Industry: req.Industry,
|
||||
Phone: phone,
|
||||
TeamSize: req.TeamSize,
|
||||
CEP: req.CEP,
|
||||
State: req.State,
|
||||
City: req.City,
|
||||
Neighborhood: req.Neighborhood,
|
||||
Street: req.Street,
|
||||
Number: req.Number,
|
||||
Complement: req.Complement,
|
||||
PrimaryColor: req.PrimaryColor,
|
||||
SecondaryColor: req.SecondaryColor,
|
||||
LogoURL: req.LogoURL,
|
||||
AdminEmail: req.Email,
|
||||
AdminPassword: req.Password,
|
||||
AdminName: req.FullName,
|
||||
}
|
||||
|
||||
tenant, admin, err := h.agencyService.RegisterAgency(internalReq)
|
||||
if err != nil {
|
||||
log.Printf("❌ Error registering agency: %v", err)
|
||||
switch err {
|
||||
case service.ErrSubdomainTaken:
|
||||
http.Error(w, err.Error(), http.StatusConflict)
|
||||
case service.ErrEmailAlreadyExists:
|
||||
http.Error(w, err.Error(), http.StatusConflict)
|
||||
case service.ErrWeakPassword:
|
||||
http.Error(w, err.Error(), http.StatusBadRequest)
|
||||
default:
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("✅ Agency created: %s (ID: %s)", tenant.Name, tenant.ID)
|
||||
|
||||
// Generate JWT token for the new admin
|
||||
claims := jwt.MapClaims{
|
||||
"user_id": admin.ID.String(),
|
||||
"email": admin.Email,
|
||||
"role": admin.Role,
|
||||
"tenant_id": tenant.ID.String(),
|
||||
"exp": time.Now().Add(time.Hour * 24 * 7).Unix(), // 7 days
|
||||
"iat": time.Now().Unix(),
|
||||
}
|
||||
|
||||
token := jwt.NewWithClaims(jwt.SigningMethodHS256, claims)
|
||||
tokenString, err := token.SignedString([]byte(h.cfg.JWT.Secret))
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to generate token", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
protocol := "http://"
|
||||
if h.cfg.App.Environment == "production" {
|
||||
protocol = "https://"
|
||||
}
|
||||
|
||||
response := map[string]interface{}{
|
||||
"token": tokenString,
|
||||
"id": admin.ID,
|
||||
"email": admin.Email,
|
||||
"name": admin.Name,
|
||||
"role": admin.Role,
|
||||
"tenantId": tenant.ID,
|
||||
"company": tenant.Name,
|
||||
"subdomain": tenant.Subdomain,
|
||||
"message": "Agency registered successfully",
|
||||
"access_url": protocol + tenant.Domain,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// RegisterClient handles client registration (ADMIN_AGENCIA only)
|
||||
func (h *AgencyRegistrationHandler) RegisterClient(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
@@ -147,9 +255,10 @@ func (h *AgencyRegistrationHandler) HandleAgency(w http.ResponseWriter, r *http.
|
||||
return
|
||||
}
|
||||
|
||||
agencyID := strings.TrimPrefix(r.URL.Path, "/api/admin/agencies/")
|
||||
if agencyID == "" || agencyID == r.URL.Path {
|
||||
http.NotFound(w, r)
|
||||
vars := mux.Vars(r)
|
||||
agencyID := vars["id"]
|
||||
if agencyID == "" {
|
||||
http.Error(w, "Missing agency ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -174,6 +283,27 @@ func (h *AgencyRegistrationHandler) HandleAgency(w http.ResponseWriter, r *http.
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(details)
|
||||
|
||||
case http.MethodPatch:
|
||||
var updateData map[string]interface{}
|
||||
if err := json.NewDecoder(r.Body).Decode(&updateData); err != nil {
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
if isActive, ok := updateData["is_active"].(bool); ok {
|
||||
if err := h.agencyService.UpdateAgencyStatus(id, isActive); err != nil {
|
||||
if errors.Is(err, service.ErrTenantNotFound) {
|
||||
http.Error(w, "Agency not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(map[string]string{"message": "Status updated"})
|
||||
|
||||
case http.MethodDelete:
|
||||
if err := h.agencyService.DeleteAgency(id); err != nil {
|
||||
if errors.Is(err, service.ErrTenantNotFound) {
|
||||
|
||||
225
backend/internal/api/handlers/agency_logo.go
Normal file
225
backend/internal/api/handlers/agency_logo.go
Normal file
@@ -0,0 +1,225 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"time"
|
||||
|
||||
"aggios-app/backend/internal/api/middleware"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
// UploadLogo handles logo file uploads
|
||||
func (h *AgencyHandler) UploadLogo(w http.ResponseWriter, r *http.Request) {
|
||||
// Only accept POST
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("Logo upload request received from tenant")
|
||||
|
||||
// Get tenant ID from context
|
||||
tenantIDVal := r.Context().Value(middleware.TenantIDKey)
|
||||
if tenantIDVal == nil {
|
||||
log.Printf("No tenant ID in context")
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// Try to get as uuid.UUID first, if that fails try string and parse
|
||||
var tenantID uuid.UUID
|
||||
var ok bool
|
||||
|
||||
tenantID, ok = tenantIDVal.(uuid.UUID)
|
||||
if !ok {
|
||||
// Try as string
|
||||
tenantIDStr, isString := tenantIDVal.(string)
|
||||
if !isString {
|
||||
log.Printf("Invalid tenant ID type: %T", tenantIDVal)
|
||||
http.Error(w, "Invalid tenant ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var err error
|
||||
tenantID, err = uuid.Parse(tenantIDStr)
|
||||
if err != nil {
|
||||
log.Printf("Failed to parse tenant ID: %v", err)
|
||||
http.Error(w, "Invalid tenant ID format", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
log.Printf("Processing logo upload for tenant: %s", tenantID)
|
||||
|
||||
// Parse multipart form (2MB max)
|
||||
const maxLogoSize = 2 * 1024 * 1024
|
||||
if err := r.ParseMultipartForm(maxLogoSize); err != nil {
|
||||
http.Error(w, "File too large", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
file, header, err := r.FormFile("logo")
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to read file", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Validate file type
|
||||
contentType := header.Header.Get("Content-Type")
|
||||
if contentType != "image/png" && contentType != "image/jpeg" && contentType != "image/svg+xml" && contentType != "image/jpg" {
|
||||
http.Error(w, "Only PNG, JPG or SVG files are allowed", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Get logo type (logo or horizontal)
|
||||
logoType := r.FormValue("type")
|
||||
if logoType != "logo" && logoType != "horizontal" {
|
||||
logoType = "logo"
|
||||
}
|
||||
|
||||
// Get current logo URL from database to delete old file
|
||||
var currentLogoURL string
|
||||
var queryErr error
|
||||
if logoType == "horizontal" {
|
||||
queryErr = h.tenantRepo.DB().QueryRow("SELECT logo_horizontal_url FROM tenants WHERE id = $1", tenantID).Scan(¤tLogoURL)
|
||||
} else {
|
||||
queryErr = h.tenantRepo.DB().QueryRow("SELECT logo_url FROM tenants WHERE id = $1", tenantID).Scan(¤tLogoURL)
|
||||
}
|
||||
if queryErr != nil && queryErr.Error() != "sql: no rows in result set" {
|
||||
log.Printf("Warning: Failed to get current logo URL: %v", queryErr)
|
||||
}
|
||||
|
||||
// Initialize MinIO client
|
||||
minioClient, err := minio.New("aggios-minio:9000", &minio.Options{
|
||||
Creds: credentials.NewStaticV4("minioadmin", "M1n10_S3cur3_P@ss_2025!", ""),
|
||||
Secure: false,
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("Failed to create MinIO client: %v", err)
|
||||
http.Error(w, "Storage service unavailable", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Ensure bucket exists
|
||||
bucketName := "aggios-logos"
|
||||
ctx := context.Background()
|
||||
exists, err := minioClient.BucketExists(ctx, bucketName)
|
||||
if err != nil {
|
||||
log.Printf("Failed to check bucket: %v", err)
|
||||
http.Error(w, "Storage error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
if !exists {
|
||||
err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{})
|
||||
if err != nil {
|
||||
log.Printf("Failed to create bucket: %v", err)
|
||||
http.Error(w, "Storage error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
// Set bucket policy to public-read
|
||||
policy := fmt.Sprintf(`{
|
||||
"Version": "2012-10-17",
|
||||
"Statement": [{
|
||||
"Effect": "Allow",
|
||||
"Principal": {"AWS": ["*"]},
|
||||
"Action": ["s3:GetObject"],
|
||||
"Resource": ["arn:aws:s3:::%s/*"]
|
||||
}]
|
||||
}`, bucketName)
|
||||
err = minioClient.SetBucketPolicy(ctx, bucketName, policy)
|
||||
if err != nil {
|
||||
log.Printf("Warning: Failed to set bucket policy: %v", err)
|
||||
}
|
||||
}
|
||||
|
||||
// Read file content
|
||||
fileBytes, err := io.ReadAll(file)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to read file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Generate unique filename
|
||||
ext := filepath.Ext(header.Filename)
|
||||
filename := fmt.Sprintf("tenants/%s/%s-%d%s", tenantID, logoType, time.Now().Unix(), ext)
|
||||
|
||||
// Upload to MinIO
|
||||
_, err = minioClient.PutObject(
|
||||
ctx,
|
||||
bucketName,
|
||||
filename,
|
||||
bytes.NewReader(fileBytes),
|
||||
int64(len(fileBytes)),
|
||||
minio.PutObjectOptions{
|
||||
ContentType: contentType,
|
||||
},
|
||||
)
|
||||
if err != nil {
|
||||
log.Printf("Failed to upload to MinIO: %v", err)
|
||||
http.Error(w, "Failed to save file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Generate public URL
|
||||
logoURL := fmt.Sprintf("http://localhost:9000/%s/%s", bucketName, filename)
|
||||
|
||||
log.Printf("Logo uploaded successfully: %s", logoURL)
|
||||
|
||||
// Delete old logo file from MinIO if exists
|
||||
if currentLogoURL != "" && currentLogoURL != "https://via.placeholder.com/150" {
|
||||
// Extract object key from URL
|
||||
// Example: http://localhost:9000/aggios-logos/tenants/uuid/logo-123.png -> tenants/uuid/logo-123.png
|
||||
oldFilename := ""
|
||||
if len(currentLogoURL) > 0 {
|
||||
// Split by bucket name
|
||||
if idx := len("http://localhost:9000/aggios-logos/"); idx < len(currentLogoURL) {
|
||||
oldFilename = currentLogoURL[idx:]
|
||||
}
|
||||
}
|
||||
|
||||
if oldFilename != "" {
|
||||
err = minioClient.RemoveObject(ctx, bucketName, oldFilename, minio.RemoveObjectOptions{})
|
||||
if err != nil {
|
||||
log.Printf("Warning: Failed to delete old logo %s: %v", oldFilename, err)
|
||||
// Don't fail the request if deletion fails
|
||||
} else {
|
||||
log.Printf("Old logo deleted successfully: %s", oldFilename)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update tenant record in database
|
||||
var err2 error
|
||||
if logoType == "horizontal" {
|
||||
_, err2 = h.tenantRepo.DB().Exec("UPDATE tenants SET logo_horizontal_url = $1, updated_at = NOW() WHERE id = $2", logoURL, tenantID)
|
||||
} else {
|
||||
_, err2 = h.tenantRepo.DB().Exec("UPDATE tenants SET logo_url = $1, updated_at = NOW() WHERE id = $2", logoURL, tenantID)
|
||||
}
|
||||
|
||||
if err2 != nil {
|
||||
log.Printf("Failed to update logo: %v", err2)
|
||||
http.Error(w, "Failed to update database", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Return success response
|
||||
response := map[string]string{
|
||||
"logo_url": logoURL,
|
||||
"message": "Logo uploaded successfully",
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
@@ -22,34 +22,50 @@ func NewAgencyHandler(tenantRepo *repository.TenantRepository) *AgencyHandler {
|
||||
}
|
||||
|
||||
type AgencyProfileResponse struct {
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
CNPJ string `json:"cnpj"`
|
||||
Email string `json:"email"`
|
||||
Phone string `json:"phone"`
|
||||
Website string `json:"website"`
|
||||
Address string `json:"address"`
|
||||
City string `json:"city"`
|
||||
State string `json:"state"`
|
||||
Zip string `json:"zip"`
|
||||
RazaoSocial string `json:"razao_social"`
|
||||
Description string `json:"description"`
|
||||
Industry string `json:"industry"`
|
||||
ID string `json:"id"`
|
||||
Name string `json:"name"`
|
||||
CNPJ string `json:"cnpj"`
|
||||
Email string `json:"email"`
|
||||
Phone string `json:"phone"`
|
||||
Website string `json:"website"`
|
||||
Address string `json:"address"`
|
||||
Neighborhood string `json:"neighborhood"`
|
||||
Number string `json:"number"`
|
||||
Complement string `json:"complement"`
|
||||
City string `json:"city"`
|
||||
State string `json:"state"`
|
||||
Zip string `json:"zip"`
|
||||
RazaoSocial string `json:"razao_social"`
|
||||
Description string `json:"description"`
|
||||
Industry string `json:"industry"`
|
||||
TeamSize string `json:"team_size"`
|
||||
PrimaryColor string `json:"primary_color"`
|
||||
SecondaryColor string `json:"secondary_color"`
|
||||
LogoURL string `json:"logo_url"`
|
||||
LogoHorizontalURL string `json:"logo_horizontal_url"`
|
||||
}
|
||||
|
||||
type UpdateAgencyProfileRequest struct {
|
||||
Name string `json:"name"`
|
||||
CNPJ string `json:"cnpj"`
|
||||
Email string `json:"email"`
|
||||
Phone string `json:"phone"`
|
||||
Website string `json:"website"`
|
||||
Address string `json:"address"`
|
||||
City string `json:"city"`
|
||||
State string `json:"state"`
|
||||
Zip string `json:"zip"`
|
||||
RazaoSocial string `json:"razao_social"`
|
||||
Description string `json:"description"`
|
||||
Industry string `json:"industry"`
|
||||
Name string `json:"name"`
|
||||
CNPJ string `json:"cnpj"`
|
||||
Email string `json:"email"`
|
||||
Phone string `json:"phone"`
|
||||
Website string `json:"website"`
|
||||
Address string `json:"address"`
|
||||
Neighborhood string `json:"neighborhood"`
|
||||
Number string `json:"number"`
|
||||
Complement string `json:"complement"`
|
||||
City string `json:"city"`
|
||||
State string `json:"state"`
|
||||
Zip string `json:"zip"`
|
||||
RazaoSocial string `json:"razao_social"`
|
||||
Description string `json:"description"`
|
||||
Industry string `json:"industry"`
|
||||
TeamSize string `json:"team_size"`
|
||||
PrimaryColor string `json:"primary_color"`
|
||||
SecondaryColor string `json:"secondary_color"`
|
||||
LogoURL string `json:"logo_url"`
|
||||
LogoHorizontalURL string `json:"logo_horizontal_url"`
|
||||
}
|
||||
|
||||
// GetProfile returns the current agency profile
|
||||
@@ -61,10 +77,8 @@ func (h *AgencyHandler) GetProfile(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Get tenant from context (set by auth middleware)
|
||||
tenantID := r.Context().Value(middleware.TenantIDKey)
|
||||
log.Printf("DEBUG GetProfile: tenantID from context = %v (type: %T)", tenantID, tenantID)
|
||||
|
||||
if tenantID == nil {
|
||||
log.Printf("DEBUG GetProfile: tenantID is nil from auth middleware")
|
||||
http.Error(w, "Tenant not found in context", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
@@ -87,20 +101,32 @@ func (h *AgencyHandler) GetProfile(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("🔍 GetProfile for tenant %s: Found %s", tid, tenant.Name)
|
||||
log.Printf("📄 Tenant Data: Address=%s, Number=%s, TeamSize=%s, RazaoSocial=%s",
|
||||
tenant.Address, tenant.Number, tenant.TeamSize, tenant.RazaoSocial)
|
||||
|
||||
response := AgencyProfileResponse{
|
||||
ID: tenant.ID.String(),
|
||||
Name: tenant.Name,
|
||||
CNPJ: tenant.CNPJ,
|
||||
Email: tenant.Email,
|
||||
Phone: tenant.Phone,
|
||||
Website: tenant.Website,
|
||||
Address: tenant.Address,
|
||||
City: tenant.City,
|
||||
State: tenant.State,
|
||||
Zip: tenant.Zip,
|
||||
RazaoSocial: tenant.RazaoSocial,
|
||||
Description: tenant.Description,
|
||||
Industry: tenant.Industry,
|
||||
ID: tenant.ID.String(),
|
||||
Name: tenant.Name,
|
||||
CNPJ: tenant.CNPJ,
|
||||
Email: tenant.Email,
|
||||
Phone: tenant.Phone,
|
||||
Website: tenant.Website,
|
||||
Address: tenant.Address,
|
||||
Neighborhood: tenant.Neighborhood,
|
||||
Number: tenant.Number,
|
||||
Complement: tenant.Complement,
|
||||
City: tenant.City,
|
||||
State: tenant.State,
|
||||
Zip: tenant.Zip,
|
||||
RazaoSocial: tenant.RazaoSocial,
|
||||
Description: tenant.Description,
|
||||
Industry: tenant.Industry,
|
||||
TeamSize: tenant.TeamSize,
|
||||
PrimaryColor: tenant.PrimaryColor,
|
||||
SecondaryColor: tenant.SecondaryColor,
|
||||
LogoURL: tenant.LogoURL,
|
||||
LogoHorizontalURL: tenant.LogoHorizontalURL,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
@@ -136,18 +162,26 @@ func (h *AgencyHandler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Prepare updates
|
||||
updates := map[string]interface{}{
|
||||
"name": req.Name,
|
||||
"cnpj": req.CNPJ,
|
||||
"razao_social": req.RazaoSocial,
|
||||
"email": req.Email,
|
||||
"phone": req.Phone,
|
||||
"website": req.Website,
|
||||
"address": req.Address,
|
||||
"city": req.City,
|
||||
"state": req.State,
|
||||
"zip": req.Zip,
|
||||
"description": req.Description,
|
||||
"industry": req.Industry,
|
||||
"name": req.Name,
|
||||
"cnpj": req.CNPJ,
|
||||
"razao_social": req.RazaoSocial,
|
||||
"email": req.Email,
|
||||
"phone": req.Phone,
|
||||
"website": req.Website,
|
||||
"address": req.Address,
|
||||
"neighborhood": req.Neighborhood,
|
||||
"number": req.Number,
|
||||
"complement": req.Complement,
|
||||
"city": req.City,
|
||||
"state": req.State,
|
||||
"zip": req.Zip,
|
||||
"description": req.Description,
|
||||
"industry": req.Industry,
|
||||
"team_size": req.TeamSize,
|
||||
"primary_color": req.PrimaryColor,
|
||||
"secondary_color": req.SecondaryColor,
|
||||
"logo_url": req.LogoURL,
|
||||
"logo_horizontal_url": req.LogoHorizontalURL,
|
||||
}
|
||||
|
||||
// Update in database
|
||||
@@ -164,21 +198,30 @@ func (h *AgencyHandler) UpdateProfile(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
response := AgencyProfileResponse{
|
||||
ID: tenant.ID.String(),
|
||||
Name: tenant.Name,
|
||||
CNPJ: tenant.CNPJ,
|
||||
Email: tenant.Email,
|
||||
Phone: tenant.Phone,
|
||||
Website: tenant.Website,
|
||||
Address: tenant.Address,
|
||||
City: tenant.City,
|
||||
State: tenant.State,
|
||||
Zip: tenant.Zip,
|
||||
RazaoSocial: tenant.RazaoSocial,
|
||||
Description: tenant.Description,
|
||||
Industry: tenant.Industry,
|
||||
ID: tenant.ID.String(),
|
||||
Name: tenant.Name,
|
||||
CNPJ: tenant.CNPJ,
|
||||
Email: tenant.Email,
|
||||
Phone: tenant.Phone,
|
||||
Website: tenant.Website,
|
||||
Address: tenant.Address,
|
||||
Neighborhood: tenant.Neighborhood,
|
||||
Number: tenant.Number,
|
||||
Complement: tenant.Complement,
|
||||
City: tenant.City,
|
||||
State: tenant.State,
|
||||
Zip: tenant.Zip,
|
||||
RazaoSocial: tenant.RazaoSocial,
|
||||
Description: tenant.Description,
|
||||
Industry: tenant.Industry,
|
||||
TeamSize: tenant.TeamSize,
|
||||
PrimaryColor: tenant.PrimaryColor,
|
||||
SecondaryColor: tenant.SecondaryColor,
|
||||
LogoURL: tenant.LogoURL,
|
||||
LogoHorizontalURL: tenant.LogoHorizontalURL,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
|
||||
239
backend/internal/api/handlers/agency_template_handler.go
Normal file
239
backend/internal/api/handlers/agency_template_handler.go
Normal file
@@ -0,0 +1,239 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"aggios-app/backend/internal/domain"
|
||||
"aggios-app/backend/internal/repository"
|
||||
"aggios-app/backend/internal/service"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type AgencyTemplateHandler struct {
|
||||
templateRepo *repository.AgencyTemplateRepository
|
||||
agencyService *service.AgencyService
|
||||
userRepo *repository.UserRepository
|
||||
tenantRepo *repository.TenantRepository
|
||||
}
|
||||
|
||||
func NewAgencyTemplateHandler(
|
||||
templateRepo *repository.AgencyTemplateRepository,
|
||||
agencyService *service.AgencyService,
|
||||
userRepo *repository.UserRepository,
|
||||
tenantRepo *repository.TenantRepository,
|
||||
) *AgencyTemplateHandler {
|
||||
return &AgencyTemplateHandler{
|
||||
templateRepo: templateRepo,
|
||||
agencyService: agencyService,
|
||||
userRepo: userRepo,
|
||||
tenantRepo: tenantRepo,
|
||||
}
|
||||
}
|
||||
|
||||
// GetTemplateBySlug - Public endpoint to get template details
|
||||
func (h *AgencyTemplateHandler) GetTemplateBySlug(w http.ResponseWriter, r *http.Request) {
|
||||
slug := r.URL.Query().Get("slug")
|
||||
if slug == "" {
|
||||
http.Error(w, "Missing slug parameter", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
template, err := h.templateRepo.FindBySlug(slug)
|
||||
if err != nil {
|
||||
log.Printf("Template not found: %v", err)
|
||||
http.Error(w, "Template not found or expired", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(template)
|
||||
}
|
||||
|
||||
// PublicRegisterAgency - Public endpoint for agency registration via template
|
||||
func (h *AgencyTemplateHandler) PublicRegisterAgency(w http.ResponseWriter, r *http.Request) {
|
||||
var req domain.AgencyRegistrationViaTemplate
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// 1. Validar template
|
||||
template, err := h.templateRepo.FindBySlug(req.TemplateSlug)
|
||||
if err != nil {
|
||||
log.Printf("Template error: %v", err)
|
||||
http.Error(w, "Invalid or expired template", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// 2. Validar campos obrigatórios
|
||||
if req.AgencyName == "" || req.Subdomain == "" || req.AdminEmail == "" || req.AdminPassword == "" {
|
||||
http.Error(w, "Missing required fields", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// 3. Validar senha
|
||||
if len(req.AdminPassword) < 8 {
|
||||
http.Error(w, "Password must be at least 8 characters", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// 4. Verificar se email já existe
|
||||
existingUser, _ := h.userRepo.FindByEmail(req.AdminEmail)
|
||||
if existingUser != nil {
|
||||
http.Error(w, "Email already registered", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
|
||||
// 5. Verificar se subdomain já existe
|
||||
existingTenant, _ := h.tenantRepo.FindBySubdomain(req.Subdomain)
|
||||
if existingTenant != nil {
|
||||
http.Error(w, "Subdomain already taken", http.StatusConflict)
|
||||
return
|
||||
}
|
||||
|
||||
// 6. Hash da senha
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.AdminPassword), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
log.Printf("Error hashing password: %v", err)
|
||||
http.Error(w, "Error processing password", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// 7. Criar tenant (agência)
|
||||
tenant := &domain.Tenant{
|
||||
Name: req.AgencyName,
|
||||
Domain: req.Subdomain + ".aggios.app",
|
||||
Subdomain: req.Subdomain,
|
||||
CNPJ: req.CNPJ,
|
||||
RazaoSocial: req.RazaoSocial,
|
||||
Website: req.Website,
|
||||
Phone: req.Phone,
|
||||
Description: req.Description,
|
||||
Industry: req.Industry,
|
||||
TeamSize: req.TeamSize,
|
||||
}
|
||||
|
||||
// Endereço (se fornecido)
|
||||
if req.Address != nil {
|
||||
tenant.Address = req.Address["street"]
|
||||
tenant.Number = req.Address["number"]
|
||||
tenant.Complement = req.Address["complement"]
|
||||
tenant.Neighborhood = req.Address["neighborhood"]
|
||||
tenant.City = req.Address["city"]
|
||||
tenant.State = req.Address["state"]
|
||||
tenant.Zip = req.Address["cep"]
|
||||
}
|
||||
|
||||
// Personalização do template
|
||||
if template.CustomPrimaryColor.Valid {
|
||||
tenant.PrimaryColor = template.CustomPrimaryColor.String
|
||||
}
|
||||
if template.CustomLogoURL.Valid {
|
||||
tenant.LogoURL = template.CustomLogoURL.String
|
||||
}
|
||||
|
||||
if err := h.tenantRepo.Create(tenant); err != nil {
|
||||
log.Printf("Error creating tenant: %v", err)
|
||||
http.Error(w, "Error creating agency", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// 8. Criar usuário admin da agência
|
||||
user := &domain.User{
|
||||
Email: req.AdminEmail,
|
||||
Password: string(hashedPassword),
|
||||
Name: req.AdminName,
|
||||
Role: "ADMIN_AGENCIA",
|
||||
TenantID: &tenant.ID,
|
||||
}
|
||||
|
||||
if err := h.userRepo.Create(user); err != nil {
|
||||
log.Printf("Error creating user: %v", err)
|
||||
http.Error(w, "Error creating admin user", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// 9. Incrementar contador de uso do template
|
||||
if err := h.templateRepo.IncrementUsageCount(template.ID.String()); err != nil {
|
||||
log.Printf("Warning: failed to increment usage count: %v", err)
|
||||
}
|
||||
|
||||
// 10. Preparar resposta com redirect
|
||||
redirectURL := template.RedirectURL.String
|
||||
if redirectURL == "" {
|
||||
redirectURL = "http://" + req.Subdomain + ".localhost/login"
|
||||
}
|
||||
|
||||
response := map[string]interface{}{
|
||||
"success": true,
|
||||
"message": template.SuccessMessage.String,
|
||||
"tenant_id": tenant.ID,
|
||||
"user_id": user.ID,
|
||||
"redirect_url": redirectURL,
|
||||
"subdomain": req.Subdomain,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
// CreateTemplate - SUPERADMIN only
|
||||
func (h *AgencyTemplateHandler) CreateTemplate(w http.ResponseWriter, r *http.Request) {
|
||||
var req domain.CreateAgencyTemplateRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "Invalid request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
formFieldsJSON, _ := repository.FormFieldsToJSON(req.FormFields)
|
||||
modulesJSON, _ := json.Marshal(req.AvailableModules)
|
||||
|
||||
template := &domain.AgencySignupTemplate{
|
||||
Name: req.Name,
|
||||
Slug: req.Slug,
|
||||
Description: req.Description,
|
||||
FormFields: formFieldsJSON,
|
||||
AvailableModules: modulesJSON,
|
||||
IsActive: true,
|
||||
}
|
||||
|
||||
if req.CustomPrimaryColor != "" {
|
||||
template.CustomPrimaryColor.Valid = true
|
||||
template.CustomPrimaryColor.String = req.CustomPrimaryColor
|
||||
}
|
||||
if req.CustomLogoURL != "" {
|
||||
template.CustomLogoURL.Valid = true
|
||||
template.CustomLogoURL.String = req.CustomLogoURL
|
||||
}
|
||||
if req.RedirectURL != "" {
|
||||
template.RedirectURL.Valid = true
|
||||
template.RedirectURL.String = req.RedirectURL
|
||||
}
|
||||
if req.SuccessMessage != "" {
|
||||
template.SuccessMessage.Valid = true
|
||||
template.SuccessMessage.String = req.SuccessMessage
|
||||
}
|
||||
|
||||
if err := h.templateRepo.Create(template); err != nil {
|
||||
log.Printf("Error creating template: %v", err)
|
||||
http.Error(w, "Error creating template", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(template)
|
||||
}
|
||||
|
||||
// ListTemplates - SUPERADMIN only
|
||||
func (h *AgencyTemplateHandler) ListTemplates(w http.ResponseWriter, r *http.Request) {
|
||||
templates, err := h.templateRepo.List()
|
||||
if err != nil {
|
||||
http.Error(w, "Error fetching templates", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(templates)
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package handlers
|
||||
import (
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
@@ -55,28 +56,38 @@ func (h *AuthHandler) Register(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// Login handles user login
|
||||
func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
|
||||
log.Printf("🔐 LOGIN HANDLER CALLED - Method: %s", r.Method)
|
||||
|
||||
if r.Method != http.MethodPost {
|
||||
log.Printf("❌ Method not allowed: %s", r.Method)
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
bodyBytes, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
log.Printf("❌ Failed to read body: %v", err)
|
||||
http.Error(w, "Failed to read request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer r.Body.Close()
|
||||
|
||||
log.Printf("📥 Raw body: %s", string(bodyBytes))
|
||||
|
||||
// Trim whitespace to avoid decode errors caused by BOM or stray chars
|
||||
sanitized := strings.TrimSpace(string(bodyBytes))
|
||||
var req domain.LoginRequest
|
||||
if err := json.Unmarshal([]byte(sanitized), &req); err != nil {
|
||||
log.Printf("❌ JSON parse error: %v", err)
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("📧 Login attempt for email: %s", req.Email)
|
||||
|
||||
response, err := h.authService.Login(req)
|
||||
if err != nil {
|
||||
log.Printf("❌ authService.Login error: %v", err)
|
||||
if err == service.ErrInvalidCredentials {
|
||||
http.Error(w, err.Error(), http.StatusUnauthorized)
|
||||
} else {
|
||||
@@ -85,6 +96,7 @@ func (h *AuthHandler) Login(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
log.Printf("✅ Login successful for %s, role=%s", response.User.Email, response.User.Role)
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
38
backend/internal/api/handlers/hash.go
Normal file
38
backend/internal/api/handlers/hash.go
Normal file
@@ -0,0 +1,38 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
type HashRequest struct {
|
||||
Password string `json:"password"`
|
||||
}
|
||||
|
||||
type HashResponse struct {
|
||||
Hash string `json:"hash"`
|
||||
}
|
||||
|
||||
func GenerateHash(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
var req HashRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
hash, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to generate hash", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(HashResponse{Hash: string(hash)})
|
||||
}
|
||||
180
backend/internal/api/handlers/signup_template.go
Normal file
180
backend/internal/api/handlers/signup_template.go
Normal file
@@ -0,0 +1,180 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"aggios-app/backend/internal/api/middleware"
|
||||
"aggios-app/backend/internal/domain"
|
||||
"aggios-app/backend/internal/repository"
|
||||
"aggios-app/backend/internal/service"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/gorilla/mux"
|
||||
)
|
||||
|
||||
type SignupTemplateHandler struct {
|
||||
repo *repository.SignupTemplateRepository
|
||||
userRepo *repository.UserRepository
|
||||
tenantRepo *repository.TenantRepository
|
||||
agencyService *service.AgencyService
|
||||
}
|
||||
|
||||
func NewSignupTemplateHandler(
|
||||
repo *repository.SignupTemplateRepository,
|
||||
userRepo *repository.UserRepository,
|
||||
tenantRepo *repository.TenantRepository,
|
||||
agencyService *service.AgencyService,
|
||||
) *SignupTemplateHandler {
|
||||
return &SignupTemplateHandler{
|
||||
repo: repo,
|
||||
userRepo: userRepo,
|
||||
tenantRepo: tenantRepo,
|
||||
agencyService: agencyService,
|
||||
}
|
||||
}
|
||||
|
||||
// CreateTemplate cria um novo template (SuperAdmin)
|
||||
func (h *SignupTemplateHandler) CreateTemplate(w http.ResponseWriter, r *http.Request) {
|
||||
var template domain.SignupTemplate
|
||||
if err := json.NewDecoder(r.Body).Decode(&template); err != nil {
|
||||
log.Printf("Error decoding request body: %v", err)
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Pegar user_id do contexto (do middleware de autenticação)
|
||||
userIDStr, ok := r.Context().Value(middleware.UserIDKey).(string)
|
||||
if !ok || userIDStr == "" {
|
||||
http.Error(w, "Unauthorized", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
userID, err := uuid.Parse(userIDStr)
|
||||
if err != nil {
|
||||
log.Printf("Error parsing user_id: %v", err)
|
||||
http.Error(w, "Invalid user ID", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
template.CreatedBy = userID
|
||||
template.IsActive = true
|
||||
|
||||
ctx := context.Background()
|
||||
if err := h.repo.Create(ctx, &template); err != nil {
|
||||
log.Printf("Error creating signup template: %v", err)
|
||||
http.Error(w, "Error creating template", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(template)
|
||||
}
|
||||
|
||||
// ListTemplates lista todos os templates (SuperAdmin)
|
||||
func (h *SignupTemplateHandler) ListTemplates(w http.ResponseWriter, r *http.Request) {
|
||||
ctx := context.Background()
|
||||
templates, err := h.repo.List(ctx)
|
||||
if err != nil {
|
||||
log.Printf("Error listing signup templates: %v", err)
|
||||
http.Error(w, "Error listing templates", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(templates)
|
||||
}
|
||||
|
||||
// GetTemplateBySlug retorna um template pelo slug (público)
|
||||
func (h *SignupTemplateHandler) GetTemplateBySlug(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
slug := vars["slug"]
|
||||
|
||||
ctx := context.Background()
|
||||
template, err := h.repo.FindBySlug(ctx, slug)
|
||||
if err != nil {
|
||||
log.Printf("Error finding signup template by slug %s: %v", slug, err)
|
||||
http.Error(w, "Template not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(template)
|
||||
}
|
||||
|
||||
// GetTemplateByID retorna um template pelo ID (SuperAdmin)
|
||||
func (h *SignupTemplateHandler) GetTemplateByID(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
idStr := vars["id"]
|
||||
|
||||
id, err := uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid template ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
template, err := h.repo.FindByID(ctx, id)
|
||||
if err != nil {
|
||||
log.Printf("Error finding signup template by ID %s: %v", idStr, err)
|
||||
http.Error(w, "Template not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(template)
|
||||
}
|
||||
|
||||
// UpdateTemplate atualiza um template (SuperAdmin)
|
||||
func (h *SignupTemplateHandler) UpdateTemplate(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
idStr := vars["id"]
|
||||
|
||||
id, err := uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid template ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var template domain.SignupTemplate
|
||||
if err := json.NewDecoder(r.Body).Decode(&template); err != nil {
|
||||
log.Printf("Error decoding request body: %v", err)
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
template.ID = id
|
||||
|
||||
ctx := context.Background()
|
||||
if err := h.repo.Update(ctx, &template); err != nil {
|
||||
log.Printf("Error updating signup template: %v", err)
|
||||
http.Error(w, "Error updating template", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(template)
|
||||
}
|
||||
|
||||
// DeleteTemplate deleta um template (SuperAdmin)
|
||||
func (h *SignupTemplateHandler) DeleteTemplate(w http.ResponseWriter, r *http.Request) {
|
||||
vars := mux.Vars(r)
|
||||
idStr := vars["id"]
|
||||
|
||||
id, err := uuid.Parse(idStr)
|
||||
if err != nil {
|
||||
http.Error(w, "Invalid template ID", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
if err := h.repo.Delete(ctx, id); err != nil {
|
||||
log.Printf("Error deleting signup template: %v", err)
|
||||
http.Error(w, "Error deleting template", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
}
|
||||
121
backend/internal/api/handlers/signup_template_register.go
Normal file
121
backend/internal/api/handlers/signup_template_register.go
Normal file
@@ -0,0 +1,121 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"aggios-app/backend/internal/domain"
|
||||
"context"
|
||||
"encoding/json"
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
// PublicSignupRequest representa o cadastro público via template
|
||||
type PublicSignupRequest struct {
|
||||
TemplateSlug string `json:"template_slug"`
|
||||
Email string `json:"email"`
|
||||
Password string `json:"password"`
|
||||
Name string `json:"name"`
|
||||
Subdomain string `json:"subdomain"`
|
||||
CompanyName string `json:"company_name"`
|
||||
}
|
||||
|
||||
// PublicRegister handles public registration via template
|
||||
func (h *SignupTemplateHandler) PublicRegister(w http.ResponseWriter, r *http.Request) {
|
||||
var req PublicSignupRequest
|
||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||
log.Printf("Error decoding request body: %v", err)
|
||||
http.Error(w, "Invalid request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
ctx := context.Background()
|
||||
|
||||
// 1. Buscar o template
|
||||
template, err := h.repo.FindBySlug(ctx, req.TemplateSlug)
|
||||
if err != nil {
|
||||
log.Printf("Error finding template: %v", err)
|
||||
http.Error(w, "Template not found", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
|
||||
// 2. Incrementar usage_count
|
||||
if err := h.repo.IncrementUsageCount(ctx, template.ID); err != nil {
|
||||
log.Printf("Error incrementing usage count: %v", err)
|
||||
}
|
||||
|
||||
// 3. Verificar se email já existe
|
||||
emailExists, err := h.userRepo.EmailExists(req.Email)
|
||||
if err != nil {
|
||||
log.Printf("Error checking email: %v", err)
|
||||
http.Error(w, "Error processing registration", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if emailExists {
|
||||
http.Error(w, "Email already registered", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// 4. Verificar se subdomain já existe (se fornecido)
|
||||
if req.Subdomain != "" {
|
||||
exists, err := h.tenantRepo.SubdomainExists(req.Subdomain)
|
||||
if err != nil {
|
||||
log.Printf("Error checking subdomain: %v", err)
|
||||
http.Error(w, "Error processing registration", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
if exists {
|
||||
http.Error(w, "Subdomain already taken", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Hash da senha
|
||||
hashedPassword, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost)
|
||||
if err != nil {
|
||||
log.Printf("Error hashing password: %v", err)
|
||||
http.Error(w, "Error processing registration", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// 6. Criar tenant (empresa/cliente)
|
||||
tenant := &domain.Tenant{
|
||||
Name: req.CompanyName,
|
||||
Domain: req.Subdomain + ".aggios.app",
|
||||
Subdomain: req.Subdomain,
|
||||
Description: "Registered via " + template.Name,
|
||||
}
|
||||
|
||||
if err := h.tenantRepo.Create(tenant); err != nil {
|
||||
log.Printf("Error creating tenant: %v", err)
|
||||
http.Error(w, "Error creating account", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// 7. Criar usuário admin do tenant
|
||||
user := &domain.User{
|
||||
Email: req.Email,
|
||||
Password: string(hashedPassword),
|
||||
Name: req.Name,
|
||||
Role: "CLIENTE",
|
||||
TenantID: &tenant.ID,
|
||||
}
|
||||
|
||||
if err := h.userRepo.Create(user); err != nil {
|
||||
log.Printf("Error creating user: %v", err)
|
||||
http.Error(w, "Error creating user", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// 8. Resposta de sucesso
|
||||
response := map[string]interface{}{
|
||||
"success": true,
|
||||
"message": template.SuccessMessage,
|
||||
"tenant_id": tenant.ID,
|
||||
"user_id": user.ID,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusCreated)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
130
backend/internal/api/handlers/upload.go
Normal file
130
backend/internal/api/handlers/upload.go
Normal file
@@ -0,0 +1,130 @@
|
||||
package handlers
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
|
||||
"aggios-app/backend/internal/api/middleware"
|
||||
"aggios-app/backend/internal/config"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/minio/minio-go/v7"
|
||||
"github.com/minio/minio-go/v7/pkg/credentials"
|
||||
)
|
||||
|
||||
// UploadHandler handles file upload endpoints
|
||||
type UploadHandler struct {
|
||||
minioClient *minio.Client
|
||||
cfg *config.Config
|
||||
}
|
||||
|
||||
// NewUploadHandler creates a new upload handler
|
||||
func NewUploadHandler(cfg *config.Config) (*UploadHandler, error) {
|
||||
// Initialize MinIO client
|
||||
minioClient, err := minio.New(cfg.Minio.Endpoint, &minio.Options{
|
||||
Creds: credentials.NewStaticV4(cfg.Minio.RootUser, cfg.Minio.RootPassword, ""),
|
||||
Secure: cfg.Minio.UseSSL,
|
||||
})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create MinIO client: %w", err)
|
||||
}
|
||||
|
||||
// Ensure bucket exists
|
||||
ctx := context.Background()
|
||||
bucketName := cfg.Minio.BucketName
|
||||
exists, err := minioClient.BucketExists(ctx, bucketName)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to check bucket existence: %w", err)
|
||||
}
|
||||
|
||||
if !exists {
|
||||
err = minioClient.MakeBucket(ctx, bucketName, minio.MakeBucketOptions{})
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to create bucket: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
return &UploadHandler{
|
||||
minioClient: minioClient,
|
||||
cfg: cfg,
|
||||
}, nil
|
||||
}
|
||||
|
||||
// UploadResponse represents the upload response
|
||||
type UploadResponse struct {
|
||||
FileID string `json:"file_id"`
|
||||
FileName string `json:"file_name"`
|
||||
FileURL string `json:"file_url"`
|
||||
FileSize int64 `json:"file_size"`
|
||||
}
|
||||
|
||||
// Upload handles file upload
|
||||
func (h *UploadHandler) Upload(w http.ResponseWriter, r *http.Request) {
|
||||
if r.Method != http.MethodPost {
|
||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
// Try to get user ID from context (optional for signup flow)
|
||||
userIDStr, _ := r.Context().Value(middleware.UserIDKey).(string)
|
||||
|
||||
// Use temp tenant for unauthenticated uploads (signup flow)
|
||||
tenantID := uuid.MustParse("00000000-0000-0000-0000-000000000000")
|
||||
if userIDStr != "" {
|
||||
// TODO: Query database to get tenant_id from user_id when authenticated
|
||||
}
|
||||
|
||||
// Parse multipart form (max 10MB)
|
||||
if err := r.ParseMultipartForm(10 << 20); err != nil {
|
||||
http.Error(w, "File too large (max 10MB)", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Get file from form
|
||||
file, header, err := r.FormFile("file")
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to read file", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
defer file.Close()
|
||||
|
||||
// Validate file type (images only)
|
||||
contentType := header.Header.Get("Content-Type")
|
||||
if !strings.HasPrefix(contentType, "image/") {
|
||||
http.Error(w, "Only images are allowed", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// Generate unique file ID
|
||||
fileID := uuid.New()
|
||||
ext := filepath.Ext(header.Filename)
|
||||
objectName := fmt.Sprintf("tenants/%s/logos/%s%s", tenantID.String(), fileID.String(), ext)
|
||||
|
||||
// Upload to MinIO
|
||||
ctx := context.Background()
|
||||
_, err = h.minioClient.PutObject(ctx, h.cfg.Minio.BucketName, objectName, file, header.Size, minio.PutObjectOptions{
|
||||
ContentType: contentType,
|
||||
})
|
||||
if err != nil {
|
||||
http.Error(w, "Failed to upload file", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
// Generate public URL (replace internal hostname with localhost for browser access)
|
||||
fileURL := fmt.Sprintf("http://localhost:9000/%s/%s", h.cfg.Minio.BucketName, objectName)
|
||||
|
||||
// Return response
|
||||
response := UploadResponse{
|
||||
FileID: fileID.String(),
|
||||
FileName: header.Filename,
|
||||
FileURL: fileURL,
|
||||
FileSize: header.Size,
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
@@ -40,17 +40,33 @@ func Auth(cfg *config.Config) func(http.Handler) http.Handler {
|
||||
return
|
||||
}
|
||||
|
||||
claims, ok := token.Claims.(jwt.MapClaims)
|
||||
if !ok {
|
||||
http.Error(w, "Invalid token claims", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
claims, ok := token.Claims.(jwt.MapClaims)
|
||||
if !ok {
|
||||
http.Error(w, "Invalid token claims", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
userID := claims["user_id"].(string)
|
||||
tenantID := claims["tenant_id"].(string)
|
||||
ctx := context.WithValue(r.Context(), UserIDKey, userID)
|
||||
ctx = context.WithValue(ctx, TenantIDKey, tenantID)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
// Verificar se user_id existe e é do tipo correto
|
||||
userIDClaim, ok := claims["user_id"]
|
||||
if !ok || userIDClaim == nil {
|
||||
http.Error(w, "Missing user_id in token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
userID, ok := userIDClaim.(string)
|
||||
if !ok {
|
||||
http.Error(w, "Invalid user_id format in token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
// tenant_id pode ser nil para SuperAdmin
|
||||
var tenantID string
|
||||
if tenantIDClaim, ok := claims["tenant_id"]; ok && tenantIDClaim != nil {
|
||||
tenantID, _ = tenantIDClaim.(string)
|
||||
}
|
||||
|
||||
ctx := context.WithValue(r.Context(), UserIDKey, userID)
|
||||
ctx = context.WithValue(ctx, TenantIDKey, tenantID)
|
||||
next.ServeHTTP(w, r.WithContext(ctx))
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user