From 96d9c679dc5b1d6b4e9f57727c0867a887313096 Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Wed, 24 Jul 2024 01:07:27 +0330 Subject: [PATCH] feat(niki): admin enumerate(count) kindbox amount (#80) --- .../http_server/admin/kind_box/enumerate.go | 51 ++++++++++++ delivery/http_server/admin/kind_box/route.go | 1 + docs/docs.go | 80 +++++++++++++++++++ docs/swagger.json | 80 +++++++++++++++++++ docs/swagger.yaml | 51 ++++++++++++ entity/admin_permission.go | 1 + param/admin/kind_box/enumerate.go | 6 ++ repository/mysql/kind_box/enumerate.go | 22 +++++ ...s_controls_table_permission_enum_field.sql | 3 +- ...565_insert_admin_access_controls_table.sql | 70 ++++++++-------- service/admin/kind_box/enumerate.go | 19 +++++ service/admin/kind_box/service.go | 1 + validator/admin/kind_box/enumerate.go | 41 ++++++++++ 13 files changed, 391 insertions(+), 35 deletions(-) create mode 100644 delivery/http_server/admin/kind_box/enumerate.go create mode 100644 param/admin/kind_box/enumerate.go create mode 100644 repository/mysql/kind_box/enumerate.go create mode 100644 service/admin/kind_box/enumerate.go create mode 100644 validator/admin/kind_box/enumerate.go diff --git a/delivery/http_server/admin/kind_box/enumerate.go b/delivery/http_server/admin/kind_box/enumerate.go new file mode 100644 index 0000000..3daa6ad --- /dev/null +++ b/delivery/http_server/admin/kind_box/enumerate.go @@ -0,0 +1,51 @@ +package adminkindboxhandler + +import ( + "net/http" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" + httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" + "github.com/labstack/echo/v4" +) + +// Enumerate godoc +// @Summary Admin enumerate kindbox +// @Tags KindBox +// @Accept json +// @Produce json +// @Param id path int true "KindBox ID" +// @Param Request body param.EnumerateKindBoxRequest true "Request" +// @Success 204 +// @Failure 400 {string} "Bad Request" +// @Failure 401 {string} "invalid or expired jwt" +// @Failure 403 {string} "user not allowed" +// @Failure 422 {object} httpmsg.ErrorResponse +// @Failure 500 {string} "something went wrong" +// @Security AuthBearerAdmin +// @Router /admin/kindboxes/{id}/enumerate [patch]. +func (h Handler) Enumerate(c echo.Context) error { + var req param.EnumerateKindBoxRequest + + if bErr := c.Bind(&req); bErr != nil { + return echo.NewHTTPError(http.StatusBadRequest) + } + + if fieldErrors, err := h.adminKindBoxVld.ValidateEnumerate(req); err != nil { + msg, code := httpmsg.Error(err) + + return c.JSON(code, echo.Map{ + "message": msg, + "errors": fieldErrors, + }) + } + + sErr := h.adminKindBoxSvc.Enumerate(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/delivery/http_server/admin/kind_box/route.go b/delivery/http_server/admin/kind_box/route.go index ec829d2..e9deebe 100644 --- a/delivery/http_server/admin/kind_box/route.go +++ b/delivery/http_server/admin/kind_box/route.go @@ -14,4 +14,5 @@ func (h Handler) SetRoutes(e *echo.Echo) { r.GET("/:id", h.Get, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxGetPermission)) r.PATCH("/assign-receiver-agent/:id", h.AssignReceiverAgent, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxAssignReceiverAgentPermission)) r.GET("", h.GetAll, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxGetAllPermission)) + r.PATCH("/:id/enumerate", h.Enumerate, middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminKindBoxEnumeratePermission)) } diff --git a/docs/docs.go b/docs/docs.go index 8d91496..b3b795c 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -559,6 +559,78 @@ const docTemplate = `{ } } }, + "/admin/kindboxes/{id}/enumerate": { + "patch": { + "security": [ + { + "AuthBearerAdmin": [] + } + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "KindBox" + ], + "summary": "Admin enumerate kindbox", + "parameters": [ + { + "type": "integer", + "description": "KindBox ID", + "name": "id", + "in": "path", + "required": true + }, + { + "description": "Request", + "name": "Request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/adminkindboxparam.EnumerateKindBoxRequest" + } + } + ], + "responses": { + "204": { + "description": "No Content" + }, + "400": { + "description": "Bad Request", + "schema": { + "type": "string" + } + }, + "401": { + "description": "invalid or expired jwt", + "schema": { + "type": "string" + } + }, + "403": { + "description": "user not allowed", + "schema": { + "type": "string" + } + }, + "422": { + "description": "Unprocessable Entity", + "schema": { + "$ref": "#/definitions/httpmsg.ErrorResponse" + } + }, + "500": { + "description": "something went wrong", + "schema": { + "type": "string" + } + } + } + } + }, "/admin/kindboxreqs": { "post": { "security": [ @@ -2434,6 +2506,14 @@ const docTemplate = `{ } } }, + "adminkindboxparam.EnumerateKindBoxRequest": { + "type": "object", + "properties": { + "amount": { + "type": "integer" + } + } + }, "adminkindboxparam.KindBoxGetAllResponse": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 3ae5d09..7c12abd 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -548,6 +548,78 @@ } } }, + "/admin/kindboxes/{id}/enumerate": { + "patch": { + "security": [ + { + "AuthBearerAdmin": [] + } + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "KindBox" + ], + "summary": "Admin enumerate kindbox", + "parameters": [ + { + "type": "integer", + "description": "KindBox ID", + "name": "id", + "in": "path", + "required": true + }, + { + "description": "Request", + "name": "Request", + "in": "body", + "required": true, + "schema": { + "$ref": "#/definitions/adminkindboxparam.EnumerateKindBoxRequest" + } + } + ], + "responses": { + "204": { + "description": "No Content" + }, + "400": { + "description": "Bad Request", + "schema": { + "type": "string" + } + }, + "401": { + "description": "invalid or expired jwt", + "schema": { + "type": "string" + } + }, + "403": { + "description": "user not allowed", + "schema": { + "type": "string" + } + }, + "422": { + "description": "Unprocessable Entity", + "schema": { + "$ref": "#/definitions/httpmsg.ErrorResponse" + } + }, + "500": { + "description": "something went wrong", + "schema": { + "type": "string" + } + } + } + } + }, "/admin/kindboxreqs": { "post": { "security": [ @@ -2423,6 +2495,14 @@ } } }, + "adminkindboxparam.EnumerateKindBoxRequest": { + "type": "object", + "properties": { + "amount": { + "type": "integer" + } + } + }, "adminkindboxparam.KindBoxGetAllResponse": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index dd5c482..d0840f6 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -77,6 +77,11 @@ definitions: receiver_agent_id: type: integer type: object + adminkindboxparam.EnumerateKindBoxRequest: + properties: + amount: + type: integer + type: object adminkindboxparam.KindBoxGetAllResponse: properties: all_kind_box: @@ -1183,6 +1188,52 @@ paths: summary: Get a specific kind box by admin tags: - KindBox + /admin/kindboxes/{id}/enumerate: + patch: + consumes: + - application/json + parameters: + - description: KindBox ID + in: path + name: id + required: true + type: integer + - description: Request + in: body + name: Request + required: true + schema: + $ref: '#/definitions/adminkindboxparam.EnumerateKindBoxRequest' + produces: + - application/json + responses: + "204": + description: No Content + "400": + description: Bad Request + schema: + type: string + "401": + description: invalid or expired jwt + schema: + type: string + "403": + description: user not allowed + schema: + type: string + "422": + description: Unprocessable Entity + schema: + $ref: '#/definitions/httpmsg.ErrorResponse' + "500": + description: something went wrong + schema: + type: string + security: + - AuthBearerAdmin: [] + summary: Admin enumerate kindbox + tags: + - KindBox /admin/kindboxes/assign-receiver-agent/{id}: patch: consumes: diff --git a/entity/admin_permission.go b/entity/admin_permission.go index 6b1b56a..f7c8142 100644 --- a/entity/admin_permission.go +++ b/entity/admin_permission.go @@ -19,4 +19,5 @@ const ( AdminKindBoxReqGetPermission = AdminPermission("kindboxreq-get") AdminKindBoxGetAwaitingReturnPermission = AdminPermission("kindbox-get_awaiting_return") AdminKindBoxReturnPermission = AdminPermission("kindbox-return") + AdminKindBoxEnumeratePermission = AdminPermission("kindbox-enumerate") ) diff --git a/param/admin/kind_box/enumerate.go b/param/admin/kind_box/enumerate.go new file mode 100644 index 0000000..3f19763 --- /dev/null +++ b/param/admin/kind_box/enumerate.go @@ -0,0 +1,6 @@ +package adminkindboxparam + +type EnumerateKindBoxRequest struct { + KindBoxID uint `json:"-" param:"id"` + Amount uint `json:"amount"` +} diff --git a/repository/mysql/kind_box/enumerate.go b/repository/mysql/kind_box/enumerate.go new file mode 100644 index 0000000..8f62378 --- /dev/null +++ b/repository/mysql/kind_box/enumerate.go @@ -0,0 +1,22 @@ +package mysqlkindbox + +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) EnumerateKindBox(ctx context.Context, kindBoxID uint, amount uint) error { + const op = "mysqlkindbox.EnumerateKindBox" + + query := `UPDATE kind_boxes SET amount = ?, status = ? WHERE id = ?` + _, err := d.conn.Conn().ExecContext(ctx, query, amount, entity.KindBoxEnumeratedStatus.String(), kindBoxID) + if err != nil { + return richerror.New(op).WithErr(err). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).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 fc0e907..baa74c3 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 @@ -16,7 +16,8 @@ ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission` 'kindboxreq-update', 'kindboxreq-get', 'kindbox-get_awaiting_return', - 'kindbox-return' + 'kindbox-return', + 'kindbox-enumerate' ) 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 a4e3993..e321d7c 100644 --- a/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql +++ b/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql @@ -1,41 +1,43 @@ -- +migrate Up -INSERT INTO `admin_access_controls` (`id`, `actor_id`, `actor_type`,`permission`) +INSERT INTO `admin_access_controls` (`actor_id`, `actor_type`,`permission`) VALUES - (DEFAULT, 1 , 'role','admin-register'), - (DEFAULT, 1 , 'role','admin-getall_agent'), - (DEFAULT, 2 , 'role','admin-getall_agent'), + (1 , 'role','admin-register'), + (1 , 'role','admin-getall_agent'), + (2 , 'role','admin-getall_agent'), - (DEFAULT, 1 , 'role','kindboxreq-add'), - (DEFAULT, 2 , 'role','kindboxreq-add'), - (DEFAULT, 1 , 'role','kindboxreq-accept'), - (DEFAULT, 2 , 'role','kindboxreq-accept'), - (DEFAULT, 1 , 'role','kindboxreq-reject'), - (DEFAULT, 2 , 'role','kindboxreq-reject'), - (DEFAULT, 1 , 'role','kindboxreq-assign_sender_agent'), - (DEFAULT, 2 , 'role','kindboxreq-assign_sender_agent'), - (DEFAULT, 1 , 'role','kindboxreq-deliver'), - (DEFAULT, 2 , 'role','kindboxreq-deliver'), - (DEFAULT, 3 , 'role','kindboxreq-deliver'), - (DEFAULT, 1 , 'role','kindboxreq-get'), - (DEFAULT, 2 , 'role','kindboxreq-get'), - (DEFAULT, 1 , 'role','kindboxreq-getall'), - (DEFAULT, 2 , 'role','kindboxreq-getall'), - (DEFAULT, 1 , 'role','kindboxreq-update'), - (DEFAULT, 2 , 'role','kindboxreq-update'), - (DEFAULT, 1 , 'role','kindboxreq-get_awaiting_delivery'), - (DEFAULT, 2 , 'role','kindboxreq-get_awaiting_delivery'), - (DEFAULT, 3 , 'role','kindboxreq-get_awaiting_delivery'), + (1 , 'role','kindboxreq-add'), + (2 , 'role','kindboxreq-add'), + (1 , 'role','kindboxreq-accept'), + (2 , 'role','kindboxreq-accept'), + (1 , 'role','kindboxreq-reject'), + (2 , 'role','kindboxreq-reject'), + (1 , 'role','kindboxreq-assign_sender_agent'), + (2 , 'role','kindboxreq-assign_sender_agent'), + (1 , 'role','kindboxreq-deliver'), + (2 , 'role','kindboxreq-deliver'), + (3 , 'role','kindboxreq-deliver'), + (1 , 'role','kindboxreq-get'), + (2 , 'role','kindboxreq-get'), + (1 , 'role','kindboxreq-getall'), + (2 , 'role','kindboxreq-getall'), + (1 , 'role','kindboxreq-update'), + (2 , 'role','kindboxreq-update'), + (1 , 'role','kindboxreq-get_awaiting_delivery'), + (2 , 'role','kindboxreq-get_awaiting_delivery'), + (3 , 'role','kindboxreq-get_awaiting_delivery'), - (DEFAULT, 1 , 'role','kindbox-assign_receiver_agent'), - (DEFAULT, 2 , 'role','kindbox-assign_receiver_agent'), - (DEFAULT, 1 , 'role','kindbox-get'), - (DEFAULT, 2 , 'role','kindbox-get'), - (DEFAULT, 1 , 'role','kindbox-getall'), - (DEFAULT, 2 , 'role','kindbox-getall'), - (DEFAULT, 1 , 'role','kindbox-get_awaiting_return'), - (DEFAULT, 3 , 'role','kindbox-get_awaiting_return'), - (DEFAULT, 1 , 'role','kindbox-return'), - (DEFAULT, 3 , 'role','kindbox-return'); + (1 , 'role','kindbox-assign_receiver_agent'), + (2 , 'role','kindbox-assign_receiver_agent'), + (1 , 'role','kindbox-get'), + (2 , 'role','kindbox-get'), + (1 , 'role','kindbox-getall'), + (2 , 'role','kindbox-getall'), + (1 , 'role','kindbox-get_awaiting_return'), + (3 , 'role','kindbox-get_awaiting_return'), + (1 , 'role','kindbox-return'), + (3 , 'role','kindbox-return'), + (1 , 'role','kindbox-enumerate'), + (2 , 'role','kindbox-enumerate'); diff --git a/service/admin/kind_box/enumerate.go b/service/admin/kind_box/enumerate.go new file mode 100644 index 0000000..596f100 --- /dev/null +++ b/service/admin/kind_box/enumerate.go @@ -0,0 +1,19 @@ +package adminkindboxservice + +import ( + "context" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) Enumerate(ctx context.Context, req param.EnumerateKindBoxRequest) error { + const op = "AdminKindBoxService.Enumerate" + + err := s.repo.EnumerateKindBox(ctx, req.KindBoxID, req.Amount) + if err != nil { + return richerror.New(op).WithErr(err) + } + + return nil +} diff --git a/service/admin/kind_box/service.go b/service/admin/kind_box/service.go index dca638d..7087d53 100644 --- a/service/admin/kind_box/service.go +++ b/service/admin/kind_box/service.go @@ -13,6 +13,7 @@ type Repository interface { GetKindBox(ctx context.Context, kindBoxID uint) (entity.KindBox, error) AssignReceiverAgent(ctx context.Context, kindBox entity.KindBox) error GetAllKindBox(ctx context.Context, filter params.FilterRequest, pagination params.PaginationRequest, sort params.SortRequest) ([]entity.KindBox, uint, error) + EnumerateKindBox(ctx context.Context, kindBoxID uint, amount uint) error } type Service struct { diff --git a/validator/admin/kind_box/enumerate.go b/validator/admin/kind_box/enumerate.go new file mode 100644 index 0000000..259a9c8 --- /dev/null +++ b/validator/admin/kind_box/enumerate.go @@ -0,0 +1,41 @@ +package adminkindboxvalidator + +import ( + "errors" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/kind_box" + 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) ValidateEnumerate(req param.EnumerateKindBoxRequest) (map[string]string, error) { + const op = "adminkindboxvalidator.ValidateEnumerate" + + if err := validation.ValidateStruct(&req, + + validation.Field(&req.KindBoxID, validation.Required, + validation.By(v.doesKindBoxExist)), + + validation.Field(&req.Amount, 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 +}