feat(agents): align create name defaults
This commit is contained in:
@@ -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