mirror of
https://github.com/dadosfera/maestro.git
synced 2026-10-06 11:39:09 +00:00
215 lines
6.2 KiB
TypeScript
215 lines
6.2 KiB
TypeScript
import { OnModuleInit, Inject, Injectable } from '@nestjs/common';
|
|
import { ClientGrpc } from '@nestjs/microservices';
|
|
import { DadosferaLogger } from '@dadosfera/dadosfera-logs';
|
|
|
|
import { ProtoServices } from '@dadosfera/protospack-v2/dist/lib/Duc';
|
|
import {
|
|
CustomersProtoService,
|
|
UsersProtoService,
|
|
} from '@dadosfera/protospack-v2/dist/lib/Duc/interfaces/write-service';
|
|
import {
|
|
AssignRoleToUserRequest,
|
|
IdRequest,
|
|
UnassignRoleFromUserRequest,
|
|
UserCreateRequest,
|
|
} from '@dadosfera/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 '@dadosfera/protospack-v2/dist/lib/Duc/interfaces/entities';
|
|
import { EnrichErrorCode } from 'src/utils/ErrorBuilder';
|
|
import { DucClient } from '../duc/client.config';
|
|
|
|
@Injectable()
|
|
export class UsersService implements OnModuleInit {
|
|
logger: DadosferaLogger;
|
|
language: LanguageEnum = LanguageEnum.ptbr;
|
|
|
|
private usersClientService: UsersProtoService;
|
|
private customersClientService: CustomersProtoService;
|
|
constructor(
|
|
@Inject(DadosferaLogger)
|
|
dadosferaLogger: DadosferaLogger,
|
|
@Inject(DucClient.name) private readonly grpcClient: ClientGrpc,
|
|
private rolesService: RolesService,
|
|
) {
|
|
this.logger = dadosferaLogger.logger;
|
|
}
|
|
|
|
onModuleInit() {
|
|
this.usersClientService = this.grpcClient.getService(
|
|
ProtoServices.UsersProtoService,
|
|
);
|
|
this.customersClientService = this.grpcClient.getService(
|
|
ProtoServices.CustomersProtoService,
|
|
);
|
|
}
|
|
|
|
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 }),
|
|
);
|
|
const usersAdj = this.adjustUsersPayload(users);
|
|
const dintinctDepartments = [...new Set(usersAdj.map((u) => u.department))]
|
|
.filter((value) => value != null)
|
|
.sort();
|
|
const dintinctJobTitles = [...new Set(usersAdj.map((u) => u.jobTitle))]
|
|
.filter((value) => value != null)
|
|
.sort();
|
|
return {
|
|
users: usersAdj,
|
|
jobTitles: dintinctJobTitles,
|
|
departments: dintinctDepartments,
|
|
};
|
|
}
|
|
|
|
async findOneById(id: string) {
|
|
const { user } = await lastValueFrom(
|
|
this.usersClientService.UserFindOneById({ id }),
|
|
);
|
|
return { user: this.adjustUsersPayload([user])[0] };
|
|
}
|
|
|
|
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,
|
|
}),
|
|
);
|
|
return { user: this.adjustUsersPayload([user])[0] };
|
|
}
|
|
|
|
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,
|
|
id,
|
|
department,
|
|
hierarchy,
|
|
jobTitle,
|
|
}),
|
|
);
|
|
return { user: this.adjustUsersPayload([user])[0] };
|
|
}
|
|
|
|
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,
|
|
}),
|
|
);
|
|
|
|
return { user: this.adjustUsersPayload([user])[0] };
|
|
}
|
|
|
|
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,
|
|
}),
|
|
);
|
|
|
|
return {
|
|
usersCreated: this.adjustUsersPayload(usersCreated),
|
|
// mapping error code to message
|
|
errorUsers: errorUsers.map(({ user, error }) => ({
|
|
user,
|
|
error: EnrichErrorCode(error).message,
|
|
})),
|
|
};
|
|
}
|
|
|
|
async resendInvite(body: IdRequest) {
|
|
return lastValueFrom(this.usersClientService.UserResendInvite(body));
|
|
}
|
|
|
|
async assignRoleToUser(body: AssignRoleToUserRequest) {
|
|
return lastValueFrom(this.usersClientService.AssignRoleToUser(body));
|
|
}
|
|
|
|
async unassignRoleFromUser(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 }),
|
|
);
|
|
}
|
|
|
|
private adjustUsersPayload(users: UserByCustomer[]) {
|
|
return users.map((u) => {
|
|
if (!u) return undefined;
|
|
return {
|
|
...u,
|
|
roles: this.rolesService.getRolesPermissionsName(u.roles),
|
|
hierarchy: HIERARCHIES[u.hierarchy]?.name[this.language] || u.hierarchy,
|
|
};
|
|
});
|
|
}
|
|
|
|
async findAllCustomers() {
|
|
return await lastValueFrom(this.customersClientService.CustomerFindAll({}));
|
|
}
|
|
}
|