Refactor ListChats response handling to support grouping
- Updated the ListChats function to build responses based on the grouping mode, ensuring that when `group_by` is set, the `data` field is omitted to prevent duplication. - Enhanced the response structure to conditionally include either `groups` or `data`, improving clarity and efficiency in chat session responses. - Revised tests to validate the new response behavior, ensuring correct handling of grouped and flat responses based on the grouping criteria.
This commit is contained in:
parent
6fef453d46
commit
77fbfc1651
2 changed files with 17 additions and 6 deletions
|
|
@ -49,15 +49,24 @@ func ListChats(c *gin.Context) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Return result
|
// Build response based on grouping mode
|
||||||
response.RespondWithSuccess(c, response.StatusOK, gin.H{
|
// When group_by is set, data should be nil to avoid duplication
|
||||||
"data": result.Data,
|
resp := gin.H{
|
||||||
"groups": result.Groups,
|
|
||||||
"page": result.Page,
|
"page": result.Page,
|
||||||
"pagesize": result.PageSize,
|
"pagesize": result.PageSize,
|
||||||
"pagecount": result.PageCount,
|
"pagecount": result.PageCount,
|
||||||
"total": result.Total,
|
"total": result.Total,
|
||||||
})
|
}
|
||||||
|
|
||||||
|
if len(result.Groups) > 0 {
|
||||||
|
// Grouped response: only include groups
|
||||||
|
resp["groups"] = result.Groups
|
||||||
|
} else {
|
||||||
|
// Flat response: only include data
|
||||||
|
resp["data"] = result.Data
|
||||||
|
}
|
||||||
|
|
||||||
|
response.RespondWithSuccess(c, response.StatusOK, resp)
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetChat retrieves a single chat session by ID
|
// GetChat retrieves a single chat session by ID
|
||||||
|
|
|
||||||
|
|
@ -300,9 +300,11 @@ func TestListChatSessions(t *testing.T) {
|
||||||
err = json.NewDecoder(resp.Body).Decode(&response)
|
err = json.NewDecoder(resp.Body).Decode(&response)
|
||||||
assert.NoError(t, err)
|
assert.NoError(t, err)
|
||||||
|
|
||||||
// Check for groups in response
|
// Check for groups in response (when group_by=time, only groups is returned, not data)
|
||||||
_, hasGroups := response["groups"]
|
_, hasGroups := response["groups"]
|
||||||
|
_, hasData := response["data"]
|
||||||
assert.True(t, hasGroups, "Response should contain groups when group_by=time")
|
assert.True(t, hasGroups, "Response should contain groups when group_by=time")
|
||||||
|
assert.False(t, hasData, "Response should NOT contain data when group_by=time (to avoid duplication)")
|
||||||
t.Logf("Successfully retrieved chat sessions with time grouping")
|
t.Logf("Successfully retrieved chat sessions with time grouping")
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue