feat(agents): align list ordering
This commit is contained in:
@@ -113,6 +113,30 @@ func (s *AgentHandlerTestSuite) TestListEmpty() {
|
||||
assert.Equal(s.T(), 0, len(data))
|
||||
}
|
||||
|
||||
func (s *AgentHandlerTestSuite) TestListOrdersByFullName() {
|
||||
agents := []service.CreateAgentRequest{
|
||||
{Email: "charlie@test.com", Name: "charlie", Role: "agent"},
|
||||
{Email: "alpha@test.com", Name: "Alpha", Role: "agent"},
|
||||
{Email: "bravo@test.com", Name: "bravo", Role: "agent"},
|
||||
}
|
||||
for _, req := range agents {
|
||||
w, c := s.makeRequest("POST", "/api/v1/accounts/1/agents", req, s.account.ID, s.user.ID)
|
||||
s.handler.Create(c)
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
}
|
||||
|
||||
w, c := s.makeRequest("GET", "/api/v1/accounts/1/agents", nil, s.account.ID, s.user.ID)
|
||||
s.handler.List(c)
|
||||
|
||||
assert.Equal(s.T(), http.StatusOK, w.Code)
|
||||
var data []map[string]interface{}
|
||||
json.Unmarshal(w.Body.Bytes(), &data)
|
||||
s.Require().Len(data, 3)
|
||||
assert.Equal(s.T(), "Alpha", data[0]["name"])
|
||||
assert.Equal(s.T(), "bravo", data[1]["name"])
|
||||
assert.Equal(s.T(), "charlie", data[2]["name"])
|
||||
}
|
||||
|
||||
func (s *AgentHandlerTestSuite) TestCreateAgent() {
|
||||
customRoleID := uint(7)
|
||||
req := service.CreateAgentRequest{
|
||||
|
||||
@@ -45,15 +45,18 @@ func (r *AgentRepo) ListByAccount(ctx context.Context, accountID uint, offset, l
|
||||
var count int64
|
||||
if err := r.db.WithContext(ctx).
|
||||
Model(&model.AccountUser{}).
|
||||
Where("account_id = ?", accountID).
|
||||
Where("account_users.account_id = ?", accountID).
|
||||
Count(&count).Error; err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
var accountUsers []model.AccountUser
|
||||
if err := r.db.WithContext(ctx).
|
||||
Where("account_id = ?", accountID).
|
||||
Order("id ASC").
|
||||
Model(&model.AccountUser{}).
|
||||
Joins("JOIN users ON users.id = account_users.user_id").
|
||||
Where("account_users.account_id = ?", accountID).
|
||||
Order("lower(users.name) ASC").
|
||||
Order("users.id ASC").
|
||||
Offset(offset).Limit(limit).
|
||||
Find(&accountUsers).Error; err != nil {
|
||||
return nil, 0, err
|
||||
@@ -65,10 +68,8 @@ func (r *AgentRepo) ListByAccount(ctx context.Context, accountID uint, offset, l
|
||||
|
||||
// Collect user IDs
|
||||
userIDs := make([]uint, len(accountUsers))
|
||||
auMap := make(map[uint]model.AccountUser) // userID → AccountUser
|
||||
for i, au := range accountUsers {
|
||||
userIDs[i] = au.UserID
|
||||
auMap[au.UserID] = au
|
||||
}
|
||||
|
||||
// Fetch users
|
||||
@@ -79,10 +80,15 @@ func (r *AgentRepo) ListByAccount(ctx context.Context, accountID uint, offset, l
|
||||
return nil, 0, err
|
||||
}
|
||||
|
||||
// Build AgentDetail list
|
||||
details := make([]AgentDetail, 0, len(users))
|
||||
userMap := make(map[uint]model.User, len(users))
|
||||
for _, u := range users {
|
||||
au, ok := auMap[u.ID]
|
||||
userMap[u.ID] = u
|
||||
}
|
||||
|
||||
// Build AgentDetail list in Chatwoot's order_by_full_name order.
|
||||
details := make([]AgentDetail, 0, len(accountUsers))
|
||||
for _, au := range accountUsers {
|
||||
u, ok := userMap[au.UserID]
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user