Files
gochat/internal/handler/api/v1/email_channel_migration_handler.go
T
2026-06-04 15:44:48 +08:00

78 lines
2.5 KiB
Go

package v1
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/internal/model"
"github.com/gochat/gochat/internal/service"
"github.com/gochat/gochat/pkg/response"
)
// EmailChannelMigrationHandler handles account-scoped email channel migration endpoints.
// Reference: Chatwoot api/v1/accounts/:account_id/email_channel_migrations — create-only
// endpoint for initiating migration of an email channel from one inbox to another.
type EmailChannelMigrationHandler struct {
svc *service.EmailChannelMigrationService
}
// NewEmailChannelMigrationHandler creates a new EmailChannelMigrationHandler with service injection.
func NewEmailChannelMigrationHandler(svc *service.EmailChannelMigrationService) *EmailChannelMigrationHandler {
return &EmailChannelMigrationHandler{svc: svc}
}
// Create initiates a new email channel migration from one inbox to another.
// POST /api/v1/accounts/:account_id/email_channel_migrations
func (h *EmailChannelMigrationHandler) Create(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
var req struct {
InboxID uint `json:"inbox_id" binding:"required"`
TargetInboxID uint `json:"target_inbox_id" binding:"required"`
}
if err := c.ShouldBindJSON(&req); err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
return
}
migration := &model.EmailChannelMigration{
AccountID: accountID,
InboxID: req.InboxID,
TargetInboxID: req.TargetInboxID,
MigrationStatus: "pending",
}
if svcErr := h.svc.Create(c.Request.Context(), migration); svcErr != nil {
handleServiceError(c, svcErr)
return
}
response.Created(c, migration)
}
// List retrieves all email channel migrations for the current account.
// GET /api/v1/accounts/:account_id/email_channel_migrations
func (h *EmailChannelMigrationHandler) List(c *gin.Context) {
accountID := getAccountID(c)
if accountID == 0 {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
return
}
migrations, err := h.svc.ListByAccount(c.Request.Context(), accountID)
if err != nil {
handleServiceError(c, err)
return
}
response.OK(c, migrations)
}
// RegisterEmailChannelMigrationRoutes registers email channel migration routes on the given router group.
func RegisterEmailChannelMigrationRoutes(g *gin.RouterGroup, h *EmailChannelMigrationHandler) {
g.POST("/", h.Create)
g.GET("/", h.List)
}