Ajustes e add usurios

This commit is contained in:
LeoMortari
2025-11-02 20:40:34 -03:00
parent ca38cee6e3
commit 0f2c051eb9
6 changed files with 151 additions and 52 deletions

View File

@@ -9,6 +9,17 @@ import { PaginatedResponse } from '@shared/dto/paginated';
import { UsuariosResponseDto } from './dto/usuarios.response';
import { CreateUsuarioDto } from './dto/create-usuario-dto';
type ListUsuariosFilters = {
name?: string;
email?: string;
};
type ListUsuariosPaginatedParams = ListUsuariosFilters & {
page?: number;
perPage?: number;
direction?: 'asc' | 'desc';
};
const SELECT = {
id: true,
uuid: true,
@@ -30,9 +41,37 @@ const SELECT = {
export class UsuariosService {
constructor(private readonly prisma: PrismaService) {}
async list(): Promise<UsuariosResponseDto[]> {
private buildWhere({
name,
email,
}: ListUsuariosFilters): Prisma.UsuarioWhereInput {
const andConditions: Prisma.UsuarioWhereInput[] = [
{ email: { not: 'admin@clipperia.com' } },
];
if (name) {
andConditions.push({
OR: [
{ nome: { contains: name, mode: 'insensitive' } },
{ sobrenome: { contains: name, mode: 'insensitive' } },
],
});
}
if (email) {
andConditions.push({ email: { contains: email, mode: 'insensitive' } });
}
return { AND: andConditions };
}
async list(
filters: ListUsuariosFilters = {},
): Promise<UsuariosResponseDto[]> {
const where = this.buildWhere(filters);
const data = await this.prisma.usuario.findMany({
where: { email: { not: 'admin@clipperia.com' } },
where,
orderBy: { id: 'desc' },
select: SELECT,
});
@@ -43,20 +82,35 @@ export class UsuariosService {
}
async listPaginated(
page: number,
perPage: number,
direction: 'asc' | 'desc' = 'desc',
params: ListUsuariosPaginatedParams,
): Promise<PaginatedResponse<UsuariosResponseDto>> {
const skip = page >= 1 ? page * perPage : 0;
const parsedPage = Number(params.page);
const safePage =
Number.isFinite(parsedPage) && parsedPage > 0
? Math.floor(parsedPage)
: 1;
const parsedPerPage = Number(params.perPage);
const safePerPage =
Number.isFinite(parsedPerPage) && parsedPerPage > 0
? Math.floor(parsedPerPage)
: 10;
const safeDirection: 'asc' | 'desc' =
params.direction === 'asc' ? 'asc' : 'desc';
const where = this.buildWhere(params);
const skip = (safePage - 1) * safePerPage;
const [rows, total] = await Promise.all([
this.prisma.usuario.findMany({
orderBy: { id: direction },
where,
orderBy: { id: safeDirection },
skip,
take: perPage ?? 1,
take: safePerPage,
select: SELECT,
}),
this.prisma.usuario.count(),
this.prisma.usuario.count({ where }),
]);
const content: UsuariosResponseDto[] = plainToInstance(
@@ -65,18 +119,18 @@ export class UsuariosService {
{ excludeExtraneousValues: true },
);
const totalPages = Math.max(1, Math.ceil(total / perPage));
const totalPages = Math.max(1, Math.ceil(total / safePerPage));
return {
content,
pagination: {
page,
direction,
perPage,
page: safePage,
direction: safeDirection,
perPage: safePerPage,
total,
totalPages,
hasNext: page < totalPages,
hasPrev: page > 1,
hasNext: safePage < totalPages,
hasPrev: safePage > 1,
},
};
}