package adminkindboxvalidator import ( "context" "fmt" "git.gocasts.ir/ebhomengo/niki/entity" agentparam "git.gocasts.ir/ebhomengo/niki/param/admin/agent" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" validation "github.com/go-ozzo/ozzo-validation/v4" ) type Repository interface { KindBoxExist(ctx context.Context, kindBoxID uint) (bool, error) GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error) } type AgentSvc interface { AgentExistByID(ctx context.Context, req agentparam.AdminAgentExistByIDRequest) (agentparam.AdminAgentExistByIDResponse, error) } type Validator struct { repo Repository AgentSvc AgentSvc } func New(repo Repository, adminSvc AgentSvc) Validator { return Validator{repo: repo, AgentSvc: adminSvc} } func (v Validator) doesKindBoxExist(ctx context.Context) validation.RuleFunc { return func(value interface{}) error { kindBoxID, ok := value.(uint) if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } exists, err := v.repo.KindBoxExist(ctx, kindBoxID) if err != nil { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } if !exists { return fmt.Errorf(errmsg.ErrorMsgNotFound) } return nil } } func (v Validator) checkKindBoxStatusForAssigningReceiverAgent(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.KindBoxReadyToReturnStatus { return fmt.Errorf(errmsg.ErrorMsgAssignReceiverAgentKindBoxStatus) } return nil } } func (v Validator) doesAgentExist(ctx context.Context) validation.RuleFunc { return func(value interface{}) error { agentID, ok := value.(uint) if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } resp, err := v.AgentSvc.AgentExistByID(ctx, agentparam.AdminAgentExistByIDRequest{AgentID: agentID}) if err != nil { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } if !resp.Exist { return fmt.Errorf(errmsg.ErrorMsgNotFound) } 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 } }