diff --git a/backend/src/libs/dto/param/pagination.params.ts b/backend/src/libs/dto/param/pagination.params.ts index c9b8989c1..dfcddcde6 100644 --- a/backend/src/libs/dto/param/pagination.params.ts +++ b/backend/src/libs/dto/param/pagination.params.ts @@ -13,4 +13,8 @@ export class PaginationParams { @IsNumber() @Min(1) size?: number; + + @IsOptional() + @Type(() => String) + searchUser?: string; } diff --git a/backend/src/modules/users/applications/get.user.application.ts b/backend/src/modules/users/applications/get.user.application.ts index b580683ed..58641c185 100644 --- a/backend/src/modules/users/applications/get.user.application.ts +++ b/backend/src/modules/users/applications/get.user.application.ts @@ -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); } } diff --git a/backend/src/modules/users/controller/users.controller.ts b/backend/src/modules/users/controller/users.controller.ts index c6571e2d7..b18b95f40 100644 --- a/backend/src/modules/users/controller/users.controller.ts +++ b/backend/src/modules/users/controller/users.controller.ts @@ -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' }) diff --git a/backend/src/modules/users/interfaces/applications/get.user.application.interface.ts b/backend/src/modules/users/interfaces/applications/get.user.application.interface.ts index b91dd53c5..7ac8d1b4b 100644 --- a/backend/src/modules/users/interfaces/applications/get.user.application.interface.ts +++ b/backend/src/modules/users/interfaces/applications/get.user.application.interface.ts @@ -13,6 +13,7 @@ export interface GetUserApplication { getAllUsersWithTeams( page?: number, - size?: number + size?: number, + searchUser?: string ): Promise<{ userWithTeams: LeanDocument[]; hasNextPage: boolean; page: number }>; } diff --git a/backend/src/modules/users/interfaces/services/get.user.service.interface.ts b/backend/src/modules/users/interfaces/services/get.user.service.interface.ts index 190e4a214..ced2ccd27 100644 --- a/backend/src/modules/users/interfaces/services/get.user.service.interface.ts +++ b/backend/src/modules/users/interfaces/services/get.user.service.interface.ts @@ -20,6 +20,7 @@ export interface GetUserService { getAllUsersWithTeams( page?: number, - size?: number + size?: number, + searchUser?: string ): Promise<{ userWithTeams: LeanDocument[]; hasNextPage: boolean; page: number }>; } diff --git a/backend/src/modules/users/repository/user.repository.interface.ts b/backend/src/modules/users/repository/user.repository.interface.ts index 206552886..0910924ab 100644 --- a/backend/src/modules/users/repository/user.repository.interface.ts +++ b/backend/src/modules/users/repository/user.repository.interface.ts @@ -7,5 +7,5 @@ export interface UserRepositoryInterface extends BaseInterfaceRepository { updateUserPassword(email: string, password: string): Promise; updateSuperAdmin(userId: string, isSAdmin: boolean): Promise; deleteUser(userId: string, withSession: boolean); - getAllWithPagination(page: number, size: number): Promise; + getAllWithPagination(page: number, size: number, searchUser?: string): Promise; } diff --git a/backend/src/modules/users/repository/user.repository.ts b/backend/src/modules/users/repository/user.repository.ts index 680b62af4..73f2af441 100644 --- a/backend/src/modules/users/repository/user.repository.ts +++ b/backend/src/modules/users/repository/user.repository.ts @@ -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'; @@ -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; + + 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 }) diff --git a/backend/src/modules/users/services/get.user.service.ts b/backend/src/modules/users/services/get.user.service.ts index aec87da18..cefdfcbed 100644 --- a/backend/src/modules/users/services/get.user.service.ts +++ b/backend/src/modules/users/services/get.user.service.ts @@ -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);