feat(campaigns): align chatwoot payloads
This commit is contained in:
@@ -1,14 +1,16 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gochat/gochat/internal/campaign"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
@@ -32,15 +34,14 @@ func (h *CampaignHandler) List(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
pg := pagination.Parse(c)
|
||||
campaigns, total, err := h.svc.List(c.Request.Context(), accountID, pg.Offset, pg.PerPage)
|
||||
campaigns, _, err := h.svc.List(c.Request.Context(), accountID, 0, 0)
|
||||
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)
|
||||
c.JSON(http.StatusOK, serializeCampaigns(campaigns, accountID))
|
||||
}
|
||||
|
||||
// Get returns a single campaign by ID.
|
||||
@@ -65,7 +66,7 @@ func (h *CampaignHandler) Get(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, campaign)
|
||||
c.JSON(http.StatusOK, serializeCampaign(campaign, accountID))
|
||||
}
|
||||
|
||||
// Create creates a new campaign within an account.
|
||||
@@ -94,7 +95,7 @@ func (h *CampaignHandler) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.Created(c, campaign)
|
||||
c.JSON(http.StatusOK, serializeCampaign(campaign, accountID))
|
||||
}
|
||||
|
||||
// Update updates an existing campaign.
|
||||
@@ -129,7 +130,7 @@ func (h *CampaignHandler) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, campaign)
|
||||
c.JSON(http.StatusOK, serializeCampaign(campaign, accountID))
|
||||
}
|
||||
|
||||
// Delete soft-deletes a campaign.
|
||||
@@ -153,7 +154,7 @@ func (h *CampaignHandler) Delete(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.NoContent(c)
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
// Start triggers a campaign execution.
|
||||
@@ -180,6 +181,68 @@ func (h *CampaignHandler) Start(c *gin.Context) {
|
||||
response.OK(c, gin.H{"message": "campaign triggered successfully"})
|
||||
}
|
||||
|
||||
func serializeCampaigns(campaigns []campaign.Campaign, accountID uint) []map[string]any {
|
||||
payload := make([]map[string]any, 0, len(campaigns))
|
||||
for i := range campaigns {
|
||||
payload = append(payload, serializeCampaign(&campaigns[i], accountID))
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func serializeCampaign(item *campaign.Campaign, accountID uint) map[string]any {
|
||||
if item == nil {
|
||||
return map[string]any{}
|
||||
}
|
||||
id := item.DisplayID
|
||||
if id == 0 {
|
||||
id = item.ID
|
||||
}
|
||||
payload := map[string]any{
|
||||
"id": id,
|
||||
"title": item.Title,
|
||||
"description": item.Description,
|
||||
"account_id": item.AccountID,
|
||||
"inbox": nil,
|
||||
"sender": nil,
|
||||
"message": item.Message,
|
||||
"template_params": campaignJSONValue(item.TemplateParams),
|
||||
"campaign_status": item.CampaignStatus,
|
||||
"enabled": item.Enabled,
|
||||
"campaign_type": item.CampaignType,
|
||||
"trigger_rules": campaignJSONValue(item.TriggerRules),
|
||||
"trigger_only_during_business_hours": item.TriggerOnlyDuringBusinessHours,
|
||||
"created_at": item.CreatedAt,
|
||||
"updated_at": item.UpdatedAt,
|
||||
}
|
||||
if item.Inbox.ID != 0 {
|
||||
payload["inbox"] = serializeInbox(&item.Inbox)
|
||||
}
|
||||
if item.Sender != nil && item.Sender.ID != 0 {
|
||||
payload["sender"] = serializeAgentUser(item.Sender, accountID, "", "", false, 0)
|
||||
}
|
||||
if item.CampaignType == campaign.CampaignTypeOneOff {
|
||||
if item.ScheduledAt != nil {
|
||||
payload["scheduled_at"] = item.ScheduledAt.Unix()
|
||||
} else {
|
||||
payload["scheduled_at"] = nil
|
||||
}
|
||||
payload["audience"] = campaignJSONValue(item.Audience)
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func campaignJSONValue(raw string) any {
|
||||
trimmed := strings.TrimSpace(raw)
|
||||
if trimmed == "" {
|
||||
return map[string]any{}
|
||||
}
|
||||
var value any
|
||||
if err := json.Unmarshal([]byte(trimmed), &value); err != nil {
|
||||
return trimmed
|
||||
}
|
||||
return value
|
||||
}
|
||||
|
||||
// Stop marks a campaign as completed.
|
||||
// POST /api/v1/accounts/:id/campaigns/:campaign_id/stop
|
||||
func (h *CampaignHandler) Stop(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user