61 lines
2.1 KiB
Go
61 lines
2.1 KiB
Go
package tenant
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
"github.com/techxcar/backend/internal/auth"
|
|
"github.com/techxcar/backend/internal/config"
|
|
"github.com/techxcar/backend/pkg/database"
|
|
)
|
|
|
|
// loginAdapter adapts *Repository to satisfy auth.LoginRepository.
|
|
type loginAdapter struct{ repo *Repository }
|
|
|
|
func (a *loginAdapter) GetSuperAdminByEmail(ctx context.Context, email string) (*auth.LoginSuperAdmin, error) {
|
|
sa, err := a.repo.GetSuperAdminByEmail(ctx, email)
|
|
if err != nil || sa == nil {
|
|
return nil, err
|
|
}
|
|
return &auth.LoginSuperAdmin{ID: sa.ID, Email: sa.Email, PasswordHash: sa.PasswordHash}, nil
|
|
}
|
|
|
|
func (a *loginAdapter) GetTenantBySlug(ctx context.Context, slug string) (*auth.LoginTenant, error) {
|
|
t, err := a.repo.GetTenantBySlug(ctx, slug)
|
|
if err != nil || t == nil {
|
|
return nil, err
|
|
}
|
|
return &auth.LoginTenant{ID: t.ID, Status: t.Status}, nil
|
|
}
|
|
|
|
func (a *loginAdapter) GetTenantUserByEmail(ctx context.Context, tenantID, email string) (*auth.LoginUser, error) {
|
|
u, err := a.repo.GetTenantUserByEmail(ctx, tenantID, email)
|
|
if err != nil || u == nil {
|
|
return nil, err
|
|
}
|
|
return &auth.LoginUser{ID: u.ID, Email: u.Email, PasswordHash: u.PasswordHash, Role: u.Role, Name: u.Name, Active: u.Active}, nil
|
|
}
|
|
|
|
// LoginAdapter returns an auth.LoginRepository backed by repo.
|
|
func LoginAdapter(repo *Repository) auth.LoginRepository {
|
|
return &loginAdapter{repo: repo}
|
|
}
|
|
|
|
func RegisterRoutes(app *fiber.App, repo *Repository, db *database.DB, cfg *config.Config) {
|
|
// Public invite routes
|
|
invites := app.Group("/api/v1/invites")
|
|
invites.Get("/:token", getInviteHandler(repo))
|
|
invites.Post("/:token/redeem", redeemInviteHandler(repo, db, cfg))
|
|
|
|
// Super-admin routes
|
|
admin := app.Group("/api/v1/admin",
|
|
auth.RequireAuth(cfg.JWTSecret),
|
|
auth.RequireRole("super_admin"),
|
|
)
|
|
admin.Get("/tenants", listTenantsHandler(repo))
|
|
admin.Post("/tenants", createTenantHandler(repo, db, cfg))
|
|
admin.Post("/tenants/:id/invite", generateInviteHandler(repo))
|
|
admin.Post("/tenants/:id/access", tenantAccessHandler(repo, cfg))
|
|
admin.Post("/invites", generatePlatformInviteHandler(repo))
|
|
}
|