From 6b68385ed13d68caa94ad3b8dd1130d764487a40 Mon Sep 17 00:00:00 2001 From: Iman Mirazimi Date: Tue, 21 May 2024 19:53:47 +0330 Subject: [PATCH] fix(niki): add validations for accept, assign and deliver kindboxreq by admin --- pkg/err_msg/message.go | 7 +-- validator/admin/kind_box_req/accept.go | 1 + validator/admin/kind_box_req/validator.go | 52 +++++++++++++++-------- 3 files changed, 40 insertions(+), 20 deletions(-) diff --git a/pkg/err_msg/message.go b/pkg/err_msg/message.go index 63bb951..8e36b46 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -1,7 +1,7 @@ package errmsg const ( - ErrorMsgAdminNotAllowed = "admin is not allowed" + ErrorMsgAdminNotAllowed = "admin is not allowed" ErrorMsgNotFound = "record not found" ErrorMsgSomethingWentWrong = "something went wrong" ErrorMsgInvalidInput = "invalid input" @@ -15,10 +15,11 @@ const ( ErrorMsgOtpCodeIsNotValid = "verification code is not valid" ErrorMsgCantScanQueryResult = "can't scan query result" ErrorMsgPhoneNumberOrPassIsIncorrect = "phone number or password is incorrect" - ErrBadRequest = "Bad request" + ErrBadRequest = "Bad request" ErrorMsgAcceptKindBoxReqStatus = "only pending requests will have the ability to be confirmed" ErrorMsgRejectKindBoxReqStatus = "only pending requests will have the ability to be rejected" 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" + ErrorMsgCountAcceptedOverflow = "count accepted is greather than count requested" ) diff --git a/validator/admin/kind_box_req/accept.go b/validator/admin/kind_box_req/accept.go index 86ea377..55b1332 100644 --- a/validator/admin/kind_box_req/accept.go +++ b/validator/admin/kind_box_req/accept.go @@ -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) diff --git a/validator/admin/kind_box_req/validator.go b/validator/admin/kind_box_req/validator.go index df0a7bc..8ca688f 100644 --- a/validator/admin/kind_box_req/validator.go +++ b/validator/admin/kind_box_req/validator.go @@ -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 -}