feat(csat): queue channel templates

This commit is contained in:
2026-06-05 17:40:04 +08:00
parent 9a1813485f
commit 7ae38d020d
9 changed files with 547 additions and 30 deletions
@@ -1,6 +1,7 @@
package v1
import (
"errors"
"net/http"
"github.com/gin-gonic/gin"
@@ -31,18 +32,12 @@ func (h *InboxCsatTemplateHandler) Show(c *gin.Context) {
return
}
template, svcErr := h.svc.ShowTemplateStatus(c.Request.Context(), inboxID)
status, svcErr := h.svc.ShowTemplateStatusResult(c.Request.Context(), inboxID)
if svcErr != nil {
handleServiceError(c, svcErr)
return
}
// If no template exists for this inbox, return empty object (Chatwoot pattern)
if template == nil {
response.OK(c, gin.H{})
return
}
response.OK(c, template)
response.OK(c, status)
}
// Create creates or updates a CSAT template for an inbox.
@@ -55,8 +50,8 @@ func (h *InboxCsatTemplateHandler) Create(c *gin.Context) {
return
}
var req service.CreateCsatTemplateRequest
if err := c.ShouldBindJSON(&req); err != nil {
req, err := bindCreateCsatTemplateRequest(c)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid request body")
return
}
@@ -67,7 +62,7 @@ func (h *InboxCsatTemplateHandler) Create(c *gin.Context) {
return
}
response.OK(c, template)
c.JSON(http.StatusCreated, template)
}
// Analyze analyzes a CSAT template message for quality.
@@ -80,8 +75,8 @@ func (h *InboxCsatTemplateHandler) Analyze(c *gin.Context) {
return
}
var req service.AnalyzeCsatTemplateRequest
if err := c.ShouldBindJSON(&req); err != nil {
req, err := bindAnalyzeCsatTemplateRequest(c)
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid request body")
return
}
@@ -93,4 +88,56 @@ func (h *InboxCsatTemplateHandler) Analyze(c *gin.Context) {
}
response.OK(c, result)
}
}
func bindCreateCsatTemplateRequest(c *gin.Context) (service.CreateCsatTemplateRequest, error) {
var body struct {
Message string `json:"message"`
ButtonText string `json:"button_text"`
Language string `json:"language"`
Template service.CreateCsatTemplateRequest `json:"template"`
}
if err := c.ShouldBindJSON(&body); err != nil {
return service.CreateCsatTemplateRequest{}, err
}
req := body.Template
if req.Message == "" {
req.Message = body.Message
}
if req.ButtonText == "" {
req.ButtonText = body.ButtonText
}
if req.Language == "" {
req.Language = body.Language
}
if req.Message == "" {
return req, errors.New("message is required")
}
return req, nil
}
func bindAnalyzeCsatTemplateRequest(c *gin.Context) (service.AnalyzeCsatTemplateRequest, error) {
var body struct {
Message string `json:"message"`
ButtonText string `json:"button_text"`
Language string `json:"language"`
Template service.AnalyzeCsatTemplateRequest `json:"template"`
}
if err := c.ShouldBindJSON(&body); err != nil {
return service.AnalyzeCsatTemplateRequest{}, err
}
req := body.Template
if req.Message == "" {
req.Message = body.Message
}
if req.ButtonText == "" {
req.ButtonText = body.ButtonText
}
if req.Language == "" {
req.Language = body.Language
}
if req.Message == "" {
return req, errors.New("message is required")
}
return req, nil
}
@@ -91,6 +91,21 @@ func (s *InboxCsatTemplateHandlerTestSuite) TestCreate_BadRequest_InvalidInboxID
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
}
func (s *InboxCsatTemplateHandlerTestSuite) TestCreate_ChatwootNestedTemplatePayload() {
r := gin.New()
r.POST("/api/v1/accounts/:account_id/inboxes/:inbox_id/csat_template", s.handler.Create)
w := httptest.NewRecorder()
body := bytes.NewBufferString(`{"template":{"message":"Please rate our support","button_text":"Rate us","language":"en"}}`)
req, _ := http.NewRequest("POST", fmt.Sprintf("/api/v1/accounts/%d/inboxes/%d/csat_template", s.account.ID, s.inbox.ID), body)
req.Header.Set("Content-Type", "application/json")
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusCreated, w.Code)
assert.Contains(s.T(), w.Body.String(), "Please rate our support")
assert.Contains(s.T(), w.Body.String(), "PENDING")
}
func (s *InboxCsatTemplateHandlerTestSuite) TestAnalyze_BadRequest_InvalidInboxID() {
r := gin.New()
r.POST("/api/v1/accounts/:account_id/inboxes/:inbox_id/csat_template/analyze", s.handler.Analyze)
@@ -101,4 +116,4 @@ func (s *InboxCsatTemplateHandlerTestSuite) TestAnalyze_BadRequest_InvalidInboxI
r.ServeHTTP(w, req)
assert.Equal(s.T(), http.StatusBadRequest, w.Code)
}
}