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-08-01 10:20:18 +00:00
|
|
|
addressparam "git.gocasts.ir/ebhomengo/niki/param/admin/address"
|
2024-02-20 20:34:51 +00:00
|
|
|
param "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
|
2024-08-01 10:20:18 +00:00
|
|
|
agentparam "git.gocasts.ir/ebhomengo/niki/param/admin/agent"
|
|
|
|
benefactorparam "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
|
2024-07-10 01:21:03 +00:00
|
|
|
refertimeparam "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time"
|
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-07-30 11:05:41 +00:00
|
|
|
KindBoxRequestExist(ctx context.Context, 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-08-01 10:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type AgentSvc interface {
|
|
|
|
AgentExistByID(ctx context.Context, req agentparam.AdminAgentExistByIDRequest) (agentparam.AdminAgentExistByIDResponse, error)
|
2024-07-10 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ReferTimeSvc interface {
|
|
|
|
GetReferTimeByID(ctx context.Context, req refertimeparam.GetReferTimeRequest) (refertimeparam.GetReferTimeResponse, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddressSvc interface {
|
2024-08-01 10:20:18 +00:00
|
|
|
GetAddressByID(ctx context.Context, req addressparam.AddressGetRequest) (addressparam.AddressGetResponse, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type BenefactorSvc interface {
|
|
|
|
BenefactorExistByID(ctx context.Context, req benefactorparam.BenefactorExistByIDRequest) (benefactorparam.BenefactorExistByIDResponse, 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
|
2024-08-01 10:20:18 +00:00
|
|
|
agentSvc AgentSvc
|
|
|
|
benefactorSvc BenefactorSvc
|
2024-07-10 01:21:03 +00:00
|
|
|
referTimeSvc ReferTimeSvc
|
|
|
|
addressSvc AddressSvc
|
2023-12-27 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func New(repo Repository, adminSvc AdminSvc, agentSvc AgentSvc, benefactorSvc BenefactorSvc, referTimeSvc ReferTimeSvc, addressSvc AddressSvc) Validator {
|
|
|
|
return Validator{repo: repo, adminSvc: adminSvc, agentSvc: agentSvc, benefactorSvc: benefactorSvc, referTimeSvc: referTimeSvc, addressSvc: addressSvc}
|
2023-12-27 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) doesKindBoxRequestExist(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
kindboxreqID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if isExist, err := v.repo.KindBoxRequestExist(ctx, 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 14:07:51 +00:00
|
|
|
}
|
2024-08-01 10:20:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v Validator) CheckKindBoxReqStatusForAccepting(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
kindboxreqID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
kindBox, err := v.repo.GetByID(ctx, kindboxreqID)
|
2024-01-22 14:07:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-08-01 10:20:18 +00:00
|
|
|
if kindBox.Status != entity.KindBoxReqPendingStatus {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgAcceptKindBoxReqStatus)
|
2024-01-22 14:07:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
2024-01-25 17:47:10 +00:00
|
|
|
}
|
2024-01-22 14:07:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) CheckKindBoxReqStatusForRejecting(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
kindboxreqID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
kindBox, err := v.repo.GetByID(ctx, kindboxreqID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if kindBox.Status != entity.KindBoxReqPendingStatus {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgRejectKindBoxReqStatus)
|
|
|
|
}
|
2024-01-22 15:21:13 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-22 15:21:13 +00:00
|
|
|
}
|
2024-06-12 08:15:28 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) checkKindBoxReqStatusForDelivering(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
kindboxreqID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
kindBoxReq, err := v.repo.GetByID(ctx, kindboxreqID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if kindBoxReq.Status != entity.KindBoxReqAssignedSenderAgentStatus {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgDeliverKindBoxReqStatus)
|
|
|
|
}
|
2024-02-20 20:34:51 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-02-20 20:34:51 +00:00
|
|
|
}
|
2024-06-12 08:15:28 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) checkKindBoxReqStatusForAssigningSenderAgent(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
kindboxreqID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
kindBoxReq, err := v.repo.GetByID(ctx, kindboxreqID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if kindBoxReq.Status != entity.KindBoxReqAcceptedStatus {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgAssignSenderAgentKindBoxReqStatus)
|
|
|
|
}
|
2024-02-20 20:34:51 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-02-20 20:34:51 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) checkCountAcceptedMustBeLessThanCountRequested(ctx context.Context, kindboxreqID uint) validation.RuleFunc {
|
2024-05-21 16:23:47 +00:00
|
|
|
return func(value interface{}) error {
|
|
|
|
countAccepted, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-08-01 10:20:18 +00:00
|
|
|
kindBoxReq, err := v.repo.GetByID(ctx, kindboxreqID)
|
2024-05-21 16:23:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if kindBoxReq.CountRequested < countAccepted {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgCountAcceptedOverflow)
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-21 16:23:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) doesAgentAdminExist(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
adminID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
resp, err := v.adminSvc.AdminExistByID(ctx, param.AdminExistByIDRequest{
|
|
|
|
AdminID: adminID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if resp.Admin.Role != entity.AdminAgentRole {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgAdminIsNotAgent)
|
|
|
|
}
|
2024-03-14 13:38:42 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-03-14 13:38:42 +00:00
|
|
|
}
|
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)
|
|
|
|
}
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-06-12 08:15:28 +00:00
|
|
|
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)
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-06-12 08:15:28 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2024-07-10 01:21:03 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) isReferTimeIDValid(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
referTimeID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
resp, gErr := v.referTimeSvc.GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{
|
|
|
|
ReferTimeID: referTimeID,
|
|
|
|
})
|
|
|
|
if gErr != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgReferTimeNotFound)
|
|
|
|
}
|
|
|
|
if resp.ReferTime.Status != entity.ReferTimeActiveStatus {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgReferTimeIsNotActive)
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-10 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) doesAgentExist(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
agentID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
resp, err := v.agentSvc.AgentExistByID(ctx, agentparam.AdminAgentExistByIDRequest{AgentID: agentID})
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if !resp.Exist {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
2024-07-10 01:21:03 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-10 01:21:03 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) doesBenefactorAddressExist(ctx context.Context, kindBoxReqID uint) validation.RuleFunc {
|
2024-07-10 01:21:03 +00:00
|
|
|
return func(value interface{}) error {
|
|
|
|
addressID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
kindBoxReq, err := v.repo.GetByID(ctx, kindBoxReqID)
|
2024-07-10 01:21:03 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
address, gErr := v.addressSvc.GetAddressByID(ctx, addressparam.AddressGetRequest{AddressID: addressID})
|
|
|
|
if gErr != nil {
|
2024-07-10 01:21:03 +00:00
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if address.Address.BenefactorID != kindBoxReq.BenefactorID {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2024-07-12 21:25:20 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) IsSerialNumbersCountValid(ctx context.Context, kindBoxReqID uint) validation.RuleFunc {
|
2024-07-12 21:25:20 +00:00
|
|
|
return func(value interface{}) error {
|
|
|
|
serialNumbers, ok := value.([]string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-08-01 10:20:18 +00:00
|
|
|
kindBoxReq, gErr := v.repo.GetByID(ctx, kindBoxReqID)
|
2024-07-12 21:25:20 +00:00
|
|
|
if gErr != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if len(serialNumbers) != 0 && len(serialNumbers) > int(kindBoxReq.CountAccepted) {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgInvalidSerialNumberRange)
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-07-12 21:25:20 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|