Compare commits
4
Commits
be64a85350
...
8626ef11ec
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8626ef11ec | ||
|
|
cbd90a7850 | ||
|
|
e281f4ef65 | ||
|
|
fd3bd0ae5d |
No files matched your search
@@ -24,6 +24,7 @@ require (
|
||||
github.com/goccy/go-yaml v1.18.0 // indirect
|
||||
github.com/golang-jwt/jwt/v4 v4.4.2 // indirect
|
||||
github.com/golang/snappy v0.0.4 // indirect
|
||||
github.com/google/go-cmp v0.7.0 // indirect
|
||||
github.com/google/uuid v1.6.0 // indirect
|
||||
github.com/jackc/pgpassfile v1.0.0 // indirect
|
||||
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
|
||||
|
||||
@@ -51,6 +51,8 @@ github.com/golang-jwt/jwt/v4 v4.4.2 h1:rcc4lwaZgFMCZ5jxF9ABolDcIHdBytAFgqFPbSJQA
|
||||
github.com/golang-jwt/jwt/v4 v4.4.2/go.mod h1:m21LjoU+eqJr34lmDMbreY2eSTRJ1cv77w39/MY0Ch0=
|
||||
github.com/golang/snappy v0.0.4 h1:yAGX7huGHXlcLOEtBnF4w7FQwA26wojNCwOYAEhLjQM=
|
||||
github.com/golang/snappy v0.0.4/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.7.0 h1:wk8382ETsv4JYUZwIsn6YpYiWiBsYLSJiTsyBybVuN8=
|
||||
github.com/google/go-cmp v0.7.0/go.mod h1:pXiqmnSA92OHEEa9HXL2W4E7lf9JzCmGVUdgjX3N/iU=
|
||||
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
|
||||
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
|
||||
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package access
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"testing"
|
||||
|
||||
"github.com/google/go-cmp/cmp"
|
||||
)
|
||||
|
||||
type testMenuMapping struct {
|
||||
testContext string
|
||||
model []RolePageModel
|
||||
expected []AvailableMenuResponse
|
||||
}
|
||||
|
||||
func TestMappingMenuModelToResponse(t *testing.T) {
|
||||
dataset := []testMenuMapping{
|
||||
{
|
||||
testContext: "all menu",
|
||||
model: []RolePageModel{
|
||||
{ID: "1", Name: "MenuParent1", Level: 1, Sort: 1},
|
||||
{ID: "2", Name: "MenuChild11", Level: 2, Sort: 1, ParentId: sql.NullString{String: "1"}},
|
||||
{ID: "3", Name: "MenuParent2", Level: 1, Sort: 2},
|
||||
{ID: "4", Name: "MenuChild21", Level: 2, Sort: 1, ParentId: sql.NullString{String: "3"}},
|
||||
{ID: "5", Name: "MenuChild22", Level: 2, Sort: 2, ParentId: sql.NullString{String: "3"}},
|
||||
{ID: "6", Name: "MenuChild23", Level: 2, Sort: 3, ParentId: sql.NullString{String: "3"}},
|
||||
},
|
||||
expected: []AvailableMenuResponse{
|
||||
{ID: "1", Header: "MenuParent1", Children: []*AvailableMenuChildResponse{
|
||||
{Title: "MenuChild11", Children: nil},
|
||||
}},
|
||||
{ID: "3", Header: "MenuParent2", Children: []*AvailableMenuChildResponse{
|
||||
{Title: "MenuChild21", Children: nil},
|
||||
{Title: "MenuChild22", Children: nil},
|
||||
{Title: "MenuChild23", Children: nil},
|
||||
}},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
for _, tCase := range dataset {
|
||||
t.Run(tCase.testContext, func(t *testing.T) {
|
||||
result := MapMenuModelToResponse(tCase.model)
|
||||
|
||||
if diff := cmp.Diff(tCase.expected, result); diff != "" {
|
||||
t.Errorf("mismatch (-expected +result):\n%s", diff)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -161,7 +161,7 @@ func (r accessRepo) GetAvailablePageByKeycloakId(c context.Context, keycloakId s
|
||||
user := &fetchUsers[0]
|
||||
|
||||
var permissionIds []string
|
||||
fetchUserPermission, err := r.getUserPermissionByUserId(c, dbconn, user.ID)
|
||||
fetchUserPermission, err := r.getUserActivePermissionByUserId(c, dbconn, user.ID)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
@@ -408,21 +408,19 @@ func (r accessRepo) UpdateRolePermission(c context.Context, req UpdateRolePageRe
|
||||
return err
|
||||
}
|
||||
|
||||
if req.Status { // insert when is_active = true
|
||||
// select active page
|
||||
var pageIds []string
|
||||
for _, page := range req.AccessPage {
|
||||
if page.IsActive {
|
||||
pageIds = append(pageIds, page.ID)
|
||||
}
|
||||
// select active page
|
||||
var pageIds []string
|
||||
for _, page := range req.AccessPage {
|
||||
if page.IsActive {
|
||||
pageIds = append(pageIds, page.ID)
|
||||
}
|
||||
}
|
||||
|
||||
// inserting new permission
|
||||
err = r.insertPagePermission(c, tx, req.ID, pageIds)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
// inserting new permission
|
||||
err = r.insertPagePermission(c, tx, req.ID, pageIds)
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
tx.Commit()
|
||||
@@ -557,7 +555,7 @@ func (r accessRepo) updateActivePermissionById(c context.Context, tx *sql.Tx, pe
|
||||
log.Printf("failed create update permission query : %v", err)
|
||||
return err
|
||||
}
|
||||
_, err = tx.ExecContext(c, sql, args)
|
||||
_, err = tx.ExecContext(c, sql, args...)
|
||||
if err != nil {
|
||||
log.Printf("failed executing update permission : %v", err)
|
||||
return err
|
||||
@@ -672,20 +670,36 @@ func (r accessRepo) getOrCreateUserPermission(c context.Context, db *sqlx.DB, tx
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r accessRepo) getUserPermissionByUserId(c context.Context, db *sqlx.DB, userId string) ([]RoleUserPermissionModel, error) {
|
||||
func (r accessRepo) getUserActivePermissionByUserId(c context.Context, db *sqlx.DB, userId string) ([]RoleUserPermissionModel, error) {
|
||||
var result []RoleUserPermissionModel
|
||||
|
||||
query := queryUtils.DynamicQuery{
|
||||
From: TBL_USER_PERMISSION,
|
||||
From: TBL_USER_PERMISSION,
|
||||
Aliases: "up",
|
||||
Fields: []queryUtils.SelectField{
|
||||
{Expression: "id"},
|
||||
{Expression: "up.id"},
|
||||
{Expression: "id_user"},
|
||||
{Expression: "id_permission"},
|
||||
},
|
||||
Joins: []queryUtils.Join{
|
||||
{
|
||||
Type: "LEFT",
|
||||
Table: TBL_PERMISSION,
|
||||
Alias: "p",
|
||||
OnConditions: queryUtils.FilterGroup{
|
||||
Filters: []queryUtils.DynamicFilter{
|
||||
{
|
||||
Column: "p.id", Operator: queryUtils.OpEqual, Value: "up.id_permission",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
Filters: []queryUtils.FilterGroup{
|
||||
{
|
||||
Filters: []queryUtils.DynamicFilter{
|
||||
{Column: "id_user", Operator: queryUtils.OpEqual, Value: userId},
|
||||
{Column: "p.is_active", Operator: queryUtils.OpEqual, Value: true},
|
||||
}, LogicOp: "AND",
|
||||
},
|
||||
},
|
||||
|
||||
@@ -67,11 +67,11 @@ type AccessPage struct {
|
||||
Page string `json:"page"`
|
||||
IsActive bool `json:"is_active" binding:"required"`
|
||||
Sort int `json:"sort"`
|
||||
ParentId *string `json:"parent_id"`
|
||||
ParentId *string `json:"parent_id" binding:"uuid"`
|
||||
}
|
||||
|
||||
type DetailRolePageResponse struct {
|
||||
ID string `json:"id" db:"id" binding:"required"`
|
||||
ID string `json:"id" db:"id" binding:"required,uuid"`
|
||||
Name string `json:"name" db:"name"`
|
||||
Status bool `json:"status" db:"is_active"`
|
||||
AccessPage []AccessPage `json:"access_page"`
|
||||
|
||||
@@ -35,6 +35,8 @@ func errorMessage(fe validator.FieldError) string {
|
||||
errorInfo = "harus diisi maksimal " + fe.Param()
|
||||
case "oneof":
|
||||
errorInfo = "hanya bisa diisi dengan : " + fe.Param()
|
||||
case "uuid":
|
||||
errorInfo = "harus diisi dengan uuid"
|
||||
default:
|
||||
errorInfo = "tidak valid"
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user