feat(niki): remove validation from service and add all validation to validation directory

This commit is contained in:
miaad shahrokhi 2023-12-28 17:13:06 +03:30
parent 4697645e64
commit 4a5cc910db
31 changed files with 171 additions and 138 deletions

View File

@ -6,7 +6,7 @@ const (
KindBoxPendingSendStatus KindBoxStatus = iota + 1 KindBoxPendingSendStatus KindBoxStatus = iota + 1
KindBoxSentStatus KindBoxSentStatus
KindBoxPendingReceivedStatus KindBoxPendingReceivedStatus
KindBoxRecievedStatus KindBoxReceivedStatus
KindBoxEnumeratedStatus KindBoxEnumeratedStatus
) )
@ -14,7 +14,7 @@ const (
kindBoxPendingSendStatus = "pending-send" kindBoxPendingSendStatus = "pending-send"
kindBoxSentStatus = "sent" kindBoxSentStatus = "sent"
kindBoxPendingReceivedStatus = "pending-received" kindBoxPendingReceivedStatus = "pending-received"
kindBoxRecievedStatus = "received" kindBoxReceivedStatus = "received"
kindBoxEnumeratedStatus = "enumerated" kindBoxEnumeratedStatus = "enumerated"
) )
@ -26,8 +26,8 @@ func (s KindBoxStatus) String() string {
return kindBoxSentStatus return kindBoxSentStatus
case KindBoxPendingReceivedStatus: case KindBoxPendingReceivedStatus:
return kindBoxPendingReceivedStatus return kindBoxPendingReceivedStatus
case KindBoxRecievedStatus: case KindBoxReceivedStatus:
return kindBoxRecievedStatus return kindBoxReceivedStatus
case KindBoxEnumeratedStatus: case KindBoxEnumeratedStatus:
return kindBoxEnumeratedStatus return kindBoxEnumeratedStatus
} }
@ -43,11 +43,63 @@ func MapToKindBoxStatus(statusStr string) KindBoxStatus {
return KindBoxSentStatus return KindBoxSentStatus
case kindBoxPendingReceivedStatus: case kindBoxPendingReceivedStatus:
return KindBoxPendingReceivedStatus return KindBoxPendingReceivedStatus
case kindBoxRecievedStatus: case kindBoxReceivedStatus:
return KindBoxRecievedStatus return KindBoxReceivedStatus
case kindBoxEnumeratedStatus: case kindBoxEnumeratedStatus:
return KindBoxEnumeratedStatus return KindBoxEnumeratedStatus
} }
return KindBoxStatus(0) return KindBoxStatus(0)
} }
package entity
type KindBoxStatus uint
const (
KindBoxPendingSendStatus KindBoxStatus = iota + 1
KindBoxSentStatus
KindBoxPendingReceivedStatus
KindBoxReceivedStatus
KindBoxEnumeratedStatus
)
var kindBoxStatusStrings = map[KindBoxStatus]string{
KindBoxPendingSendStatus: "pending-send",
KindBoxSentStatus: "sent",
KindBoxPendingReceivedStatus: "pending-received",
KindBoxReceivedStatus: "received",
KindBoxEnumeratedStatus: "enumerated",
}
func (s KindBoxStatus) String() string {
return kindBoxStatusStrings[s]
}
// AllKindBoxStatus returns a slice containing all string values of KindBoxStatus.
func AllKindBoxStatus() []string {
statusStrings := make([]string, len(kindBoxStatusStrings))
for status, str := range kindBoxStatusStrings {
statusStrings[int(status)-1] = str
}
return statusStrings
}
// MapToKindBoxStatus converts a string to the corresponding KindBoxStatus value.
func MapToKindBoxStatus(statusStr string) KindBoxStatus {
for status, str := range kindBoxStatusStrings {
if str == statusStr {
return status
}
}
return KindBoxStatus(0)
}
// AllKindBoxStatus returns a slice containing all string values of KindBoxStatus.
func AllKindBoxStatus() []string {
statusStrings := make([]string, len(kindBoxStatusStrings))
for status, str := range kindBoxStatusStrings {
statusStrings[int(status)-1] = str
}
return statusStrings
}

View File

@ -11,15 +11,6 @@ import (
func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.KindBoxAddResponse, error) { func (s Service) Add(ctx context.Context, req param.KindBoxAddRequest) (param.KindBoxAddResponse, error) {
const op = "adminkindboxservice.Add" const op = "adminkindboxservice.Add"
// TODO: check validation
// exist, err := s.benefactorService.IsBenefactorExist(ctx, req.BenefactorID)
// if err != nil {
// return param.KindBoxAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
//}
//if !exist {
// return param.KindBoxAddResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotFound).WithKind(richerror.KindInvalid)
//}
kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{ kindBox, err := s.repo.AddKindBox(ctx, entity.KindBox{
BenefactorID: req.BenefactorID, BenefactorID: req.BenefactorID,
KindBoxReqID: req.KindBoxReqID, KindBoxReqID: req.KindBoxReqID,

View File

@ -3,9 +3,7 @@ package adminkindboxservice
import ( import (
"context" "context"
entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
@ -13,17 +11,6 @@ func (s Service) Delete(ctx context.Context, req param.KindBoxDeleteRequest) (pa
// TODO: Does business domain need to delete an kindbox ? // TODO: Does business domain need to delete an kindbox ?
const op = "adminkindboxservice.Delete" const op = "adminkindboxservice.Delete"
kb, gErr := s.repo.GetKindBox(ctx, req.KindBoxID)
if gErr != nil {
return param.KindBoxDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
}
if kb.Status != entity.KindBoxPendingSendStatus {
return param.KindBoxDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
if kb.BenefactorID != req.BenefactorID {
return param.KindBoxDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
dErr := s.repo.DeleteKindBox(ctx, req.KindBoxID) dErr := s.repo.DeleteKindBox(ctx, req.KindBoxID)
if dErr != nil { if dErr != nil {
return param.KindBoxDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected) return param.KindBoxDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected)

View File

@ -15,10 +15,6 @@ func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.Ki
if err != nil { if err != nil {
return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
} }
// TODO : ref to service.Update()
if kindBox.BenefactorID != req.BenefactorID {
return param.KindBoxGetResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
return param.KindBoxGetResponse{KindBox: kindBox}, nil return param.KindBoxGetResponse{KindBox: kindBox}, nil
} }

View File

@ -13,17 +13,8 @@ func (s Service) Update(ctx context.Context, req param.KindBoxUpdateRequest) (pa
// TODO: can benefactor update its Request ? // TODO: can benefactor update its Request ?
// TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ? // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ?
// TODO: updating data(s) may have side-effect on other entities by masood-keshvary accepted -> rejected // TODO: updating data(s) may have side-effect on other entities by masood-keshvary accepted -> rejected
const op = "adminkindboxservice.Update" const op = "adminkindboxservice.Update"
kb, gErr := s.repo.GetKindBox(ctx, req.KindBoxID)
if gErr != nil {
return param.KindBoxUpdateResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
}
if kb.BenefactorID != req.BenefactorID {
return param.KindBoxUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
kindBox, uErr := s.repo.UpdateKindBox(ctx, req.KindBoxID, entity.KindBox{ kindBox, uErr := s.repo.UpdateKindBox(ctx, req.KindBoxID, entity.KindBox{
TotalAmount: req.TotalAmount, TotalAmount: req.TotalAmount,
ReceiverID: req.ReceiverID, ReceiverID: req.ReceiverID,

View File

@ -11,15 +11,6 @@ import (
func (s Service) Add(ctx context.Context, req param.KindBoxReqAddRequest) (param.KindBoxReqAddResponse, error) { func (s Service) Add(ctx context.Context, req param.KindBoxReqAddRequest) (param.KindBoxReqAddResponse, error) {
const op = "adminkindboxreqservice.Add" const op = "adminkindboxreqservice.Add"
// TODO: check validation
// exist, err := s.benefactorService.IsBenefactorExist(ctx, req.BenefactorID)
// if err != nil {
// return param.KindBoxReqAddResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
//}
// if !exist {
// return param.KindBoxReqAddResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotFound).WithKind(richerror.KindInvalid)
//}
kindBoxReq, err := s.repo.AddKindBoxReq(ctx, entity.KindBoxReq{ kindBoxReq, err := s.repo.AddKindBoxReq(ctx, entity.KindBoxReq{
BenefactorID: req.BenefactorID, BenefactorID: req.BenefactorID,
TypeID: req.TypeID, TypeID: req.TypeID,

View File

@ -3,9 +3,7 @@ package adminkindboxreqservice
import ( import (
"context" "context"
entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
@ -13,17 +11,6 @@ func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest)
// TODO: Does business domain need to delete an kindboxreq ? // TODO: Does business domain need to delete an kindboxreq ?
const op = "adminkindboxreqservice.Delete" const op = "adminkindboxreqservice.Delete"
kbr, gErr := s.repo.GetKindBoxReq(ctx, req.KindBoxReqID)
if gErr != nil {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
}
if kbr.BenefactorID != req.BenefactorID {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
if kbr.Status != entity.KindBoxReqPendingStatus {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID) dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID)
if dErr != nil { if dErr != nil {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected) return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected)

View File

@ -4,7 +4,6 @@ import (
"context" "context"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
@ -16,9 +15,6 @@ func (s Service) Get(ctx context.Context, req param.KindBoxReqGetRequest) (param
if err != nil { if err != nil {
return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
} }
if kindBoxReq.BenefactorID != req.BenefactorID {
return param.KindBoxReqGetResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
return param.KindBoxReqGetResponse{KindBoxReq: kindBoxReq}, nil return param.KindBoxReqGetResponse{KindBoxReq: kindBoxReq}, nil
} }

View File

@ -15,10 +15,7 @@ func (s Service) Get(ctx context.Context, req param.KindBoxGetRequest) (param.Ki
if err != nil { if err != nil {
return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) return param.KindBoxGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
} }
// TODO : ref to service.Update()
if kindBox.BenefactorID != req.BenefactorID {
return param.KindBoxGetResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
return param.KindBoxGetResponse{KindBox: kindBox}, nil return param.KindBoxGetResponse{KindBox: kindBox}, nil
} }

View File

@ -3,9 +3,7 @@ package userkindboxreqservice
import ( import (
"context" "context"
entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
@ -13,17 +11,6 @@ func (s Service) Delete(ctx context.Context, req param.KindBoxReqDeleteRequest)
// TODO: Does business domain need to delete an kindboxreq ? // TODO: Does business domain need to delete an kindboxreq ?
const op = "userkindboxreqservice.Delete" const op = "userkindboxreqservice.Delete"
kbr, gErr := s.repo.GetKindBoxReq(ctx, req.KindBoxReqID)
if gErr != nil {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
}
if kbr.BenefactorID != req.BenefactorID {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
if kbr.Status != entity.KindBoxReqPendingStatus {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID) dErr := s.repo.DeleteKindBoxReq(ctx, req.KindBoxReqID)
if dErr != nil { if dErr != nil {
return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected) return param.KindBoxReqDeleteResponse{}, richerror.New(op).WithErr(dErr).WithKind(richerror.KindUnexpected)

View File

@ -4,7 +4,6 @@ import (
"context" "context"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
@ -15,10 +14,6 @@ func (s Service) Get(ctx context.Context, req param.KindBoxReqGetRequest) (param
if err != nil { if err != nil {
return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) return param.KindBoxReqGetResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
} }
// TODO : ref to service.Update()
if kindBoxReq.BenefactorID != req.BenefactorID {
return param.KindBoxReqGetResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
return param.KindBoxReqGetResponse{KindBoxReq: kindBoxReq}, nil return param.KindBoxReqGetResponse{KindBoxReq: kindBoxReq}, nil
} }

View File

@ -5,7 +5,6 @@ import (
entity "git.gocasts.ir/ebhomengo/niki/entity" entity "git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req" param "git.gocasts.ir/ebhomengo/niki/param/benefactor/kind_box_req"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
) )
@ -14,17 +13,6 @@ func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest)
// TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ? // TODO: Is Update Mothod Service Responsible to check which kindboxreqID belongs to benefactorID ?
const op = "userkindboxreqservice.Update" const op = "userkindboxreqservice.Update"
kbr, gErr := s.repo.GetKindBoxReq(ctx, req.KindBoxReqID)
if gErr != nil {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithErr(gErr).WithKind(richerror.KindUnexpected)
}
if kbr.BenefactorID != req.BenefactorID {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgUserNotAllowed).WithKind(richerror.KindForbidden)
}
if kbr.Status != entity.KindBoxReqPendingStatus {
return param.KindBoxReqUpdateResponse{}, richerror.New(op).WithMessage(errmsg.ErrorMsgInvalidStatus).WithKind(richerror.KindInvalid)
}
kindBoxReq, uErr := s.repo.UpdateKindBoxReq(ctx, req.KindBoxReqID, entity.KindBoxReq{ kindBoxReq, uErr := s.repo.UpdateKindBoxReq(ctx, req.KindBoxReqID, entity.KindBoxReq{
BenefactorID: req.BenefactorID, BenefactorID: req.BenefactorID,
TypeID: req.TypeID, TypeID: req.TypeID,

View File

@ -4,6 +4,7 @@ import (
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"github.com/go-ozzo/ozzo-validation/is"
validation "github.com/go-ozzo/ozzo-validation/v4" validation "github.com/go-ozzo/ozzo-validation/v4"
) )
@ -11,18 +12,18 @@ func (v Validator) ValidateAdminAddRequest(req param.KindBoxAddRequest) (map[str
const op = "adminkindbox.KindBoxAddRequest" const op = "adminkindbox.KindBoxAddRequest"
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.SerialNumber, validation.Required), validation.Field(&req.SerialNumber, validation.Required, is.Alphanumeric),
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(&req.BenefactorID))), validation.By(v.doesBenefactorExist(req.BenefactorID))),
validation.Field(&req.SenderID, validation.Field(&req.SenderID,
validation.Required, validation.Required,
validation.By(v.doesEmployeeExist(&req.SenderID))), validation.By(v.doesEmployeeExist(req.SenderID))),
validation.Field(&req.KindBoxReqID, validation.Field(&req.KindBoxReqID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))), validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -13,11 +13,13 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxDeleteRequest) (map[st
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(&req.BenefactorID))), validation.By(v.doesBenefactorExist(req.BenefactorID))),
validation.Field(&req.KindBoxID, validation.Field(&req.KindBoxID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxExist(&req.KindBoxID))), validation.By(v.hasPendingStatus(req.KindBoxID)),
validation.By(v.doesKindBoxExist(req.KindBoxID)),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -17,6 +17,7 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[stri
validation.Field(&req.KindBoxID, validation.Field(&req.KindBoxID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID)),
validation.By(v.doesKindBoxExist(&req.KindBoxID))), validation.By(v.doesKindBoxExist(&req.KindBoxID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -1,9 +1,11 @@
package adminkindbox package adminkindbox
import ( import (
"git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
"github.com/go-ozzo/ozzo-validation/is"
validation "github.com/go-ozzo/ozzo-validation/v4" validation "github.com/go-ozzo/ozzo-validation/v4"
) )
@ -13,13 +15,14 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[st
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(&req.BenefactorID))), validation.By(v.doesBenefactorExist(req.BenefactorID))),
validation.Field(&req.KindBoxID, validation.Field(&req.KindBoxID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxExist(req.BenefactorID)),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID))), validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID))),
validation.Field(&req.SerialNumber, validation.EnsureString), validation.Field(&req.SerialNumber, is.Alphanumeric),
validation.Field(&req.TotalAmount, validation.Min(0)), validation.Field(&req.TotalAmount, validation.Min(0)),
@ -30,7 +33,7 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxUpdateRequest) (map[st
validation.By(v.doesEmployeeExist(req.ReceiverID))), validation.By(v.doesEmployeeExist(req.ReceiverID))),
validation.Field(&req.Status, validation.Field(&req.Status,
validation.By(v.hasCorrectStatus(req.Status.String))), validation.In(entity.AllKindBoxStatus())),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -12,6 +12,7 @@ type Repository interface {
BenefactorExist(id uint) (bool, error) BenefactorExist(id uint) (bool, error)
KindBoxExist(id uint) (bool, error) KindBoxExist(id uint) (bool, error)
KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error) KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error)
PendingStatus(id uint) (bool, error)
CheckStatus(status string) (bool, error) CheckStatus(status string) (bool, error)
} }
@ -83,3 +84,13 @@ func (v Validator) hasCorrectStatus(value interface{}) error {
return nil return nil
} }
func (v Validator) hasPendingStatus(value interface{}) error {
kindboxID := value.(uint)
_, err := v.repo.PendingStatus(kindboxID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}

View File

@ -16,11 +16,11 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist)), validation.By(v.doesBenefactorExist(req.BenefactorID))),
validation.Field(&req.TypeID, validation.Field(&req.TypeID,
validation.Required, validation.Required,
validation.By(v.doesTypeExist)), validation.By(v.doesTypeExist(req.TypeID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -17,7 +17,9 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map
validation.Field(&req.KindBoxReqID, validation.Field(&req.KindBoxReqID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID))), validation.By(v.hasPendingStatus(req.KindBoxReqID)),
validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -17,7 +17,8 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxReqGetRequest) (map[s
validation.Field(&req.KindBoxReqID, validation.Field(&req.KindBoxReqID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID))), validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -19,7 +19,9 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map
validation.Field(&req.KindBoxReqID, validation.Field(&req.KindBoxReqID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxBelongToBenefactor(req.KindBoxReqID, req.BenefactorID))), validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)),
validation.By(v.hasPendingStatus(req.KindBoxReqID)),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))),
validation.Field(&req.TypeID, validation.Field(&req.TypeID,
validation.By(v.doesTypeExist(req.TypeID))), validation.By(v.doesTypeExist(req.TypeID))),

View File

@ -16,6 +16,7 @@ type Repository interface {
KindBoxRequestExist(id int) (bool, error) KindBoxRequestExist(id int) (bool, error)
TypeExist(id int) (bool, error) TypeExist(id int) (bool, error)
KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error) KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error)
PendingStatus(id uint) (bool, error)
} }
type Validator struct { type Validator struct {
@ -66,3 +67,13 @@ func (v Validator) doesTypeExist(value interface{}) error {
return nil return nil
} }
func (v Validator) hasPendingStatus(value interface{}) error {
kindboxID := value.(uint)
_, err := v.repo.PendingStatus(kindboxID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}

View File

@ -13,11 +13,12 @@ func (v Validator) ValidateGetByIDRequest(req param.KindBoxGetRequest) (map[stri
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(&req.BenefactorID))), validation.By(v.doesBenefactorExist(req.BenefactorID))),
validation.Field(&req.KindBoxID, validation.Field(&req.KindBoxID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxExist(&req.KindBoxID))), validation.By(v.doesKindBoxExist(req.KindBoxID)),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -13,7 +13,7 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxGetAllRequest) (map[st
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(&req.BenefactorID))), validation.By(v.doesBenefactorExist(req.BenefactorID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -7,8 +7,9 @@ import (
) )
type Repository interface { type Repository interface {
BenefactorExist(id int) (bool, error) BenefactorExist(id uint) (bool, error)
KindBoxExist(id int) (bool, error) KindBoxExist(id uint) (bool, error)
KindBoxBelongToBenefactor(bId uint, kbId uint) (bool, error)
} }
type Validator struct { type Validator struct {
@ -20,7 +21,7 @@ func New(repo Repository) Validator {
} }
func (v Validator) doesBenefactorExist(value interface{}) error { func (v Validator) doesBenefactorExist(value interface{}) error {
benefactorID := value.(int) benefactorID := value.(uint)
_, err := v.repo.BenefactorExist(benefactorID) _, err := v.repo.BenefactorExist(benefactorID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -30,7 +31,7 @@ func (v Validator) doesBenefactorExist(value interface{}) error {
} }
func (v Validator) doesKindBoxExist(value interface{}) error { func (v Validator) doesKindBoxExist(value interface{}) error {
kindBoxID := value.(int) kindBoxID := value.(uint)
_, err := v.repo.KindBoxExist(kindBoxID) _, err := v.repo.KindBoxExist(kindBoxID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -38,3 +39,14 @@ func (v Validator) doesKindBoxExist(value interface{}) error {
return nil return nil
} }
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID interface{}, kindBoxID interface{}) error {
kbId := kindBoxID.(uint)
bId := benefactorID.(uint)
_, err := v.repo.KindBoxBelongToBenefactor(bId, kbId)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}

View File

@ -16,11 +16,11 @@ func (v Validator) ValidateAddRequest(req param.KindBoxReqAddRequest) (map[strin
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(&req.BenefactorID))), validation.By(v.doesBenefactorExist(req.BenefactorID))),
validation.Field(&req.TypeID, validation.Field(&req.TypeID,
validation.Required, validation.Required,
validation.By(v.doesTypeExist(&req.TypeID))), validation.By(v.doesTypeExist(req.TypeID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -13,11 +13,13 @@ func (v Validator) ValidateDeleteRequest(req param.KindBoxReqDeleteRequest) (map
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(&req.BenefactorID))), validation.By(v.doesBenefactorExist(req.BenefactorID))),
validation.Field(&req.KindBoxReqID, validation.Field(&req.KindBoxReqID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))), validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)),
validation.By(v.hasPendingStatus(req.KindBoxReqID)),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -13,11 +13,12 @@ func (v Validator) ValidateGetRequest(req param.KindBoxReqGetRequest) (map[strin
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(&req.BenefactorID))), validation.By(v.doesBenefactorExist(req.BenefactorID))),
validation.Field(&req.KindBoxReqID, validation.Field(&req.KindBoxReqID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))), validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -13,7 +13,7 @@ func (v Validator) ValidateGetAllRequest(req param.KindBoxReqGetAllRequest) (map
if err := validation.ValidateStruct(&req, if err := validation.ValidateStruct(&req,
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(&req.BenefactorID))), validation.By(v.doesBenefactorExist(req.BenefactorID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -15,15 +15,17 @@ func (v Validator) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map
validation.Field(&req.BenefactorID, validation.Field(&req.BenefactorID,
validation.Required, validation.Required,
validation.By(v.doesBenefactorExist(&req.BenefactorID))), validation.By(v.doesBenefactorExist(req.BenefactorID))),
validation.Field(&req.KindBoxReqID, validation.Field(&req.KindBoxReqID,
validation.Required, validation.Required,
validation.By(v.doesKindBoxRequestExist(&req.KindBoxReqID))), validation.By(v.doesKindBoxRequestExist(req.KindBoxReqID)),
validation.By(v.hasPendingStatus(req.KindBoxReqID)),
validation.By(v.doesKindBoxBelongToBenefactor(req.BenefactorID, req.KindBoxReqID))),
validation.Field(&req.TypeID, validation.Field(&req.TypeID,
validation.Required, validation.Required,
validation.By(v.doesTypeExist(&req.TypeID))), validation.By(v.doesTypeExist(req.TypeID))),
); err != nil { ); err != nil {
fieldErrors := make(map[string]string) fieldErrors := make(map[string]string)

View File

@ -12,9 +12,11 @@ const (
) )
type Repository interface { type Repository interface {
BenefactorExist(id int) (bool, error) BenefactorExist(id uint) (bool, error)
KindBoxReqExist(id int) (bool, error) KindBoxReqExist(id uint) (bool, error)
TypeExist(id int) (bool, error) TypeExist(id uint) (bool, error)
KindBoxBelongToBenefactor(bId uint, kbId uint) (bool, error)
PendingStatus(id uint) (bool, error)
} }
type Validator struct { type Validator struct {
@ -26,7 +28,7 @@ func New(repo Repository) Validator {
} }
func (v Validator) doesBenefactorExist(value interface{}) error { func (v Validator) doesBenefactorExist(value interface{}) error {
benefactorID := value.(int) benefactorID := value.(uint)
_, err := v.repo.BenefactorExist(benefactorID) _, err := v.repo.BenefactorExist(benefactorID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -36,7 +38,7 @@ func (v Validator) doesBenefactorExist(value interface{}) error {
} }
func (v Validator) doesTypeExist(value interface{}) error { func (v Validator) doesTypeExist(value interface{}) error {
typeID := value.(int) typeID := value.(uint)
_, err := v.repo.TypeExist(typeID) _, err := v.repo.TypeExist(typeID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -46,7 +48,7 @@ func (v Validator) doesTypeExist(value interface{}) error {
} }
func (v Validator) doesKindBoxRequestExist(value interface{}) error { func (v Validator) doesKindBoxRequestExist(value interface{}) error {
kindBoxReqID := value.(int) kindBoxReqID := value.(uint)
_, err := v.repo.KindBoxReqExist(kindBoxReqID) _, err := v.repo.KindBoxReqExist(kindBoxReqID)
if err != nil { if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound) return fmt.Errorf(errmsg.ErrorMsgNotFound)
@ -54,3 +56,24 @@ func (v Validator) doesKindBoxRequestExist(value interface{}) error {
return nil return nil
} }
func (v Validator) doesKindBoxBelongToBenefactor(benefactorID interface{}, kindBoxID interface{}) error {
kbId := kindBoxID.(uint)
bId := benefactorID.(uint)
_, err := v.repo.KindBoxBelongToBenefactor(bId, kbId)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}
func (v Validator) hasPendingStatus(value interface{}) error {
kindboxID := value.(uint)
_, err := v.repo.PendingStatus(kindboxID)
if err != nil {
return fmt.Errorf(errmsg.ErrorMsgNotFound)
}
return nil
}