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-07-10 01:21:03 +00:00
|
|
|
refertimeparam "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time"
|
|
|
|
addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
|
2024-01-22 14:07:51 +00:00
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
2024-06-21 06:57:35 +00:00
|
|
|
benefactorsvc "git.gocasts.ir/ebhomengo/niki/service/admin/benefactor"
|
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-07-10 01:21:03 +00:00
|
|
|
AgentExistByID(ctx context.Context, agentID uint) (bool, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReferTimeSvc interface {
|
|
|
|
GetReferTimeByID(ctx context.Context, req refertimeparam.GetReferTimeRequest) (refertimeparam.GetReferTimeResponse, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddressSvc interface {
|
|
|
|
AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error)
|
2024-02-20 20:34:51 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 21:55:15 +00:00
|
|
|
type Validator struct {
|
2024-06-21 06:57:35 +00:00
|
|
|
repo Repository
|
|
|
|
adminSvc AdminSvc
|
|
|
|
benefactorSvc benefactorsvc.Service
|
2024-07-10 01:21:03 +00:00
|
|
|
referTimeSvc ReferTimeSvc
|
|
|
|
addressSvc AddressSvc
|
2023-12-27 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
2024-07-10 01:21:03 +00:00
|
|
|
func New(repo Repository, adminSvc AdminSvc, benefactorSvc benefactorsvc.Service, referTimeSvc ReferTimeSvc, addressSvc AddressSvc) Validator {
|
|
|
|
return Validator{repo: repo, adminSvc: adminSvc, benefactorSvc: benefactorSvc, referTimeSvc: referTimeSvc, addressSvc: addressSvc}
|
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
|
|
|
|
}
|
|
|
|
}
|
2024-07-10 01:21:03 +00:00
|
|
|
|
|
|
|
func (v Validator) isReferTimeIDValid(value interface{}) error {
|
|
|
|
referTimeID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
resp, gErr := v.referTimeSvc.GetReferTimeByID(context.Background(), refertimeparam.GetReferTimeRequest{
|
|
|
|
ReferTimeID: referTimeID,
|
|
|
|
})
|
|
|
|
if gErr != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgReferTimeNotFound)
|
|
|
|
}
|
|
|
|
if resp.ReferTime.Status != entity.ReferTimeActiveStatus {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgReferTimeIsNotActive)
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Validator) doesBenefactorAddressExist(kindBoxReqID uint) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
addressID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
|
|
|
|
kindBoxReq, err := v.repo.GetByID(context.Background(), kindBoxReqID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
|
|
|
|
address, aErr := v.addressSvc.AddressExistByID(context.Background(), addressparam.GetAddressByIDRequest{ID: addressID})
|
|
|
|
if aErr != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if address.Address == nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
if address.Address.BenefactorID != kindBoxReq.BenefactorID {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|