minor change validation Accept/Reject kind box request

This commit is contained in:
Abolfazl Nourzad 2024-01-25 21:17:10 +03:30
parent 79be4d0a0e
commit 96eb93bb69
Signed by: abolfazl
GPG Key ID: 183534166EB62E0B
3 changed files with 13 additions and 37 deletions

View File

@ -14,6 +14,6 @@ const (
ErrorMsgOtpCodeIsNotValid = "verification code is not valid" ErrorMsgOtpCodeIsNotValid = "verification code is not valid"
ErrorMsgCantScanQueryResult = "can't scan query result" ErrorMsgCantScanQueryResult = "can't scan query result"
ErrorMsgPhoneNumberOrPassIsIncorrect = "phone number or password is incorrect" ErrorMsgPhoneNumberOrPassIsIncorrect = "phone number or password is incorrect"
ErrorMsgReAcceptKindBoxReq = "accepted request cannot be re-accepted" ErrorMsgAcceptKindBoxReqStatus = "only pending requests will have the ability to be confirmed"
ErrorMsgReRejectKindBoxReq = "rejected request cannot be re-rejected" ErrorMsgRejectKindBoxReqStatus = "only pending requests will have the ability to be rejected"
) )

View File

@ -74,37 +74,6 @@ func (d DB) KindBoxRequestExist(id uint) (bool, error) {
return true, nil 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 { func (d DB) RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error {
op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq") op := richerror.Op("mysqlkindboxreq.RejectKindBoxReq")
statement, err := d.conn.Conn(). statement, err := d.conn.Conn().

View File

@ -1,8 +1,10 @@
package adminkindboxreqvalidator package adminkindboxreqvalidator
import ( import (
"context"
"errors" "errors"
"fmt" "fmt"
"git.gocasts.ir/ebhomengo/niki/entity"
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
) )
@ -18,8 +20,7 @@ type Repository interface {
// 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) // PendingStatus(id uint) (bool, error)
CheckKindBoxReqStatusForAccepting(id uint) error GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error)
CheckKindBoxReqStatusForRejecting(id uint) error
} }
type Validator struct { type Validator struct {
@ -79,10 +80,13 @@ func (v Validator) CheckKindBoxReqStatusForAccepting(value interface{}) error {
if !ok { if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
} }
err := v.repo.CheckKindBoxReqStatusForAccepting(kindboxreqID) kindBox, err := v.repo.GetByID(context.Background(), kindboxreqID)
if err != nil { if err != nil {
return err return err
} }
if kindBox.Status != entity.KindBoxReqPendingStatus {
return fmt.Errorf(errmsg.ErrorMsgAcceptKindBoxReqStatus)
}
return nil return nil
} }
@ -92,10 +96,13 @@ func (v Validator) CheckKindBoxReqStatusForRejecting(value interface{}) error {
if !ok { if !ok {
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong) return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
} }
err := v.repo.CheckKindBoxReqStatusForRejecting(kindboxreqID) kindBox, err := v.repo.GetByID(context.Background(), kindboxreqID)
if err != nil { if err != nil {
return err return err
} }
if kindBox.Status != entity.KindBoxReqPendingStatus {
return fmt.Errorf(errmsg.ErrorMsgRejectKindBoxReqStatus)
}
return nil return nil
} }