second commit
This commit is contained in:
@@ -0,0 +1,234 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/gochat/gochat/internal/service"
|
||||
applogger "github.com/gochat/gochat/pkg/logger"
|
||||
"github.com/gochat/gochat/pkg/response"
|
||||
)
|
||||
|
||||
// AssignmentPolicyHandler handles AssignmentPolicy CRUD + auto-assignment actions.
|
||||
// Reference: Chatwoot app/controllers/api/v1/assignment_policies_controller.rb
|
||||
type AssignmentPolicyHandler struct {
|
||||
svc *service.AssignmentPolicyService
|
||||
}
|
||||
|
||||
// NewAssignmentPolicyHandler creates a new AssignmentPolicy handler.
|
||||
func NewAssignmentPolicyHandler(svc *service.AssignmentPolicyService) *AssignmentPolicyHandler {
|
||||
return &AssignmentPolicyHandler{svc: svc}
|
||||
}
|
||||
|
||||
// GetAccountPolicy returns the account-level assignment policy.
|
||||
// GET /api/v1/accounts/:id/assignment_policy
|
||||
func (h *AssignmentPolicyHandler) GetAccountPolicy(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
|
||||
return
|
||||
}
|
||||
|
||||
policy, svcErr := h.svc.GetAccountPolicy(c.Request.Context(), accountID)
|
||||
if svcErr != nil {
|
||||
applogger.L().Errorf("Get assignment policy for account %d: %v", accountID, svcErr)
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, policy)
|
||||
}
|
||||
|
||||
// CreateAccountPolicy creates the account-level assignment policy.
|
||||
// POST /api/v1/accounts/:id/assignment_policy
|
||||
func (h *AssignmentPolicyHandler) CreateAccountPolicy(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
|
||||
return
|
||||
}
|
||||
|
||||
var req service.CreatePolicyRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
policy, svcErr := h.svc.CreateAccountPolicy(c.Request.Context(), accountID, req)
|
||||
if svcErr != nil {
|
||||
applogger.L().Errorf("Create assignment policy for account %d: %v", accountID, svcErr)
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.Created(c, policy)
|
||||
}
|
||||
|
||||
// UpdateAccountPolicy updates the account-level assignment policy.
|
||||
// PUT /api/v1/accounts/:id/assignment_policy
|
||||
func (h *AssignmentPolicyHandler) UpdateAccountPolicy(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseUint(c.Param("policy_id"), 10, 32)
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid policy ID")
|
||||
return
|
||||
}
|
||||
|
||||
var req service.UpdatePolicyRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
policy, svcErr := h.svc.UpdateAccountPolicy(c.Request.Context(), uint(id), accountID, req)
|
||||
if svcErr != nil {
|
||||
applogger.L().Errorf("Update assignment policy %d for account %d: %v", id, accountID, svcErr)
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, policy)
|
||||
}
|
||||
|
||||
// DeleteAccountPolicy deletes the account-level assignment policy.
|
||||
// DELETE /api/v1/accounts/:id/assignment_policy/:policy_id
|
||||
func (h *AssignmentPolicyHandler) DeleteAccountPolicy(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseUint(c.Param("policy_id"), 10, 32)
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid policy ID")
|
||||
return
|
||||
}
|
||||
|
||||
if svcErr := h.svc.DeleteAccountPolicy(c.Request.Context(), uint(id), accountID); svcErr != nil {
|
||||
applogger.L().Errorf("Delete assignment policy %d for account %d: %v", id, accountID, svcErr)
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.NoContent(c)
|
||||
}
|
||||
|
||||
// GetInboxPolicy returns the inbox-level assignment policy override.
|
||||
// GET /api/v1/accounts/:id/inboxes/:inbox_id/assignment_policy
|
||||
func (h *AssignmentPolicyHandler) GetInboxPolicy(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
|
||||
return
|
||||
}
|
||||
|
||||
inboxID, err := strconv.ParseUint(c.Param("inbox_id"), 10, 32)
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid inbox ID")
|
||||
return
|
||||
}
|
||||
|
||||
policy, svcErr := h.svc.GetInboxPolicy(c.Request.Context(), accountID, uint(inboxID))
|
||||
if svcErr != nil {
|
||||
applogger.L().Errorf("Get inbox assignment policy for inbox %d: %v", inboxID, svcErr)
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, policy)
|
||||
}
|
||||
|
||||
// CreateInboxPolicy creates an inbox-level assignment policy override.
|
||||
// POST /api/v1/accounts/:id/inboxes/:inbox_id/assignment_policy
|
||||
func (h *AssignmentPolicyHandler) CreateInboxPolicy(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
|
||||
return
|
||||
}
|
||||
|
||||
inboxID, err := strconv.ParseUint(c.Param("inbox_id"), 10, 32)
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid inbox ID")
|
||||
return
|
||||
}
|
||||
|
||||
var req service.CreateInboxPolicyRequest
|
||||
req.InboxID = uint(inboxID) // Set from URL param
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
policy, svcErr := h.svc.CreateInboxPolicy(c.Request.Context(), accountID, req)
|
||||
if svcErr != nil {
|
||||
applogger.L().Errorf("Create inbox assignment policy for inbox %d: %v", inboxID, svcErr)
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.Created(c, policy)
|
||||
}
|
||||
|
||||
// UpdateInboxPolicy updates an inbox-level assignment policy override.
|
||||
// PUT /api/v1/accounts/:id/inboxes/:inbox_id/assignment_policy/:policy_id
|
||||
func (h *AssignmentPolicyHandler) UpdateInboxPolicy(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseUint(c.Param("policy_id"), 10, 32)
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid policy ID")
|
||||
return
|
||||
}
|
||||
|
||||
var req service.UpdateInboxPolicyRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
policy, svcErr := h.svc.UpdateInboxPolicy(c.Request.Context(), uint(id), accountID, req)
|
||||
if svcErr != nil {
|
||||
applogger.L().Errorf("Update inbox assignment policy %d: %v", id, svcErr)
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, policy)
|
||||
}
|
||||
|
||||
// DeleteInboxPolicy deletes an inbox-level assignment policy override.
|
||||
// DELETE /api/v1/accounts/:id/inboxes/:inbox_id/assignment_policy/:policy_id
|
||||
func (h *AssignmentPolicyHandler) DeleteInboxPolicy(c *gin.Context) {
|
||||
accountID := getAccountID(c)
|
||||
if accountID == 0 {
|
||||
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
|
||||
return
|
||||
}
|
||||
|
||||
id, err := strconv.ParseUint(c.Param("policy_id"), 10, 32)
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid policy ID")
|
||||
return
|
||||
}
|
||||
|
||||
if svcErr := h.svc.DeleteInboxPolicy(c.Request.Context(), uint(id), accountID); svcErr != nil {
|
||||
applogger.L().Errorf("Delete inbox assignment policy %d: %v", id, svcErr)
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.NoContent(c)
|
||||
}
|
||||
Reference in New Issue
Block a user