feat: add ToRoleResponse function and integrate logging in role service
This commit is contained in:
parent
02c3c97a76
commit
8d852521c4
|
|
@ -3,7 +3,9 @@ package dto
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
|
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/database/entities"
|
||||||
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
|
@ -145,3 +147,57 @@ type AssignMenuRequest struct {
|
||||||
type RemoveMenuRequest struct {
|
type RemoveMenuRequest struct {
|
||||||
MenuID []string `json:"menu_ids" binding:"required,dive,uuid4"`
|
MenuID []string `json:"menu_ids" binding:"required,dive,uuid4"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func ToRoleResponse(role entities.M_Role) RoleResponse {
|
||||||
|
var permissions []RolePermissionsResponse
|
||||||
|
for _, p := range role.Permissions {
|
||||||
|
permissions = append(permissions, RolePermissionsResponse{
|
||||||
|
ID: p.ID.String(),
|
||||||
|
Name: p.Name,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
var client pkgdto.IdNameResponse
|
||||||
|
if role.Client.ID != uuid.Nil {
|
||||||
|
client = pkgdto.IdNameResponse{
|
||||||
|
ID: role.Client.ID.String(),
|
||||||
|
Name: role.Client.Name,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var menus []RoleMenuResponse
|
||||||
|
for _, m := range role.Menus {
|
||||||
|
var parent *RoleMenuResponse
|
||||||
|
if m.Parent != nil {
|
||||||
|
parent = &RoleMenuResponse{
|
||||||
|
ID: m.Parent.ID.String(),
|
||||||
|
Name: m.Parent.Name,
|
||||||
|
IconUrl: m.Parent.IconUrl,
|
||||||
|
Url: m.Parent.Url,
|
||||||
|
Sequence: m.Parent.Sequence,
|
||||||
|
Parent: nil,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
menus = append(menus, RoleMenuResponse{
|
||||||
|
ID: m.ID.String(),
|
||||||
|
Name: m.Name,
|
||||||
|
IconUrl: m.IconUrl,
|
||||||
|
Url: m.Url,
|
||||||
|
Sequence: m.Sequence,
|
||||||
|
Parent: parent,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
return RoleResponse{
|
||||||
|
ID: role.ID.String(),
|
||||||
|
Name: role.Name,
|
||||||
|
Description: role.Description,
|
||||||
|
IconUrl: role.IconUrl,
|
||||||
|
Type: role.Type,
|
||||||
|
HomeUrl: role.HomeUrl,
|
||||||
|
Level: role.Level,
|
||||||
|
Client: client,
|
||||||
|
Permissions: permissions,
|
||||||
|
Menus: menus,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -11,7 +11,8 @@ import (
|
||||||
"github.com/Caknoooo/go-gin-clean-starter/modules/role/repository"
|
"github.com/Caknoooo/go-gin-clean-starter/modules/role/repository"
|
||||||
userDto "github.com/Caknoooo/go-gin-clean-starter/modules/user/dto"
|
userDto "github.com/Caknoooo/go-gin-clean-starter/modules/user/dto"
|
||||||
userService "github.com/Caknoooo/go-gin-clean-starter/modules/user/service"
|
userService "github.com/Caknoooo/go-gin-clean-starter/modules/user/service"
|
||||||
pkgdto "github.com/Caknoooo/go-gin-clean-starter/pkg/dto"
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/constants"
|
||||||
|
"github.com/Caknoooo/go-gin-clean-starter/pkg/utils"
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
|
|
@ -40,6 +41,7 @@ type roleService struct {
|
||||||
jwtService service.JWTService
|
jwtService service.JWTService
|
||||||
userService userService.UserService
|
userService userService.UserService
|
||||||
db *gorm.DB
|
db *gorm.DB
|
||||||
|
log *logrus.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetAll implements RoleService.
|
// GetAll implements RoleService.
|
||||||
|
|
@ -51,13 +53,12 @@ func (r *roleService) GetAll(ctx context.Context) ([]dto.RoleResponse, error) {
|
||||||
|
|
||||||
var responses []dto.RoleResponse
|
var responses []dto.RoleResponse
|
||||||
for _, role := range roles {
|
for _, role := range roles {
|
||||||
responses = append(responses, ToRoleResponse(role))
|
responses = append(responses, dto.ToRoleResponse(role))
|
||||||
}
|
}
|
||||||
|
|
||||||
return responses, nil
|
return responses, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AssignMenusToRole implements RoleService.
|
|
||||||
// AssignMenusToRole implements RoleService.
|
// AssignMenusToRole implements RoleService.
|
||||||
func (r *roleService) AssignMenusToRole(ctx context.Context, roleId string, menuIds []string) error {
|
func (r *roleService) AssignMenusToRole(ctx context.Context, roleId string, menuIds []string) error {
|
||||||
if len(menuIds) == 0 {
|
if len(menuIds) == 0 {
|
||||||
|
|
@ -257,14 +258,10 @@ func (r *roleService) AssignRolesToUser(ctx context.Context, userId string, role
|
||||||
|
|
||||||
// CreateRole implements RoleService.
|
// CreateRole implements RoleService.
|
||||||
func (r *roleService) CreateRole(ctx context.Context, req dto.RoleCreateRequest) (dto.RoleResponse, error) {
|
func (r *roleService) CreateRole(ctx context.Context, req dto.RoleCreateRequest) (dto.RoleResponse, error) {
|
||||||
// _, exists, err := r.roleRepo.CheckRoleName(ctx, r.db, req.Name)
|
userID := ""
|
||||||
// if err != nil && err != gorm.ErrRecordNotFound {
|
if ctx != nil {
|
||||||
// return dto.RoleResponse{}, err
|
userID = utils.GetUserID(ctx)
|
||||||
// }
|
}
|
||||||
// if exists {
|
|
||||||
// return dto.RoleResponse{}, dto.ErrRoleAlreadyExists
|
|
||||||
// }
|
|
||||||
|
|
||||||
clientUUID, err := uuid.Parse(req.ClientID)
|
clientUUID, err := uuid.Parse(req.ClientID)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dto.RoleResponse{}, err
|
return dto.RoleResponse{}, err
|
||||||
|
|
@ -277,27 +274,49 @@ func (r *roleService) CreateRole(ctx context.Context, req dto.RoleCreateRequest)
|
||||||
Type: req.Type,
|
Type: req.Type,
|
||||||
HomeUrl: req.HomeUrl,
|
HomeUrl: req.HomeUrl,
|
||||||
ClientID: clientUUID,
|
ClientID: clientUUID,
|
||||||
|
FullAuditTrail: utils.FillAuditTrail(ctx, constants.CREATE),
|
||||||
}
|
}
|
||||||
|
|
||||||
createdRole, err := r.roleRepo.CreateRole(ctx, r.db, role)
|
createdRole, err := r.roleRepo.CreateRole(ctx, r.db, role)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dto.RoleResponse{}, err
|
return dto.RoleResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := r.roleRepo.GetRoleByID(ctx, r.db, createdRole.ID.String())
|
result, err := r.roleRepo.GetRoleByID(ctx, r.db, createdRole.ID.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dto.RoleResponse{}, err
|
return dto.RoleResponse{}, err
|
||||||
}
|
}
|
||||||
return ToRoleResponse(result), nil
|
r.log.WithFields(logrus.Fields{
|
||||||
|
"user_id": userID,
|
||||||
|
"action": "create",
|
||||||
|
"entity": "role",
|
||||||
|
"entity_id": createdRole.ID.String(),
|
||||||
|
}).Info("Role created")
|
||||||
|
return dto.ToRoleResponse(result), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// DeleteRole implements RoleService.
|
// DeleteRole implements RoleService.
|
||||||
func (r *roleService) DeleteRole(ctx context.Context, id string) error {
|
func (r *roleService) DeleteRole(ctx context.Context, id string) error {
|
||||||
if _, err := r.roleRepo.GetRoleByID(ctx, r.db, id); err != nil {
|
role, err := r.roleRepo.GetRoleByID(ctx, r.db, id)
|
||||||
|
if err != nil {
|
||||||
return dto.ErrRoleNotFound
|
return dto.ErrRoleNotFound
|
||||||
}
|
}
|
||||||
return r.roleRepo.DeleteRole(ctx, r.db, id)
|
role.FullAuditTrail = utils.FillAuditTrail(ctx, constants.DELETE)
|
||||||
|
if _, err := r.roleRepo.UpdateRole(ctx, r.db, role); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if err := r.roleRepo.DeleteRole(ctx, r.db, id); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
userID := ""
|
||||||
|
if ctx != nil {
|
||||||
|
userID = utils.GetUserID(ctx)
|
||||||
|
}
|
||||||
|
r.log.WithFields(logrus.Fields{
|
||||||
|
"user_id": userID,
|
||||||
|
"action": "delete",
|
||||||
|
"entity": "role",
|
||||||
|
"entity_id": id,
|
||||||
|
}).Info("Role deleted")
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRoleByID implements RoleService.
|
// GetRoleByID implements RoleService.
|
||||||
|
|
@ -307,7 +326,7 @@ func (r *roleService) GetRoleByID(ctx context.Context, id string) (dto.RoleRespo
|
||||||
return dto.RoleResponse{}, err
|
return dto.RoleResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return ToRoleResponse(role), nil
|
return dto.ToRoleResponse(role), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRoles implements RoleService.
|
// GetRoles implements RoleService.
|
||||||
|
|
@ -316,44 +335,6 @@ func (r *roleService) GetRoles(ctx context.Context, filter query.RoleFilter) ([]
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetRolesByUserID implements RoleService.
|
// GetRolesByUserID implements RoleService.
|
||||||
// func (r *roleService) GetRolesByUserID(ctx context.Context, userId string) ([]dto.RoleResponse, error) {
|
|
||||||
// if _, err := r.userService.GetUserById(ctx, userId); err != nil {
|
|
||||||
// return nil, userDto.ErrUserNotFound
|
|
||||||
// }
|
|
||||||
|
|
||||||
// roles, err := r.roleRepo.GetRolesByUserID(ctx, r.db, userId)
|
|
||||||
// if err != nil {
|
|
||||||
// logrus.Error("Error fetching roles for user ", userId, ": ", err)
|
|
||||||
// return nil, err
|
|
||||||
// }
|
|
||||||
|
|
||||||
// logrus.Info("Fetched ", len(roles), " roles for user ", userId)
|
|
||||||
|
|
||||||
// var responses []dto.RoleResponse
|
|
||||||
// for _, role := range roles {
|
|
||||||
// var permissions []dto.RolePermissionsResponse
|
|
||||||
// for _, p := range role.Permissions {
|
|
||||||
// permissions = append(permissions, dto.RolePermissionsResponse{
|
|
||||||
// ID: p.ID.String(),
|
|
||||||
// Name: p.Name,
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
|
|
||||||
// responses = append(responses, dto.RoleResponse{
|
|
||||||
// ID: role.ID.String(),
|
|
||||||
// Name: role.Name,
|
|
||||||
// Description: role.Description,
|
|
||||||
// IconUrl: role.IconUrl,
|
|
||||||
// Type: role.Type,
|
|
||||||
// HomeUrl: role.HomeUrl,
|
|
||||||
// ClientID: role.ClientID.String(),
|
|
||||||
// Permissions: permissions,
|
|
||||||
// })
|
|
||||||
// }
|
|
||||||
|
|
||||||
// return responses, nil
|
|
||||||
// }
|
|
||||||
|
|
||||||
func (r *roleService) GetRolesByUserID(ctx context.Context, userId string) ([]dto.RoleResponse, error) {
|
func (r *roleService) GetRolesByUserID(ctx context.Context, userId string) ([]dto.RoleResponse, error) {
|
||||||
if _, err := r.userService.GetUserById(ctx, userId); err != nil {
|
if _, err := r.userService.GetUserById(ctx, userId); err != nil {
|
||||||
return nil, userDto.ErrUserNotFound
|
return nil, userDto.ErrUserNotFound
|
||||||
|
|
@ -366,7 +347,7 @@ func (r *roleService) GetRolesByUserID(ctx context.Context, userId string) ([]dt
|
||||||
|
|
||||||
var responses []dto.RoleResponse
|
var responses []dto.RoleResponse
|
||||||
for _, role := range roles {
|
for _, role := range roles {
|
||||||
responses = append(responses, ToRoleResponse(role))
|
responses = append(responses, dto.ToRoleResponse(role))
|
||||||
}
|
}
|
||||||
|
|
||||||
return responses, nil
|
return responses, nil
|
||||||
|
|
@ -470,16 +451,8 @@ func (r *roleService) UpdateRole(ctx context.Context, id string, req dto.RoleUpd
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dto.RoleResponse{}, dto.ErrRoleNotFound
|
return dto.RoleResponse{}, dto.ErrRoleNotFound
|
||||||
}
|
}
|
||||||
|
before := existingRole
|
||||||
if req.Name != "" {
|
if req.Name != "" {
|
||||||
// _, exists, err := r.roleRepo.CheckRoleName(ctx, r.db, req.Name)
|
|
||||||
|
|
||||||
// if err != nil {
|
|
||||||
// return dto.RoleResponse{}, err
|
|
||||||
// }
|
|
||||||
// if exists {
|
|
||||||
// return dto.RoleResponse{}, dto.ErrRoleAlreadyExists
|
|
||||||
// }
|
|
||||||
existingRole.Name = req.Name
|
existingRole.Name = req.Name
|
||||||
}
|
}
|
||||||
if req.Description != "" {
|
if req.Description != "" {
|
||||||
|
|
@ -497,72 +470,28 @@ func (r *roleService) UpdateRole(ctx context.Context, id string, req dto.RoleUpd
|
||||||
if req.Level != 0 {
|
if req.Level != 0 {
|
||||||
existingRole.Level = req.Level
|
existingRole.Level = req.Level
|
||||||
}
|
}
|
||||||
|
existingRole.FullAuditTrail = utils.FillAuditTrail(ctx, constants.UPDATE)
|
||||||
updatedRole, err := r.roleRepo.UpdateRole(ctx, r.db, existingRole)
|
updatedRole, err := r.roleRepo.UpdateRole(ctx, r.db, existingRole)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dto.RoleResponse{}, err
|
return dto.RoleResponse{}, err
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := r.roleRepo.GetRoleByID(ctx, r.db, updatedRole.ID.String())
|
result, err := r.roleRepo.GetRoleByID(ctx, r.db, updatedRole.ID.String())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return dto.RoleResponse{}, err
|
return dto.RoleResponse{}, err
|
||||||
}
|
}
|
||||||
|
changes := utils.GetChangedFields(before, result)
|
||||||
return ToRoleResponse(result), nil
|
userID := ""
|
||||||
}
|
if ctx != nil {
|
||||||
|
userID = utils.GetUserID(ctx)
|
||||||
func ToRoleResponse(role entities.M_Role) dto.RoleResponse {
|
|
||||||
var permissions []dto.RolePermissionsResponse
|
|
||||||
for _, p := range role.Permissions {
|
|
||||||
permissions = append(permissions, dto.RolePermissionsResponse{
|
|
||||||
ID: p.ID.String(),
|
|
||||||
Name: p.Name,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
var client pkgdto.IdNameResponse
|
|
||||||
if role.Client.ID != uuid.Nil {
|
|
||||||
client = pkgdto.IdNameResponse{
|
|
||||||
ID: role.Client.ID.String(),
|
|
||||||
Name: role.Client.Name,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
var menus []dto.RoleMenuResponse
|
|
||||||
for _, m := range role.Menus {
|
|
||||||
var parent *dto.RoleMenuResponse
|
|
||||||
if m.Parent != nil {
|
|
||||||
parent = &dto.RoleMenuResponse{
|
|
||||||
ID: m.Parent.ID.String(),
|
|
||||||
Name: m.Parent.Name,
|
|
||||||
IconUrl: m.Parent.IconUrl,
|
|
||||||
Url: m.Parent.Url,
|
|
||||||
Sequence: m.Parent.Sequence,
|
|
||||||
Parent: nil, // atau rekursif jika ingin nested lebih dalam
|
|
||||||
}
|
|
||||||
}
|
|
||||||
menus = append(menus, dto.RoleMenuResponse{
|
|
||||||
ID: m.ID.String(),
|
|
||||||
Name: m.Name,
|
|
||||||
IconUrl: m.IconUrl,
|
|
||||||
Url: m.Url,
|
|
||||||
Sequence: m.Sequence,
|
|
||||||
Parent: parent,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
return dto.RoleResponse{
|
|
||||||
ID: role.ID.String(),
|
|
||||||
Name: role.Name,
|
|
||||||
Description: role.Description,
|
|
||||||
IconUrl: role.IconUrl,
|
|
||||||
Type: role.Type,
|
|
||||||
HomeUrl: role.HomeUrl,
|
|
||||||
Level: role.Level,
|
|
||||||
Client: client,
|
|
||||||
Permissions: permissions,
|
|
||||||
Menus: menus,
|
|
||||||
}
|
}
|
||||||
|
r.log.WithFields(logrus.Fields{
|
||||||
|
"user_id": userID,
|
||||||
|
"action": "update",
|
||||||
|
"entity": "role",
|
||||||
|
"entity_id": id,
|
||||||
|
"changes": changes,
|
||||||
|
}).Info("Role updated")
|
||||||
|
return dto.ToRoleResponse(result), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewRoleService(
|
func NewRoleService(
|
||||||
|
|
@ -571,6 +500,7 @@ func NewRoleService(
|
||||||
jwtService service.JWTService,
|
jwtService service.JWTService,
|
||||||
userService userService.UserService,
|
userService userService.UserService,
|
||||||
db *gorm.DB,
|
db *gorm.DB,
|
||||||
|
log *logrus.Logger,
|
||||||
) RoleService {
|
) RoleService {
|
||||||
return &roleService{
|
return &roleService{
|
||||||
roleRepo: roleRepo,
|
roleRepo: roleRepo,
|
||||||
|
|
@ -578,5 +508,6 @@ func NewRoleService(
|
||||||
jwtService: jwtService,
|
jwtService: jwtService,
|
||||||
userService: userService,
|
userService: userService,
|
||||||
db: db,
|
db: db,
|
||||||
|
log: log,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -176,7 +176,7 @@ func RegisterDependencies(injector *do.Injector) {
|
||||||
sequenceServ := sequenceService.NewSequenceService(sequenceRepository, db)
|
sequenceServ := sequenceService.NewSequenceService(sequenceRepository, db)
|
||||||
userServ := userService.NewUserService(userRepository, roleRepository, warehouseRepository, clientRepository, refreshTokenRepository, jwtService, db)
|
userServ := userService.NewUserService(userRepository, roleRepository, warehouseRepository, clientRepository, refreshTokenRepository, jwtService, db)
|
||||||
productService := productService.NewProductService(productRepository, db, inventoryTransactionRepository, inventoryStorageRepository, categoryRepository, sequenceServ)
|
productService := productService.NewProductService(productRepository, db, inventoryTransactionRepository, inventoryStorageRepository, categoryRepository, sequenceServ)
|
||||||
roleServ := roleService.NewRoleService(roleRepository, refreshTokenRepository, jwtService, userServ, db)
|
roleServ := roleService.NewRoleService(roleRepository, refreshTokenRepository, jwtService, userServ, db, log)
|
||||||
menuSvc := menuService.NewMenuService(menuRepository, jwtService, db)
|
menuSvc := menuService.NewMenuService(menuRepository, jwtService, db)
|
||||||
maintenanceGroupServ := maintGroupService.NewMaintenanceGroupService(maintenanceGroupRepository, maintenanceGroupRoleRepository, maintenanceGroupRoleUserRepository, db)
|
maintenanceGroupServ := maintGroupService.NewMaintenanceGroupService(maintenanceGroupRepository, maintenanceGroupRoleRepository, maintenanceGroupRoleUserRepository, db)
|
||||||
clientServ := clientService.NewClientService(clientRepository, db)
|
clientServ := clientService.NewClientService(clientRepository, db)
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue