feat(agents): align update validation errors
This commit is contained in:
@@ -23,6 +23,8 @@ type AgentService struct {
|
||||
db *gorm.DB
|
||||
}
|
||||
|
||||
var ErrAgentNameBlank = errors.New("agent name cannot be blank")
|
||||
|
||||
// NewAgentService creates a new Agent service.
|
||||
func NewAgentService(agentRepo *repository.AgentRepo, db *gorm.DB) *AgentService {
|
||||
return &AgentService{agentRepo: agentRepo, db: db}
|
||||
@@ -54,6 +56,7 @@ type UpdateAgentRequest struct {
|
||||
Availability string `json:"availability,omitempty" validate:"omitempty,oneof=online offline busy"`
|
||||
AutoOffline bool `json:"auto_offline"`
|
||||
CustomRoleID *uint `json:"custom_role_id,omitempty"`
|
||||
nameSet bool
|
||||
autoOfflineSet bool
|
||||
customRoleSet bool
|
||||
}
|
||||
@@ -69,11 +72,13 @@ func (r *UpdateAgentRequest) UnmarshalJSON(data []byte) error {
|
||||
return err
|
||||
}
|
||||
*r = UpdateAgentRequest(decoded)
|
||||
_, r.nameSet = raw["name"]
|
||||
_, r.autoOfflineSet = raw["auto_offline"]
|
||||
_, r.customRoleSet = raw["custom_role_id"]
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r UpdateAgentRequest) NameSet() bool { return r.nameSet }
|
||||
func (r UpdateAgentRequest) AutoOfflineSet() bool { return r.autoOfflineSet }
|
||||
func (r UpdateAgentRequest) CustomRoleIDSet() bool { return r.customRoleSet }
|
||||
|
||||
@@ -134,6 +139,9 @@ func (s *AgentService) Update(ctx context.Context, userID, accountID uint, req U
|
||||
if err := pkgvalidator.ValidateStruct(req); err != nil {
|
||||
return nil, fmt.Errorf("validation: %w", err)
|
||||
}
|
||||
if req.NameSet() && strings.TrimSpace(req.Name) == "" {
|
||||
return nil, ErrAgentNameBlank
|
||||
}
|
||||
|
||||
return s.agentRepo.UpdateAgent(ctx, userID, accountID, req.Name, req.Role, req.Availability, req.AutoOffline, req.AutoOfflineSet(), req.CustomRoleID, req.CustomRoleIDSet())
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user