fix(niki): add validations for accept, assign and deliver kindboxreq by admin

This commit is contained in:
Iman Mirazimi 2024-05-21 19:53:47 +03:30
parent 948a240282
commit 6b68385ed1
3 changed files with 40 additions and 20 deletions

View File

@ -21,4 +21,5 @@ const (
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"
ErrorMsgAdminIsNotAgent = "admin is not agent"
ErrorMsgCountAcceptedOverflow = "count accepted is greather than count requested"
)

View File

@ -18,6 +18,7 @@ func (v Validator) ValidateAcceptRequest(req param.KindBoxReqAcceptRequest) (map
validation.Field(&req.CountAccepted,
validation.Required,
validation.Min(uint(MinKindBoxReq)), validation.Max(uint(MaxKindBoxReq)),
validation.By(v.checkCountAcceptedMustBeLessThanCountRequested(req.ID)),
),
); err != nil {
fieldErrors := make(map[string]string)

View File

@ -8,6 +8,7 @@ import (
"git.gocasts.ir/ebhomengo/niki/entity"
param "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
validation "github.com/go-ozzo/ozzo-validation/v4"
)
const (
@ -81,7 +82,21 @@ func (v Validator) CheckKindBoxReqStatusForRejecting(value interface{}) error {
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 {
kindboxreqID, ok := value.(uint)
if !ok {
@ -98,12 +113,31 @@ func (v Validator) checkKindBoxReqStatusForAssigningSenderAgent(value interface{
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 {
adminID, ok := value.(uint)
if !ok {
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 {
return err
}
@ -113,19 +147,3 @@ func (v Validator) doesAgentAdminExist(value interface{}) error {
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
}