feat(agents): align create name defaults
This commit is contained in:
@@ -158,6 +158,27 @@ func (s *AgentHandlerTestSuite) TestCreateAgent() {
|
||||
assert.Contains(s.T(), data, "confirmed")
|
||||
}
|
||||
|
||||
func (s *AgentHandlerTestSuite) TestCreateAgentDefaultsBlankNameFromEmail() {
|
||||
req := map[string]interface{}{
|
||||
"agent": map[string]interface{}{
|
||||
"email": "fallback-name@test.com",
|
||||
"name": "",
|
||||
},
|
||||
}
|
||||
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.Body.String())
|
||||
var data map[string]interface{}
|
||||
json.Unmarshal(w.Body.Bytes(), &data)
|
||||
assert.Equal(s.T(), "fallback-name@test.com", data["email"])
|
||||
assert.Equal(s.T(), "fallback-name", data["name"])
|
||||
|
||||
var user model.User
|
||||
s.Require().NoError(s.db.Where("email = ?", "fallback-name@test.com").First(&user).Error)
|
||||
assert.Equal(s.T(), "fallback-name", user.Name)
|
||||
}
|
||||
|
||||
func (s *AgentHandlerTestSuite) TestCreateAgentDuplicate() {
|
||||
// Create first agent
|
||||
req := service.CreateAgentRequest{
|
||||
|
||||
@@ -142,6 +142,12 @@ func (r *AgentRepo) CreateAgent(ctx context.Context, accountID uint, inviterID u
|
||||
err := r.db.WithContext(ctx).Where("email = ?", email).First(&user).Error
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
// Create new user
|
||||
if name == "" {
|
||||
name = email
|
||||
if atIdx := indexOfAt(email); atIdx > 0 {
|
||||
name = email[:atIdx]
|
||||
}
|
||||
}
|
||||
user = model.User{
|
||||
Name: name,
|
||||
Email: email,
|
||||
|
||||
@@ -39,7 +39,7 @@ func (s *AgentService) DB() *gorm.DB {
|
||||
// Reference: Chatwoot agents_controller.rb#create → new_agent_params (email, name, role, availability, auto_offline)
|
||||
type CreateAgentRequest struct {
|
||||
Email string `json:"email" validate:"required,email"`
|
||||
Name string `json:"name" validate:"required,min=1"`
|
||||
Name string `json:"name" validate:"omitempty,min=1"`
|
||||
Role string `json:"role" validate:"omitempty,oneof=agent administrator"`
|
||||
Availability string `json:"availability" validate:"omitempty,oneof=online offline busy"`
|
||||
AutoOffline bool `json:"auto_offline"`
|
||||
@@ -108,12 +108,16 @@ func (s *AgentService) Create(ctx context.Context, accountID uint, inviterID uin
|
||||
if availability == "" {
|
||||
availability = "offline"
|
||||
}
|
||||
name := strings.TrimSpace(req.Name)
|
||||
if name == "" {
|
||||
name = agentNameFromEmail(req.Email)
|
||||
}
|
||||
|
||||
customRoleID := uint(0)
|
||||
if req.CustomRoleID != nil {
|
||||
customRoleID = *req.CustomRoleID
|
||||
}
|
||||
detail, err := s.agentRepo.CreateAgent(ctx, accountID, inviterID, req.Name, req.Email, role, availability, req.AutoOffline, customRoleID)
|
||||
detail, err := s.agentRepo.CreateAgent(ctx, accountID, inviterID, name, req.Email, role, availability, req.AutoOffline, customRoleID)
|
||||
if err != nil {
|
||||
if errors.Is(err, repository.ErrAlreadyMember) {
|
||||
return nil, repository.ErrAlreadyMember
|
||||
@@ -171,6 +175,14 @@ func isValidBulkAgentEmail(email string) bool {
|
||||
return err == nil && addr.Address == email
|
||||
}
|
||||
|
||||
func agentNameFromEmail(email string) string {
|
||||
email = strings.TrimSpace(email)
|
||||
if at := strings.Index(email, "@"); at > 0 {
|
||||
return email[:at]
|
||||
}
|
||||
return email
|
||||
}
|
||||
|
||||
// AvailableAgentCount returns the number of additional agents that can be added to an account.
|
||||
// Reference: Chatwoot agents_controller.rb#available_agent_count — usage_limits[:agents] - agents.count
|
||||
// Returns -1 if no limit is set (0 = unlimited in our convention).
|
||||
|
||||
Reference in New Issue
Block a user