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:59:18 +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 14:07:51 +00:00
|
|
|
KindBoxRequestExist(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) 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
|
|
|
|
}
|