From 96eb93bb699cb8eac3b0cd96b3aa9c875734797a Mon Sep 17 00:00:00 2001 From: Abolfazl Nourzad Date: Thu, 25 Jan 2024 21:17:10 +0330 Subject: [PATCH] minor change validation Accept/Reject kind box request --- pkg/err_msg/message.go | 4 +-- repository/mysql/kind_box_req/kind_box_req.go | 31 ------------------- validator/admin/kind_box_req/validator.go | 15 ++++++--- 3 files changed, 13 insertions(+), 37 deletions(-) diff --git a/pkg/err_msg/message.go b/pkg/err_msg/message.go index 5d24ce5..4c7c36c 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -14,6 +14,6 @@ const ( ErrorMsgOtpCodeIsNotValid = "verification code is not valid" ErrorMsgCantScanQueryResult = "can't scan query result" ErrorMsgPhoneNumberOrPassIsIncorrect = "phone number or password is incorrect" - ErrorMsgReAcceptKindBoxReq = "accepted request cannot be re-accepted" - ErrorMsgReRejectKindBoxReq = "rejected request cannot be re-rejected" + ErrorMsgAcceptKindBoxReqStatus = "only pending requests will have the ability to be confirmed" + ErrorMsgRejectKindBoxReqStatus = "only pending requests will have the ability to be rejected" ) diff --git a/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index 02aca25..0a86519 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -74,37 +74,6 @@ func (d DB) KindBoxRequestExist(id uint) (bool, error) { return true, nil } -func (d DB) CheckKindBoxReqStatusForAccepting(id uint) error { - op := richerror.Op("mysqlkindboxreq.CheckKindBoxReqStatusForAccepting") - - k, err := d.GetByID(context.Background(), id) - if err != nil { - return richerror.New(op).WithErr(err) - } - if k.Status == entity.KindBoxReqAcceptedStatus { - return richerror.New(op).WithMessage(errmsg.ErrorMsgReAcceptKindBoxReq).WithMeta(map[string]interface{}{ - "id": id, - }).WithKind(richerror.KindInvalid) - } - - return nil -} - -func (d DB) CheckKindBoxReqStatusForRejecting(id uint) error { - op := richerror.Op("mysqlkindboxreq.CheckKindBoxReqStatusForRejecting") - k, err := d.GetByID(context.Background(), id) - if err != nil { - return richerror.New(op).WithErr(err) - } - if k.Status == entity.KindBoxReqRejectedStatus { - return richerror.New(op).WithMessage(errmsg.ErrorMsgReRejectKindBoxReq).WithMeta(map[string]interface{}{ - "id": id, - }).WithKind(richerror.KindInvalid) - } - - return nil -} - func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error { op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq") statement, err := d.conn.Conn(). diff --git a/validator/admin/kind_box_req/validator.go b/validator/admin/kind_box_req/validator.go index 75d54db..c208c1d 100644 --- a/validator/admin/kind_box_req/validator.go +++ b/validator/admin/kind_box_req/validator.go @@ -1,8 +1,10 @@ package adminkindboxreqvalidator import ( + "context" "errors" "fmt" + "git.gocasts.ir/ebhomengo/niki/entity" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" ) @@ -18,8 +20,7 @@ type Repository interface { // TypeExist(id int) (bool, error) // KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error) // PendingStatus(id uint) (bool, error) - CheckKindBoxReqStatusForAccepting(id uint) error - CheckKindBoxReqStatusForRejecting(id uint) error + GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error) } type Validator struct { @@ -79,10 +80,13 @@ func (v Validator) CheckKindBoxReqStatusForAccepting(value interface{}) error { if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } - err := v.repo.CheckKindBoxReqStatusForAccepting(kindboxreqID) + kindBox, err := v.repo.GetByID(context.Background(), kindboxreqID) if err != nil { return err } + if kindBox.Status != entity.KindBoxReqPendingStatus { + return fmt.Errorf(errmsg.ErrorMsgAcceptKindBoxReqStatus) + } return nil } @@ -92,10 +96,13 @@ func (v Validator) CheckKindBoxReqStatusForRejecting(value interface{}) error { if !ok { return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) } - err := v.repo.CheckKindBoxReqStatusForRejecting(kindboxreqID) + kindBox, err := v.repo.GetByID(context.Background(), kindboxreqID) if err != nil { return err } + if kindBox.Status != entity.KindBoxReqPendingStatus { + return fmt.Errorf(errmsg.ErrorMsgRejectKindBoxReqStatus) + } return nil }