Adiciona controller do Ollama e reestrutura arquitetura
This commit is contained in:
141
src/modules/usuarios/usuarios.service.ts
Normal file
141
src/modules/usuarios/usuarios.service.ts
Normal file
@@ -0,0 +1,141 @@
|
||||
import bcrypt from 'bcrypt';
|
||||
import { Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { plainToInstance } from 'class-transformer';
|
||||
|
||||
import { Prisma, Usuario } from 'generated/prisma';
|
||||
|
||||
import { PrismaService } from '@prisma/prisma.service';
|
||||
import { PaginatedResponse } from '@shared/dto/paginated';
|
||||
import { UsuariosResponseDto } from './dto/usuarios.response';
|
||||
import { CreateUsuarioDto } from './dto/create-usuario-dto';
|
||||
|
||||
const SELECT = {
|
||||
id: true,
|
||||
uuid: true,
|
||||
email: true,
|
||||
email_verificado: true,
|
||||
nome: true,
|
||||
sobrenome: true,
|
||||
papel: true,
|
||||
status: true,
|
||||
ultimo_login_em: true,
|
||||
ultimo_login_ip: true,
|
||||
tentativas_login_falhadas: true,
|
||||
bloqueado_ate: true,
|
||||
criado_em: true,
|
||||
atualizado_em: true,
|
||||
} as const;
|
||||
|
||||
@Injectable()
|
||||
export class UsuariosService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
async list(): Promise<UsuariosResponseDto[]> {
|
||||
const data = await this.prisma.usuario.findMany({
|
||||
where: { email: { not: 'admin@clipperia.com' } },
|
||||
orderBy: { id: 'desc' },
|
||||
select: SELECT,
|
||||
});
|
||||
|
||||
return plainToInstance(UsuariosResponseDto, data, {
|
||||
excludeExtraneousValues: true,
|
||||
});
|
||||
}
|
||||
|
||||
async listPaginated(
|
||||
page: number,
|
||||
perPage: number,
|
||||
direction: 'asc' | 'desc' = 'desc',
|
||||
): Promise<PaginatedResponse<UsuariosResponseDto>> {
|
||||
const skip = page >= 1 ? page * perPage : 0;
|
||||
|
||||
const [rows, total] = await Promise.all([
|
||||
this.prisma.usuario.findMany({
|
||||
orderBy: { id: direction },
|
||||
skip,
|
||||
take: perPage ?? 1,
|
||||
select: SELECT,
|
||||
}),
|
||||
this.prisma.usuario.count(),
|
||||
]);
|
||||
|
||||
const content: UsuariosResponseDto[] = plainToInstance(
|
||||
UsuariosResponseDto,
|
||||
rows,
|
||||
{ excludeExtraneousValues: true },
|
||||
);
|
||||
|
||||
const totalPages = Math.max(1, Math.ceil(total / perPage));
|
||||
|
||||
return {
|
||||
content,
|
||||
pagination: {
|
||||
page,
|
||||
direction,
|
||||
perPage,
|
||||
total,
|
||||
totalPages,
|
||||
hasNext: page < totalPages,
|
||||
hasPrev: page > 1,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async get(uuid: string): Promise<UsuariosResponseDto> {
|
||||
const row = await this.prisma.usuario.findUnique({
|
||||
where: { uuid },
|
||||
select: SELECT,
|
||||
});
|
||||
|
||||
if (!row) {
|
||||
throw new NotFoundException('Usuário não encontrado');
|
||||
}
|
||||
|
||||
return plainToInstance(UsuariosResponseDto, row, {
|
||||
excludeExtraneousValues: true,
|
||||
});
|
||||
}
|
||||
|
||||
async update(id: number, data: Prisma.UsuarioUpdateInput): Promise<Usuario> {
|
||||
return this.prisma.usuario.update({
|
||||
where: { id },
|
||||
data,
|
||||
});
|
||||
}
|
||||
|
||||
async create(dto: CreateUsuarioDto): Promise<UsuariosResponseDto> {
|
||||
const { email, password, nome, sobrenome } = dto;
|
||||
|
||||
const senhaCriptografada = await bcrypt.hash(password, 10);
|
||||
|
||||
const usuario = await this.prisma.usuario.create({
|
||||
data: {
|
||||
email,
|
||||
password: senhaCriptografada,
|
||||
nome,
|
||||
sobrenome,
|
||||
},
|
||||
});
|
||||
|
||||
return plainToInstance(UsuariosResponseDto, usuario, {
|
||||
excludeExtraneousValues: true,
|
||||
});
|
||||
}
|
||||
|
||||
async delete(id: number): Promise<Usuario> {
|
||||
return this.prisma.usuario.delete({
|
||||
where: { id },
|
||||
});
|
||||
}
|
||||
|
||||
async emailVerificado(uuid: string): Promise<UsuariosResponseDto> {
|
||||
const usuario = await this.prisma.usuario.update({
|
||||
where: { uuid },
|
||||
data: { email_verificado: 'V' },
|
||||
});
|
||||
|
||||
return plainToInstance(UsuariosResponseDto, usuario, {
|
||||
excludeExtraneousValues: true,
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user