Skip to content

Commit

Permalink
chore: users all for admin
Browse files Browse the repository at this point in the history
  • Loading branch information
Matheusafonsouza committed Jan 10, 2025
1 parent 04ccca2 commit 88df14a
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 8 deletions.
22 changes: 22 additions & 0 deletions src/users/dtos/listUsersQuery.dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { IsEmail, IsNotEmpty, IsOptional } from 'class-validator';

export class ListUsersQueryDto {
@IsEmail()
@IsOptional()
email?: string;

@IsOptional()
firstName?: string;

@IsOptional()
lastName?: string;

@IsOptional()
phone?: string;

@IsNotEmpty()
perPage: number;

@IsNotEmpty()
page: number;
}
6 changes: 4 additions & 2 deletions src/users/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import {
Put,
UseGuards,
Request,
Query,
} from '@nestjs/common';
import { UpdateUserDto } from './dtos/updateUser.dto';
import { AuthGuard } from '../auth/auth.guard';
import { Roles } from '../auth/roles.decorator';
import { UserRoles } from '../database/entities/user.entity';
import { RolesGuard } from '../auth/roles.guard';
import { ListUsersQueryDto } from './dtos/listUsersQuery.dto';

@Controller('users')
export class UsersController {
Expand All @@ -22,8 +24,8 @@ export class UsersController {
@Roles(UserRoles.Admin)
@UseGuards(AuthGuard, RolesGuard)
@Get()
findAll() {
return this.usersService.findAll();
findAll(@Query() query: ListUsersQueryDto) {
return this.usersService.findAll(query);
}

@Get(':id')
Expand Down
17 changes: 13 additions & 4 deletions src/users/users.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ jest.mock('bcrypt', () => ({
const repositoryMockFactory = () => ({
find: jest.fn(),
findOneBy: jest.fn(),
findAndCount: jest.fn(),
save: jest.fn(),
delete: jest.fn(),
});
Expand All @@ -41,11 +42,19 @@ describe('UsersService', () => {
describe('findAll', () => {
it('should return an array of users', async () => {
const users = [new User(), new User()];
jest.spyOn(userRepository, 'find').mockResolvedValueOnce(users);
jest
.spyOn(userRepository, 'findAndCount')
.mockResolvedValueOnce([users, 2]);

const result = await service.findAll();
expect(result).toEqual(users);
expect(userRepository.find).toHaveBeenCalledTimes(1);
const result = await service.findAll({
perPage: 10,
page: 0,
});
expect(result).toEqual({
items: users,
total: 2,
});
expect(userRepository.findAndCount).toHaveBeenCalledTimes(1);
});
});

Expand Down
27 changes: 25 additions & 2 deletions src/users/users.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { User } from '../database/entities/user.entity';
import * as bcrypt from 'bcrypt';
import { ListUsersQueryDto } from './dtos/listUsersQuery.dto';

@Injectable()
export class UsersService {
Expand All @@ -16,8 +17,30 @@ export class UsersService {
private usersRepository: Repository<User>,
) {}

findAll(): Promise<User[]> {
return this.usersRepository.find();
async findAll(query: ListUsersQueryDto): Promise<{
items: User[];
total: number;
}> {
const whereConditions = {};
if (query.email) whereConditions['email'] = query.email;
if (query.firstName) whereConditions['firstName'] = query.firstName;
if (query.lastName) whereConditions['lastName'] = query.lastName;
if (query.phone) whereConditions['phone'] = query.phone;

const take = query.perPage || 10;
const skip = query.page || 0;

const [result, total] = await this.usersRepository.findAndCount({
where: whereConditions,
take: take,
skip: skip,
order: { createdAt: 'DESC' },
});

return {
items: result,
total,
};
}

findOne(id: string): Promise<User | null> {
Expand Down

0 comments on commit 88df14a

Please sign in to comment.