feat(agent-bots): align account payloads
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
"github.com/gochat/gochat/internal/service"
|
||||
"github.com/gochat/gochat/pkg/pagination"
|
||||
"github.com/gochat/gochat/pkg/response"
|
||||
@@ -33,32 +34,41 @@ func (h *AgentBotHandler) List(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
page := pagination.Parse(c)
|
||||
bots, total, svcErr := h.svc.ListAccessible(c.Request.Context(), accountID, page.Offset, page.PerPage)
|
||||
bots, svcErr := h.svc.ListAccessibleAll(c.Request.Context(), accountID)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OKWithMeta(c, bots, page.Page, page.PerPage, total)
|
||||
payload := make([]gin.H, 0, len(bots))
|
||||
for i := range bots {
|
||||
payload = append(payload, serializeAccountAgentBot(&bots[i], accountID))
|
||||
}
|
||||
c.JSON(http.StatusOK, payload)
|
||||
}
|
||||
|
||||
// Get retrieves a single agent bot by ID.
|
||||
// GET /api/v1/accounts/:account_id/agent_bots/:id
|
||||
func (h *AgentBotHandler) Get(c *gin.Context) {
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
agentBotID, err := parseUintParam(c, "agent_bot_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid agent bot ID")
|
||||
return
|
||||
}
|
||||
|
||||
bot, svcErr := h.svc.Get(c.Request.Context(), agentBotID)
|
||||
bot, svcErr := h.svc.GetAccessible(c.Request.Context(), accountID, agentBotID)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, bot)
|
||||
c.JSON(http.StatusOK, serializeAccountAgentBot(bot, accountID))
|
||||
}
|
||||
|
||||
// Create creates a new agent bot scoped to an account.
|
||||
@@ -71,7 +81,7 @@ func (h *AgentBotHandler) Create(c *gin.Context) {
|
||||
}
|
||||
|
||||
var req service.CreateAgentBotRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
@@ -85,12 +95,18 @@ func (h *AgentBotHandler) Create(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.Created(c, bot)
|
||||
c.JSON(http.StatusOK, serializeAccountAgentBot(bot, accountID))
|
||||
}
|
||||
|
||||
// Update modifies an existing agent bot.
|
||||
// PUT /api/v1/accounts/:account_id/agent_bots/:id
|
||||
func (h *AgentBotHandler) Update(c *gin.Context) {
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
agentBotID, err := parseUintParam(c, "agent_bot_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid agent bot ID")
|
||||
@@ -98,96 +114,143 @@ func (h *AgentBotHandler) Update(c *gin.Context) {
|
||||
}
|
||||
|
||||
var req service.UpdateAgentBotRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
if err := c.ShouldBind(&req); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
bot, svcErr := h.svc.Update(c.Request.Context(), agentBotID, req)
|
||||
bot, svcErr := h.svc.UpdateByAccount(c.Request.Context(), accountID, agentBotID, req)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, bot)
|
||||
c.JSON(http.StatusOK, serializeAccountAgentBot(bot, accountID))
|
||||
}
|
||||
|
||||
// Delete removes an agent bot by ID.
|
||||
// DELETE /api/v1/accounts/:account_id/agent_bots/:id
|
||||
func (h *AgentBotHandler) Delete(c *gin.Context) {
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
agentBotID, err := parseUintParam(c, "agent_bot_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid agent bot ID")
|
||||
return
|
||||
}
|
||||
|
||||
if svcErr := h.svc.Delete(c.Request.Context(), agentBotID); svcErr != nil {
|
||||
if svcErr := h.svc.DeleteByAccount(c.Request.Context(), accountID, agentBotID); svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.NoContent(c)
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
// ResetToken generates a new access token for the bot.
|
||||
// POST /api/v1/accounts/:account_id/agent_bots/:id/reset_token
|
||||
// Reference: Chatwoot AgentBotsController#reset_access_token
|
||||
func (h *AgentBotHandler) ResetToken(c *gin.Context) {
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
agentBotID, err := parseUintParam(c, "agent_bot_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid agent bot ID")
|
||||
return
|
||||
}
|
||||
|
||||
newToken, svcErr := h.svc.ResetToken(c.Request.Context(), agentBotID)
|
||||
bot, svcErr := h.svc.ResetTokenByAccount(c.Request.Context(), accountID, agentBotID)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, gin.H{
|
||||
"access_token": newToken,
|
||||
})
|
||||
c.JSON(http.StatusOK, serializeAccountAgentBot(bot, accountID))
|
||||
}
|
||||
|
||||
// ResetSecret generates a new webhook signing secret for the bot.
|
||||
// POST /api/v1/accounts/:account_id/agent_bots/:id/reset_secret
|
||||
// Reference: Chatwoot AgentBotsController#reset_secret
|
||||
func (h *AgentBotHandler) ResetSecret(c *gin.Context) {
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
agentBotID, err := parseUintParam(c, "agent_bot_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid agent bot ID")
|
||||
return
|
||||
}
|
||||
|
||||
newSecret, svcErr := h.svc.ResetSecret(c.Request.Context(), agentBotID)
|
||||
bot, svcErr := h.svc.ResetSecretByAccount(c.Request.Context(), accountID, agentBotID)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, gin.H{
|
||||
"secret": newSecret,
|
||||
})
|
||||
c.JSON(http.StatusOK, serializeAccountAgentBot(bot, accountID))
|
||||
}
|
||||
|
||||
// DeleteAvatar removes the bot's avatar URL.
|
||||
// POST /api/v1/accounts/:account_id/agent_bots/:id/delete_avatar
|
||||
// Reference: Chatwoot AgentBotsController#destroy_avatar
|
||||
func (h *AgentBotHandler) DeleteAvatar(c *gin.Context) {
|
||||
accountID, err := parseUintParam(c, "account_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid account_id")
|
||||
return
|
||||
}
|
||||
|
||||
agentBotID, err := parseUintParam(c, "agent_bot_id")
|
||||
if err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid agent bot ID")
|
||||
return
|
||||
}
|
||||
|
||||
bot, svcErr := h.svc.DeleteAvatar(c.Request.Context(), agentBotID)
|
||||
bot, svcErr := h.svc.DeleteAvatarByAccount(c.Request.Context(), accountID, agentBotID)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, bot)
|
||||
c.JSON(http.StatusOK, serializeAccountAgentBot(bot, accountID))
|
||||
}
|
||||
|
||||
func serializeAccountAgentBot(bot *model.AgentBot, accountID uint) gin.H {
|
||||
if bot == nil {
|
||||
return gin.H{}
|
||||
}
|
||||
systemBot := bot.AccountID == nil
|
||||
payload := gin.H{
|
||||
"id": bot.ID,
|
||||
"name": bot.Name,
|
||||
"description": bot.Description,
|
||||
"thumbnail": bot.AvatarURL,
|
||||
"bot_type": bot.BotType,
|
||||
"bot_config": bot.Config,
|
||||
"account_id": bot.AccountID,
|
||||
"system_bot": systemBot,
|
||||
}
|
||||
if !systemBot {
|
||||
payload["outgoing_url"] = bot.OutgoingURL
|
||||
}
|
||||
if bot.AccountID != nil && *bot.AccountID == accountID && bot.AccessToken != "" {
|
||||
payload["access_token"] = bot.AccessToken
|
||||
}
|
||||
if bot.AccountID != nil && *bot.AccountID == accountID && bot.Secret != "" {
|
||||
payload["secret"] = bot.Secret
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
// --- Platform (super admin) AgentBot endpoints ---
|
||||
@@ -347,6 +410,7 @@ func (h *AgentBotHandler) PlatformResetConfig(c *gin.Context) {
|
||||
|
||||
response.OK(c, bot)
|
||||
}
|
||||
|
||||
// ListAccessible retrieves agent bots accessible to an account (global + account-scoped), paginated.
|
||||
// GET /api/v1/accounts/:account_id/agent_bots/accessible
|
||||
func (h *AgentBotHandler) ListAccessible(c *gin.Context) {
|
||||
|
||||
Reference in New Issue
Block a user