forked from ebhomengo/niki
1
0
Fork 0

fix(validation): Add validation for KindBox status in enumeration by admin

This commit is contained in:
Fatemeh Javadi 2024-08-24 21:43:29 +03:30
parent 013fd8cf5c
commit d1ebfbb525
3 changed files with 22 additions and 2 deletions

View File

@ -35,5 +35,6 @@ const (
ErrorMsgSortDirectionShouldBeAscOrDesc = "sort direction should be asc or desc" ErrorMsgSortDirectionShouldBeAscOrDesc = "sort direction should be asc or desc"
ErrorMsgSortFieldIsNotValid = "sort field is not valid" ErrorMsgSortFieldIsNotValid = "sort field is not valid"
ErrorMsgAssignReceiverAgentKindBoxStatus = "only ready to return kindboxes can be assigned to a receiver agent" 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" ErrorMsgInvalidSerialNumberRange = "invalid serial number range"
) )

View File

@ -14,11 +14,12 @@ func (v Validator) ValidateEnumerate(ctx context.Context, req param.EnumerateKin
const op = "adminkindboxvalidator.ValidateEnumerate" const op = "adminkindboxvalidator.ValidateEnumerate"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.KindBoxID, validation.Required, 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), validation.Field(&req.Amount, validation.Required),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -81,3 +81,21 @@ func (v Validator) doesAgentExist(ctx context.Context) validation.RuleFunc {
return nil 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
}
}