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-06-12 08:15:28 +00:00
|
|
|
"slices"
|
2024-01-22 14:07:51 +00:00
|
|
|
|
2024-01-25 17:59:18 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
2024-06-12 08:15:28 +00:00
|
|
|
params "git.gocasts.ir/ebhomengo/niki/param"
|
2024-02-20 20:34:51 +00:00
|
|
|
param "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
|
2024-01-22 14:07:51 +00:00
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
2024-05-21 16:23:47 +00:00
|
|
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
2024-01-22 14:07:51 +00:00
|
|
|
)
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2024-02-20 20:34:51 +00:00
|
|
|
type AdminSvc interface {
|
|
|
|
AdminExistByID(ctx context.Context, req param.AdminExistByIDRequest) (param.AdminExistByIDResponse, error)
|
2024-06-14 08:41:36 +00:00
|
|
|
BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddressSvc interface {
|
|
|
|
AddressExistByID(ctx context.Context, request param.GetAddressByIDRequest) (param.GetAddressByIDResponse, error)
|
2024-02-20 20:34:51 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 21:55:15 +00:00
|
|
|
type Validator struct {
|
2024-06-14 08:41:36 +00:00
|
|
|
repo Repository
|
|
|
|
adminSvc AdminSvc
|
|
|
|
addressSvc AddressSvc
|
2023-12-27 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
2024-05-17 20:16:28 +00:00
|
|
|
func New(repo Repository, adminSvc AdminSvc) Validator {
|
|
|
|
return Validator{repo: repo, adminSvc: adminSvc}
|
2023-12-27 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
2024-06-12 08:15:28 +00:00
|
|
|
|
2024-05-21 16:23:47 +00:00
|
|
|
func (v Validator) checkKindBoxReqStatusForDelivering(value interface{}) error {
|
2024-02-20 20:34:51 +00:00
|
|
|
kindboxreqID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
kindBoxReq, err := v.repo.GetByID(context.Background(), kindboxreqID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-21 16:23:47 +00:00
|
|
|
if kindBoxReq.Status != entity.KindBoxReqAssignedSenderAgentStatus {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgDeliverKindBoxReqStatus)
|
2024-02-20 20:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-12 08:15:28 +00:00
|
|
|
|
2024-05-21 16:23:47 +00:00
|
|
|
func (v Validator) checkKindBoxReqStatusForAssigningSenderAgent(value interface{}) error {
|
|
|
|
kindboxreqID, ok := value.(uint)
|
2024-02-20 20:34:51 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-05-21 16:23:47 +00:00
|
|
|
kindBoxReq, err := v.repo.GetByID(context.Background(), kindboxreqID)
|
2024-02-20 20:34:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-21 16:23:47 +00:00
|
|
|
if kindBoxReq.Status != entity.KindBoxReqAcceptedStatus {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgAssignSenderAgentKindBoxReqStatus)
|
2024-02-20 20:34:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-21 16:23:47 +00:00
|
|
|
func (v Validator) checkCountAcceptedMustBeLessThanCountRequested(kindboxreqID uint) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
countAccepted, 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.CountRequested < countAccepted {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgCountAcceptedOverflow)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Validator) doesAgentAdminExist(value interface{}) error {
|
|
|
|
adminID, ok := value.(uint)
|
2024-03-14 13:38:42 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-05-21 16:23:47 +00:00
|
|
|
resp, err := v.adminSvc.AdminExistByID(context.Background(), param.AdminExistByIDRequest{
|
|
|
|
AdminID: adminID,
|
|
|
|
})
|
2024-03-14 13:38:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-05-21 16:23:47 +00:00
|
|
|
if resp.Admin.Role != entity.AdminAgentRole {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgAdminIsNotAgent)
|
2024-03-14 13:38:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-06-12 08:15:28 +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
|
|
|
|
}
|
|
|
|
}
|