mirror of
https://github.com/dadosfera/maestro.git
synced 2026-10-15 14:19:08 +00:00
FEAT: new user endpoints
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { ApiProperty, ApiPropertyOptional, OmitType } from '@nestjs/swagger';
|
||||
import { RoleDto } from 'src/modules/roles/dto/entities';
|
||||
import { RoleDto, UserRole } from 'src/modules/roles/dto/entities';
|
||||
export class Customer {
|
||||
@ApiProperty()
|
||||
id: string;
|
||||
@@ -23,8 +23,8 @@ export class User {
|
||||
name: string;
|
||||
@ApiProperty()
|
||||
email: string;
|
||||
@ApiProperty({ type: [RoleDto] })
|
||||
roles: RoleDto[];
|
||||
@ApiProperty({ type: [UserRole] })
|
||||
roles: UserRole[];
|
||||
@ApiProperty()
|
||||
jobTitle?: string;
|
||||
@ApiProperty()
|
||||
@@ -66,7 +66,7 @@ export class CreateUserReq {
|
||||
@ApiProperty()
|
||||
email: string;
|
||||
@ApiPropertyOptional()
|
||||
roles?: string[];
|
||||
roleNames?: string[];
|
||||
@ApiPropertyOptional()
|
||||
jobTitle?: string;
|
||||
@ApiPropertyOptional()
|
||||
@@ -84,15 +84,15 @@ export class BatchCreateUserReq {
|
||||
}
|
||||
export class BatchCreateUserRes {
|
||||
@ApiProperty({ type: [User] })
|
||||
users: User[];
|
||||
usersCreated: User[];
|
||||
@ApiProperty({ type: [CreateUserReq] })
|
||||
errorUsers: CreateUserReq[];
|
||||
}
|
||||
|
||||
export class UpdateUserReq {
|
||||
@ApiPropertyOptional()
|
||||
name?: string;
|
||||
@ApiPropertyOptional()
|
||||
roles?: string[];
|
||||
@ApiPropertyOptional()
|
||||
jobTitle?: string;
|
||||
@ApiPropertyOptional()
|
||||
department?: string;
|
||||
@@ -100,6 +100,11 @@ export class UpdateUserReq {
|
||||
hierarchy?: number;
|
||||
}
|
||||
|
||||
export class UpdateUserRes {
|
||||
@ApiProperty()
|
||||
user: UserByCustomer;
|
||||
}
|
||||
|
||||
export class GetAllUsersByCustomerIdReq {
|
||||
@ApiPropertyOptional()
|
||||
s?: string;
|
||||
@@ -131,4 +136,16 @@ export class SearchUserReq {
|
||||
s: string;
|
||||
}
|
||||
|
||||
export class SetUserRolesReq {
|
||||
@ApiProperty()
|
||||
userId: string;
|
||||
@ApiProperty()
|
||||
roleNames: string[];
|
||||
}
|
||||
|
||||
export class SetUserRolesRes {
|
||||
@ApiProperty()
|
||||
user: UserByCustomer;
|
||||
}
|
||||
|
||||
export class SearchUserRes extends GetAllUsersByCustomerIdRes {}
|
||||
|
||||
@@ -25,7 +25,11 @@ import {
|
||||
CreateUserRes,
|
||||
DeleteUserRes,
|
||||
GetAllUsersByCustomerIdRes,
|
||||
SetUserRolesReq,
|
||||
SetUserRolesRes,
|
||||
UnassignRoleToUserReq,
|
||||
UpdateUserReq,
|
||||
UpdateUserRes,
|
||||
UserByCustomer,
|
||||
} from './dtos/entities';
|
||||
import { UsersService } from './users.service';
|
||||
@@ -63,18 +67,6 @@ export class UsersController {
|
||||
return await this.userService.createUser(body);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@RequireAllPermissions(PERMISSIONS.USERS.permissions.ADMIN)
|
||||
@ApiCreatedResponse({ type: CreateUserRes })
|
||||
async updateUser(
|
||||
@User() user: RequestUser,
|
||||
@Body() body: CreateUserReq,
|
||||
@Param('id') id: string,
|
||||
) {
|
||||
this.logger.info('createUser', { user });
|
||||
return await this.userService.updateUser(body, id);
|
||||
}
|
||||
|
||||
@Post('batch')
|
||||
@RequireAllPermissions(PERMISSIONS.USERS.permissions.ADMIN)
|
||||
@ApiCreatedResponse({ type: BatchCreateUserRes })
|
||||
@@ -94,6 +86,14 @@ export class UsersController {
|
||||
return await this.userService.deleteUser({ id });
|
||||
}
|
||||
|
||||
@Patch('roles')
|
||||
@RequireAllPermissions(PERMISSIONS.USERS.permissions.ADMIN)
|
||||
@ApiOkResponse({ type: SetUserRolesRes })
|
||||
async setUserRoles(@User() user: RequestUser, @Body() body: SetUserRolesReq) {
|
||||
this.logger.info('createUser', { user });
|
||||
return await this.userService.setRoles(body);
|
||||
}
|
||||
|
||||
@Put('role')
|
||||
@RequireAllPermissions(PERMISSIONS.USERS.permissions.ADMIN)
|
||||
async assignRoleToUser(
|
||||
@@ -123,4 +123,16 @@ export class UsersController {
|
||||
this.logger.info('findOneById', { user });
|
||||
return await this.userService.findOneById(id);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
@RequireAllPermissions(PERMISSIONS.USERS.permissions.ADMIN)
|
||||
@ApiOkResponse({ type: UpdateUserRes })
|
||||
async updateUser(
|
||||
@User() user: RequestUser,
|
||||
@Body() body: UpdateUserReq,
|
||||
@Param('id') id: string,
|
||||
) {
|
||||
this.logger.info('createUser', { user });
|
||||
return await this.userService.updateUser(body, id);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,8 @@ import { UsersService } from './users.service';
|
||||
import { DadosferaLogger } from 'dadosfera-logs';
|
||||
import { ClientsModule } from '@nestjs/microservices';
|
||||
import { DucClient } from 'src/clients/duc/client.config';
|
||||
import { RolesModule } from '../roles/roles.module';
|
||||
import { PermissionsModule } from '../permissions/permissions.module';
|
||||
|
||||
const ducClient = new DucClient();
|
||||
|
||||
@@ -15,6 +17,8 @@ const ducClient = new DucClient();
|
||||
...ducClient.config(),
|
||||
},
|
||||
]),
|
||||
RolesModule,
|
||||
PermissionsModule,
|
||||
],
|
||||
controllers: [UsersController],
|
||||
providers: [UsersService, DadosferaLogger],
|
||||
|
||||
@@ -14,87 +14,145 @@ import { lastValueFrom } from 'rxjs';
|
||||
import {
|
||||
BatchCreateUserReq,
|
||||
CreateUserReq,
|
||||
SetUserRolesReq,
|
||||
UpdateUserReq,
|
||||
} from './dtos/entities';
|
||||
import { RolesService } from '../roles/roles.service';
|
||||
|
||||
@Injectable()
|
||||
export class UsersService implements OnModuleInit {
|
||||
logger: any;
|
||||
|
||||
private usersService: UsersProtoService;
|
||||
private usersClientService: UsersProtoService;
|
||||
constructor(
|
||||
@Inject(DadosferaLogger)
|
||||
dadosferaLogger: DadosferaLogger,
|
||||
@Inject('DUC_PACKAGE') private readonly grpcClient: ClientGrpc,
|
||||
private rolesService: RolesService,
|
||||
) {
|
||||
this.logger = dadosferaLogger.logger;
|
||||
}
|
||||
|
||||
onModuleInit() {
|
||||
this.usersService = this.grpcClient.getService<UsersProtoService>(
|
||||
this.usersClientService = this.grpcClient.getService<UsersProtoService>(
|
||||
ProtoServices.UsersProtoService,
|
||||
);
|
||||
}
|
||||
|
||||
async findAllUsersByCustomerId(customerId: string) {
|
||||
return lastValueFrom(
|
||||
this.usersService.UserFindAllByCustomerId({ customerId }),
|
||||
const { users } = await lastValueFrom(
|
||||
this.usersClientService.UserFindAllByCustomerId({ customerId }),
|
||||
);
|
||||
users?.forEach((u) => {
|
||||
const roles: any[] = this.rolesService.getRolesPermissionsName(u.roles);
|
||||
u.roles = roles;
|
||||
});
|
||||
return users;
|
||||
}
|
||||
|
||||
async findOneById(id: string) {
|
||||
return lastValueFrom(this.usersService.UserFindOneById({ id }));
|
||||
const { user } = await lastValueFrom(
|
||||
this.usersClientService.UserFindOneById({ id }),
|
||||
);
|
||||
if (user) {
|
||||
const roles: any[] = this.rolesService.getRolesPermissionsName(
|
||||
user.roles,
|
||||
);
|
||||
user.roles = roles;
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
async createUser(user: CreateUserReq) {
|
||||
const { email, department, hierarchy, jobTitle, name, roles } = user;
|
||||
return lastValueFrom(
|
||||
this.usersService.UserCreate({
|
||||
async createUser(req: CreateUserReq) {
|
||||
const { email, department, hierarchy, jobTitle, name, roleNames } = req;
|
||||
const { user } = await lastValueFrom(
|
||||
this.usersClientService.UserCreate({
|
||||
email,
|
||||
hierarchy,
|
||||
name,
|
||||
roleIds: [],
|
||||
department,
|
||||
jobTitle,
|
||||
roleNames: roles,
|
||||
roleNames,
|
||||
}),
|
||||
);
|
||||
if (user) {
|
||||
const roles: any[] = this.rolesService.getRolesPermissionsName(
|
||||
user.roles,
|
||||
);
|
||||
user.roles = roles;
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
async updateUser(user: UpdateUserReq, id: string) {
|
||||
const { department, hierarchy, jobTitle, name, roles } = user;
|
||||
return lastValueFrom(
|
||||
this.usersService.UserUpdate({
|
||||
async updateUser(req: UpdateUserReq, id: string) {
|
||||
const { department, hierarchy, jobTitle, name } = req;
|
||||
const { user } = await lastValueFrom(
|
||||
this.usersClientService.UserUpdate({
|
||||
name,
|
||||
customerId: '',
|
||||
customerId: undefined,
|
||||
id,
|
||||
department,
|
||||
hierarchy,
|
||||
jobTitle,
|
||||
}),
|
||||
);
|
||||
if (user) {
|
||||
const roles: any[] = this.rolesService.getRolesPermissionsName(
|
||||
user.roles,
|
||||
);
|
||||
user.roles = roles;
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
async setRoles(body: SetUserRolesReq) {
|
||||
const { roleNames, userId } = body;
|
||||
const clearRoles = roleNames && roleNames.length === 0;
|
||||
const { user } = await lastValueFrom(
|
||||
this.usersClientService.UserSetRoles({
|
||||
userId,
|
||||
roleIds: [],
|
||||
roleNames,
|
||||
clearRoles,
|
||||
}),
|
||||
);
|
||||
if (user) {
|
||||
const roles: any[] = this.rolesService.getRolesPermissionsName(
|
||||
user.roles,
|
||||
);
|
||||
user.roles = roles;
|
||||
}
|
||||
return user;
|
||||
}
|
||||
async batchCreateUser({ users }: BatchCreateUserReq) {
|
||||
const usersToCreate: UserCreateRequest[] = users.map((u) => ({
|
||||
...u,
|
||||
roleIds: [],
|
||||
roleNames: u.roles,
|
||||
roleNames: u.roleNames,
|
||||
name: u.name || '',
|
||||
hierarchy: u.hierarchy || null,
|
||||
}));
|
||||
return lastValueFrom(
|
||||
this.usersService.UserBatchCreate({
|
||||
const { errorUsers, usersCreated } = await lastValueFrom(
|
||||
this.usersClientService.UserBatchCreate({
|
||||
users: usersToCreate,
|
||||
}),
|
||||
);
|
||||
usersCreated?.forEach((u) => {
|
||||
const roles: any[] = this.rolesService.getRolesPermissionsName(u.roles);
|
||||
u.roles = roles;
|
||||
});
|
||||
return { errorUsers, usersCreated };
|
||||
}
|
||||
async assignRoleToUser(body: AssignRoleToUserRequest) {
|
||||
return lastValueFrom(this.usersService.AssignRoleToUser(body));
|
||||
return lastValueFrom(this.usersClientService.AssignRoleToUser(body));
|
||||
}
|
||||
|
||||
async unassignRoleToUser(body: UnassignRoleFromUserRequest) {
|
||||
return lastValueFrom(this.usersService.UnassignRoleFromUser(body));
|
||||
return lastValueFrom(this.usersClientService.UnassignRoleFromUser(body));
|
||||
}
|
||||
|
||||
async deleteUser(id: IdRequest) {
|
||||
return lastValueFrom(this.usersService.UserRemove(id));
|
||||
return lastValueFrom(this.usersClientService.UserRemove(id));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user