backend/pkg/permission/usecase/user_role_usecase.go

153 lines
3.7 KiB
Go
Raw Permalink Normal View History

2025-10-07 09:29:47 +00:00
package usecase
import (
2025-10-10 15:25:36 +00:00
"backend/pkg/permission/domain"
"backend/pkg/permission/domain/entity"
"backend/pkg/permission/domain/repository"
"backend/pkg/permission/domain/usecase"
2025-10-07 09:29:47 +00:00
"context"
2025-10-10 15:25:36 +00:00
"fmt"
2025-10-07 09:29:47 +00:00
"time"
2025-10-10 15:25:36 +00:00
"go.mongodb.org/mongo-driver/v2/bson"
2025-10-07 09:29:47 +00:00
)
2025-10-10 15:25:36 +00:00
type UserRoleUseCaseParam struct {
UserRoleRepo repository.UserRoleRepository
RoleRepo repository.RoleRepository
}
2025-10-07 09:29:47 +00:00
type userRoleUseCase struct {
2025-10-10 15:25:36 +00:00
UserRoleUseCaseParam
2025-10-07 09:29:47 +00:00
}
// NewUserRoleUseCase 建立使用者角色 UseCase
2025-10-10 15:25:36 +00:00
func NewUserRoleUseCase(param UserRoleUseCaseParam) usecase.UserRoleUseCase {
2025-10-07 09:29:47 +00:00
return &userRoleUseCase{
2025-10-10 15:25:36 +00:00
UserRoleUseCaseParam: param,
2025-10-07 09:29:47 +00:00
}
}
func (uc *userRoleUseCase) Assign(ctx context.Context, req usecase.AssignRoleRequest) (*usecase.UserRoleResponse, error) {
// 檢查角色是否存在
2025-10-10 15:25:36 +00:00
role, err := uc.RoleRepo.GetByUID(ctx, req.RoleUID)
2025-10-07 09:29:47 +00:00
if err != nil {
return nil, err
}
2025-10-10 15:25:36 +00:00
if !role.Status.IsActive() {
return nil, fmt.Errorf("role is not active")
2025-10-07 09:29:47 +00:00
}
// 檢查使用者是否已有角色
2025-10-10 15:25:36 +00:00
exists, err := uc.UserRoleRepo.Exists(ctx, req.UserUID)
2025-10-07 09:29:47 +00:00
if err != nil {
return nil, err
}
if exists {
2025-10-10 15:25:36 +00:00
return nil, fmt.Errorf("user role already exists")
2025-10-07 09:29:47 +00:00
}
// 建立使用者角色
2025-10-10 15:25:36 +00:00
now := time.Now().Unix()
2025-10-07 09:29:47 +00:00
userRole := &entity.UserRole{
2025-10-10 15:25:36 +00:00
ID: bson.NewObjectID(),
2025-10-07 09:29:47 +00:00
UID: req.UserUID,
RoleID: req.RoleUID,
Brand: req.Brand,
2025-10-10 15:25:36 +00:00
Status: domain.RecordActive,
2025-10-07 09:29:47 +00:00
}
2025-10-10 15:25:36 +00:00
userRole.CreateTime = now
userRole.UpdateTime = now
2025-10-07 09:29:47 +00:00
2025-10-10 15:25:36 +00:00
if err := uc.UserRoleRepo.Create(ctx, userRole); err != nil {
2025-10-07 09:29:47 +00:00
return nil, err
}
return uc.toResponse(userRole), nil
}
func (uc *userRoleUseCase) Update(ctx context.Context, userUID, roleUID string) (*usecase.UserRoleResponse, error) {
// 檢查角色是否存在
2025-10-10 15:25:36 +00:00
role, err := uc.RoleRepo.GetByUID(ctx, roleUID)
2025-10-07 09:29:47 +00:00
if err != nil {
return nil, err
}
2025-10-10 15:25:36 +00:00
if !role.Status.IsActive() {
return nil, fmt.Errorf("role is not active")
2025-10-07 09:29:47 +00:00
}
// 更新使用者角色
2025-10-10 15:25:36 +00:00
userRole, err := uc.UserRoleRepo.Update(ctx, userUID, roleUID)
2025-10-07 09:29:47 +00:00
if err != nil {
return nil, err
}
return uc.toResponse(userRole), nil
}
func (uc *userRoleUseCase) Remove(ctx context.Context, userUID string) error {
2025-10-10 15:25:36 +00:00
return uc.UserRoleRepo.Delete(ctx, userUID)
2025-10-07 09:29:47 +00:00
}
func (uc *userRoleUseCase) Get(ctx context.Context, userUID string) (*usecase.UserRoleResponse, error) {
2025-10-10 15:25:36 +00:00
userRole, err := uc.UserRoleRepo.Get(ctx, userUID)
2025-10-07 09:29:47 +00:00
if err != nil {
return nil, err
}
return uc.toResponse(userRole), nil
}
func (uc *userRoleUseCase) GetByRole(ctx context.Context, roleUID string) ([]*usecase.UserRoleResponse, error) {
// 檢查角色是否存在
2025-10-10 15:25:36 +00:00
if _, err := uc.RoleRepo.GetByUID(ctx, roleUID); err != nil {
2025-10-07 09:29:47 +00:00
return nil, err
}
2025-10-10 15:25:36 +00:00
userRoles, err := uc.UserRoleRepo.GetByRoleID(ctx, roleUID)
2025-10-07 09:29:47 +00:00
if err != nil {
return nil, err
}
result := make([]*usecase.UserRoleResponse, 0, len(userRoles))
for _, ur := range userRoles {
result = append(result, uc.toResponse(ur))
}
return result, nil
}
func (uc *userRoleUseCase) List(ctx context.Context, filter usecase.UserRoleFilterRequest) ([]*usecase.UserRoleResponse, error) {
repoFilter := repository.UserRoleFilter{
Brand: filter.Brand,
RoleID: filter.RoleID,
Status: filter.Status,
}
2025-10-10 15:25:36 +00:00
userRoles, err := uc.UserRoleRepo.List(ctx, repoFilter)
2025-10-07 09:29:47 +00:00
if err != nil {
return nil, err
}
result := make([]*usecase.UserRoleResponse, 0, len(userRoles))
for _, ur := range userRoles {
result = append(result, uc.toResponse(ur))
}
return result, nil
}
func (uc *userRoleUseCase) toResponse(userRole *entity.UserRole) *usecase.UserRoleResponse {
return &usecase.UserRoleResponse{
UserUID: userRole.UID,
RoleUID: userRole.RoleID,
Brand: userRole.Brand,
2025-10-10 15:25:36 +00:00
CreateTime: time.Unix(userRole.CreateTime, 0).UTC().Format(time.RFC3339),
UpdateTime: time.Unix(userRole.UpdateTime, 0).UTC().Format(time.RFC3339),
2025-10-07 09:29:47 +00:00
}
}
2025-10-10 15:25:36 +00:00