feat(agents): align update validation errors
This commit is contained in:
@@ -149,6 +149,13 @@ func (h *AgentHandler) Update(c *gin.Context) {
|
||||
|
||||
agent, svcErr := h.svc.Update(c.Request.Context(), uint(id), accountID, req)
|
||||
if svcErr != nil {
|
||||
if errors.Is(svcErr, service.ErrAgentNameBlank) {
|
||||
c.JSON(http.StatusUnprocessableEntity, gin.H{
|
||||
"message": "Name can't be blank",
|
||||
"attributes": []string{"name"},
|
||||
})
|
||||
return
|
||||
}
|
||||
applogger.L().Errorf("Update agent %d for account %d: %v", id, accountID, svcErr)
|
||||
handleServiceError(c, svcErr)
|
||||
return
|
||||
|
||||
@@ -293,6 +293,39 @@ func (s *AgentHandlerTestSuite) TestUpdateAgent() {
|
||||
assert.Equal(s.T(), false, disableData["auto_offline"])
|
||||
}
|
||||
|
||||
func (s *AgentHandlerTestSuite) TestUpdateAgentBlankNameReturnsRecordInvalidShape() {
|
||||
req := service.CreateAgentRequest{
|
||||
Email: "blank-update@test.com",
|
||||
Name: "Blank Update",
|
||||
Role: "agent",
|
||||
}
|
||||
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)
|
||||
|
||||
var created map[string]interface{}
|
||||
json.Unmarshal(w.Body.Bytes(), &created)
|
||||
agentID := uint(created["id"].(float64))
|
||||
|
||||
updateReq := map[string]any{
|
||||
"agent": map[string]any{
|
||||
"name": "",
|
||||
},
|
||||
}
|
||||
w2, c2 := s.makeRequest("PATCH", fmt.Sprintf("/api/v1/accounts/1/agents/%d", agentID), updateReq, s.account.ID, s.user.ID)
|
||||
s.handler.Update(c2)
|
||||
|
||||
assert.Equal(s.T(), http.StatusUnprocessableEntity, w2.Code, w2.Body.String())
|
||||
var data map[string]interface{}
|
||||
json.Unmarshal(w2.Body.Bytes(), &data)
|
||||
assert.Equal(s.T(), "Name can't be blank", data["message"])
|
||||
assert.Equal(s.T(), []interface{}{"name"}, data["attributes"])
|
||||
|
||||
var user model.User
|
||||
s.Require().NoError(s.db.First(&user, agentID).Error)
|
||||
assert.Equal(s.T(), "Blank Update", user.Name)
|
||||
}
|
||||
|
||||
func (s *AgentHandlerTestSuite) TestDeleteAgent() {
|
||||
// Create an agent
|
||||
req := service.CreateAgentRequest{
|
||||
|
||||
@@ -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