2023-12-30 19:51:22 +00:00
|
|
|
package adminkindboxreqvalidator
|
2023-12-27 21:55:15 +00:00
|
|
|
|
2024-01-22 14:07:51 +00:00
|
|
|
import (
|
2024-01-25 17:47:10 +00:00
|
|
|
"context"
|
2024-01-22 14:07:51 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2024-01-25 17:47:10 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
2024-01-22 14:07:51 +00:00
|
|
|
|
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
|
|
|
)
|
|
|
|
|
2023-12-27 21:55:15 +00:00
|
|
|
const (
|
|
|
|
MinKindBoxReq = 1
|
|
|
|
MaxKindBoxReq = 100
|
|
|
|
)
|
|
|
|
|
|
|
|
type Repository interface {
|
2024-01-22 08:14:57 +00:00
|
|
|
// BenefactorExist(id int) (bool, error)
|
2024-01-22 14:07:51 +00:00
|
|
|
KindBoxRequestExist(id uint) (bool, error)
|
2024-01-22 08:14:57 +00:00
|
|
|
// TypeExist(id int) (bool, error)
|
|
|
|
// KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error)
|
|
|
|
// PendingStatus(id uint) (bool, error)
|
2024-01-25 17:47:10 +00:00
|
|
|
GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error)
|
2023-12-27 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Validator struct {
|
|
|
|
repo Repository
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(repo Repository) Validator {
|
|
|
|
return Validator{repo: repo}
|
|
|
|
}
|
|
|
|
|
2024-01-22 14:07:51 +00:00
|
|
|
// func (v Validator) doesBenefactorExist(value interface{}) error {
|
|
|
|
// benefactorID, ok := value.(int)
|
2024-01-22 08:14:57 +00:00
|
|
|
// if !ok {
|
2024-01-22 14:07:51 +00:00
|
|
|
// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
2024-01-22 08:14:57 +00:00
|
|
|
// }
|
2024-01-22 14:07:51 +00:00
|
|
|
// _, err := v.repo.BenefactorExist(benefactorID)
|
2024-01-22 08:14:57 +00:00
|
|
|
// if err != nil {
|
|
|
|
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
//
|
2024-01-22 14:07:51 +00:00
|
|
|
// func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
|
|
|
|
// return func(value interface{}) error {
|
|
|
|
// kbID, ok := value.(uint)
|
|
|
|
// if !ok {
|
|
|
|
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
// }
|
|
|
|
// _, err := v.repo.KindBoxBelongToBenefactor(benefactorID, kbID)
|
|
|
|
// if err != nil {
|
|
|
|
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
// }
|
2024-01-22 08:14:57 +00:00
|
|
|
//
|
2024-01-22 14:07:51 +00:00
|
|
|
// return nil
|
|
|
|
// }
|
|
|
|
// }
|
|
|
|
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 {
|
2024-01-22 14:07:51 +00:00
|
|
|
kindboxreqID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-01-25 17:47:10 +00:00
|
|
|
kindBox, err := v.repo.GetByID(context.Background(), kindboxreqID)
|
2024-01-22 14:07:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-25 17:47:10 +00:00
|
|
|
if kindBox.Status != entity.KindBoxReqPendingStatus {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgAcceptKindBoxReqStatus)
|
|
|
|
}
|
2024-01-22 14:07:51 +00:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2024-01-25 17:47:10 +00:00
|
|
|
kindBox, err := v.repo.GetByID(context.Background(), kindboxreqID)
|
2024-01-22 15:21:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-25 17:47:10 +00:00
|
|
|
if kindBox.Status != entity.KindBoxReqPendingStatus {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgRejectKindBoxReqStatus)
|
|
|
|
}
|
2024-01-22 15:21:13 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-01-22 08:14:57 +00:00
|
|
|
//
|
|
|
|
// func (v Validator) doesTypeExist(value interface{}) error {
|
|
|
|
// typeID, ok := value.(int)
|
|
|
|
// if !ok {
|
|
|
|
// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
// }
|
|
|
|
// _, err := v.repo.TypeExist(typeID)
|
|
|
|
// if err != nil {
|
|
|
|
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return nil
|
|
|
|
//}
|
|
|
|
//
|
2024-01-22 14:07:51 +00:00
|
|
|
// func (v Validator) hasPendingStatus(value interface{}) error {
|
2024-01-22 08:14:57 +00:00
|
|
|
// kindboxID, ok := value.(uint)
|
|
|
|
// if !ok {
|
|
|
|
// return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
// }
|
|
|
|
// _, err := v.repo.PendingStatus(kindboxID)
|
|
|
|
// if err != nil {
|
|
|
|
// return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
// }
|
|
|
|
//
|
|
|
|
// return nil
|
|
|
|
//}
|