206 lines
6.3 KiB
Go
206 lines
6.3 KiB
Go
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/pagination"
|
|
"github.com/gochat/gochat/pkg/response"
|
|
)
|
|
|
|
// CampaignHandler handles Campaign CRUD + trigger actions.
|
|
// Reference: Chatwoot app/controllers/api/v1/campaigns_controller.rb
|
|
type CampaignHandler struct {
|
|
svc *service.CampaignService
|
|
}
|
|
|
|
// NewCampaignHandler creates a new Campaign handler.
|
|
func NewCampaignHandler(svc *service.CampaignService) *CampaignHandler {
|
|
return &CampaignHandler{svc: svc}
|
|
}
|
|
|
|
// List returns all campaigns for an account.
|
|
// GET /api/v1/accounts/:id/campaigns
|
|
func (h *CampaignHandler) 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)
|
|
campaigns, total, err := h.svc.List(c.Request.Context(), accountID, pg.Offset, pg.PerPage)
|
|
if err != nil {
|
|
applogger.L().Errorf("List campaigns for account %d: %v", accountID, err)
|
|
handleServiceError(c, err)
|
|
return
|
|
}
|
|
|
|
response.OKWithMeta(c, campaigns, pg.Page, pg.PerPage, total)
|
|
}
|
|
|
|
// Get returns a single campaign by ID.
|
|
// GET /api/v1/accounts/:id/campaigns/:campaign_id
|
|
func (h *CampaignHandler) Get(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("campaign_id"), 10, 32)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid campaign ID")
|
|
return
|
|
}
|
|
|
|
campaign, svcErr := h.svc.Get(c.Request.Context(), uint(id), accountID)
|
|
if svcErr != nil {
|
|
applogger.L().Errorf("Get campaign %d for account %d: %v", id, accountID, svcErr)
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OK(c, campaign)
|
|
}
|
|
|
|
// Create creates a new campaign within an account.
|
|
// POST /api/v1/accounts/:id/campaigns
|
|
func (h *CampaignHandler) Create(c *gin.Context) {
|
|
accountID := getAccountID(c)
|
|
if accountID == 0 {
|
|
response.AbortWithStatusError(c, http.StatusUnauthorized, response.ErrUnauthorized, "account not identified")
|
|
return
|
|
}
|
|
|
|
// Chatwoot: params.require(:campaign) → request body must be {"campaign": {...}}
|
|
var wrapper struct {
|
|
Campaign service.CreateCampaignRequest `json:"campaign"`
|
|
}
|
|
if err := c.ShouldBindJSON(&wrapper); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
|
|
return
|
|
}
|
|
req := wrapper.Campaign
|
|
|
|
campaign, svcErr := h.svc.Create(c.Request.Context(), accountID, req)
|
|
if svcErr != nil {
|
|
applogger.L().Errorf("Create campaign for account %d: %v", accountID, svcErr)
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.Created(c, campaign)
|
|
}
|
|
|
|
// Update updates an existing campaign.
|
|
// PUT /api/v1/accounts/:id/campaigns/:campaign_id
|
|
func (h *CampaignHandler) Update(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("campaign_id"), 10, 32)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid campaign ID")
|
|
return
|
|
}
|
|
|
|
// Chatwoot: params.require(:campaign) → request body must be {"campaign": {...}}
|
|
var wrapper struct {
|
|
Campaign service.UpdateCampaignRequest `json:"campaign"`
|
|
}
|
|
if err := c.ShouldBindJSON(&wrapper); err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, err.Error())
|
|
return
|
|
}
|
|
req := wrapper.Campaign
|
|
|
|
campaign, svcErr := h.svc.Update(c.Request.Context(), uint(id), accountID, req)
|
|
if svcErr != nil {
|
|
applogger.L().Errorf("Update campaign %d for account %d: %v", id, accountID, svcErr)
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OK(c, campaign)
|
|
}
|
|
|
|
// Delete soft-deletes a campaign.
|
|
// DELETE /api/v1/accounts/:id/campaigns/:campaign_id
|
|
func (h *CampaignHandler) Delete(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("campaign_id"), 10, 32)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid campaign ID")
|
|
return
|
|
}
|
|
|
|
if svcErr := h.svc.Delete(c.Request.Context(), uint(id), accountID); svcErr != nil {
|
|
applogger.L().Errorf("Delete campaign %d for account %d: %v", id, accountID, svcErr)
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.NoContent(c)
|
|
}
|
|
|
|
// Start triggers a campaign execution.
|
|
// POST /api/v1/accounts/:id/campaigns/:campaign_id/start
|
|
func (h *CampaignHandler) Start(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("campaign_id"), 10, 32)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid campaign ID")
|
|
return
|
|
}
|
|
|
|
if svcErr := h.svc.Start(c.Request.Context(), uint(id), accountID); svcErr != nil {
|
|
applogger.L().Errorf("Start campaign %d for account %d: %v", id, accountID, svcErr)
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OK(c, gin.H{"message": "campaign triggered successfully"})
|
|
}
|
|
|
|
// Stop marks a campaign as completed.
|
|
// POST /api/v1/accounts/:id/campaigns/:campaign_id/stop
|
|
func (h *CampaignHandler) Stop(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("campaign_id"), 10, 32)
|
|
if err != nil {
|
|
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid campaign ID")
|
|
return
|
|
}
|
|
|
|
if svcErr := h.svc.Stop(c.Request.Context(), uint(id), accountID); svcErr != nil {
|
|
applogger.L().Errorf("Stop campaign %d for account %d: %v", id, accountID, svcErr)
|
|
handleServiceError(c, svcErr)
|
|
return
|
|
}
|
|
|
|
response.OK(c, gin.H{"message": "campaign stopped successfully"})
|
|
}
|