From 1823191c4e3dafa365fbe9c93230c27f8b219c07 Mon Sep 17 00:00:00 2001 From: Abolfazl Norzad Date: Mon, 22 Jan 2024 18:51:13 +0330 Subject: [PATCH] rejecting kind-box-request --- .../http_server/admin/kind_box_req/reject.go | 40 ++++++++++++++++++ .../http_server/admin/kind_box_req/route.go | 1 + param/admin/kind_box_req/reject.go | 24 +++++++++++ pkg/err_msg/message.go | 1 + repository/mysql/kind_box_req/kind_box_req.go | 35 +++++++++++++++- repository/mysql/kind_box_req/scan.go | 1 + service/admin/kind_box_req/reject.go | 36 ++++++++++++++++ service/admin/kind_box_req/service.go | 1 + validator/admin/kind_box_req/accept.go | 2 +- validator/admin/kind_box_req/reject.go | 41 +++++++++++++++++++ validator/admin/kind_box_req/validator.go | 16 +++++++- 11 files changed, 195 insertions(+), 3 deletions(-) create mode 100644 delivery/http_server/admin/kind_box_req/reject.go create mode 100644 param/admin/kind_box_req/reject.go create mode 100644 service/admin/kind_box_req/reject.go create mode 100644 validator/admin/kind_box_req/reject.go diff --git a/delivery/http_server/admin/kind_box_req/reject.go b/delivery/http_server/admin/kind_box_req/reject.go new file mode 100644 index 0000000..2aa1bd4 --- /dev/null +++ b/delivery/http_server/admin/kind_box_req/reject.go @@ -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) +} diff --git a/delivery/http_server/admin/kind_box_req/route.go b/delivery/http_server/admin/kind_box_req/route.go index 60ff249..c0bcc53 100644 --- a/delivery/http_server/admin/kind_box_req/route.go +++ b/delivery/http_server/admin/kind_box_req/route.go @@ -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) } diff --git a/param/admin/kind_box_req/reject.go b/param/admin/kind_box_req/reject.go new file mode 100644 index 0000000..56119b1 --- /dev/null +++ b/param/admin/kind_box_req/reject.go @@ -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"` +} diff --git a/pkg/err_msg/message.go b/pkg/err_msg/message.go index a7744c4..5d24ce5 100644 --- a/pkg/err_msg/message.go +++ b/pkg/err_msg/message.go @@ -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" ) diff --git a/repository/mysql/kind_box_req/kind_box_req.go b/repository/mysql/kind_box_req/kind_box_req.go index ae937a8..b0f8e15 100644 --- a/repository/mysql/kind_box_req/kind_box_req.go +++ b/repository/mysql/kind_box_req/kind_box_req.go @@ -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 +} diff --git a/repository/mysql/kind_box_req/scan.go b/repository/mysql/kind_box_req/scan.go index 8cda054..5a165d5 100644 --- a/repository/mysql/kind_box_req/scan.go +++ b/repository/mysql/kind_box_req/scan.go @@ -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 } diff --git a/service/admin/kind_box_req/reject.go b/service/admin/kind_box_req/reject.go new file mode 100644 index 0000000..1734bca --- /dev/null +++ b/service/admin/kind_box_req/reject.go @@ -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 +} diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index 077c203..80a415b 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -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. diff --git a/validator/admin/kind_box_req/accept.go b/validator/admin/kind_box_req/accept.go index d41a40d..86ea377 100644 --- a/validator/admin/kind_box_req/accept.go +++ b/validator/admin/kind_box_req/accept.go @@ -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, diff --git a/validator/admin/kind_box_req/reject.go b/validator/admin/kind_box_req/reject.go new file mode 100644 index 0000000..2822223 --- /dev/null +++ b/validator/admin/kind_box_req/reject.go @@ -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 +} diff --git a/validator/admin/kind_box_req/validator.go b/validator/admin/kind_box_req/validator.go index 515e34c..75d54db 100644 --- a/validator/admin/kind_box_req/validator.go +++ b/validator/admin/kind_box_req/validator.go @@ -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)