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"
|
2023-12-21 13:00:31 +00:00
|
|
|
"fmt"
|
2024-01-16 16:13:06 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
|
|
|
addressparam "git.gocasts.ir/ebhomengo/niki/param/benefactor/address"
|
|
|
|
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/benefactore"
|
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"
|
2024-05-21 17:38:43 +00:00
|
|
|
"time"
|
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-01-16 16:13:06 +00:00
|
|
|
type BenefactorSvc interface {
|
|
|
|
BenefactorExistByID(ctx context.Context, request param.BenefactorExistByIDRequest) (param.BenefactorExistByIDResponse, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddressSvc interface {
|
|
|
|
AddressExistByID(ctx context.Context, request addressparam.GetAddressByIDRequest) (addressparam.GetAddressByIDResponse, error)
|
2023-12-21 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
2024-05-31 14:49:04 +00:00
|
|
|
type Repository interface {
|
|
|
|
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 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-05-31 14:49:04 +00:00
|
|
|
func New(benefactorSvc BenefactorSvc, addressSvc AddressSvc, repo Repository) Validator {
|
|
|
|
return Validator{benefactorSvc: benefactorSvc, addressSvc: addressSvc, repo: repo}
|
2023-12-21 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
2023-12-27 21:55:15 +00:00
|
|
|
func (v Validator) doesBenefactorExist(value interface{}) error {
|
2024-01-01 07:22:14 +00:00
|
|
|
benefactorID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-01-16 16:13:06 +00:00
|
|
|
_, err := v.benefactorSvc.BenefactorExistByID(context.Background(), param.BenefactorExistByIDRequest{ID: benefactorID})
|
2023-12-21 13:00:31 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Validator) doesTypeExist(value interface{}) error {
|
2024-01-16 16:13:06 +00:00
|
|
|
typeID, ok := value.(entity.KindBoxType)
|
2024-01-01 07:22:14 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-01-16 16:13:06 +00:00
|
|
|
if !typeID.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-01-16 16:13:06 +00:00
|
|
|
func (v Validator) doesAddressExist(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-01-16 16:13:06 +00:00
|
|
|
address, err := v.addressSvc.AddressExistByID(context.Background(), 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 == nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
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 {
|
|
|
|
date, ok := value.(string)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
t, err := time.Parse(time.DateTime, date)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
|
|
|
|
}
|
|
|
|
if t.Before(time.Now()) {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-05-31 14:49:04 +00:00
|
|
|
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID uint) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
kindBoxReqID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
kindBoxReq, err := v.repo.GetKindBoxReqByID(context.Background(), kindBoxReqID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
if kindBoxReq.BenefactorID != benefactorID {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgKindBoxReqDoesntBelongToBenefactor)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v Validator) doesKindBoxRequestHavePendingStatus(value interface{}) error {
|
|
|
|
kindBoxReqID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
kindBoxReq, err := v.repo.GetKindBoxReqByID(context.Background(), kindBoxReqID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
|
|
|
if kindBoxReq.Status != entity.KindBoxReqPendingStatus {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgInvalidStatus)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|