2024-01-02 14:04:16 +00:00
|
|
|
package benefactorkindboxreqvalidator
|
2023-12-21 13:00:31 +00:00
|
|
|
|
|
|
|
import (
|
2024-01-16 16:13:06 +00:00
|
|
|
"context"
|
2024-07-24 23:45:04 +00:00
|
|
|
"errors"
|
2023-12-21 13:00:31 +00:00
|
|
|
"fmt"
|
2024-07-03 17:18:06 +00:00
|
|
|
"slices"
|
2024-06-14 08:41:36 +00:00
|
|
|
"time"
|
|
|
|
|
2024-01-16 16:13:06 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
2024-07-24 23:45:04 +00:00
|
|
|
params "git.gocasts.ir/ebhomengo/niki/param"
|
2024-01-16 16:13:06 +00:00
|
|
|
addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
|
2024-08-01 10:20:18 +00:00
|
|
|
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactor"
|
|
|
|
refertimeparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/refer_time"
|
2023-12-21 13:00:31 +00:00
|
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
2023-12-30 19:51:22 +00:00
|
|
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
2023-12-21 13:00:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2024-01-16 16:13:06 +00:00
|
|
|
MinKindBoxReq uint = 1
|
|
|
|
MaxKindBoxReq uint = 100
|
2023-12-21 13:00:31 +00:00
|
|
|
)
|
|
|
|
|
2024-08-10 17:47:21 +00:00
|
|
|
//go:generate mockery --name BenefactorSvc
|
2024-01-16 16:13:06 +00:00
|
|
|
type BenefactorSvc interface {
|
|
|
|
BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error)
|
|
|
|
}
|
|
|
|
|
2024-08-10 17:47:21 +00:00
|
|
|
//go:generate mockery --name AddressSvc
|
2024-01-16 16:13:06 +00:00
|
|
|
type AddressSvc interface {
|
|
|
|
AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error)
|
2023-12-21 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
2024-08-10 17:47:21 +00:00
|
|
|
//go:generate mockery --name ReferTimeSvc
|
2024-05-31 12:36:48 +00:00
|
|
|
type ReferTimeSvc interface {
|
|
|
|
GetReferTimeByID(ctx context.Context, req refertimeparam.GetReferTimeRequest) (refertimeparam.GetReferTimeResponse, error)
|
|
|
|
}
|
|
|
|
|
2024-08-10 17:47:21 +00:00
|
|
|
//go:generate mockery --name Repository
|
2024-05-31 14:49:04 +00:00
|
|
|
type Repository interface {
|
2024-07-30 11:05:41 +00:00
|
|
|
KindBoxRequestExist(ctx context.Context, id uint) (bool, error)
|
2024-05-31 14:49:04 +00:00
|
|
|
GetKindBoxReqByID(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error)
|
|
|
|
}
|
|
|
|
|
2023-12-21 13:00:31 +00:00
|
|
|
type Validator struct {
|
2024-01-16 16:13:06 +00:00
|
|
|
benefactorSvc BenefactorSvc
|
|
|
|
addressSvc AddressSvc
|
2024-05-31 12:36:48 +00:00
|
|
|
referTimeSvc ReferTimeSvc
|
2024-05-31 14:49:04 +00:00
|
|
|
repo Repository
|
2023-12-21 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-26 12:11:50 +00:00
|
|
|
type ValidatorError struct {
|
|
|
|
Fields map[string]string `json:"error"`
|
|
|
|
Err error `json:"message"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v ValidatorError) Error() string {
|
|
|
|
var err string
|
|
|
|
|
|
|
|
for key, value := range v.Fields {
|
|
|
|
err += fmt.Sprintf("%s: %s\n", key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-07-03 17:18:06 +00:00
|
|
|
func New(benefactorSvc BenefactorSvc, addressSvc AddressSvc, referTimeSvc ReferTimeSvc, repo Repository) Validator {
|
|
|
|
return Validator{benefactorSvc: benefactorSvc, addressSvc: addressSvc, referTimeSvc: referTimeSvc, repo: repo}
|
2023-12-21 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) doesBenefactorExist(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
benefactorID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-08-10 17:47:21 +00:00
|
|
|
res, err := v.benefactorSvc.BenefactorExistByID(ctx, param.BenefactorExistByIDRequest{ID: benefactorID})
|
2024-08-01 10:20:18 +00:00
|
|
|
if err != nil {
|
2024-08-10 17:47:21 +00:00
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if !res.Existed {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgBenefactorNotFound)
|
2024-08-01 10:20:18 +00:00
|
|
|
}
|
2023-12-21 13:00:31 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-12-21 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v Validator) doesTypeExist(value interface{}) error {
|
2024-07-03 17:18:06 +00:00
|
|
|
kindBoxType, ok := value.(entity.KindBoxType)
|
2024-01-01 07:22:14 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-07-03 17:18:06 +00:00
|
|
|
if !kindBoxType.IsValid() {
|
2023-12-27 21:55:15 +00:00
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-12-28 13:43:06 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) doesAddressExist(ctx context.Context, benefactorID uint) validation.RuleFunc {
|
2023-12-30 19:51:22 +00:00
|
|
|
return func(value interface{}) error {
|
2024-01-16 16:13:06 +00:00
|
|
|
addressID, ok := value.(uint)
|
2024-01-01 07:22:14 +00:00
|
|
|
if !ok {
|
2024-01-16 16:13:06 +00:00
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
2024-01-01 07:22:14 +00:00
|
|
|
}
|
2024-08-01 10:20:18 +00:00
|
|
|
address, err := v.addressSvc.AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: addressID})
|
2023-12-30 19:51:22 +00:00
|
|
|
if err != nil {
|
2024-01-16 16:13:06 +00:00
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if address.Address.BenefactorID != benefactorID {
|
2023-12-30 19:51:22 +00:00
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
2023-12-28 13:43:06 +00:00
|
|
|
|
2023-12-30 19:51:22 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-12-28 13:43:06 +00:00
|
|
|
}
|
|
|
|
|
2024-05-21 17:38:43 +00:00
|
|
|
func (v Validator) isDateValid(value interface{}) error {
|
2024-05-31 12:36:48 +00:00
|
|
|
date, ok := value.(time.Time)
|
2024-05-21 17:38:43 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-05-31 12:36:48 +00:00
|
|
|
if date.Before(time.Now()) {
|
2024-05-21 17:38:43 +00:00
|
|
|
return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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-05-31 12:36:48 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) doesKindBoxBelongToBenefactor(ctx context.Context, benefactorID uint) validation.RuleFunc {
|
2024-05-31 14:49:04 +00:00
|
|
|
return func(value interface{}) error {
|
|
|
|
kindBoxReqID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
2024-08-01 10:20:18 +00:00
|
|
|
kindBoxReq, err := v.repo.GetKindBoxReqByID(ctx, kindBoxReqID)
|
2024-05-31 14:49:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
if kindBoxReq.BenefactorID != benefactorID {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgKindBoxReqDoesntBelongToBenefactor)
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-05-31 14:49:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
func (v Validator) doesKindBoxRequestHavePendingStatus(ctx context.Context) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
kindBoxReqID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
kindBoxReq, err := v.repo.GetKindBoxReqByID(ctx, kindBoxReqID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
if kindBoxReq.Status != entity.KindBoxReqPendingStatus {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgInvalidStatus)
|
|
|
|
}
|
2024-07-24 23:45:04 +00:00
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-05-31 14:49:04 +00:00
|
|
|
}
|
2024-07-03 17:18:06 +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-07-03 17:18:06 +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-07-03 17:18:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
2024-07-23 19:21:02 +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)
|
2024-07-23 19:21:02 +00:00
|
|
|
}
|
2024-08-01 10:20:18 +00:00
|
|
|
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")
|
|
|
|
}
|
2024-07-23 19:21:02 +00:00
|
|
|
}
|
|
|
|
|
2024-08-01 10:20:18 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-07-23 19:21:02 +00:00
|
|
|
}
|
|
|
|
|
2024-08-10 17:47:21 +00:00
|
|
|
func (v Validator) doesBenefactorAddressExist(ctx context.Context, benefactorID uint) validation.RuleFunc {
|
2024-07-23 19:21:02 +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
|
|
|
address, aErr := v.addressSvc.AddressExistByID(ctx, addressparam.GetAddressByIDRequest{ID: addressID})
|
2024-07-23 19:21:02 +00:00
|
|
|
if aErr != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-08-10 17:47:21 +00:00
|
|
|
if address.Address.BenefactorID != benefactorID {
|
2024-07-23 19:21:02 +00:00
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|