feat(csat): align account report payloads
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package v1
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"net/http"
|
||||
@@ -36,20 +37,18 @@ func (h *CsatSurveyHandler) List(c *gin.Context) {
|
||||
|
||||
filter := buildCsatFilter(c)
|
||||
|
||||
responses, total, svcErr := h.svc.ListByAccount(c.Request.Context(), accountID, filter)
|
||||
responses, _, svcErr := h.svc.ListByAccount(c.Request.Context(), accountID, filter)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{
|
||||
"csat_survey_responses": responses,
|
||||
"meta": gin.H{
|
||||
"count": total,
|
||||
"page": filter.Page,
|
||||
"per_page": filter.PageSize,
|
||||
},
|
||||
})
|
||||
payload := make([]map[string]any, 0, len(responses))
|
||||
for i := range responses {
|
||||
payload = append(payload, h.serializeCsatSurveyResponse(c.Request.Context(), &responses[i]))
|
||||
}
|
||||
|
||||
c.JSON(http.StatusOK, payload)
|
||||
}
|
||||
|
||||
// Metrics computes aggregated CSAT statistics for an account.
|
||||
@@ -69,7 +68,7 @@ func (h *CsatSurveyHandler) Metrics(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, metrics)
|
||||
c.JSON(http.StatusOK, metrics)
|
||||
}
|
||||
|
||||
// UpdateReviewNotes updates the review notes on a CSAT survey response.
|
||||
@@ -85,22 +84,28 @@ func (h *CsatSurveyHandler) UpdateReviewNotes(c *gin.Context) {
|
||||
|
||||
var body struct {
|
||||
CsatReviewNotes string `json:"csat_review_notes"`
|
||||
ReviewNotes string `json:"review_notes"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
return
|
||||
}
|
||||
|
||||
if svcErr := h.svc.UpdateReviewNotes(c.Request.Context(), id, body.CsatReviewNotes, userID); svcErr != nil {
|
||||
notes := body.CsatReviewNotes
|
||||
if notes == "" {
|
||||
notes = body.ReviewNotes
|
||||
}
|
||||
if svcErr := h.svc.UpdateReviewNotes(c.Request.Context(), id, notes, userID); svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, gin.H{
|
||||
"id": id,
|
||||
"csat_review_notes": body.CsatReviewNotes,
|
||||
"updated_by_id": userID,
|
||||
})
|
||||
resp, svcErr := h.svc.GetByID(c.Request.Context(), id)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
c.JSON(http.StatusOK, h.serializeCsatSurveyResponse(c.Request.Context(), resp))
|
||||
}
|
||||
|
||||
// Update updates a CSAT survey response (rating, feedback, review_notes).
|
||||
@@ -116,6 +121,7 @@ func (h *CsatSurveyHandler) Update(c *gin.Context) {
|
||||
Rating int `json:"rating"`
|
||||
FeedbackMessage string `json:"feedback_message"`
|
||||
CsatReviewNotes string `json:"csat_review_notes"`
|
||||
ReviewNotes string `json:"review_notes"`
|
||||
}
|
||||
if err := c.ShouldBindJSON(&body); err != nil {
|
||||
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrValidation, err.Error())
|
||||
@@ -126,13 +132,17 @@ func (h *CsatSurveyHandler) Update(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
resp, svcErr := h.svc.Update(c.Request.Context(), id, body.Rating, body.FeedbackMessage, body.CsatReviewNotes)
|
||||
notes := body.CsatReviewNotes
|
||||
if notes == "" {
|
||||
notes = body.ReviewNotes
|
||||
}
|
||||
resp, svcErr := h.svc.Update(c.Request.Context(), id, body.Rating, body.FeedbackMessage, notes)
|
||||
if svcErr != nil {
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
}
|
||||
|
||||
response.OK(c, resp)
|
||||
c.JSON(http.StatusOK, h.serializeCsatSurveyResponse(c.Request.Context(), resp))
|
||||
}
|
||||
|
||||
// PublicGet retrieves a CSAT survey response by conversation UUID (no auth required).
|
||||
@@ -221,18 +231,36 @@ func buildCsatFilter(c *gin.Context) automation.CsatListFilter {
|
||||
if v := c.Query("agent_id"); v != "" {
|
||||
filter.AgentID = csatUintPtr(csatParseUint(v))
|
||||
}
|
||||
filter.AgentIDs = csatUintList(c.QueryArray("user_ids[]"))
|
||||
if len(filter.AgentIDs) == 0 {
|
||||
filter.AgentIDs = csatUintList(c.QueryArray("user_ids"))
|
||||
}
|
||||
if len(filter.AgentIDs) == 0 && c.Query("user_ids") != "" {
|
||||
filter.AgentIDs = csatUintCSV(c.Query("user_ids"))
|
||||
}
|
||||
if v := c.Query("inbox_id"); v != "" {
|
||||
filter.InboxID = csatUintPtr(csatParseUint(v))
|
||||
}
|
||||
if v := c.Query("team_id"); v != "" {
|
||||
filter.TeamID = csatUintPtr(csatParseUint(v))
|
||||
}
|
||||
if v := c.Query("rating"); v != "" {
|
||||
if n, err := strconv.Atoi(v); err == nil {
|
||||
filter.Rating = &n
|
||||
}
|
||||
}
|
||||
|
||||
// since filter (RFC3339 timestamp)
|
||||
// since filter (Chatwoot frontend sends Unix seconds; keep RFC3339 compatibility for local callers)
|
||||
if v := c.Query("since"); v != "" {
|
||||
t, err := time.Parse(time.RFC3339, v)
|
||||
t, err := parseCsatQueryTime(v)
|
||||
if err == nil {
|
||||
filter.Since = &t
|
||||
}
|
||||
}
|
||||
|
||||
// until filter (RFC3339 timestamp)
|
||||
// until filter (Chatwoot frontend sends Unix seconds; keep RFC3339 compatibility for local callers)
|
||||
if v := c.Query("until"); v != "" {
|
||||
t, err := time.Parse(time.RFC3339, v)
|
||||
t, err := parseCsatQueryTime(v)
|
||||
if err == nil {
|
||||
filter.Until = &t
|
||||
}
|
||||
@@ -276,6 +304,93 @@ func csatUintPtr(v uint) *uint {
|
||||
return &v
|
||||
}
|
||||
|
||||
func csatUintList(values []string) []uint {
|
||||
ids := make([]uint, 0, len(values))
|
||||
for _, value := range values {
|
||||
if value == "" {
|
||||
continue
|
||||
}
|
||||
ids = append(ids, csatUintCSV(value)...)
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func csatUintCSV(value string) []uint {
|
||||
parts := strings.Split(value, ",")
|
||||
ids := make([]uint, 0, len(parts))
|
||||
for _, part := range parts {
|
||||
part = strings.TrimSpace(part)
|
||||
if part == "" {
|
||||
continue
|
||||
}
|
||||
if parsed, err := csatParseUintFull(part); err == nil && parsed > 0 {
|
||||
ids = append(ids, parsed)
|
||||
}
|
||||
}
|
||||
return ids
|
||||
}
|
||||
|
||||
func parseCsatQueryTime(value string) (time.Time, error) {
|
||||
if unix, err := strconv.ParseInt(value, 10, 64); err == nil {
|
||||
return time.Unix(unix, 0), nil
|
||||
}
|
||||
return time.Parse(time.RFC3339, value)
|
||||
}
|
||||
|
||||
func (h *CsatSurveyHandler) serializeCsatSurveyResponse(ctx context.Context, csat *automation.CsatSurveyResponse) map[string]any {
|
||||
if csat == nil {
|
||||
return nil
|
||||
}
|
||||
conversationID := uint(0)
|
||||
var conversation model.Conversation
|
||||
if err := h.svc.DB().WithContext(ctx).First(&conversation, csat.ConversationID).Error; err == nil {
|
||||
if conversation.DisplayID != nil && *conversation.DisplayID != 0 {
|
||||
conversationID = *conversation.DisplayID
|
||||
} else {
|
||||
conversationID = conversation.ID
|
||||
}
|
||||
}
|
||||
|
||||
payload := map[string]any{
|
||||
"id": csat.ID,
|
||||
"rating": csat.Rating,
|
||||
"feedback_message": csat.FeedbackMessage,
|
||||
"csat_review_notes": csat.CsatReviewNotes,
|
||||
"review_notes_updated_at": unixPtr(csat.ReviewNotesUpdatedAt),
|
||||
"account_id": csat.AccountID,
|
||||
"message_id": csat.MessageID,
|
||||
"conversation_id": conversationID,
|
||||
"created_at": csat.CreatedAt.Unix(),
|
||||
}
|
||||
|
||||
if csat.ReviewNotesUpdatedByID != nil && *csat.ReviewNotesUpdatedByID != 0 {
|
||||
var reviewer model.User
|
||||
if err := h.svc.DB().WithContext(ctx).First(&reviewer, *csat.ReviewNotesUpdatedByID).Error; err == nil {
|
||||
payload["review_notes_updated_by"] = map[string]any{"id": reviewer.ID, "name": reviewer.Name}
|
||||
}
|
||||
}
|
||||
if csat.ContactID != 0 {
|
||||
var contact model.Contact
|
||||
if err := h.svc.DB().WithContext(ctx).First(&contact, csat.ContactID).Error; err == nil {
|
||||
payload["contact"] = serializeContact(&contact)
|
||||
}
|
||||
}
|
||||
if csat.AssignedAgentID != nil && *csat.AssignedAgentID != 0 {
|
||||
var agent model.User
|
||||
if err := h.svc.DB().WithContext(ctx).First(&agent, *csat.AssignedAgentID).Error; err == nil {
|
||||
payload["assigned_agent"] = serializeUser(&agent, csat.AccountID)
|
||||
}
|
||||
}
|
||||
return payload
|
||||
}
|
||||
|
||||
func unixPtr(value *time.Time) any {
|
||||
if value == nil {
|
||||
return nil
|
||||
}
|
||||
return value.Unix()
|
||||
}
|
||||
|
||||
// Download exports CSAT survey responses as CSV.
|
||||
// GET /api/v1/accounts/:account_id/csat_survey_responses/download
|
||||
// Reference: Chatwoot csat_survey_responses_controller#download — CSV format matches Chatwoot template
|
||||
|
||||
@@ -43,6 +43,8 @@ func (s *CsatSurveyHandlerTestSuite) SetupSuite() {
|
||||
s.Require().NoError(err)
|
||||
s.Require().NoError(db.AutoMigrate(
|
||||
&model.Account{},
|
||||
&model.User{},
|
||||
&model.AccountUser{},
|
||||
&model.Inbox{},
|
||||
&model.Contact{},
|
||||
&model.Conversation{},
|
||||
@@ -65,6 +67,8 @@ func (s *CsatSurveyHandlerTestSuite) SetupTest() {
|
||||
s.db.Exec("DELETE FROM conversations")
|
||||
s.db.Exec("DELETE FROM contacts")
|
||||
s.db.Exec("DELETE FROM inboxes")
|
||||
s.db.Exec("DELETE FROM account_users")
|
||||
s.db.Exec("DELETE FROM users")
|
||||
}
|
||||
|
||||
func (s *CsatSurveyHandlerTestSuite) TearDownSuite() {
|
||||
@@ -100,13 +104,78 @@ func (s *CsatSurveyHandlerTestSuite) TestMetrics_Success() {
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
func (s *CsatSurveyHandlerTestSuite) TestList_ChatwootPayloadAndFilters() {
|
||||
createdAt := time.Now().Add(-2 * time.Hour).Truncate(time.Second)
|
||||
agent, reviewer, contact, conversation, message := s.seedAccountCsatResponseGraph(createdAt, 5)
|
||||
otherContact := &model.Contact{AccountID: s.account.ID, Name: "Other Contact"}
|
||||
s.Require().NoError(s.db.Create(otherContact).Error)
|
||||
otherInbox := &model.Inbox{AccountID: s.account.ID, Name: "Other Inbox", ChannelType: "web_widget", Enabled: true}
|
||||
s.Require().NoError(s.db.Create(otherInbox).Error)
|
||||
otherConversation := &model.Conversation{AccountID: s.account.ID, InboxID: otherInbox.ID, ContactID: otherContact.ID, Status: "resolved", ChannelType: "web_widget", Channel: "web_widget"}
|
||||
s.Require().NoError(s.db.Create(otherConversation).Error)
|
||||
s.Require().NoError(s.db.Create(&automation.CsatSurveyResponse{AccountID: s.account.ID, ConversationID: otherConversation.ID, ContactID: otherContact.ID, Rating: 3}).Error)
|
||||
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/accounts/:account_id/csat_survey_responses", s.handler.List)
|
||||
|
||||
teamID := *conversation.TeamID
|
||||
url := fmt.Sprintf("/api/v1/accounts/%d/csat_survey_responses?since=%d&until=%d&user_ids=%d&inbox_id=%d&team_id=%d&rating=5",
|
||||
s.account.ID, createdAt.Add(-time.Hour).Unix(), createdAt.Add(time.Hour).Unix(), agent.ID, conversation.InboxID, teamID)
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", url, nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var payload []map[string]any
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &payload))
|
||||
s.Require().Len(payload, 1)
|
||||
item := payload[0]
|
||||
assert.Equal(s.T(), float64(5), item["rating"])
|
||||
assert.Equal(s.T(), "Great", item["feedback_message"])
|
||||
assert.Equal(s.T(), "Needs follow up", item["csat_review_notes"])
|
||||
assert.Equal(s.T(), float64(42), item["conversation_id"])
|
||||
assert.Equal(s.T(), float64(message.ID), item["message_id"])
|
||||
assert.Equal(s.T(), float64(createdAt.Unix()), item["created_at"])
|
||||
|
||||
contactPayload := item["contact"].(map[string]any)
|
||||
assert.Equal(s.T(), contact.Name, contactPayload["name"])
|
||||
assert.Equal(s.T(), contact.Email, contactPayload["email"])
|
||||
agentPayload := item["assigned_agent"].(map[string]any)
|
||||
assert.Equal(s.T(), agent.Email, agentPayload["email"])
|
||||
reviewerPayload := item["review_notes_updated_by"].(map[string]any)
|
||||
assert.Equal(s.T(), reviewer.Name, reviewerPayload["name"])
|
||||
}
|
||||
|
||||
func (s *CsatSurveyHandlerTestSuite) TestMetrics_ChatwootPayloadAndFilters() {
|
||||
createdAt := time.Now().Add(-2 * time.Hour).Truncate(time.Second)
|
||||
agent, _, _, conversation, _ := s.seedAccountCsatResponseGraph(createdAt, 5)
|
||||
|
||||
r := gin.New()
|
||||
r.GET("/api/v1/accounts/:account_id/csat_survey_responses/metrics", s.handler.Metrics)
|
||||
|
||||
url := fmt.Sprintf("/api/v1/accounts/%d/csat_survey_responses/metrics?since=%d&until=%d&user_ids=%d&inbox_id=%d&rating=5",
|
||||
s.account.ID, createdAt.Add(-time.Hour).Unix(), createdAt.Add(time.Hour).Unix(), agent.ID, conversation.InboxID)
|
||||
w := httptest.NewRecorder()
|
||||
req, _ := http.NewRequest("GET", url, nil)
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var payload map[string]any
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &payload))
|
||||
assert.Equal(s.T(), float64(1), payload["total_count"])
|
||||
assert.Equal(s.T(), float64(1), payload["total_sent_messages_count"])
|
||||
ratings := payload["ratings_count"].(map[string]any)
|
||||
assert.Equal(s.T(), float64(1), ratings["5"])
|
||||
}
|
||||
|
||||
func (s *CsatSurveyHandlerTestSuite) TestUpdateReviewNotes_Success() {
|
||||
survey := &automation.CsatSurveyResponse{AccountID: s.account.ID, ConversationID: 1, Rating: 5}
|
||||
s.Require().NoError(s.db.Create(survey).Error)
|
||||
_, reviewer, _, _, _ := s.seedAccountCsatResponseGraph(time.Now().Add(-time.Hour), 5)
|
||||
var survey automation.CsatSurveyResponse
|
||||
s.Require().NoError(s.db.First(&survey).Error)
|
||||
|
||||
r := gin.New()
|
||||
r.PUT("/api/v1/accounts/:account_id/csats/:id/review_notes", func(c *gin.Context) {
|
||||
c.Set("user_id", float64(1))
|
||||
c.Set("user_id", float64(reviewer.ID))
|
||||
s.handler.UpdateReviewNotes(c)
|
||||
})
|
||||
|
||||
@@ -117,6 +186,11 @@ func (s *CsatSurveyHandlerTestSuite) TestUpdateReviewNotes_Success() {
|
||||
r.ServeHTTP(w, req)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var payload map[string]any
|
||||
s.Require().NoError(json.Unmarshal(w.Body.Bytes(), &payload))
|
||||
assert.Equal(s.T(), "good conversation", payload["csat_review_notes"])
|
||||
reviewerPayload := payload["review_notes_updated_by"].(map[string]any)
|
||||
assert.Equal(s.T(), reviewer.Name, reviewerPayload["name"])
|
||||
}
|
||||
|
||||
func (s *CsatSurveyHandlerTestSuite) TestUpdate_Success() {
|
||||
@@ -249,3 +323,52 @@ func (s *CsatSurveyHandlerTestSuite) seedPublicCsatSurvey(createdAt time.Time) (
|
||||
s.Require().NoError(s.db.First(message, message.ID).Error)
|
||||
return conversation, message
|
||||
}
|
||||
|
||||
func (s *CsatSurveyHandlerTestSuite) seedAccountCsatResponseGraph(createdAt time.Time, rating int) (*model.User, *model.User, *model.Contact, *model.Conversation, *model.Message) {
|
||||
agent := &model.User{AccountID: s.account.ID, Name: "CSAT Agent", Email: "csat-agent@example.com", Role: "agent", Active: true}
|
||||
reviewer := &model.User{AccountID: s.account.ID, Name: "CSAT Reviewer", Email: "csat-reviewer@example.com", Role: "administrator", Active: true}
|
||||
s.Require().NoError(s.db.Create(agent).Error)
|
||||
s.Require().NoError(s.db.Create(reviewer).Error)
|
||||
s.Require().NoError(s.db.Create(&model.AccountUser{AccountID: s.account.ID, UserID: agent.ID, Role: "agent"}).Error)
|
||||
s.Require().NoError(s.db.Create(&model.AccountUser{AccountID: s.account.ID, UserID: reviewer.ID, Role: "administrator"}).Error)
|
||||
|
||||
contact := &model.Contact{AccountID: s.account.ID, Name: "CSAT Contact", Email: "csat@example.com", PhoneNumber: "+15550000"}
|
||||
s.Require().NoError(s.db.Create(contact).Error)
|
||||
inbox := &model.Inbox{AccountID: s.account.ID, Name: "CSAT Inbox", ChannelType: "web_widget", Enabled: true}
|
||||
s.Require().NoError(s.db.Create(inbox).Error)
|
||||
displayID := uint(42)
|
||||
teamID := uint(7)
|
||||
conversation := &model.Conversation{
|
||||
AccountID: s.account.ID,
|
||||
DisplayID: &displayID,
|
||||
InboxID: inbox.ID,
|
||||
ContactID: contact.ID,
|
||||
AssigneeID: &agent.ID,
|
||||
TeamID: &teamID,
|
||||
Status: "resolved",
|
||||
ChannelType: "web_widget",
|
||||
Channel: "web_widget",
|
||||
}
|
||||
s.Require().NoError(s.db.Create(conversation).Error)
|
||||
message := &model.Message{ConversationID: conversation.ID, AccountID: s.account.ID, InboxID: inbox.ID, ContentType: "input_csat", MessageType: "outgoing", Content: "Rate this"}
|
||||
s.Require().NoError(s.db.Create(message).Error)
|
||||
s.Require().NoError(s.db.Model(message).Updates(map[string]any{"created_at": createdAt, "updated_at": createdAt}).Error)
|
||||
s.Require().NoError(s.db.First(message, message.ID).Error)
|
||||
reviewerID := reviewer.ID
|
||||
updatedAt := createdAt.Add(30 * time.Minute)
|
||||
response := &automation.CsatSurveyResponse{
|
||||
AccountID: s.account.ID,
|
||||
ConversationID: conversation.ID,
|
||||
ContactID: contact.ID,
|
||||
MessageID: &message.ID,
|
||||
AssignedAgentID: &agent.ID,
|
||||
Rating: rating,
|
||||
FeedbackMessage: "Great",
|
||||
CsatReviewNotes: "Needs follow up",
|
||||
ReviewNotesUpdatedByID: &reviewerID,
|
||||
ReviewNotesUpdatedAt: &updatedAt,
|
||||
}
|
||||
s.Require().NoError(s.db.Create(response).Error)
|
||||
s.Require().NoError(s.db.Model(response).Updates(map[string]any{"created_at": createdAt, "updated_at": createdAt}).Error)
|
||||
return agent, reviewer, contact, conversation, message
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user