forked from ebhomengo/niki
fix(niki): add validations for accept, assign and deliver kindboxreq by admin
This commit is contained in:
parent
948a240282
commit
6b68385ed1
|
@ -21,4 +21,5 @@ const (
|
||||||
ErrorMsgAssignSenderAgentKindBoxReqStatus = "only accepted kind_box_reqs will have the ability to be assign sender agent"
|
ErrorMsgAssignSenderAgentKindBoxReqStatus = "only accepted kind_box_reqs will have the ability to be assign sender agent"
|
||||||
ErrorMsgDeliverKindBoxReqStatus = "only assigned requests will have the ability to be delivered"
|
ErrorMsgDeliverKindBoxReqStatus = "only assigned requests will have the ability to be delivered"
|
||||||
ErrorMsgAdminIsNotAgent = "admin is not agent"
|
ErrorMsgAdminIsNotAgent = "admin is not agent"
|
||||||
|
ErrorMsgCountAcceptedOverflow = "count accepted is greather than count requested"
|
||||||
)
|
)
|
||||||
|
|
|
@ -18,6 +18,7 @@ func (v Validator) ValidateAcceptRequest(req param.KindBoxReqAcceptRequest) (map
|
||||||
validation.Field(&req.CountAccepted,
|
validation.Field(&req.CountAccepted,
|
||||||
validation.Required,
|
validation.Required,
|
||||||
validation.Min(uint(MinKindBoxReq)), validation.Max(uint(MaxKindBoxReq)),
|
validation.Min(uint(MinKindBoxReq)), validation.Max(uint(MaxKindBoxReq)),
|
||||||
|
validation.By(v.checkCountAcceptedMustBeLessThanCountRequested(req.ID)),
|
||||||
),
|
),
|
||||||
); err != nil {
|
); err != nil {
|
||||||
fieldErrors := make(map[string]string)
|
fieldErrors := make(map[string]string)
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
|
param "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
|
||||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||||
|
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -81,7 +82,21 @@ func (v Validator) CheckKindBoxReqStatusForRejecting(value interface{}) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
func (v Validator) checkKindBoxReqStatusForDelivering(value interface{}) error {
|
||||||
|
kindboxreqID, ok := value.(uint)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
||||||
|
}
|
||||||
|
kindBoxReq, err := v.repo.GetByID(context.Background(), kindboxreqID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if kindBoxReq.Status != entity.KindBoxReqAssignedSenderAgentStatus {
|
||||||
|
return fmt.Errorf(errmsg.ErrorMsgDeliverKindBoxReqStatus)
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
func (v Validator) checkKindBoxReqStatusForAssigningSenderAgent(value interface{}) error {
|
func (v Validator) checkKindBoxReqStatusForAssigningSenderAgent(value interface{}) error {
|
||||||
kindboxreqID, ok := value.(uint)
|
kindboxreqID, ok := value.(uint)
|
||||||
if !ok {
|
if !ok {
|
||||||
|
@ -98,12 +113,31 @@ func (v Validator) checkKindBoxReqStatusForAssigningSenderAgent(value interface{
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (v Validator) checkCountAcceptedMustBeLessThanCountRequested(kindboxreqID uint) validation.RuleFunc {
|
||||||
|
return func(value interface{}) error {
|
||||||
|
countAccepted, ok := value.(uint)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
||||||
|
}
|
||||||
|
kindBoxReq, err := v.repo.GetByID(context.Background(), kindboxreqID)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if kindBoxReq.CountRequested < countAccepted {
|
||||||
|
return fmt.Errorf(errmsg.ErrorMsgCountAcceptedOverflow)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (v Validator) doesAgentAdminExist(value interface{}) error {
|
func (v Validator) doesAgentAdminExist(value interface{}) error {
|
||||||
adminID, ok := value.(uint)
|
adminID, ok := value.(uint)
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
||||||
}
|
}
|
||||||
resp, err := v.adminSvc.AdminExistByID(context.Background(), param.AdminExistByIDRequest{AdminID: adminID})
|
resp, err := v.adminSvc.AdminExistByID(context.Background(), param.AdminExistByIDRequest{
|
||||||
|
AdminID: adminID,
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
@ -113,19 +147,3 @@ func (v Validator) doesAgentAdminExist(value interface{}) error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (v Validator) checkKindBoxReqStatusForDelivering(value interface{}) error {
|
|
||||||
kindboxreqID, ok := value.(uint)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
|
||||||
}
|
|
||||||
kindBoxReq, err := v.repo.GetByID(context.Background(), kindboxreqID)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
if kindBoxReq.Status != entity.KindBoxReqAssignedSenderAgentStatus {
|
|
||||||
return fmt.Errorf(errmsg.ErrorMsgDeliverKindBoxReqStatus)
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in New Issue