Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: backend done for search user #705

Merged
merged 2 commits into from
Dec 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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