feat(admin): add admin my profile route(#217)

This commit is contained in:
Hamed Xamani 2024-12-03 18:43:30 +03:30
parent 5359a0746f
commit b87f02db05
7 changed files with 268 additions and 0 deletions

View File

@ -0,0 +1,47 @@
package adminhandler
import (
"net/http"
adminserviceparam "git.gocasts.ir/ebhomengo/niki/param/admin/admin"
"git.gocasts.ir/ebhomengo/niki/pkg/claim"
httpmsg "git.gocasts.ir/ebhomengo/niki/pkg/http_msg"
"github.com/labstack/echo/v4"
)
// Profile godoc
// @Summary Admin profile
// @Tags Admins
// @Accept json
// @Produce json
// @Success 200 {object} adminserviceparam.ProfileResponse
// @Failure 400 {string} "Bad request"
// @Failure 401 {string} "invalid or expired jwt"
// @Failure 403 {string} "user not allowed"
// @Failure 404 {string} "record not found"
// @Failure 500 {string} "something went wrong"
// @Security AuthBearerAdmin
// @Router /admins/profile [get].
func (h Handler) Profile(c echo.Context) error {
var req adminserviceparam.ProfileRequest
if bErr := c.Bind(&req); bErr != nil {
return echo.NewHTTPError(http.StatusBadRequest)
}
req.AdminID = claim.GetClaimsFromEchoContext(c).UserID
resp, sErr := h.adminSvc.AdminGetProfile(c.Request().Context(), req)
if sErr != nil {
msg, code := httpmsg.Error(sErr)
if resp.FieldErrors != nil {
return c.JSON(code, echo.Map{
"message": msg,
"errors": resp.FieldErrors,
})
}
return echo.NewHTTPError(code, msg)
}
return c.JSON(http.StatusOK, resp)
}

View File

@ -14,6 +14,7 @@ func (h Handler) SetRoutes(e *echo.Echo) {
r.POST("/register", h.Register, middleware.Auth(h.authSvc), middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminAdminRegisterPermission)) r.POST("/register", h.Register, middleware.Auth(h.authSvc), middleware.AdminAuthorization(h.adminAuthorizeSvc, entity.AdminAdminRegisterPermission))
r.POST("/login-by-phone", h.LoginByPhoneNumber) r.POST("/login-by-phone", h.LoginByPhoneNumber)
r.POST("/refresh-access", h.RefreshAccess) r.POST("/refresh-access", h.RefreshAccess)
r.GET("/profile", h.Profile, middleware.Auth(h.authSvc))
//nolint:gocritic //nolint:gocritic
//r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq" //r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq"
} }

View File

@ -1408,6 +1408,63 @@ const docTemplate = `{
} }
} }
}, },
"/admins/profile": {
"get": {
"security": [
{
"AuthBearerAdmin": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Admins"
],
"summary": "Admin profile",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/adminserviceparam.ProfileResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
},
"401": {
"description": "invalid or expired jwt",
"schema": {
"type": "string"
}
},
"403": {
"description": "user not allowed",
"schema": {
"type": "string"
}
},
"404": {
"description": "record not found",
"schema": {
"type": "string"
}
},
"500": {
"description": "something went wrong",
"schema": {
"type": "string"
}
}
}
}
},
"/admins/refer-times": { "/admins/refer-times": {
"get": { "get": {
"security": [ "security": [
@ -4137,6 +4194,20 @@ const docTemplate = `{
} }
} }
}, },
"adminserviceparam.ProfileResponse": {
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/adminserviceparam.Data"
},
"field_errors": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
},
"adminserviceparam.RefreshAccessRequest": { "adminserviceparam.RefreshAccessRequest": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -1397,6 +1397,63 @@
} }
} }
}, },
"/admins/profile": {
"get": {
"security": [
{
"AuthBearerAdmin": []
}
],
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"Admins"
],
"summary": "Admin profile",
"responses": {
"200": {
"description": "OK",
"schema": {
"$ref": "#/definitions/adminserviceparam.ProfileResponse"
}
},
"400": {
"description": "Bad request",
"schema": {
"type": "string"
}
},
"401": {
"description": "invalid or expired jwt",
"schema": {
"type": "string"
}
},
"403": {
"description": "user not allowed",
"schema": {
"type": "string"
}
},
"404": {
"description": "record not found",
"schema": {
"type": "string"
}
},
"500": {
"description": "something went wrong",
"schema": {
"type": "string"
}
}
}
}
},
"/admins/refer-times": { "/admins/refer-times": {
"get": { "get": {
"security": [ "security": [
@ -4126,6 +4183,20 @@
} }
} }
}, },
"adminserviceparam.ProfileResponse": {
"type": "object",
"properties": {
"data": {
"$ref": "#/definitions/adminserviceparam.Data"
},
"field_errors": {
"type": "object",
"additionalProperties": {
"type": "string"
}
}
}
},
"adminserviceparam.RefreshAccessRequest": { "adminserviceparam.RefreshAccessRequest": {
"type": "object", "type": "object",
"properties": { "properties": {

View File

@ -691,6 +691,15 @@ definitions:
tokens: tokens:
$ref: '#/definitions/adminserviceparam.Tokens' $ref: '#/definitions/adminserviceparam.Tokens'
type: object type: object
adminserviceparam.ProfileResponse:
properties:
data:
$ref: '#/definitions/adminserviceparam.Data'
field_errors:
additionalProperties:
type: string
type: object
type: object
adminserviceparam.RefreshAccessRequest: adminserviceparam.RefreshAccessRequest:
properties: properties:
refresh_token: refresh_token:
@ -2193,6 +2202,42 @@ paths:
summary: "Admin login by\tPhoneNumber" summary: "Admin login by\tPhoneNumber"
tags: tags:
- Admins - Admins
/admins/profile:
get:
consumes:
- application/json
produces:
- application/json
responses:
"200":
description: OK
schema:
$ref: '#/definitions/adminserviceparam.ProfileResponse'
"400":
description: Bad request
schema:
type: string
"401":
description: invalid or expired jwt
schema:
type: string
"403":
description: user not allowed
schema:
type: string
"404":
description: record not found
schema:
type: string
"500":
description: something went wrong
schema:
type: string
security:
- AuthBearerAdmin: []
summary: Admin profile
tags:
- Admins
/admins/refer-times: /admins/refer-times:
get: get:
consumes: consumes:

View File

@ -0,0 +1,10 @@
package adminserviceparam
type ProfileRequest struct {
AdminID uint `json:"-"`
}
type ProfileResponse struct {
Data Data `json:"data"`
FieldErrors map[string]string `json:"field_errors,omitempty"`
}

View File

@ -17,3 +17,26 @@ func (s Service) AdminExistByID(ctx context.Context, req param.AdminExistByIDReq
return param.AdminExistByIDResponse{Admin: admin}, nil return param.AdminExistByIDResponse{Admin: admin}, nil
} }
func (s Service) AdminGetProfile(ctx context.Context, req param.ProfileRequest) (param.ProfileResponse, error) {
const op = "adminservice.AdminGetProfile"
admin, err := s.repo.GetAdminByID(ctx, req.AdminID)
if err != nil {
return param.ProfileResponse{}, richerror.New(op).WithErr(err).WithKind(richerror.KindUnexpected)
}
return param.ProfileResponse{
Data: param.Data{
ID: admin.ID,
FirstName: admin.FirstName,
LastName: admin.LastName,
PhoneNumber: admin.PhoneNumber,
Role: admin.Role,
Description: admin.Description,
Email: admin.Email,
Gender: admin.Gender,
Status: admin.Status,
},
}, nil
}