feat(niki): add get all agents by admin

This commit is contained in:
Erfan Mohammadi 2024-06-10 18:19:13 +03:30
parent d5add40d9e
commit 3e39c9b5e9
12 changed files with 292 additions and 2 deletions

View File

@ -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)
}

View File

@ -15,4 +15,5 @@ func (h Handler) SetRoutes(e *echo.Echo) {
r.POST("/login-by-phone", h.LoginByPhoneNumber) r.POST("/login-by-phone", h.LoginByPhoneNumber)
//nolint:gocritic //nolint:gocritic
//r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq" //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))
} }

View File

@ -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": { "/admins/login-by-phone": {
"post": { "post": {
"consumes": [ "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": { "adminserviceparam.LoginWithPhoneNumberRequest": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -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": { "/admins/login-by-phone": {
"post": { "post": {
"consumes": [ "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": { "adminserviceparam.LoginWithPhoneNumberRequest": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -157,6 +157,28 @@ definitions:
- $ref: '#/definitions/entity.AdminStatus' - $ref: '#/definitions/entity.AdminStatus'
example: 1 example: 1
type: object 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: adminserviceparam.LoginWithPhoneNumberRequest:
properties: properties:
password: password:
@ -785,6 +807,26 @@ paths:
summary: Reject a kindboxreq by admin summary: Reject a kindboxreq by admin
tags: tags:
- KindBoxReq - 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: /admins/login-by-phone:
post: post:
consumes: consumes:

View File

@ -9,4 +9,5 @@ const (
AdminKindBoxReqGetAllPermission = AdminPermission("kindboxreq-getall") AdminKindBoxReqGetAllPermission = AdminPermission("kindboxreq-getall")
AdminKindBoxReqDeliverPermission = AdminPermission("kindboxreq-deliver") AdminKindBoxReqDeliverPermission = AdminPermission("kindboxreq-deliver")
AdminKindBoxReqAssignSenderAgentPermission = AdminPermission("kindboxreq-assign_sender_agent") AdminKindBoxReqAssignSenderAgentPermission = AdminPermission("kindboxreq-assign_sender_agent")
AdminAdminGetAllAgentPermission = AdminPermission("admin-getall_agent")
) )

View File

@ -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"`
}

View File

@ -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
}

View File

@ -6,7 +6,8 @@ ALTER TABLE `admin_access_controls` MODIFY COLUMN `permission`
'kindboxreq-reject', 'kindboxreq-reject',
'kindboxreq-getall', 'kindboxreq-getall',
'kindboxreq-deliver', 'kindboxreq-deliver',
'kindboxreq-assign_sender_agent' 'kindboxreq-assign_sender_agent',
'admin-getall_agent'
) NOT NULL; ) NOT NULL;
-- +migrate Down -- +migrate Down

View File

@ -11,7 +11,9 @@ INSERT INTO `admin_access_controls` (`id`, `actor_id`, `actor_type`,`permission`
(9, 2 , 'role','kindboxreq-reject'), (9, 2 , 'role','kindboxreq-reject'),
(10, 2 , 'role','kindboxreq-getall'), (10, 2 , 'role','kindboxreq-getall'),
(11, 2 , 'role','kindboxreq-deliver'), (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 -- +migrate Down
DELETE DELETE

View File

@ -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
}

View File

@ -18,6 +18,7 @@ type Repository interface {
AddAdmin(ctx context.Context, admin entity.Admin) (entity.Admin, error) AddAdmin(ctx context.Context, admin entity.Admin) (entity.Admin, error)
GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error) GetAdminByPhoneNumber(ctx context.Context, phoneNumber string) (entity.Admin, error)
GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error) GetAdminByID(ctx context.Context, adminID uint) (entity.Admin, error)
GetAllAgent(ctx context.Context) ([]entity.Admin, error)
} }
type Service struct { type Service struct {