diff --git a/docs/docs.go b/docs/docs.go index ff397e0..26ddab9 100644 --- a/docs/docs.go +++ b/docs/docs.go @@ -945,6 +945,43 @@ const docTemplate = `{ } }, "definitions": { + "access.AccessPage": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "is_active": { + "type": "boolean" + }, + "page": { + "type": "string" + }, + "parent_id": { + "type": "string" + } + } + }, + "access.DetailRolePageResponse": { + "type": "object", + "properties": { + "access_page": { + "type": "array", + "items": { + "$ref": "#/definitions/access.AccessPage" + } + }, + "id": { + "type": "string" + }, + "name": { + "type": "string" + }, + "status": { + "type": "boolean" + } + } + }, "access.ListRolePermissionPaginateResponse": { "type": "object", "properties": { @@ -965,7 +1002,7 @@ const docTemplate = `{ "id": { "type": "string" }, - "isActive": { + "is_active": { "type": "boolean" }, "name": { diff --git a/docs/swagger.json b/docs/swagger.json index 21b0de3..c6df21e 100644 --- a/docs/swagger.json +++ b/docs/swagger.json @@ -939,6 +939,43 @@ } }, "definitions": { + "access.AccessPage": { + "type": "object", + "properties": { + "id": { + "type": "string" + }, + "is_active": { + "type": "boolean" + }, + "page": { + "type": "string" + }, + "parent_id": { + "type": "string" + } + } + }, + "access.DetailRolePageResponse": { + "type": "object", + "properties": { + "access_page": { + "type": "array", + "items": { + "$ref": "#/definitions/access.AccessPage" + } + }, + "id": { + "type": "string" + }, + "name": { + "type": "string" + }, + "status": { + "type": "boolean" + } + } + }, "access.ListRolePermissionPaginateResponse": { "type": "object", "properties": { @@ -959,7 +996,7 @@ "id": { "type": "string" }, - "isActive": { + "is_active": { "type": "boolean" }, "name": { diff --git a/docs/swagger.yaml b/docs/swagger.yaml index 98fbe40..d85f617 100644 --- a/docs/swagger.yaml +++ b/docs/swagger.yaml @@ -1,5 +1,29 @@ basePath: /api definitions: + access.AccessPage: + properties: + id: + type: string + is_active: + type: boolean + page: + type: string + parent_id: + type: string + type: object + access.DetailRolePageResponse: + properties: + access_page: + items: + $ref: '#/definitions/access.AccessPage' + type: array + id: + type: string + name: + type: string + status: + type: boolean + type: object access.ListRolePermissionPaginateResponse: properties: data: @@ -13,7 +37,7 @@ definitions: properties: id: type: string - isActive: + is_active: type: boolean name: type: string diff --git a/internal/domain/access/handler.go b/internal/domain/access/handler.go index fb53a9f..793d7eb 100644 --- a/internal/domain/access/handler.go +++ b/internal/domain/access/handler.go @@ -169,8 +169,30 @@ func (h AccessHandler) ListRolePageSettings(c *gin.Context) { )) } +// DetailRolePageSettings godoc +// @Summary Detail Role Page Settings +// @Tags Access Role +// @Param id path string true "id role" +// @Success 200 {object} DetailRolePageResponse +// @Failure 500 {object} shared.BaseErrorResponse +// @Router /antrian-operasi/{id} [get] func (h AccessHandler) DetailRolePageSettings(c *gin.Context) { + idPermission := c.Param("id") + res, err := h.repo.DetailRolePermission(c, idPermission) + if err != nil { + c.JSON(500, shared.BaseErrorResponse{ + Success: false, + Code: 500, + Message: err.Error(), + }) + return + } + + c.JSON(200, + shared.ToBaseResponse( + res, true, 200, "success get detail role permission", + )) } func (h AccessHandler) UpdateRolePageSettings(c *gin.Context) { diff --git a/internal/domain/access/model.go b/internal/domain/access/model.go index 6e0b141..a933f24 100644 --- a/internal/domain/access/model.go +++ b/internal/domain/access/model.go @@ -3,9 +3,9 @@ package access import "database/sql" type RolePermissionModel struct { - ID string `db:"id"` - Name string `db:"name"` - IsActive bool `db:"is_active"` + ID string `db:"id" json:"id"` + Name string `db:"name" json:"name"` + IsActive bool `db:"is_active" json:"is_active"` } type RolePageModel struct { diff --git a/internal/domain/access/repository.go b/internal/domain/access/repository.go index 399d16f..dd42015 100644 --- a/internal/domain/access/repository.go +++ b/internal/domain/access/repository.go @@ -26,6 +26,7 @@ type IAccessRepository interface { GetAvailablePageByKeycloakId(c context.Context, keycloakId string) ([]RolePageModel, error) ListUserRole(c context.Context, q QueryListUserRole) (ListUserRolePaginateResponse, error) ListRolePermission(c context.Context, q QueryListRolePermission) (ListRolePermissionPaginateResponse, error) + DetailRolePermission(c context.Context, permission_id string) (DetailRolePageResponse, error) } type accessRepo struct { @@ -313,6 +314,72 @@ func (r accessRepo) ListRolePermission(c context.Context, q QueryListRolePermiss return result, nil } +func (r accessRepo) DetailRolePermission(c context.Context, permission_id string) (DetailRolePageResponse, error) { + var result DetailRolePageResponse + + // fetch role + query := queryUtils.DynamicQuery{ + From: TBL_PERMISSION, + Fields: []queryUtils.SelectField{ + {Expression: "id"}, + {Expression: "name"}, + {Expression: "is_active"}, + }, + Filters: []queryUtils.FilterGroup{ + { + Filters: []queryUtils.DynamicFilter{ + {Column: "id", Operator: queryUtils.OpEqual, Value: permission_id}, + }, LogicOp: "AND", + }, + }, + } + + dbconn, err := r.db.GetSQLXDB(DB_NAME) + if err != nil { + log.Printf("Unable to connect db : %s", err) + return result, err + } + + err = r.queryBuilder.ExecuteQueryRow( + c, dbconn, query, &result) + if err != nil { + log.Printf("Unable to execute query data : %s", err) + return result, err + } + + // fetch all pages -> compare with eligible pages -> map to DetailRolePageResponse.AccessPage + allPages, err := r.getAllPages(c, dbconn) + if err != nil { + return result, err + } + + // fetch eligible page for role + eligiblePagesMap := make(map[string]bool) + eligiblePages, err := r.getPageIdsByPermissionIds(c, dbconn, []string{permission_id}) + if err != nil { + return result, err + } + for _, p := range eligiblePages { + eligiblePagesMap[p.IdPage] = true + } + + for _, p := range allPages { + isActive := false + if eligiblePagesMap[p.ID] { + isActive = true + } + + result.AccessPage = append(result.AccessPage, AccessPage{ + ID: p.ID, + Page: p.Name, + IsActive: isActive, + ParentId: &p.ParentId.String, + }) + } + + return result, nil +} + // PRIVATE FUNCTIONS // Table user function @@ -667,4 +734,29 @@ func (r accessRepo) getPageByIds(c context.Context, db *sqlx.DB, ids []string) ( return result, nil } +func (r accessRepo) getAllPages(c context.Context, db *sqlx.DB) ([]RolePageModel, error) { + var result []RolePageModel + + query := queryUtils.DynamicQuery{ + From: TBL_ROLE_PAGES, + Fields: []queryUtils.SelectField{ + {Expression: "id"}, + {Expression: "name"}, + {Expression: "icon"}, + {Expression: "url"}, + {Expression: "level"}, + {Expression: "sort"}, + {Expression: "parent"}, + }, + } + + err := r.queryBuilder.ExecuteQuery(c, db, query, &result) + if err != nil { + log.Printf("error executing fetch pages : %v", err) + return nil, err + } + + return result, nil +} + // End page functions diff --git a/internal/domain/access/response.go b/internal/domain/access/response.go index e53bc1f..2155ef4 100644 --- a/internal/domain/access/response.go +++ b/internal/domain/access/response.go @@ -61,3 +61,16 @@ type ListRolePermissionPaginateResponse struct { Data []RolePermissionModel Paging shared.PaginationInfo } + +type AccessPage struct { + ID string `json:"id"` + Page string `json:"page"` + IsActive bool `json:"is_active"` + ParentId *string `json:"parent_id"` +} +type DetailRolePageResponse struct { + ID string `json:"id" db:"id"` + Name string `json:"name" db:"name"` + Status bool `json:"status" db:"is_active"` + AccessPage []AccessPage `json:"access_page"` +} diff --git a/internal/domain/access/routes.go b/internal/domain/access/routes.go index 3a4427d..b382ea7 100644 --- a/internal/domain/access/routes.go +++ b/internal/domain/access/routes.go @@ -15,4 +15,5 @@ func RegisterRoutes(r *gin.RouterGroup, dbService database.Service) { r.GET("/list-user", accessHandler.ListUserRole) r.GET("/role-permission", accessHandler.ListRolePageSettings) + r.GET("/role-permission/:id", accessHandler.DetailRolePageSettings) }