2024-01-02 14:04:16 +00:00
|
|
|
package benefactorkindboxvalidator
|
2023-12-21 13:00:31 +00:00
|
|
|
|
|
|
|
import (
|
2024-06-25 21:58:11 +00:00
|
|
|
"context"
|
2023-12-21 13:00:31 +00:00
|
|
|
"fmt"
|
2024-06-25 21:58:11 +00:00
|
|
|
"time"
|
2023-12-21 13:00:31 +00:00
|
|
|
|
2024-06-25 21:58:11 +00:00
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
|
|
|
refertimeparam "git.gocasts.ir/ebhomengo/niki/param/admin/refer_time"
|
|
|
|
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"
|
2023-12-21 13:00:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type Repository interface {
|
2024-06-25 21:58:11 +00:00
|
|
|
BenefactorKindBoxExist(ctx context.Context, benefactorID uint, kindBoxID uint) (bool, error)
|
|
|
|
GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
type ReferTimeSvc interface {
|
|
|
|
GetReferTimeByID(ctx context.Context, req refertimeparam.GetReferTimeRequest) (refertimeparam.GetReferTimeResponse, error)
|
2023-12-21 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Validator struct {
|
2024-06-25 21:58:11 +00:00
|
|
|
repo Repository
|
|
|
|
benefactorSvc BenefactorSvc
|
|
|
|
addressSvc AddressSvc
|
|
|
|
referTimeSvc ReferTimeSvc
|
2023-12-21 13:00:31 +00:00
|
|
|
}
|
|
|
|
|
2024-06-25 21:58:11 +00:00
|
|
|
func New(repo Repository, benefactorSvc BenefactorSvc, addressSvc AddressSvc, referTimeSvc ReferTimeSvc) Validator {
|
|
|
|
return Validator{repo: repo, benefactorSvc: benefactorSvc, addressSvc: addressSvc, referTimeSvc: referTimeSvc}
|
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-06-25 21:58:11 +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
|
|
|
|
}
|
|
|
|
|
2024-06-25 21:58:11 +00:00
|
|
|
func (v Validator) doesBenefactorKindBoxExist(benefactorID uint) validation.RuleFunc {
|
|
|
|
return func(value interface{}) error {
|
|
|
|
kbID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
exist, err := v.repo.BenefactorKindBoxExist(context.Background(), benefactorID, kbID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if !exist {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgNotFound)
|
|
|
|
}
|
2023-12-21 13:00:31 +00:00
|
|
|
|
2024-06-25 21:58:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
2023-12-21 13:00:31 +00:00
|
|
|
}
|
2023-12-28 13:43:06 +00:00
|
|
|
|
2024-06-25 21:58:11 +00:00
|
|
|
func (v Validator) doesAddressExist(benefactorID uint) validation.RuleFunc {
|
2023-12-30 19:51:22 +00:00
|
|
|
return func(value interface{}) error {
|
2024-06-25 21:58:11 +00:00
|
|
|
addressID, ok := value.(uint)
|
2024-01-01 07:22:14 +00:00
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
2024-06-25 21:58:11 +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-06-25 21:58:11 +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-06-25 21:58:11 +00:00
|
|
|
|
|
|
|
func (v Validator) isDateValid(value interface{}) error {
|
|
|
|
date, ok := value.(time.Time)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if date.Before(time.Now()) {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
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) doesKindBoxHaveDeliveredStatus(value interface{}) error {
|
|
|
|
kindBoxID, ok := value.(uint)
|
|
|
|
if !ok {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
kindBox, err := v.repo.GetKindBox(context.Background(), kindBoxID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
|
|
}
|
|
|
|
if kindBox.Status != entity.KindBoxDeliveredStatus {
|
|
|
|
return fmt.Errorf(errmsg.ErrorMsgInvalidStatus)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|