forked from ebhomengo/niki
113 lines
2.6 KiB
Go
113 lines
2.6 KiB
Go
package adminvalidator
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
|
)
|
|
|
|
const (
|
|
phoneNumberRegex = "^09\\d{9}$"
|
|
)
|
|
|
|
type Repository interface {
|
|
AdminExistByPhoneNumber(ctx context.Context, phoneNumber string) (bool, error)
|
|
AdminExistByEmail(ctx context.Context, email string) (bool, error)
|
|
}
|
|
type Validator struct {
|
|
repo Repository
|
|
}
|
|
|
|
func New(repo Repository) Validator {
|
|
return Validator{repo: repo}
|
|
}
|
|
|
|
func (v Validator) doesAdminExistByPhoneNumber(value interface{}) error {
|
|
phoneNumber, ok := value.(string)
|
|
if !ok {
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
}
|
|
adminExisted, err := v.repo.AdminExistByPhoneNumber(context.Background(), phoneNumber)
|
|
if err != nil {
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
}
|
|
if !adminExisted {
|
|
return fmt.Errorf(errmsg.ErrorMsgPhoneNumberOrPassIsIncorrect)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (v Validator) IsPhoneNumberUnique(value interface{}) error {
|
|
phoneNumber, ok := value.(*string)
|
|
if !ok {
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
}
|
|
adminExisted, err := v.repo.AdminExistByPhoneNumber(context.Background(), *phoneNumber)
|
|
if err != nil {
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
}
|
|
if adminExisted {
|
|
return fmt.Errorf(errmsg.ErrorMsgPhoneNumberIsNotUnique)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (v Validator) doesAdminExistByEmail(value interface{}) error {
|
|
email, ok := value.(*string)
|
|
if !ok {
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
}
|
|
adminExisted, err := v.repo.AdminExistByEmail(context.Background(), *email)
|
|
if err != nil {
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
}
|
|
if adminExisted {
|
|
return fmt.Errorf(errmsg.ErrorMsgPhoneNumberIsNotUnique)
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (v Validator) IsRoleValid(value interface{}) error {
|
|
role, ok := value.(*entity.AdminRole)
|
|
if !ok {
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
}
|
|
|
|
if isValid := role.IsValid(); isValid != true {
|
|
return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (v Validator) IsGenderValid(value interface{}) error {
|
|
gender, ok := value.(*entity.Gender)
|
|
if gender == nil {
|
|
return nil
|
|
}
|
|
if !ok {
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
}
|
|
|
|
if isValid := gender.IsValid(); isValid != true {
|
|
return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (v Validator) IsStatusValid(value interface{}) error {
|
|
status, ok := value.(*entity.AdminStatus)
|
|
if !ok {
|
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
}
|
|
|
|
if isValid := status.IsValid(); isValid != true {
|
|
return fmt.Errorf(errmsg.ErrorMsgInvalidInput)
|
|
}
|
|
|
|
return nil
|
|
}
|