Spaces:
Sleeping
Sleeping
Trần Viết Sơn
commited on
Commit
•
eb9e504
1
Parent(s):
5bd68a6
fix spell check
Browse files
backend/src/common/interfaces/jwt-payload.interface.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1 |
interface JwtPayload {
|
2 |
sub: string; // id người dùng
|
3 |
-
|
4 |
username: string;
|
5 |
}
|
|
|
1 |
interface JwtPayload {
|
2 |
sub: string; // id người dùng
|
3 |
+
role: string; // quyền người dùng
|
4 |
username: string;
|
5 |
}
|
backend/src/modules/order/order.controller.ts
CHANGED
@@ -26,7 +26,7 @@ export class BranchOrderController {
|
|
26 |
@Body() createOrderDto: CreateOrderDto,
|
27 |
) {
|
28 |
const userId = req['user'].sub;
|
29 |
-
const role = req['user'].
|
30 |
console.log(req['user']);
|
31 |
if (role === Role.CUSTOMER) {
|
32 |
console.log('customer');
|
|
|
26 |
@Body() createOrderDto: CreateOrderDto,
|
27 |
) {
|
28 |
const userId = req['user'].sub;
|
29 |
+
const role = req['user'].role;
|
30 |
console.log(req['user']);
|
31 |
if (role === Role.CUSTOMER) {
|
32 |
console.log('customer');
|
backend/src/modules/user/user.service.ts
CHANGED
@@ -1,11 +1,22 @@
|
|
1 |
-
import {
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import { UserEntity } from '../../entities/user.entity.js';
|
3 |
import { SignUpDto } from '../authentication/dto/sign-up.dto.js';
|
4 |
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 |
-
import {
|
|
|
|
|
|
|
|
|
|
|
9 |
|
10 |
export type User = any;
|
11 |
|
@@ -16,32 +27,37 @@ export class UserService {
|
|
16 |
private jwtService: JwtService,
|
17 |
) {}
|
18 |
|
19 |
-
async create(signUpDto: SignUpDto): Promise<UserEntity | undefined>{
|
20 |
return UserEntity.create({
|
21 |
full_name: signUpDto.full_name,
|
22 |
phone_number: signUpDto.phone_number,
|
23 |
email: signUpDto.email,
|
24 |
-
hash_password: signUpDto.password
|
25 |
-
})
|
26 |
}
|
27 |
|
28 |
-
async save(userEntity: UserEntity): Promise<UserEntity | undefined>{
|
29 |
return UserEntity.save(userEntity);
|
30 |
}
|
31 |
|
32 |
-
async findOneByField(
|
33 |
-
|
34 |
-
|
|
|
|
|
|
|
35 |
});
|
36 |
}
|
37 |
|
38 |
-
async updateUserById(userId: string, updateUserDto: UpdateUserDto){
|
39 |
-
|
40 |
await this.validateService.checkExistField('email', updateUserDto.email);
|
41 |
-
await this.validateService.checkExistField(
|
|
|
|
|
|
|
42 |
|
43 |
const user = await UserEntity.findOne({
|
44 |
-
where: { id: userId }
|
45 |
});
|
46 |
if (!user) {
|
47 |
throw new NotFoundException(`User with ID ${userId} not found`);
|
@@ -50,14 +66,17 @@ export class UserService {
|
|
50 |
Object.assign(user, updateUserDto);
|
51 |
if (updateUserDto.hash_password) {
|
52 |
const saltRounds = 10;
|
53 |
-
user.hash_password = await bcrypt.hash(
|
|
|
|
|
|
|
54 |
}
|
55 |
await UserEntity.save(user);
|
56 |
|
57 |
-
const payload = { sub: user.id, username: user.full_name,
|
58 |
-
const token = await this.jwtService.signAsync(payload)
|
59 |
return {
|
60 |
-
access_token: token
|
61 |
};
|
62 |
}
|
63 |
|
@@ -74,15 +93,17 @@ export class UserService {
|
|
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 |
-
|
|
|
|
|
86 |
const paginateConfig: PaginateConfig<UserEntity> = {
|
87 |
sortableColumns: ['id', 'full_name', 'phone_number', 'email'],
|
88 |
nullSort: 'last',
|
@@ -95,15 +116,17 @@ export class UserService {
|
|
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 |
-
|
|
|
|
|
107 |
const paginateConfig: PaginateConfig<UserEntity> = {
|
108 |
sortableColumns: ['id', 'full_name', 'phone_number', 'email'],
|
109 |
nullSort: 'last',
|
@@ -116,7 +139,7 @@ export class UserService {
|
|
116 |
FilterOperator.GT,
|
117 |
FilterOperator.GTE,
|
118 |
],
|
119 |
-
item_type: [FilterOperator.EQ]
|
120 |
},
|
121 |
};
|
122 |
return paginate(query, queryBuilder, paginateConfig);
|
|
|
1 |
+
import {
|
2 |
+
Body,
|
3 |
+
forwardRef,
|
4 |
+
Inject,
|
5 |
+
Injectable,
|
6 |
+
NotFoundException,
|
7 |
+
} from '@nestjs/common';
|
8 |
import { UserEntity } from '../../entities/user.entity.js';
|
9 |
import { SignUpDto } from '../authentication/dto/sign-up.dto.js';
|
10 |
import { UpdateUserDto } from './dto/update-user-dto.js';
|
11 |
import { ValidateService } from '../../validate/validate.service.js';
|
12 |
import * as bcrypt from 'bcrypt';
|
13 |
import { JwtService } from '@nestjs/jwt';
|
14 |
+
import {
|
15 |
+
FilterOperator,
|
16 |
+
paginate,
|
17 |
+
PaginateConfig,
|
18 |
+
PaginateQuery,
|
19 |
+
} from 'nestjs-paginate';
|
20 |
|
21 |
export type User = any;
|
22 |
|
|
|
27 |
private jwtService: JwtService,
|
28 |
) {}
|
29 |
|
30 |
+
async create(signUpDto: SignUpDto): Promise<UserEntity | undefined> {
|
31 |
return UserEntity.create({
|
32 |
full_name: signUpDto.full_name,
|
33 |
phone_number: signUpDto.phone_number,
|
34 |
email: signUpDto.email,
|
35 |
+
hash_password: signUpDto.password,
|
36 |
+
});
|
37 |
}
|
38 |
|
39 |
+
async save(userEntity: UserEntity): Promise<UserEntity | undefined> {
|
40 |
return UserEntity.save(userEntity);
|
41 |
}
|
42 |
|
43 |
+
async findOneByField(
|
44 |
+
field: string,
|
45 |
+
value: any,
|
46 |
+
): Promise<UserEntity | undefined> {
|
47 |
+
return UserEntity.findOne({
|
48 |
+
where: { [field]: value },
|
49 |
});
|
50 |
}
|
51 |
|
52 |
+
async updateUserById(userId: string, updateUserDto: UpdateUserDto) {
|
|
|
53 |
await this.validateService.checkExistField('email', updateUserDto.email);
|
54 |
+
await this.validateService.checkExistField(
|
55 |
+
'phone_number',
|
56 |
+
updateUserDto.phone_number,
|
57 |
+
);
|
58 |
|
59 |
const user = await UserEntity.findOne({
|
60 |
+
where: { id: userId },
|
61 |
});
|
62 |
if (!user) {
|
63 |
throw new NotFoundException(`User with ID ${userId} not found`);
|
|
|
66 |
Object.assign(user, updateUserDto);
|
67 |
if (updateUserDto.hash_password) {
|
68 |
const saltRounds = 10;
|
69 |
+
user.hash_password = await bcrypt.hash(
|
70 |
+
updateUserDto.hash_password,
|
71 |
+
saltRounds,
|
72 |
+
); // Mã hóa mật khẩu
|
73 |
}
|
74 |
await UserEntity.save(user);
|
75 |
|
76 |
+
const payload = { sub: user.id, username: user.full_name, role: user.role };
|
77 |
+
const token = await this.jwtService.signAsync(payload);
|
78 |
return {
|
79 |
+
access_token: token,
|
80 |
};
|
81 |
}
|
82 |
|
|
|
93 |
FilterOperator.GT,
|
94 |
FilterOperator.GTE,
|
95 |
],
|
96 |
+
item_type: [FilterOperator.EQ],
|
97 |
},
|
98 |
};
|
99 |
return paginate(query, UserEntity.createQueryBuilder(), paginateConfig);
|
100 |
}
|
101 |
|
102 |
async findAllByName(fullName: string, query: PaginateQuery) {
|
103 |
+
const queryBuilder = UserEntity.createQueryBuilder('users').where(
|
104 |
+
'users.full_name = :fullName',
|
105 |
+
{ fullName },
|
106 |
+
);
|
107 |
const paginateConfig: PaginateConfig<UserEntity> = {
|
108 |
sortableColumns: ['id', 'full_name', 'phone_number', 'email'],
|
109 |
nullSort: 'last',
|
|
|
116 |
FilterOperator.GT,
|
117 |
FilterOperator.GTE,
|
118 |
],
|
119 |
+
item_type: [FilterOperator.EQ],
|
120 |
},
|
121 |
};
|
122 |
return paginate(query, queryBuilder, paginateConfig);
|
123 |
}
|
124 |
|
125 |
async findAllByRole(role: string, query: PaginateQuery) {
|
126 |
+
const queryBuilder = UserEntity.createQueryBuilder('users').where(
|
127 |
+
'users.role = :role',
|
128 |
+
{ role },
|
129 |
+
);
|
130 |
const paginateConfig: PaginateConfig<UserEntity> = {
|
131 |
sortableColumns: ['id', 'full_name', 'phone_number', 'email'],
|
132 |
nullSort: 'last',
|
|
|
139 |
FilterOperator.GT,
|
140 |
FilterOperator.GTE,
|
141 |
],
|
142 |
+
item_type: [FilterOperator.EQ],
|
143 |
},
|
144 |
};
|
145 |
return paginate(query, queryBuilder, paginateConfig);
|