Skip to content

Commit

Permalink
feat: backend done for search user (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
GuiSanto authored Dec 19, 2022
1 parent 7aae98d commit cc73496
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 14 deletions.
4 changes: 4 additions & 0 deletions backend/src/libs/dto/param/pagination.params.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ export class PaginationParams {
@IsNumber()
@Min(1)
size?: number;

@IsOptional()
@Type(() => String)
searchUser?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ export class GetUserApplicationImpl implements GetUserApplication {
return this.getUserService.getAllUsersWithPagination(page, size);
}

getAllUsersWithTeams(page?: number, size?: number) {
return this.getUserService.getAllUsersWithTeams(page, size);
getAllUsersWithTeams(page?: number, size?: number, searchUser?: string) {
return this.getUserService.getAllUsersWithTeams(page, size, searchUser);
}
}
4 changes: 2 additions & 2 deletions backend/src/modules/users/controller/users.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,8 @@ export default class UsersController {
type: InternalServerErrorResponse
})
@Get('teams')
getAllUsersWithTeams(@Query() { page, size }: PaginationParams) {
return this.getUserApp.getAllUsersWithTeams(page, size);
getAllUsersWithTeams(@Query() { page, size, searchUser }: PaginationParams) {
return this.getUserApp.getAllUsersWithTeams(page, size, searchUser);
}

@ApiOperation({ summary: 'Update user is super admin' })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface GetUserApplication {

getAllUsersWithTeams(
page?: number,
size?: number
size?: number,
searchUser?: string
): Promise<{ userWithTeams: LeanDocument<UserWithTeams>[]; hasNextPage: boolean; page: number }>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface GetUserService {

getAllUsersWithTeams(
page?: number,
size?: number
size?: number,
searchUser?: string
): Promise<{ userWithTeams: LeanDocument<UserWithTeams>[]; hasNextPage: boolean; page: number }>;
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ export interface UserRepositoryInterface extends BaseInterfaceRepository<User> {
updateUserPassword(email: string, password: string): Promise<User>;
updateSuperAdmin(userId: string, isSAdmin: boolean): Promise<User>;
deleteUser(userId: string, withSession: boolean);
getAllWithPagination(page: number, size: number): Promise<User[]>;
getAllWithPagination(page: number, size: number, searchUser?: string): Promise<User[]>;
}
18 changes: 15 additions & 3 deletions backend/src/modules/users/repository/user.repository.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Injectable } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { FilterQuery, Model } from 'mongoose';
import { MongoGenericRepository } from 'src/libs/repositories/mongo/mongo-generic.repository';
import User, { UserDocument } from '../entities/user.schema';
import { UserRepositoryInterface } from './user.repository.interface';
Expand Down Expand Up @@ -45,9 +45,21 @@ export class UserRepository
return this.findOneAndRemove(userId, withSession);
}

getAllWithPagination(page: number, size: number) {
getAllWithPagination(page: number, size: number, searchUser?: string) {
let query: FilterQuery<UserDocument>;

if (searchUser) {
query = {
$or: [
{ firstName: { $regex: new RegExp('^.*' + searchUser + '.*$'), $options: 'i' } },
{ lastName: { $regex: new RegExp('^.*' + searchUser + '.*$'), $options: 'i' } },
{ email: { $regex: new RegExp('^.*' + searchUser + '.*$'), $options: 'i' } }
]
};
}

return this.model
.find()
.find(query)
.skip(page * size)
.limit(size)
.sort({ firstName: 1, lastName: 1 })
Expand Down
8 changes: 4 additions & 4 deletions backend/src/modules/users/services/get.user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ export default class GetUserServiceImpl implements GetUserService {
return this.userRepository.findAll({ password: 0, currentHashedRefreshToken: 0 });
}

getAllUsersWithPagination(page?: number, size?: number) {
return this.userRepository.getAllWithPagination(page, size);
getAllUsersWithPagination(page?: number, size?: number, searchUser?: string) {
return this.userRepository.getAllWithPagination(page, size, searchUser);
}

async getAllUsersWithTeams(page = 0, size = 15) {
const users = await this.getAllUsersWithPagination(page, size);
async getAllUsersWithTeams(page = 0, size = 15, searchUser?: string) {
const users = await this.getAllUsersWithPagination(page, size, searchUser);

const count = await this.userRepository.countDocuments();
const hasNextPage = page + 1 < Math.ceil(count / size);
Expand Down

0 comments on commit cc73496

Please sign in to comment.