diff --git a/delivery/http_server/admin/admin/profile.go b/delivery/http_server/admin/admin/profile.go new file mode 100644 index 00000000..47b73a6d --- /dev/null +++ b/delivery/http_server/admin/admin/profile.go @@ -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) +} diff --git a/delivery/http_server/admin/admin/route.go b/delivery/http_server/admin/admin/route.go index 2d3f820b..5379eb6b 100644 --- a/delivery/http_server/admin/admin/route.go +++ b/delivery/http_server/admin/admin/route.go @@ -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("/login-by-phone", h.LoginByPhoneNumber) r.POST("/refresh-access", h.RefreshAccess) + r.GET("/profile", h.Profile, middleware.Auth(h.authSvc)) //nolint:gocritic //r.PATCH("/:id", h.Update).Name = "admin-updatekindboxreq" } diff --git a/docs/docs.go b/docs/docs.go index ab6a6d40..82c871dd 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -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": { "get": { "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": { "type": "object", "properties": { diff --git a/docs/swagger.json b/docs/swagger.json index 296b8dce..a8a8814a 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -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": { "get": { "security": [ @@ -4126,6 +4183,20 @@ } } }, + "adminserviceparam.ProfileResponse": { + "type": "object", + "properties": { + "data": { + "$ref": "#/definitions/adminserviceparam.Data" + }, + "field_errors": { + "type": "object", + "additionalProperties": { + "type": "string" + } + } + } + }, "adminserviceparam.RefreshAccessRequest": { "type": "object", "properties": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index fd63facc..26cc118a 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -691,6 +691,15 @@ definitions: tokens: $ref: '#/definitions/adminserviceparam.Tokens' type: object + adminserviceparam.ProfileResponse: + properties: + data: + $ref: '#/definitions/adminserviceparam.Data' + field_errors: + additionalProperties: + type: string + type: object + type: object adminserviceparam.RefreshAccessRequest: properties: refresh_token: @@ -2193,6 +2202,42 @@ paths: summary: "Admin login by\tPhoneNumber" tags: - 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: get: consumes: diff --git a/param/admin/admin/profile.go b/param/admin/admin/profile.go new file mode 100644 index 00000000..5aa02365 --- /dev/null +++ b/param/admin/admin/profile.go @@ -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"` +} diff --git a/service/admin/admin/get.go b/service/admin/admin/get.go index fb135d58..86755237 100644 --- a/service/admin/admin/get.go +++ b/service/admin/admin/get.go @@ -17,3 +17,26 @@ func (s Service) AdminExistByID(ctx context.Context, req param.AdminExistByIDReq 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 +}