2023-12-30 19:51:22 +00:00
|
|
|
package adminkindboxvalidator
|
2023-12-27 21:55:15 +00:00
|
|
|
|
2024-07-01 17:13:28 +00:00
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-07-09 17:17:04 +00:00
|
|
|
"slices"
|
2024-07-01 17:13:28 +00:00
|
|
|
|
2024-07-02 20:17:40 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
2024-07-09 17:17:04 +00:00
|
|
|
params "git.gocasts.ir/ebhomengo/niki/param"
|
2024-07-01 17:13:28 +00:00
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
2024-07-09 17:17:04 +00:00
|
|
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
2024-07-01 17:13:28 +00:00
|
|
|
)
|
|
|
|
|
2023-12-27 21:55:15 +00:00
|
|
|
type Repository interface {
|
2024-07-01 17:13:28 +00:00
|
|
|
KindBoxExist(ctx context.Context, kindBoxID uint) (bool, error)
|
2024-07-02 20:17:40 +00:00
|
|
|
GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type AdminSvc interface {
|
|
|
|
AgentExistByID(ctx context.Context, agentID uint) (bool, error)
|
2023-12-27 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Validator struct {
|
2024-07-02 20:17:40 +00:00
|
|
|
repo Repository
|
|
|
|
adminSvc AdminSvc
|
2023-12-27 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
2024-07-02 20:17:40 +00:00
|
|
|
func New(repo Repository, adminSvc AdminSvc) Validator {
|
|
|
|
return Validator{repo: repo, adminSvc: adminSvc}
|
2023-12-27 21:55:15 +00:00
|
|
|
}
|
2024-07-01 17:13:28 +00:00
|
|
|
|
|
|
|
func (v Validator) doesKindBoxExist(value interface{}) error {
|
|
|
|
kindBoxID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
exists, err := v.repo.KindBoxExist(context.Background(), kindBoxID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-02 20:17:40 +00:00
|
|
|
|
|
|
|
func (v Validator) checkKindBoxStatusForAssigningReceiverAgent(value interface{}) error {
|
|
|
|
kindboxID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
kindBox, err := v.repo.GetKindBox(context.Background(), 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(value interface{}) error {
|
|
|
|
agentID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
exists, err := v.adminSvc.AgentExistByID(context.Background(), agentID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if !exists {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-09 17:17:04 +00:00
|
|
|
|
|
|
|
func (v Validator) areFilterFieldsValid(validFilters []string) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
filters, ok := value.(params.FilterRequest)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
for filter := range filters {
|
|
|
|
if !slices.Contains(validFilters, filter) {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgFiltersAreNotValid)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Validator) areSortFieldsValid(validSortFields []string) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
sort, ok := value.(params.SortRequest)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if sort.Field == "" && sort.Direction != "" {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSortFieldIsRequired)
|
|
|
|
}
|
|
|
|
if sort.Direction != "" && sort.Direction != params.AscSortDirection && sort.Direction != params.DescSortDirection {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSortDirectionShouldBeAscOrDesc)
|
|
|
|
}
|
|
|
|
if sort.Field != "" && !slices.Contains(validSortFields, sort.Field) {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSortFieldIsNotValid)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|