forked from ebhomengo/niki
1
0
Fork 0
niki/validator/admin/kind_box/validator.go

84 lines
2.2 KiB
Go
Raw Normal View History

package adminkindboxvalidator
2024-07-01 17:13:28 +00:00
import (
"context"
"fmt"
"git.gocasts.ir/ebhomengo/niki/entity"
agentparam "git.gocasts.ir/ebhomengo/niki/param/admin/agent"
2024-07-01 17:13:28 +00:00
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4"
2024-07-01 17:13:28 +00:00
)
type Repository interface {
2024-07-01 17:13:28 +00:00
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}
}
2024-07-01 17:13:28 +00:00
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)
}
2024-07-01 17:13:28 +00:00
return nil
}
2024-07-01 17:13:28 +00:00
}
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
}
}