128 lines
3.5 KiB
Go
128 lines
3.5 KiB
Go
package controller
|
|
|
|
import (
|
|
"net/http"
|
|
"users_management/m/model/dto"
|
|
"users_management/m/usecase"
|
|
"users_management/m/utils/common"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
type UserRegistrationController struct {
|
|
userUC usecase.UsersUsecase
|
|
rg *gin.RouterGroup
|
|
}
|
|
|
|
func NewUserRegistrationController(userUC usecase.UsersUsecase, rg *gin.RouterGroup) *UserRegistrationController {
|
|
return &UserRegistrationController{
|
|
userUC: userUC,
|
|
rg: rg,
|
|
}
|
|
}
|
|
|
|
func (c *UserRegistrationController) Route() {
|
|
registration := c.rg.Group("/user-registration")
|
|
{
|
|
// Public registration endpoint
|
|
registration.POST("/register", c.registerUser)
|
|
}
|
|
|
|
adminRegistration := c.rg.Group("/user-registration")
|
|
adminRegistration.GET("/pending", c.getPendingUsers)
|
|
adminRegistration.PUT("/approve/:id", c.approveUser)
|
|
adminRegistration.PUT("/reject/:id", c.rejectUser)
|
|
}
|
|
|
|
func (c *UserRegistrationController) RegisterUser(ctx *gin.Context) {
|
|
c.registerUser(ctx)
|
|
}
|
|
|
|
func (c *UserRegistrationController) GetPendingUsers(ctx *gin.Context) {
|
|
c.getPendingUsers(ctx)
|
|
}
|
|
|
|
func (c *UserRegistrationController) ApproveUser(ctx *gin.Context) {
|
|
c.approveUser(ctx)
|
|
}
|
|
|
|
func (c *UserRegistrationController) RejectUser(ctx *gin.Context) {
|
|
c.rejectUser(ctx)
|
|
}
|
|
|
|
func (c *UserRegistrationController) registerUser(ctx *gin.Context) {
|
|
var registerDTO dto.UserRegisterDTO
|
|
if err := ctx.ShouldBindJSON(®isterDTO); err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
err := c.userUC.RegisterUser(registerDTO)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
response := gin.H{
|
|
"message": "Registration successful. Your account is pending approval from an administrator.",
|
|
"status": "pending",
|
|
"next_steps": "Please wait for an administrator to approve your account before you can log in.",
|
|
}
|
|
|
|
common.SingleResponses(ctx, "User registered successfully", response)
|
|
}
|
|
|
|
func (c *UserRegistrationController) getPendingUsers(ctx *gin.Context) {
|
|
|
|
users, err := c.userUC.GetPendingUsers()
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusInternalServerError, err.Error())
|
|
return
|
|
}
|
|
|
|
response := gin.H{
|
|
"users": users,
|
|
"total": len(users),
|
|
}
|
|
|
|
common.SingleResponses(ctx, "Pending users retrieved successfully", response)
|
|
}
|
|
|
|
func (c *UserRegistrationController) approveUser(ctx *gin.Context) {
|
|
userID, err := uuid.Parse(ctx.Param("id"))
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid user ID")
|
|
return
|
|
}
|
|
|
|
err = c.userUC.ApproveUser(userID)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "User approved successfully", gin.H{
|
|
"user_id": userID,
|
|
"status": "approved",
|
|
})
|
|
}
|
|
|
|
func (c *UserRegistrationController) rejectUser(ctx *gin.Context) {
|
|
userID, err := uuid.Parse(ctx.Param("id"))
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, "Invalid user ID")
|
|
return
|
|
}
|
|
|
|
err = c.userUC.RejectUser(userID)
|
|
if err != nil {
|
|
common.ErrorResponses(ctx, http.StatusBadRequest, err.Error())
|
|
return
|
|
}
|
|
|
|
common.SingleResponses(ctx, "User rejected successfully", gin.H{
|
|
"user_id": userID,
|
|
"status": "rejected",
|
|
})
|
|
} |