diff --git a/pkg/err_msg/message.go b/pkg/err_msg/message.go index 0968013..8ec7e87 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -35,5 +35,6 @@ const ( ErrorMsgSortDirectionShouldBeAscOrDesc = "sort direction should be asc or desc" ErrorMsgSortFieldIsNotValid = "sort field is not valid" ErrorMsgAssignReceiverAgentKindBoxStatus = "only ready to return kindboxes can be assigned to a receiver agent" + ErrorMsgReturnKindBoxStatus = "only returned kindboxes can be enumerated" ErrorMsgInvalidSerialNumberRange = "invalid serial number range" ) diff --git a/validator/admin/kind_box/enumerate.go b/validator/admin/kind_box/enumerate.go index 8906d95..4a738a2 100644 --- a/validator/admin/kind_box/enumerate.go +++ b/validator/admin/kind_box/enumerate.go @@ -14,11 +14,12 @@ func (v Validator) ValidateEnumerate(ctx context.Context, req param.EnumerateKin const op = "adminkindboxvalidator.ValidateEnumerate" if err := validation.ValidateStruct(&req, - validation.Field(&req.KindBoxID, validation.Required, - validation.By(v.doesKindBoxExist(ctx))), + validation.By(v.doesKindBoxExist(ctx)), + validation.By(v.CheckKindBoxStatusForEnumeration(ctx))), validation.Field(&req.Amount, validation.Required), + ); err != nil { fieldErrors := make(map[string]string) diff --git a/validator/admin/kind_box/validator.go b/validator/admin/kind_box/validator.go index 3f360ed..25405cd 100644 --- a/validator/admin/kind_box/validator.go +++ b/validator/admin/kind_box/validator.go @@ -81,3 +81,21 @@ func (v Validator) doesAgentExist(ctx context.Context) validation.RuleFunc { return nil } } + +func (v Validator) CheckKindBoxStatusForEnumeration(ctx context.Context) validation.RuleFunc { + return func(value interface{}) error { + kindBoxID, ok := value.(uint) + if !ok { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + kindBox, err := v.repo.GetKindBox(ctx, kindBoxID) + if err != nil { + return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) + } + if kindBox.Status != entity.KindBoxReturnedStatus { + return fmt.Errorf(errmsg.ErrorMsgReturnKindBoxStatus) + } + + return nil + } +} \ No newline at end of file