From 3e39c9b5e9ba0368468f13808d2c54ee5728e631 Mon Sep 17 00:00:00 2001 From: ErfanTech Date: Mon, 10 Jun 2024 18:19:13 +0330 Subject: [PATCH] feat(niki): add get all agents by admin --- .../http_server/admin/admin/get_all_agent.go | 28 ++++++++ delivery/http_server/admin/admin/route.go | 1 + docs/docs.go | 65 +++++++++++++++++++ docs/swagger.json | 65 +++++++++++++++++++ docs/swagger.yaml | 42 ++++++++++++ entity/admin_permission.go | 1 + param/admin/admin/get_all_agent.go | 12 ++++ repository/mysql/admin/get_all_agent.go | 41 ++++++++++++ ...s_controls_table_permission_enum_field.sql | 3 +- ...565_insert_admin_access_controls_table.sql | 4 +- service/admin/admin/get_all_agent.go | 31 +++++++++ service/admin/admin/service.go | 1 + 12 files changed, 292 insertions(+), 2 deletions(-) create mode 100644 delivery/http_server/admin/admin/get_all_agent.go create mode 100644 param/admin/admin/get_all_agent.go create mode 100644 repository/mysql/admin/get_all_agent.go create mode 100644 service/admin/admin/get_all_agent.go diff --git a/delivery/http_server/admin/admin/get_all_agent.go b/delivery/http_server/admin/admin/get_all_agent.go new file mode 100644 index 0000000..1cedf42 --- /dev/null +++ b/delivery/http_server/admin/admin/get_all_agent.go @@ -0,0 +1,28 @@ +package adminhandler + +import ( + "net/http" + + httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg" + "github.com/labstack/echo/v4" +) + +// GetAllAgent godoc +// @Summary Get all agents by admin +// @Tags Admin +// @Accept json +// @Produce json +// @Success 200 {object} adminserviceparam.GetAllAgentResponse +// @Failure 400 {string} "Bad request" +// @Security AuthBearerAdmin +// @Router /admins/agents [get] +func (h Handler) GetAllAgent(c echo.Context) error { + resp, sErr := h.adminSvc.GetAllAgent(c.Request().Context()) + if sErr != nil { + msg, code := httpmsg.Error(sErr) + + return echo.NewHTTPError(code, msg) + } + + return c.JSON(http.StatusOK, resp) +} diff --git a/delivery/http_server/admin/admin/route.go b/delivery/http_server/admin/admin/route.go index 544be17..616a76c 100644 --- a/delivery/http_server/admin/admin/route.go +++ b/delivery/http_server/admin/admin/route.go @@ -15,4 +15,5 @@ func (h Handler) SetRoutes(e *echo.Echo) { r.POST("/login-by-phone", h.LoginByPhoneNumber) //nolint:gocritic //r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq" + r.GET("/agents", h.GetAllAgent, middleware.Auth(h.authSvc, h.authConfig), middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminAdminGetAllAgentPermission)) } diff --git a/docs/docs.go b/docs/docs.go index 548a97b..6976d7b 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -463,6 +463,39 @@ const docTemplate = `{ } } }, + "/admins/agents": { + "get": { + "security": [ + { + "AuthBearerAdmin": [] + } + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "Get all agents by admin", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/adminserviceparam.GetAllAgentResponse" + } + }, + "400": { + "description": "Bad request", + "schema": { + "type": "string" + } + } + } + } + }, "/admins/login-by-phone": { "post": { "consumes": [ @@ -1075,6 +1108,38 @@ const docTemplate = `{ } } }, + "adminserviceparam.Agent": { + "type": "object", + "properties": { + "first_name": { + "type": "string", + "example": "John" + }, + "id": { + "type": "integer", + "example": 1 + }, + "last_name": { + "type": "string", + "example": "Doe" + }, + "phone_number": { + "type": "string", + "example": "09123456789" + } + } + }, + "adminserviceparam.GetAllAgentResponse": { + "type": "object", + "properties": { + "agents": { + "type": "array", + "items": { + "$ref": "#/definitions/adminserviceparam.Agent" + } + } + } + }, "adminserviceparam.LoginWithPhoneNumberRequest": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 0b76ae4..13b97a3 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -452,6 +452,39 @@ } } }, + "/admins/agents": { + "get": { + "security": [ + { + "AuthBearerAdmin": [] + } + ], + "consumes": [ + "application/json" + ], + "produces": [ + "application/json" + ], + "tags": [ + "Admin" + ], + "summary": "Get all agents by admin", + "responses": { + "200": { + "description": "OK", + "schema": { + "$ref": "#/definitions/adminserviceparam.GetAllAgentResponse" + } + }, + "400": { + "description": "Bad request", + "schema": { + "type": "string" + } + } + } + } + }, "/admins/login-by-phone": { "post": { "consumes": [ @@ -1064,6 +1097,38 @@ } } }, + "adminserviceparam.Agent": { + "type": "object", + "properties": { + "first_name": { + "type": "string", + "example": "John" + }, + "id": { + "type": "integer", + "example": 1 + }, + "last_name": { + "type": "string", + "example": "Doe" + }, + "phone_number": { + "type": "string", + "example": "09123456789" + } + } + }, + "adminserviceparam.GetAllAgentResponse": { + "type": "object", + "properties": { + "agents": { + "type": "array", + "items": { + "$ref": "#/definitions/adminserviceparam.Agent" + } + } + } + }, "adminserviceparam.LoginWithPhoneNumberRequest": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index cf511af..8ea0dff 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -157,6 +157,28 @@ definitions: - $ref: '#/definitions/entity.AdminStatus' example: 1 type: object + adminserviceparam.Agent: + properties: + first_name: + example: John + type: string + id: + example: 1 + type: integer + last_name: + example: Doe + type: string + phone_number: + example: "09123456789" + type: string + type: object + adminserviceparam.GetAllAgentResponse: + properties: + agents: + items: + $ref: '#/definitions/adminserviceparam.Agent' + type: array + type: object adminserviceparam.LoginWithPhoneNumberRequest: properties: password: @@ -785,6 +807,26 @@ paths: summary: Reject a kindboxreq by admin tags: - KindBoxReq + /admins/agents: + get: + consumes: + - application/json + produces: + - application/json + responses: + "200": + description: OK + schema: + $ref: '#/definitions/adminserviceparam.GetAllAgentResponse' + "400": + description: Bad request + schema: + type: string + security: + - AuthBearerAdmin: [] + summary: Get all agents by admin + tags: + - Admin /admins/login-by-phone: post: consumes: diff --git a/entity/admin_permission.go b/entity/admin_permission.go index f421511..822a25b 100644 --- a/entity/admin_permission.go +++ b/entity/admin_permission.go @@ -9,4 +9,5 @@ const ( AdminKindBoxReqGetAllPermission = AdminPermission("kindboxreq-getall") AdminKindBoxReqDeliverPermission = AdminPermission("kindboxreq-deliver") AdminKindBoxReqAssignSenderAgentPermission = AdminPermission("kindboxreq-assign_sender_agent") + AdminAdminGetAllAgentPermission = AdminPermission("admin-getall_agent") ) diff --git a/param/admin/admin/get_all_agent.go b/param/admin/admin/get_all_agent.go new file mode 100644 index 0000000..ee0a167 --- /dev/null +++ b/param/admin/admin/get_all_agent.go @@ -0,0 +1,12 @@ +package adminserviceparam + +type GetAllAgentResponse struct { + Agents []Agent `json:"agents"` +} + +type Agent struct { + ID uint `json:"id" example:"1"` + FirstName string `json:"first_name" example:"John"` + LastName string `json:"last_name" example:"Doe"` + PhoneNumber string `json:"phone_number" example:"09123456789"` +} diff --git a/repository/mysql/admin/get_all_agent.go b/repository/mysql/admin/get_all_agent.go new file mode 100644 index 0000000..432e362 --- /dev/null +++ b/repository/mysql/admin/get_all_agent.go @@ -0,0 +1,41 @@ +package mysqladmin + +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) GetAllAgent(ctx context.Context) ([]entity.Admin, error) { + const op = "mysqladmin.GetAllAgent" + + agents := make([]entity.Admin, 0) + query := `SELECT id, first_name, last_name, phone_number FROM admins WHERE role = 'agent' AND status = 'active'` + rows, err := d.conn.Conn().QueryContext(ctx, query) + if err != nil { + + return nil, richerror.New(op).WithErr(err).WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + defer rows.Close() + + for rows.Next() { + var agent entity.Admin + sErr := rows.Scan(&agent.ID, &agent.FirstName, &agent.LastName, &agent.PhoneNumber) + if err != nil { + + return nil, richerror.New(op).WithErr(sErr). + WithMessage(errmsg.ErrorMsgCantScanQueryResult).WithKind(richerror.KindUnexpected) + } + agents = append(agents, agent) + } + + if rErr := rows.Err(); rErr != nil { + + return nil, richerror.New(op).WithErr(rErr). + WithMessage(errmsg.ErrorMsgSomethingWentWrong).WithKind(richerror.KindUnexpected) + } + + return agents, 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 9e525b2..da3496a 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 @@ -6,7 +6,8 @@ ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission` 'kindboxreq-reject', 'kindboxreq-getall', 'kindboxreq-deliver', - 'kindboxreq-assign_sender_agent' + 'kindboxreq-assign_sender_agent', + 'admin-getall_agent' ) 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 70607b3..2dd20a8 100644 --- a/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql +++ b/repository/mysql/migration/1708712565_insert_admin_access_controls_table.sql @@ -11,7 +11,9 @@ INSERT INTO `admin_access_controls` (`id`, `actor_id`, `actor_type`,`permission` (9, 2 , 'role','kindboxreq-reject'), (10, 2 , 'role','kindboxreq-getall'), (11, 2 , 'role','kindboxreq-deliver'), - (12, 2 , 'role','kindboxreq-assign_sender_agent'); + (12, 2 , 'role','kindboxreq-assign_sender_agent'), + (13, 1, 'role', 'admin-getall_agent'), + (13, 2, 'role', 'admin-getall_agent'); -- +migrate Down DELETE diff --git a/service/admin/admin/get_all_agent.go b/service/admin/admin/get_all_agent.go new file mode 100644 index 0000000..5973892 --- /dev/null +++ b/service/admin/admin/get_all_agent.go @@ -0,0 +1,31 @@ +package adminservice + +import ( + "context" + + param "git.gocasts.ir/ebhomengo/niki/param/admin/admin" + richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error" +) + +func (s Service) GetAllAgent(ctx context.Context) (param.GetAllAgentResponse, error) { + const op = "adminservice.GetAllAgent" + + agentsInfo := make([]param.Agent, 0) + + agents, err := s.repo.GetAllAgent(ctx) + if err != nil { + + return param.GetAllAgentResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected) + } + + for _, agent := range agents { + agentsInfo = append(agentsInfo, param.Agent{ + ID: agent.ID, + FirstName: agent.FirstName, + LastName: agent.LastName, + PhoneNumber: agent.PhoneNumber, + }) + } + + return param.GetAllAgentResponse{Agents: agentsInfo}, nil +} diff --git a/service/admin/admin/service.go b/service/admin/admin/service.go index 7eb4113..5921ba4 100644 --- a/service/admin/admin/service.go +++ b/service/admin/admin/service.go @@ -18,6 +18,7 @@ type Repository interface { AddAdmin(ctx context.Context, admin entity.Admin) (entity.Admin, error) GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error) GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error) + GetAllAgent(ctx context.Context) ([]entity.Admin, error) } type Service struct {