forked from ebhomengo/niki
rejecting kind-box-request
This commit is contained in:
parent
5708f0c414
commit
1823191c4e
|
@ -0,0 +1,40 @@
|
|||
package adminkindboxreqhandler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
|
||||
"github.com/labstack/echo/v4"
|
||||
)
|
||||
|
||||
func (h Handler) Reject(c echo.Context) error {
|
||||
var req param.KindBoxReqRejectRequest
|
||||
if bErr := c.Bind(&req); bErr != nil {
|
||||
return echo.NewHTTPError(http.StatusBadRequest)
|
||||
}
|
||||
num, cErr := strconv.ParseUint(c.Param("id"), 10, 64)
|
||||
if cErr != nil {
|
||||
return c.JSON(http.StatusBadRequest, errmsg.ErrorMsgInvalidInput)
|
||||
}
|
||||
req.ID = uint(num)
|
||||
if fieldErrors, err := h.adminKindBoxReqVld.ValidateRejectRequest(req); err != nil {
|
||||
msg, code := httpmsg.Error(err)
|
||||
|
||||
return c.JSON(code, echo.Map{
|
||||
"message": msg,
|
||||
"errors": fieldErrors,
|
||||
})
|
||||
}
|
||||
|
||||
resp, sErr := h.adminKindBoxReqSvc.Reject(c.Request().Context(), req)
|
||||
if sErr != nil {
|
||||
msg, code := httpmsg.Error(sErr)
|
||||
|
||||
return echo.NewHTTPError(code, msg)
|
||||
}
|
||||
|
||||
return c.JSON(http.StatusCreated, resp)
|
||||
}
|
|
@ -14,4 +14,5 @@ func (h Handler) SetRoutes(e *echo.Echo) {
|
|||
//nolint:gocritic
|
||||
//r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq"
|
||||
r.PATCH("/accept-kind-box-req/:id", h.Accept)
|
||||
r.PATCH("/reject-kind-box-req/:id", h.Reject)
|
||||
}
|
||||
|
|
|
@ -0,0 +1,24 @@
|
|||
package adminkindboxreqparam
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"git.gocasts.ir/ebhomengo/niki/entity"
|
||||
)
|
||||
|
||||
type KindBoxReqRejectRequest struct {
|
||||
ID uint `json:"id"`
|
||||
Description string `json:"description"`
|
||||
}
|
||||
|
||||
type KindBoxReqRejectResponse struct {
|
||||
ID uint `json:"id"`
|
||||
KindBoxType entity.KindBoxType `json:"kind_box_type"`
|
||||
CountRequested uint `json:"count_requested"`
|
||||
CountAccepted uint `json:"count_accepted"`
|
||||
BenefactorID uint `json:"benefactor_id"`
|
||||
Status entity.KindBoxReqStatus `json:"status"`
|
||||
Description string `json:"description"`
|
||||
ReferDate time.Time `json:"refer_date"`
|
||||
AddressID uint `json:"address_id"`
|
||||
}
|
|
@ -15,4 +15,5 @@ const (
|
|||
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"
|
||||
)
|
||||
|
|
|
@ -28,7 +28,7 @@ func (d DB) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (en
|
|||
return kindBoxReq, nil
|
||||
}
|
||||
|
||||
func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccepted uint) error {
|
||||
func (d DB) AcceptKindBoxReq(ctx context.Context, kindBoxReqID, countAccepted uint) error {
|
||||
op := richerror.Op("mysqlkindboxreq.AcceptKindBoxReq")
|
||||
statement, err := d.conn.Conn().
|
||||
Prepare(`update kind_box_reqs set count_accepted = ? , status = ? where id = ?`)
|
||||
|
@ -89,3 +89,36 @@ func (d DB) CheckKindBoxReqStatusForAccepting(id uint) error {
|
|||
|
||||
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().
|
||||
Prepare(`update kind_box_reqs set description = ? , status = ? where id = ?`)
|
||||
defer statement.Close()
|
||||
if err != nil {
|
||||
return richerror.New(op).WithErr(err).
|
||||
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
_, eErr := statement.ExecContext(ctx, description, entity.KindBoxReqRejectedStatus.String(), kindBoxReqID)
|
||||
if eErr != nil {
|
||||
return richerror.New(op).WithErr(eErr).
|
||||
WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -15,5 +15,6 @@ func scanKindBoxReq(scanner mysql.Scanner) (entity.KindBoxReq, error) {
|
|||
|
||||
kindBoxReq.Status = entity.MapToKindBoxReqStatus(kindBoxStatus)
|
||||
kindBoxReq.KindBoxType = entity.MapToKindBoxType(kindBoxType)
|
||||
|
||||
return kindBoxReq, err
|
||||
}
|
||||
|
|
|
@ -0,0 +1,36 @@
|
|||
package adminkindboxreqservice
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
)
|
||||
|
||||
func (s Service) Reject(ctx context.Context, req param.KindBoxReqRejectRequest) (param.KindBoxReqRejectResponse, error) {
|
||||
const op = "adminkindboxreqservice.Reject"
|
||||
err := s.repo.RejectKindBoxReq(ctx, req.ID, req.Description)
|
||||
if err != nil {
|
||||
return param.KindBoxReqRejectResponse{}, richerror.New(op).WithErr(err)
|
||||
}
|
||||
|
||||
// fire new event to create a kind-box.
|
||||
// get kind box req
|
||||
|
||||
kindBoxReq, gErr := s.repo.GetByID(ctx, req.ID)
|
||||
if gErr != nil {
|
||||
return param.KindBoxReqRejectResponse{}, richerror.New(op).WithErr(err)
|
||||
}
|
||||
|
||||
return param.KindBoxReqRejectResponse{
|
||||
ID: kindBoxReq.ID,
|
||||
KindBoxType: kindBoxReq.KindBoxType,
|
||||
CountRequested: kindBoxReq.CountRequested,
|
||||
CountAccepted: kindBoxReq.CountAccepted,
|
||||
BenefactorID: kindBoxReq.BenefactorID,
|
||||
Status: kindBoxReq.Status,
|
||||
Description: kindBoxReq.Description,
|
||||
ReferDate: kindBoxReq.ReferDate,
|
||||
AddressID: kindBoxReq.AddressID,
|
||||
}, nil
|
||||
}
|
|
@ -15,6 +15,7 @@ type Repository interface {
|
|||
// GetKindBoxReq(ctx context.Context, kindBoxReqID uint) (entity.KindBoxReq, error)
|
||||
AcceptKindBoxReq(ctx context.Context, kindBoxReqID uint, countAccepted uint) error
|
||||
GetByID(ctx context.Context, id uint) (entity.KindBoxReq, error)
|
||||
RejectKindBoxReq(ctx context.Context, kindBoxReqID uint, description string) error
|
||||
}
|
||||
|
||||
// TODO: check validation.
|
||||
|
|
|
@ -13,7 +13,7 @@ func (v Validator) ValidateAcceptRequest(req param.KindBoxReqAcceptRequest) (map
|
|||
const op = "adminkindboxreqvalidator.ValidateAcceptRequest"
|
||||
|
||||
if err := validation.ValidateStruct(&req,
|
||||
validation.Field(&req.ID, validation.Required, validation.By(v.doesKindBoxRequestExist), validation.By(v.checkKindBoxReqStatus)),
|
||||
validation.Field(&req.ID, validation.Required, validation.By(v.doesKindBoxRequestExist), validation.By(v.CheckKindBoxReqStatusForAccepting)),
|
||||
|
||||
validation.Field(&req.CountAccepted,
|
||||
validation.Required,
|
||||
|
|
|
@ -0,0 +1,41 @@
|
|||
package adminkindboxreqvalidator
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req"
|
||||
errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
|
||||
richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
|
||||
validation "github.com/go-ozzo/ozzo-validation/v4"
|
||||
)
|
||||
|
||||
func (v Validator) ValidateRejectRequest(req param.KindBoxReqRejectRequest) (map[string]string, error) {
|
||||
const op = "adminkindboxreqvalidator.ValidateRejectRequest"
|
||||
|
||||
if err := validation.ValidateStruct(&req,
|
||||
validation.Field(&req.ID, validation.Required, validation.By(v.doesKindBoxRequestExist), validation.By(v.CheckKindBoxReqStatusForRejecting)),
|
||||
|
||||
validation.Field(&req.Description,
|
||||
validation.Required,
|
||||
),
|
||||
); err != nil {
|
||||
fieldErrors := make(map[string]string)
|
||||
|
||||
var errV validation.Errors
|
||||
if errors.As(err, &errV) {
|
||||
for key, value := range errV {
|
||||
if value != nil {
|
||||
fieldErrors[key] = value.Error()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return fieldErrors, richerror.New(op).
|
||||
WithMessage(errmsg.ErrorMsgInvalidInput).
|
||||
WithKind(richerror.KindInvalid).
|
||||
WithMeta(map[string]interface{}{"req": req}).
|
||||
WithErr(err)
|
||||
}
|
||||
|
||||
return map[string]string{}, nil
|
||||
}
|
|
@ -19,6 +19,7 @@ type Repository interface {
|
|||
// KindBoxBelongToBenefactor(benefactorID uint, kindboxID uint) (bool, error)
|
||||
// PendingStatus(id uint) (bool, error)
|
||||
CheckKindBoxReqStatusForAccepting(id uint) error
|
||||
CheckKindBoxReqStatusForRejecting(id uint) error
|
||||
}
|
||||
|
||||
type Validator struct {
|
||||
|
@ -73,7 +74,7 @@ func (v Validator) doesKindBoxRequestExist(value interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (v Validator) checkKindBoxReqStatus(value interface{}) error {
|
||||
func (v Validator) CheckKindBoxReqStatusForAccepting(value interface{}) error {
|
||||
kindboxreqID, ok := value.(uint)
|
||||
if !ok {
|
||||
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
||||
|
@ -86,6 +87,19 @@ func (v Validator) checkKindBoxReqStatus(value interface{}) error {
|
|||
return nil
|
||||
}
|
||||
|
||||
func (v Validator) CheckKindBoxReqStatusForRejecting(value interface{}) error {
|
||||
kindboxreqID, ok := value.(uint)
|
||||
if !ok {
|
||||
return fmt.Errorf(errmsg.ErrorMsgSomethingWentWrong)
|
||||
}
|
||||
err := v.repo.CheckKindBoxReqStatusForRejecting(kindboxreqID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
//
|
||||
// func (v Validator) doesTypeExist(value interface{}) error {
|
||||
// typeID, ok := value.(int)
|
||||
|
|
Loading…
Reference in New Issue