FEAT: new user endpoints

This commit is contained in:
Gabriel Rosa
2022-07-18 17:50:12 -03:00
parent a55da8e20f
commit cfcb8c1c42
11 changed files with 158 additions and 50 deletions
+79 -21
View File
@@ -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));
}
}