feat(inboxes): align agent bot member actions
This commit is contained in:
@@ -9,6 +9,7 @@ import (
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/gochat/gochat/internal/model"
|
||||
"github.com/gochat/gochat/internal/repository"
|
||||
"github.com/gochat/gochat/internal/service"
|
||||
"github.com/gochat/gochat/pkg/response"
|
||||
@@ -576,29 +577,29 @@ func (h *InboxHandler) SetAgentBot(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
var req service.SetAgentBotRequest
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body: " + err.Error()})
|
||||
return
|
||||
req := service.SetAgentBotRequest{}
|
||||
if c.Request.Body != nil && c.Request.ContentLength != 0 {
|
||||
if err := c.ShouldBindJSON(&req); err != nil {
|
||||
if c.Request.ContentLength < 0 && errors.Is(err, io.EOF) {
|
||||
// Chunked empty body behaves like omitted params in Chatwoot and disconnects the bot.
|
||||
} else {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid request body: " + err.Error()})
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
if !h.svc.Ready() {
|
||||
response.AbortWithStatusError(c, http.StatusUnprocessableEntity, response.ErrInternal, "failed to set agent bot")
|
||||
return
|
||||
}
|
||||
|
||||
binding, svcErr := h.svc.SetAgentBot(c.Request.Context(), accountID, inboxID, req)
|
||||
_, svcErr := h.svc.SetAgentBot(c.Request.Context(), accountID, inboxID, req)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
if binding == nil {
|
||||
// Agent bot was removed
|
||||
c.JSON(http.StatusOK, gin.H{"message": "agent bot removed from inbox"})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, binding)
|
||||
c.Status(http.StatusOK)
|
||||
}
|
||||
|
||||
// Health checks the health status of an inbox's channel connection.
|
||||
@@ -738,11 +739,28 @@ func (h *InboxHandler) GetAgentBot(c *gin.Context) {
|
||||
}
|
||||
|
||||
if agentBot == nil {
|
||||
c.JSON(http.StatusOK, gin.H{"agent_bot": nil})
|
||||
c.JSON(http.StatusOK, gin.H{"agent_bot": gin.H{}})
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"agent_bot": agentBot})
|
||||
c.JSON(http.StatusOK, gin.H{"agent_bot": serializeAgentBot(agentBot)})
|
||||
}
|
||||
|
||||
func serializeAgentBot(bot *model.AgentBot) gin.H {
|
||||
if bot == nil {
|
||||
return gin.H{}
|
||||
}
|
||||
return gin.H{
|
||||
"id": bot.ID,
|
||||
"name": bot.Name,
|
||||
"description": bot.Description,
|
||||
"thumbnail": bot.AvatarURL,
|
||||
"outgoing_url": bot.OutgoingURL,
|
||||
"bot_type": bot.BotType,
|
||||
"bot_config": bot.Config,
|
||||
"account_id": bot.AccountID,
|
||||
"access_token": bot.AccessToken,
|
||||
}
|
||||
}
|
||||
|
||||
// DeleteAvatar removes the avatar URL from an inbox.
|
||||
|
||||
Reference in New Issue
Block a user