mirror of
https://github.com/dadosfera/maestro.git
synced 2026-10-30 02:09:09 +00:00
192 lines
5.4 KiB
TypeScript
192 lines
5.4 KiB
TypeScript
import { OnModuleInit, Inject, Injectable } from '@nestjs/common';
|
|
import { ClientGrpc } from '@nestjs/microservices';
|
|
import { DadosferaLogger } from 'dadosfera-logs';
|
|
|
|
import { ProtoServices } from 'protospack-v2/dist/lib/Duc';
|
|
import { UsersProtoService } from 'protospack-v2/dist/lib/Duc/interfaces/write-service';
|
|
import {
|
|
AssignRoleToUserRequest,
|
|
IdRequest,
|
|
UnassignRoleFromUserRequest,
|
|
UserCreateRequest,
|
|
} from 'protospack-v2/dist/lib/Duc/interfaces/messages';
|
|
import { lastValueFrom } from 'rxjs';
|
|
import {
|
|
BatchCreateUserReq,
|
|
CreateUserReq,
|
|
Hierarchy,
|
|
SetUserRolesReq,
|
|
UpdateUserReq,
|
|
} from './dtos/entities';
|
|
import { RolesService } from '../roles/roles.service';
|
|
import { HIERARCHIES } from './hierarchies';
|
|
import { LanguageEnum } from 'src/utils/languages.enum';
|
|
import { UserByCustomer } from 'protospack-v2/dist/lib/Duc/interfaces/entities';
|
|
|
|
@Injectable()
|
|
export class UsersService implements OnModuleInit {
|
|
logger: any;
|
|
language: LanguageEnum = LanguageEnum.ptbr;
|
|
|
|
private usersClientService: UsersProtoService;
|
|
constructor(
|
|
@Inject(DadosferaLogger)
|
|
dadosferaLogger: DadosferaLogger,
|
|
@Inject('DUC_PACKAGE') private readonly grpcClient: ClientGrpc,
|
|
private rolesService: RolesService,
|
|
) {
|
|
this.logger = dadosferaLogger.logger;
|
|
}
|
|
|
|
onModuleInit() {
|
|
this.usersClientService = this.grpcClient.getService<UsersProtoService>(
|
|
ProtoServices.UsersProtoService,
|
|
);
|
|
}
|
|
|
|
setLanguage(l: LanguageEnum = LanguageEnum.ptbr) {
|
|
if (Object.values(LanguageEnum).includes(l)) this.language = l;
|
|
this.rolesService.setLanguage(l);
|
|
}
|
|
|
|
async findAllUsersByCustomerId(customerId: string) {
|
|
const { users } = await lastValueFrom(
|
|
this.usersClientService.UserFindAllByCustomerId({ customerId }),
|
|
);
|
|
this.adjustUsersPayload(users);
|
|
const dintinctDepartments = [...new Set(users.map((u) => u.department))]
|
|
.filter((value) => value != null)
|
|
.sort();
|
|
const dintinctJobTitles = [...new Set(users.map((u) => u.jobTitle))]
|
|
.filter((value) => value != null)
|
|
.sort();
|
|
return {
|
|
users,
|
|
jobTitles: dintinctJobTitles,
|
|
departments: dintinctDepartments,
|
|
};
|
|
}
|
|
|
|
async findOneById(id: string) {
|
|
const { user } = await lastValueFrom(
|
|
this.usersClientService.UserFindOneById({ id }),
|
|
);
|
|
this.adjustUsersPayload([user]);
|
|
return { user };
|
|
}
|
|
|
|
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,
|
|
}),
|
|
);
|
|
this.adjustUsersPayload([user]);
|
|
return { user };
|
|
}
|
|
|
|
async updateUser(req: UpdateUserReq, id: string, customerId: string) {
|
|
const { department, hierarchy, jobTitle, name, roleNames } = req;
|
|
if (roleNames) {
|
|
await this.setRoles({ roleNames, userId: id }, customerId);
|
|
}
|
|
const { user } = await lastValueFrom(
|
|
this.usersClientService.UserUpdate({
|
|
name,
|
|
customerId: undefined,
|
|
id,
|
|
department,
|
|
hierarchy,
|
|
jobTitle,
|
|
}),
|
|
);
|
|
this.adjustUsersPayload([user]);
|
|
return { user };
|
|
}
|
|
|
|
async setRoles(body: SetUserRolesReq, customerId: string) {
|
|
const { roleNames, userId } = body;
|
|
const clearRoles = roleNames && roleNames.length === 0;
|
|
const { user } = await lastValueFrom(
|
|
this.usersClientService.UserSetRoles({
|
|
userId,
|
|
roleIds: [],
|
|
roleNames,
|
|
clearRoles,
|
|
customerId,
|
|
}),
|
|
);
|
|
this.adjustUsersPayload([user]);
|
|
return { user };
|
|
}
|
|
async batchCreateUser({ users }: BatchCreateUserReq) {
|
|
const usersToCreate: UserCreateRequest[] = users.map(
|
|
({ roleNames, email, department, hierarchy, jobTitle, name }) => ({
|
|
roleNames,
|
|
email,
|
|
department,
|
|
hierarchy,
|
|
jobTitle,
|
|
name,
|
|
roleIds: [],
|
|
}),
|
|
);
|
|
const { errorUsers, usersCreated } = await lastValueFrom(
|
|
this.usersClientService.UserBatchCreate({
|
|
users: usersToCreate,
|
|
}),
|
|
);
|
|
this.adjustUsersPayload(usersCreated);
|
|
return { errorUsers, usersCreated };
|
|
}
|
|
async assignRoleToUser(body: AssignRoleToUserRequest) {
|
|
return lastValueFrom(this.usersClientService.AssignRoleToUser(body));
|
|
}
|
|
|
|
async unassignRoleToUser(body: UnassignRoleFromUserRequest) {
|
|
return lastValueFrom(this.usersClientService.UnassignRoleFromUser(body));
|
|
}
|
|
|
|
async deleteUser(id: IdRequest) {
|
|
return lastValueFrom(this.usersClientService.UserRemove(id));
|
|
}
|
|
|
|
getAllHierarchies(): { hierarchies: Hierarchy[] } {
|
|
const hierarchies = Object.keys(HIERARCHIES).map((key) => ({
|
|
id: key,
|
|
name: HIERARCHIES[key]?.name[this.language] || key,
|
|
}));
|
|
return { hierarchies };
|
|
}
|
|
|
|
async getAllDepartments(customerId?: string) {
|
|
return lastValueFrom(
|
|
this.usersClientService.FindDistinctDepartments({ customerId }),
|
|
);
|
|
}
|
|
|
|
async getAllJobTitles(customerId?: string) {
|
|
return lastValueFrom(
|
|
this.usersClientService.FindDistinctJobTitles({ customerId }),
|
|
);
|
|
}
|
|
|
|
adjustUsersPayload(users: UserByCustomer[]) {
|
|
users?.forEach((u) => {
|
|
if (!u) return;
|
|
const roles: any[] = this.rolesService.getRolesPermissionsName(u.roles);
|
|
u.roles = roles;
|
|
u.hierarchy =
|
|
HIERARCHIES[u.hierarchy]?.name[this.language] || u.hierarchy;
|
|
});
|
|
return users;
|
|
}
|
|
}
|