second commit
This commit is contained in:
@@ -0,0 +1,166 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/gochat/gochat/internal/service"
|
||||
applogger "github.com/gochat/gochat/pkg/logger"
|
||||
"github.com/gochat/gochat/pkg/pagination"
|
||||
"github.com/gochat/gochat/pkg/response"
|
||||
)
|
||||
|
||||
// CustomRoleHandler handles CustomRole CRUD operations.
|
||||
// Reference: Chatwoot enterprise/app/controllers/api/v1/custom_roles_controller.rb
|
||||
type CustomRoleHandler struct {
|
||||
svc *service.CustomRoleService
|
||||
}
|
||||
|
||||
// NewCustomRoleHandler creates a new CustomRole handler.
|
||||
func NewCustomRoleHandler(svc *service.CustomRoleService) *CustomRoleHandler {
|
||||
return &CustomRoleHandler{svc: svc}
|
||||
}
|
||||
|
||||
// List returns all custom roles for an account.
|
||||
// GET /api/v1/accounts/:account_id/custom_roles
|
||||
func (h *CustomRoleHandler) List(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
|
||||
return
|
||||
}
|
||||
|
||||
pg := pagination.Parse(c)
|
||||
roles, total, err := h.svc.List(c.Request.Context(), accountID, pg.Page, pg.PerPage)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("List custom roles for account %d: %v", accountID, err)
|
||||
handleServiceError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.OKWithMeta(c, roles, pg.Page, pg.PerPage, total)
|
||||
}
|
||||
|
||||
// Create creates a new custom role for an account.
|
||||
// POST /api/v1/accounts/:account_id/custom_roles
|
||||
// Request body uses Chatwoot-style wrapper: { "custom_role": { "name": "...", "permissions": {...} } }
|
||||
func (h *CustomRoleHandler) Create(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
|
||||
return
|
||||
}
|
||||
|
||||
var wrapper struct {
|
||||
CustomRole service.CreateCustomRoleRequest `json:"custom_role"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&wrapper); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
role, err := h.svc.Create(c.Request.Context(), accountID, wrapper.CustomRole)
|
||||
if err != nil {
|
||||
applogger.L().Errorf("Create custom role for account %d: %v", accountID, err)
|
||||
handleServiceError(c, err)
|
||||
return
|
||||
}
|
||||
|
||||
response.Created(c, role)
|
||||
}
|
||||
|
||||
// Get returns a single custom role by ID.
|
||||
// GET /api/v1/accounts/:account_id/custom_roles/:id
|
||||
func (h *CustomRoleHandler) Get(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
|
||||
return
|
||||
}
|
||||
|
||||
id, err := parseUintParam(c, "id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
|
||||
role, svcErr := h.svc.GetByID(c.Request.Context(), id, accountID)
|
||||
if svcErr != nil {
|
||||
applogger.L().Errorf("Get custom role %d for account %d: %v", id, accountID, svcErr)
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, role)
|
||||
}
|
||||
|
||||
// Update updates an existing custom role.
|
||||
// PUT /api/v1/accounts/:account_id/custom_roles/:id
|
||||
// Request body uses Chatwoot-style wrapper: { "custom_role": { "name": "...", "permissions": {...} } }
|
||||
func (h *CustomRoleHandler) Update(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
|
||||
return
|
||||
}
|
||||
|
||||
id, err := parseUintParam(c, "id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
|
||||
var wrapper struct {
|
||||
CustomRole service.UpdateCustomRoleRequest `json:"custom_role"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&wrapper); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
role, svcErr := h.svc.Update(c.Request.Context(), id, accountID, wrapper.CustomRole)
|
||||
if svcErr != nil {
|
||||
applogger.L().Errorf("Update custom role %d for account %d: %v", id, accountID, svcErr)
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, role)
|
||||
}
|
||||
|
||||
// Delete soft-deletes a custom role.
|
||||
// DELETE /api/v1/accounts/:account_id/custom_roles/:id
|
||||
func (h *CustomRoleHandler) Delete(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
|
||||
return
|
||||
}
|
||||
|
||||
id, err := parseUintParam(c, "id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid id")
|
||||
return
|
||||
}
|
||||
|
||||
if svcErr := h.svc.Delete(c.Request.Context(), id, accountID); svcErr != nil {
|
||||
applogger.L().Errorf("Delete custom role %d for account %d: %v", id, accountID, svcErr)
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.NoContent(c)
|
||||
}
|
||||
|
||||
// RegisterCustomRoleRoutes registers custom role routes on a gin.RouterGroup.
|
||||
func RegisterCustomRoleRoutes(rg *gin.RouterGroup, h *CustomRoleHandler) {
|
||||
customRoles := rg.Group("/custom_roles")
|
||||
{
|
||||
customRoles.GET("/", h.List)
|
||||
customRoles.POST("/", h.Create)
|
||||
customRoles.GET("/:id", h.Get)
|
||||
customRoles.PUT("/:id", h.Update)
|
||||
customRoles.DELETE("/:id", h.Delete)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user