niki/validator/admin/kind_box_req/validator.go

94 lines
2.1 KiB
Go
Raw Normal View History

package adminkindboxreqvalidator
import (
"context"
"errors"
"fmt"
2024-01-25 17:59:18 +00:00
"git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
)
const (
MinKindBoxReq = 1
MaxKindBoxReq = 100
)
type Repository interface {
KindBoxRequestExist(id uint) (bool, error)
GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error)
}
type Validator struct {
repo Repository
}
func New(repo Repository) Validator {
return Validator{repo: repo}
}
func (v Validator) doesKindBoxRequestExist(value interface{}) error {
kindboxreqID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
if isExist, err := v.repo.KindBoxRequestExist(kindboxreqID); !isExist || err != nil {
if err != nil {
return err
}
if !isExist {
return errors.New("kind box request is not exist")
}
}
return nil
}
2024-01-22 15:21:13 +00:00
func (v Validator) CheckKindBoxReqStatusForAccepting(value interface{}) error {
kindboxreqID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
kindBox, err := v.repo.GetByID(context.Background(), kindboxreqID)
if err != nil {
return err
}
if kindBox.Status != entity.KindBoxReqPendingStatus {
return fmt.Errorf(errmsg.ErrorMsgAcceptKindBoxReqStatus)
}
return nil
}
2024-01-22 15:21:13 +00:00
func (v Validator) CheckKindBoxReqStatusForRejecting(value interface{}) error {
kindboxreqID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
kindBox, err := v.repo.GetByID(context.Background(), kindboxreqID)
2024-01-22 15:21:13 +00:00
if err != nil {
return err
}
if kindBox.Status != entity.KindBoxReqPendingStatus {
return fmt.Errorf(errmsg.ErrorMsgRejectKindBoxReqStatus)
}
2024-01-22 15:21:13 +00:00
return nil
}
func (v Validator) checkKindBoxReqStatusForDelivering(value interface{}) error {
kindboxreqID, ok := value.(uint)
if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
}
kindBoxReq, err := v.repo.GetByID(context.Background(), kindboxreqID)
if err != nil {
return err
}
if kindBoxReq.Status != entity.KindBoxReqAssignedSenderAgentStatus {
return fmt.Errorf(errmsg.ErrorMsgDeliverKindBoxReqStatus)
}
return nil
}