Trần Viết Sơn commited on
Commit
b7a6e24
2 Parent(s): 980e7c3 9caa8e7

Merge pull request #46 from PBL6-team-CATS/feature/user

Browse files
backend/src/modules/user/user.controller.ts CHANGED
@@ -1,17 +1,27 @@
1
- import { Body, Controller, Get, Param, Post, Request } from '@nestjs/common';
2
  import { UserService } from './user.service.js';
3
  import { UserEntity } from 'src/entities/user.entity.js';
4
- import { UpdateUserDto } from './dto/update-user-dto.js';
 
 
5
 
6
  @Controller('users')
7
  export class UsersController {
8
  constructor(private readonly usersService: UserService) {}
9
 
10
- @Get(':username')
11
- async getUser(
12
- @Param('username') username: string,
 
13
  ): Promise<UserEntity | undefined> {
14
- return this.usersService.findOne(username);
 
 
 
 
 
 
 
15
  }
16
 
17
  @Post('updateUser')
@@ -20,4 +30,17 @@ export class UsersController {
20
  const updateUserDto = req.body;
21
  return this.usersService.updateUserById(userId, updateUserDto);
22
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  }
 
1
+ import { Body, Controller, Get, Param, Post, Query, Request } from '@nestjs/common';
2
  import { UserService } from './user.service.js';
3
  import { UserEntity } from 'src/entities/user.entity.js';
4
+ import { Roles } from '../authentication/authorization/roles.decorator.js';
5
+ import { Role } from '../../common/enums/role.enum.js';
6
+ import { Paginate, PaginateQuery } from 'nestjs-paginate';
7
 
8
  @Controller('users')
9
  export class UsersController {
10
  constructor(private readonly usersService: UserService) {}
11
 
12
+ @Get('id')
13
+ @Roles(Role.ADMIN, Role.AREA_MANAGER, Role.BRANCH_MANAGER)
14
+ async getUserById(
15
+ @Query('id') id: string
16
  ): Promise<UserEntity | undefined> {
17
+ return this.usersService.findOneByField("id", id)
18
+ }
19
+
20
+ @Get('fullname')
21
+ @Roles(Role.ADMIN, Role.AREA_MANAGER, Role.BRANCH_MANAGER)
22
+ async getUserByFullname( @Query('full_name') fullName: string, @Paginate() query: PaginateQuery, ) {
23
+ console.log(fullName)
24
+ return this.usersService.findAllByName(fullName, query);
25
  }
26
 
27
  @Post('updateUser')
 
30
  const updateUserDto = req.body;
31
  return this.usersService.updateUserById(userId, updateUserDto);
32
  }
33
+
34
+ @Get('getAll')
35
+ @Roles(Role.ADMIN, Role.AREA_MANAGER, Role.BRANCH_MANAGER)
36
+ async findAllUser(@Paginate() query: PaginateQuery) {
37
+ return this.usersService.findAllUser(query);
38
+ }
39
+
40
+ @Get('role')
41
+ @Roles(Role.ADMIN, Role.AREA_MANAGER, Role.BRANCH_MANAGER)
42
+ async getUserByRole( @Query('role') role: string, @Paginate() query: PaginateQuery, ) {
43
+ console.log(role)
44
+ return this.usersService.findAllByRole(role, query);
45
+ }
46
  }
backend/src/modules/user/user.service.ts CHANGED
@@ -5,6 +5,7 @@ import { UpdateUserDto } from './dto/update-user-dto.js';
5
  import { ValidateService } from '../../validate/validate.service.js';
6
  import * as bcrypt from 'bcrypt';
7
  import { JwtService } from '@nestjs/jwt';
 
8
 
9
  export type User = any;
10
 
@@ -15,10 +16,6 @@ export class UserService {
15
  private jwtService: JwtService,
16
  ) {}
17
 
18
- async findOne(username: string): Promise<UserEntity | undefined> {
19
- return UserEntity.findOne({ where: { full_name: username } });
20
- }
21
-
22
  async create(signUpDto: SignUpDto): Promise<UserEntity | undefined>{
23
  return UserEntity.create({
24
  full_name: signUpDto.full_name,
@@ -63,4 +60,65 @@ export class UserService {
63
  access_token: token
64
  };
65
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
  }
 
5
  import { ValidateService } from '../../validate/validate.service.js';
6
  import * as bcrypt from 'bcrypt';
7
  import { JwtService } from '@nestjs/jwt';
8
+ import { FilterOperator, paginate, PaginateConfig, PaginateQuery } from 'nestjs-paginate';
9
 
10
  export type User = any;
11
 
 
16
  private jwtService: JwtService,
17
  ) {}
18
 
 
 
 
 
19
  async create(signUpDto: SignUpDto): Promise<UserEntity | undefined>{
20
  return UserEntity.create({
21
  full_name: signUpDto.full_name,
 
60
  access_token: token
61
  };
62
  }
63
+
64
+ async findAllUser(query: PaginateQuery) {
65
+ const paginateConfig: PaginateConfig<UserEntity> = {
66
+ sortableColumns: ['id', 'full_name', 'phone_number', 'email'],
67
+ nullSort: 'last',
68
+ defaultSortBy: [['id', 'DESC']],
69
+ searchableColumns: ['full_name'],
70
+ filterableColumns: {
71
+ full_name: [
72
+ FilterOperator.LT,
73
+ FilterOperator.LTE,
74
+ FilterOperator.GT,
75
+ FilterOperator.GTE,
76
+ ],
77
+ item_type: [FilterOperator.EQ]
78
+ },
79
+ };
80
+ return paginate(query, UserEntity.createQueryBuilder(), paginateConfig);
81
+ }
82
+
83
+ async findAllByName(fullName: string, query: PaginateQuery) {
84
+ const queryBuilder = UserEntity.createQueryBuilder('users')
85
+ .where('users.full_name = :fullName', { fullName });
86
+ const paginateConfig: PaginateConfig<UserEntity> = {
87
+ sortableColumns: ['id', 'full_name', 'phone_number', 'email'],
88
+ nullSort: 'last',
89
+ defaultSortBy: [['id', 'DESC']],
90
+ searchableColumns: ['full_name'],
91
+ filterableColumns: {
92
+ full_name: [
93
+ FilterOperator.LT,
94
+ FilterOperator.LTE,
95
+ FilterOperator.GT,
96
+ FilterOperator.GTE,
97
+ ],
98
+ item_type: [FilterOperator.EQ]
99
+ },
100
+ };
101
+ return paginate(query, queryBuilder, paginateConfig);
102
+ }
103
+
104
+ async findAllByRole(role: string, query: PaginateQuery) {
105
+ const queryBuilder = UserEntity.createQueryBuilder('users')
106
+ .where('users.role = :role', { role });
107
+ const paginateConfig: PaginateConfig<UserEntity> = {
108
+ sortableColumns: ['id', 'full_name', 'phone_number', 'email'],
109
+ nullSort: 'last',
110
+ defaultSortBy: [['id', 'DESC']],
111
+ searchableColumns: ['full_name'],
112
+ filterableColumns: {
113
+ full_name: [
114
+ FilterOperator.LT,
115
+ FilterOperator.LTE,
116
+ FilterOperator.GT,
117
+ FilterOperator.GTE,
118
+ ],
119
+ item_type: [FilterOperator.EQ]
120
+ },
121
+ };
122
+ return paginate(query, queryBuilder, paginateConfig);
123
+ }
124
  }