From ecb3e9aedc17636c652df1aed1449020a0e76233 Mon Sep 17 00:00:00 2001 From: AMiR Date: Tue, 9 Jul 2024 18:15:50 +0330 Subject: [PATCH] "feat(niki): add update kind box request by admin" --- .../http_server/admin/kind_box_req/route.go | 1 + .../http_server/admin/kind_box_req/update.go | 44 +++++++++++++ entity/admin_permission.go | 1 + param/admin/kind_box_req/update.go | 23 ++++--- repository/mysql/kind_box_req/update.go | 25 ++++++++ ...s_controls_table_permission_enum_field.sql | 3 +- ...565_insert_admin_access_controls_table.sql | 4 +- service/admin/kind_box_req/service.go | 1 + service/admin/kind_box_req/update.go | 37 +++++++++++ validator/admin/kind_box_req/update.go | 61 +++++++++++++++++++ 10 files changed, 189 insertions(+), 11 deletions(-) create mode 100644 delivery/http_server/admin/kind_box_req/update.go create mode 100644 repository/mysql/kind_box_req/update.go create mode 100644 service/admin/kind_box_req/update.go create mode 100644 validator/admin/kind_box_req/update.go diff --git a/delivery/http_server/admin/kind_box_req/route.go b/delivery/http_server/admin/kind_box_req/route.go index be9bd8d..be1cdfd 100644 --- a/delivery/http_server/admin/kind_box_req/route.go +++ b/delivery/http_server/admin/kind_box_req/route.go @@ -18,4 +18,5 @@ func (h Handler) SetRoutes(e *echo.Echo) { r.GET("", h.GetAll, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAllPermission)) r.GET("/awaiting-delivery/:id", h.GetAwaitingDelivery, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAwaitingDeliveryPermission)) r.GET("/awaiting-delivery", h.GetAllAwaitingDelivery, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqGetAwaitingDeliveryPermission)) + r.PUT("/:id", h.Update, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxReqUpdatePermission)) } diff --git a/delivery/http_server/admin/kind_box_req/update.go b/delivery/http_server/admin/kind_box_req/update.go new file mode 100644 index 0000000..6a0b493 --- /dev/null +++ b/delivery/http_server/admin/kind_box_req/update.go @@ -0,0 +1,44 @@ +package adminkindboxreqhandler + +import ( + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" + "github.com/labstack/echo/v4" + "net/http" +) + +// Update godoc +// @Summary Update kind box request by admin +// @Tags KindBoxReq +// @Accept json +// @Produce json +// @Param id path int true "KindBoxReq ID" +// @Param Request body param.KindBoxReqUpdateRequest true "Update KindBoxReq Request Body" +// @Success 204 +// @Failure 400 {string} "Bad request" +// @Security AuthBearerAdmin +// @Router /admin/kindboxreqs/{id} [put]. +func (h Handler) Update(c echo.Context) error { + var req param.KindBoxReqUpdateRequest + if bErr := c.Bind(&req); bErr != nil { + return echo.NewHTTPError(http.StatusBadRequest) + } + + if fieldErrors, err := h.adminKindBoxReqVld.ValidateUpdateRequest(req); err != nil { + msg, code := httpmsg.Error(err) + + return c.JSON(code, echo.Map{ + "message": msg, + "errors": fieldErrors, + }) + } + + sErr := h.adminKindBoxReqSvc.Update(c.Request().Context(), req) + if sErr != nil { + msg, code := httpmsg.Error(sErr) + + return echo.NewHTTPError(code, msg) + } + + return c.JSON(http.StatusNoContent, nil) +} diff --git a/entity/admin_permission.go b/entity/admin_permission.go index 5cd9ae6..734dc29 100644 --- a/entity/admin_permission.go +++ b/entity/admin_permission.go @@ -15,4 +15,5 @@ const ( AdminKindBoxGetPermission = AdminPermission("kindbox-get") AdminKindBoxAssignReceiverAgentPermission = AdminPermission("kindbox-assign_receiver_agent") AdminKindBoxGetAllPermission = AdminPermission("kindbox-getall") + AdminKindBoxReqUpdatePermission = AdminPermission("kindboxreq-update") ) diff --git a/param/admin/kind_box_req/update.go b/param/admin/kind_box_req/update.go index 2f2859a..5eb93b2 100644 --- a/param/admin/kind_box_req/update.go +++ b/param/admin/kind_box_req/update.go @@ -1,14 +1,19 @@ package adminkindboxreqparam -import entity "git.gocasts.ir/ebhomengo/niki/entity" +import ( + "time" + + "git.gocasts.ir/ebhomengo/niki/entity" +) type KindBoxReqUpdateRequest struct { - BenefactorID uint - KindBoxReqID uint - uint - CountRequested uint -} - -type KindBoxReqUpdateResponse struct { - KindBoxReq entity.KindBoxReq + ID uint `json:"-" param:"id"` + KindBoxType entity.KindBoxType `json:"kind_box_type"` + CountRequested uint `json:"count_requested"` + CountAccepted uint `json:"count_accepted"` + Description string `json:"description"` + DeliverReferTimeID uint `json:"deliver_refer_time_id"` + DeliverReferDate time.Time `json:"deliver_refer_date"` + DeliverAddressID uint `json:"deliver_address_id"` + SenderAgentID uint `json:"sender_agent_id"` } diff --git a/repository/mysql/kind_box_req/update.go b/repository/mysql/kind_box_req/update.go new file mode 100644 index 0000000..db77ce6 --- /dev/null +++ b/repository/mysql/kind_box_req/update.go @@ -0,0 +1,25 @@ +package mysqlkindboxreq + +import ( + "context" + + "git.gocasts.ir/ebhomengo/niki/entity" + errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (d DB) UpdateKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) error { + const op = "mysqlkindboxreq.UpdateKindBoxReq" + + query := `UPDATE kind_box_reqs + SET benfactor_id = ?, kind_box_type = ?, count_requested = ?, count_accepted = ?, description = ?, status = ?, deliver_refer_time_id = ?, deliver_refer_date = ?, deliver_address_id + ?, sender_agent_id = ?, delivered_at = ? + WHERE id = ? AND deleted_at IS NULL` + _, uErr := d.conn.Conn().ExecContext(ctx, query, kindBoxReq.BenefactorID, kindBoxReq.KindBoxType, kindBoxReq.CountRequested, kindBoxReq.CountAccepted, kindBoxReq.Description, kindBoxReq.Status, kindBoxReq.DeliverReferTimeID, kindBoxReq.DeliverReferDate, kindBoxReq.DeliverAddressID, kindBoxReq.SenderAgentID, kindBoxReq.DeliveredAt) + if uErr != nil { + + return richerror.New(op).WithErr(uErr).WithMessage(errmsg.ErrorMsgCantUpdateRecord). + WithKind(richerror.KindUnexpected) + } + + return nil +} diff --git a/repository/mysql/migration/1708712564_alter_admin_access_controls_table_permission_enum_field.sql b/repository/mysql/migration/1708712564_alter_admin_access_controls_table_permission_enum_field.sql index 62ef269..4b02654 100644 --- a/repository/mysql/migration/1708712564_alter_admin_access_controls_table_permission_enum_field.sql +++ b/repository/mysql/migration/1708712564_alter_admin_access_controls_table_permission_enum_field.sql @@ -12,7 +12,8 @@ ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission` 'kindbox-get', 'kindboxreq-add', 'kindbox-assign_receiver_agent', - 'kindbox-getall' + 'kindbox-getall', + 'kindboxreq-update' ) NOT NULL; -- +migrate Down \ No newline at end of file diff --git a/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql b/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql index c7051b1..30aaa21 100644 --- a/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql +++ b/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql @@ -23,7 +23,9 @@ INSERT INTO `admin_access_controls` (`id`, `actor_id`, `actor_type`,`permission` (21, 1 , 'role','kindboxreq-add'), (22, 2 , 'role','kindboxreq-add'), (23, 1 , 'role','kindbox-getall'), - (24, 2 , 'role','kindbox-getall'); + (24, 2 , 'role','kindbox-getall'), + (25, 1 , 'role','kindboxreq-update'), + (26, 2 , 'role','kindboxreq-update'); -- +migrate Down diff --git a/service/admin/kind_box_req/service.go b/service/admin/kind_box_req/service.go index 4f13686..9bd11f7 100644 --- a/service/admin/kind_box_req/service.go +++ b/service/admin/kind_box_req/service.go @@ -18,6 +18,7 @@ type Repository interface { GetAllKindBoxReq(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBoxReq, uint, error) GetAwaitingDeliveryByAgent(ctx context.Context, kindBoxReqID uint, agentID uint) (entity.KindBoxReq, error) AddKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) (entity.KindBoxReq, error) + UpdateKindBoxReq(ctx context.Context, kindBoxReq entity.KindBoxReq) error } type KindBoxSvc interface { diff --git a/service/admin/kind_box_req/update.go b/service/admin/kind_box_req/update.go new file mode 100644 index 0000000..1be9fba --- /dev/null +++ b/service/admin/kind_box_req/update.go @@ -0,0 +1,37 @@ +package adminkindboxreqservice + +import ( + "context" + + "git.gocasts.ir/ebhomengo/niki/entity" + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box_req" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) Update(ctx context.Context, req param.KindBoxReqUpdateRequest) error { + const op = "adminkindboxreqservice.Update" + + kindBoxReq, err := s.repo.GetByID(ctx, req.ID) + if err != nil { + return richerror.New(op).WithErr(err) + } + uErr := s.repo.UpdateKindBoxReq(ctx, entity.KindBoxReq{ + ID: req.ID, + BenefactorID: kindBoxReq.BenefactorID, + KindBoxType: req.KindBoxType, + CountRequested: req.CountRequested, + CountAccepted: req.CountAccepted, + Description: req.Description, + Status: kindBoxReq.Status, + DeliverReferTimeID: req.DeliverReferTimeID, + DeliverReferDate: req.DeliverReferDate, + DeliverAddressID: req.DeliverAddressID, + SenderAgentID: req.SenderAgentID, + DeliveredAt: kindBoxReq.DeliveredAt, + }) + if uErr != nil { + return richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + + return nil +} diff --git a/validator/admin/kind_box_req/update.go b/validator/admin/kind_box_req/update.go new file mode 100644 index 0000000..cd104a4 --- /dev/null +++ b/validator/admin/kind_box_req/update.go @@ -0,0 +1,61 @@ +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) ValidateUpdateRequest(req param.KindBoxReqUpdateRequest) (map[string]string, error) { + const op = "adminkindboxreqvalidator.ValidateUpdateRequest" + + if err := validation.ValidateStruct(&req, + + validation.Field(&req.ID, validation.Required, validation.By(v.doesKindBoxRequestExist)), + + validation.Field(&req.CountRequested, validation.Required, validation.Min(uint(MinKindBoxReq)), validation.Max(uint(MaxKindBoxReq))), + + validation.Field(&req.KindBoxType, + validation.Required, + validation.By(v.doesTypeExist)), + + validation.Field(&req.DeliverAddressID, + validation.Required, + validation.By(v.doesAddressExist(req.BenefactorID))), + + validation.Field(&req.DeliverReferDate, + validation.Required, + validation.By(v.isDateValid), + ), + validation.Field(&req.DeliverReferTimeID, + validation.Required, + validation.By(v.isDateValid), + ), + validation.Field(&req.SenderAgentID, + validation.Required, + validation.By(v.isDateValid), + ), + ); 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 +}