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"
|
2024-07-30 20:30:24 +00:00
|
|
|
"errors"
|
2024-07-01 17:13:28 +00:00
|
|
|
"fmt"
|
2024-09-15 07:36:09 +00:00
|
|
|
params "git.gocasts.ir/ebhomengo/niki/param"
|
|
|
|
"slices"
|
2024-07-30 20:30:24 +00:00
|
|
|
"time"
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-07-30 20:30:24 +00:00
|
|
|
entity "git.gocasts.ir/ebhomengo/niki/entity"
|
|
|
|
adminaddressparam "git.gocasts.ir/ebhomengo/niki/param/admin/address"
|
2024-08-01 10:20:18 +00:00
|
|
|
agentparam "git.gocasts.ir/ebhomengo/niki/param/admin/agent"
|
2024-07-30 20:30:24 +00:00
|
|
|
param "git.gocasts.ir/ebhomengo/niki/param/admin/benefactor"
|
|
|
|
refertimeparam "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time"
|
2024-07-01 17:13:28 +00:00
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
2024-07-30 20:30:24 +00:00
|
|
|
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
2024-08-01 10:20:18 +00:00
|
|
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
2024-07-01 17:13:28 +00:00
|
|
|
)
|
|
|
|
|
2024-07-30 20:30:24 +00:00
|
|
|
//go:generate mockery --name Repository
|
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)
|
|
|
|
}
|
|
|
|
|
2024-07-30 20:30:24 +00:00
|
|
|
//go:generate mockery --name AgentSvc
|
2024-08-01 10:20:18 +00:00
|
|
|
type AgentSvc interface {
|
|
|
|
AgentExistByID(ctx context.Context, req agentparam.AdminAgentExistByIDRequest) (agentparam.AdminAgentExistByIDResponse, error)
|
2023-12-27 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 20:30:24 +00:00
|
|
|
//go:generate mockery --name AddressSvc
|
|
|
|
type AddressSvc interface {
|
|
|
|
GetAddressByID(ctx context.Context, request adminaddressparam.AddressGetRequest) (adminaddressparam.AddressGetResponse, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:generate mockery --name ReferTimeSvc
|
|
|
|
type ReferTimeSvc interface {
|
|
|
|
GetReferTimeByID(ctx context.Context, req refertimeparam.GetReferTimeRequest) (refertimeparam.GetReferTimeResponse, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
//go:generate mockery --name BenefactorSvc
|
|
|
|
type BenefactorSvc interface {
|
|
|
|
BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error)
|
|
|
|
}
|
|
|
|
|
2023-12-27 21:55:15 +00:00
|
|
|
type Validator struct {
|
2024-07-30 20:30:24 +00:00
|
|
|
repo Repository
|
|
|
|
agentSvc AgentSvc
|
|
|
|
benefactorSvc BenefactorSvc
|
|
|
|
referTimeSvc ReferTimeSvc
|
|
|
|
addressSvc AddressSvc
|
2023-12-27 21:55:15 +00:00
|
|
|
}
|
|
|
|
|
2024-07-30 20:30:24 +00:00
|
|
|
func New(repo Repository, agentSvc AgentSvc, benefactorSvc BenefactorSvc, referTimeSvc ReferTimeSvc, addressSvc AddressSvc) Validator {
|
|
|
|
return Validator{repo: repo, agentSvc: agentSvc, benefactorSvc: benefactorSvc, referTimeSvc: referTimeSvc, addressSvc: addressSvc}
|
2023-12-27 21:55:15 +00:00
|
|
|
}
|
2024-07-01 17:13:28 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) doesKindBoxExist(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
2024-07-30 20:30:24 +00:00
|
|
|
const op = "Validator.doesKindBoxExist"
|
2024-08-01 10:20:18 +00:00
|
|
|
kindBoxID, ok := value.(uint)
|
|
|
|
if !ok {
|
2024-07-30 20:30:24 +00:00
|
|
|
return richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindInvalid)
|
|
|
|
}
|
|
|
|
if kindBoxID == 0 {
|
|
|
|
return richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidInput).WithKind(richerror.KindInvalid)
|
2024-08-01 10:20:18 +00:00
|
|
|
}
|
|
|
|
exists, err := v.repo.KindBoxExist(ctx, kindBoxID)
|
|
|
|
if err != nil {
|
2024-07-30 20:30:24 +00:00
|
|
|
return richerror.New(op).WithErr(err).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
2024-08-01 10:20:18 +00:00
|
|
|
}
|
|
|
|
if !exists {
|
2024-07-30 20:30:24 +00:00
|
|
|
return richerror.New(op).WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindNotFound)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Validator) doesBenefactorExist(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
const op = "Validator.doesBenefactorExist"
|
|
|
|
|
|
|
|
benefactorID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return richerror.New(op).
|
|
|
|
WithMessage("invalid type for benefactorID").
|
|
|
|
WithKind(richerror.KindInvalid)
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := v.benefactorSvc.BenefactorExistByID(ctx, param.BenefactorExistByIDRequest{ID: benefactorID})
|
|
|
|
if err != nil {
|
|
|
|
return richerror.New(op).
|
|
|
|
WithErr(err).
|
|
|
|
WithMessage("could not check if benefactor exists").
|
|
|
|
WithKind(richerror.KindUnexpected)
|
2024-08-01 10:20:18 +00:00
|
|
|
}
|
2024-07-01 17:13:28 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-01 17:13:28 +00:00
|
|
|
}
|
2024-07-02 20:17:40 +00:00
|
|
|
|
2024-07-30 20:30:24 +00:00
|
|
|
func (v Validator) doesBenefactorHaveKindBox(ctx context.Context, kindBoxID uint, benefactorID uint) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
const op = "Validator.doesBenefactorHaveKindBox"
|
|
|
|
|
|
|
|
fetchedKindBox, err := v.repo.GetKindBox(ctx, kindBoxID)
|
|
|
|
if err != nil {
|
|
|
|
|
|
|
|
return richerror.New(op).WithErr(err).WithMessage("could not fetch KindBox").WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
if fetchedKindBox.BenefactorID != benefactorID {
|
|
|
|
return richerror.New(op).WithMessage("benefactor does not have the specified kind box").WithKind(richerror.KindInvalid)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) checkKindBoxStatusForAssigningReceiverAgent(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
2024-07-30 20:30:24 +00:00
|
|
|
const op = "Validator.checkKindBoxStatusForAssigningReceiverAgent"
|
2024-08-01 10:20:18 +00:00
|
|
|
kindboxID, ok := value.(uint)
|
|
|
|
if !ok {
|
2024-07-30 20:30:24 +00:00
|
|
|
return richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindInvalid)
|
2024-08-01 10:20:18 +00:00
|
|
|
}
|
|
|
|
kindBox, err := v.repo.GetKindBox(ctx, kindboxID)
|
|
|
|
if err != nil {
|
2024-07-30 20:30:24 +00:00
|
|
|
return richerror.New(op).WithErr(err).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
2024-08-01 10:20:18 +00:00
|
|
|
}
|
|
|
|
if kindBox.Status != entity.KindBoxReadyToReturnStatus {
|
2024-07-30 20:30:24 +00:00
|
|
|
return richerror.New(op).WithMessage(errmsg.ErrorMsgAssignReceiverAgentKindBoxStatus).WithKind(richerror.KindInvalid)
|
2024-08-01 10:20:18 +00:00
|
|
|
}
|
2024-07-02 20:17:40 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-02 20:17:40 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) doesAgentExist(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
2024-07-30 20:30:24 +00:00
|
|
|
const op = "Validator.doesAgentExist"
|
2024-08-01 10:20:18 +00:00
|
|
|
agentID, ok := value.(uint)
|
|
|
|
if !ok {
|
2024-07-30 20:30:24 +00:00
|
|
|
return richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindInvalid)
|
2024-08-01 10:20:18 +00:00
|
|
|
}
|
2024-07-30 20:30:24 +00:00
|
|
|
resp, err := v.agentSvc.AgentExistByID(ctx, agentparam.AdminAgentExistByIDRequest{AgentID: agentID})
|
2024-08-01 10:20:18 +00:00
|
|
|
if err != nil {
|
2024-07-30 20:30:24 +00:00
|
|
|
return richerror.New(op).WithErr(err).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
2024-08-01 10:20:18 +00:00
|
|
|
}
|
|
|
|
if !resp.Exist {
|
2024-07-30 20:30:24 +00:00
|
|
|
return richerror.New(op).WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindNotFound)
|
2024-08-01 10:20:18 +00:00
|
|
|
}
|
2024-07-30 20:30:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2024-07-02 20:17:40 +00:00
|
|
|
|
2024-07-30 20:30:24 +00:00
|
|
|
func (v Validator) isReturnReferTimeIDValid(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
const op = "Validator.isReturnTimeIDValid"
|
|
|
|
referTimeID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindInvalid)
|
|
|
|
}
|
|
|
|
resp, err := v.referTimeSvc.GetReferTimeByID(ctx, refertimeparam.GetReferTimeRequest{
|
|
|
|
ReferTimeID: referTimeID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return richerror.New(op).WithErr(err).WithMessage(errmsg.ErrorMsgReferTimeNotFound).WithKind(richerror.KindNotFound)
|
|
|
|
}
|
|
|
|
if resp.ReferTime.Status != entity.ReferTimeActiveStatus {
|
|
|
|
return richerror.New(op).WithMessage(errmsg.ErrorMsgReferTimeIsNotActive).WithKind(richerror.KindInvalid)
|
|
|
|
}
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-02 20:17:40 +00:00
|
|
|
}
|
2024-08-24 18:13:29 +00:00
|
|
|
|
|
|
|
func (v Validator) CheckKindBoxStatusForEnumeration(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
kindBoxID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
kindBox, err := v.repo.GetKindBox(ctx, kindBoxID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if kindBox.Status != entity.KindBoxReturnedStatus {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgReturnKindBoxStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2024-07-30 20:30:24 +00:00
|
|
|
}
|
|
|
|
func (v Validator) doesBenefactorAddressExist(ctx context.Context, kindBoxID uint, benefactorID uint, addressID uint) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
const op = "Validator.doesBenefactorAddressExist"
|
|
|
|
addressRequest := adminaddressparam.AddressGetRequest{
|
|
|
|
AddressID: addressID,
|
|
|
|
}
|
|
|
|
addressResponse, err := v.addressSvc.GetAddressByID(ctx, addressRequest)
|
|
|
|
if err != nil {
|
|
|
|
return richerror.New(op).WithErr(err).WithMessage("failed to retrieve address").WithKind(richerror.KindUnexpected)
|
|
|
|
}
|
|
|
|
|
|
|
|
if addressResponse.Address.BenefactorID != benefactorID {
|
|
|
|
return richerror.New(op).WithMessage("the specified address does not belong to the benefactor").WithKind(richerror.KindInvalid)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Validator) isDateValid(value interface{}) error {
|
|
|
|
const op = "Validator.isDateValid"
|
|
|
|
date, ok := value.(time.Time)
|
|
|
|
if !ok {
|
|
|
|
return richerror.New(op).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindInvalid)
|
|
|
|
}
|
|
|
|
if date.Before(time.Now()) {
|
|
|
|
return richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidInput).WithKind(richerror.KindInvalid)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Validator) isAmountValid(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
const op = "Validator.isAmountValid"
|
|
|
|
amount, ok := value.(uint)
|
|
|
|
if !ok || amount == 0 {
|
|
|
|
return richerror.New(op).WithMessage("amount is required").WithKind(richerror.KindInvalid)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-09-15 07:36:09 +00:00
|
|
|
// processing errors
|
2024-07-30 20:30:24 +00:00
|
|
|
func (v Validator) processValidationErrors(err error, fieldErrors map[string]string) {
|
|
|
|
var errV validation.Errors
|
|
|
|
if errors.As(err, &errV) {
|
|
|
|
for key, value := range errV {
|
|
|
|
if value != nil {
|
|
|
|
fieldErrors[key] = value.Error()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-15 07:36:09 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
}
|