Artteiv commited on
Commit
9e8f9e3
1 Parent(s): 1f7602b

fix: update branch admin

Browse files
backend/package-lock.json CHANGED
The diff for this file is too large to render. See raw diff
 
backend/package.json CHANGED
@@ -1,95 +1,95 @@
1
- {
2
- "type": "module",
3
- "name": "backend",
4
- "version": "0.0.1",
5
- "description": "",
6
- "author": "",
7
- "private": true,
8
- "license": "UNLICENSED",
9
- "scripts": {
10
- "build": "nest build",
11
- "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
12
- "start": "nest start",
13
- "start:dev": "nest start --watch",
14
- "start:debug": "nest start --debug --watch",
15
- "start:prod": "node dist/main",
16
- "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
17
- "test": "jest",
18
- "test:watch": "jest --watch",
19
- "test:cov": "jest --coverage",
20
- "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
21
- "test:e2e": "jest --config ./test/jest-e2e.json",
22
- "typeorm": "typeorm-ts-node-esm",
23
- "db:migrate:generate": "npm run typeorm migration:generate -- -d ./src/config/typeorm.ts",
24
- "db:migrate:up": "npm run typeorm migration:run -- -d ./src/config/typeorm.ts",
25
- "db:migrate:down": "npm run typeorm migration:revert -- -d ./src/config/typeorm.ts"
26
- },
27
- "dependencies": {
28
- "@nestjs/common": "^10.0.0",
29
- "@nestjs/config": "^3.2.3",
30
- "@nestjs/core": "^10.0.0",
31
- "@nestjs/jwt": "^10.2.0",
32
- "@nestjs/mapped-types": "*",
33
- "@nestjs/passport": "^10.0.3",
34
- "@nestjs/platform-express": "^10.0.0",
35
- "@nestjs/typeorm": "^10.0.2",
36
- "bcrypt": "^5.1.1",
37
- "class-transformer": "^0.5.1",
38
- "class-validator": "^0.14.1",
39
- "dateformat": "^5.0.3",
40
- "dotenv": "^16.4.5",
41
- "mysql2": "^3.11.3",
42
- "nest-access-control": "^3.1.0",
43
- "nestjs-paginate": "^9.3.0",
44
- "passport": "^0.7.0",
45
- "passport-jwt": "^4.0.1",
46
- "passport-local": "^1.0.0",
47
- "pg": "^8.13.0",
48
- "reflect-metadata": "^0.2.0",
49
- "rxjs": "^7.8.1",
50
- "typeorm": "^0.3.20",
51
- "vnpay": "^1.6.0"
52
- },
53
- "devDependencies": {
54
- "@nestjs/cli": "^10.0.0",
55
- "@nestjs/schematics": "^10.0.0",
56
- "@nestjs/testing": "^10.0.0",
57
- "@types/dateformat": "^5.0.2",
58
- "@types/express": "^4.17.17",
59
- "@types/jest": "^29.5.2",
60
- "@types/multer": "^1.4.12",
61
- "@types/node": "^20.3.1",
62
- "@types/supertest": "^6.0.0",
63
- "@typescript-eslint/eslint-plugin": "^8.0.0",
64
- "@typescript-eslint/parser": "^8.0.0",
65
- "eslint": "^8.42.0",
66
- "eslint-config-prettier": "^9.0.0",
67
- "eslint-plugin-prettier": "^5.0.0",
68
- "jest": "^29.5.0",
69
- "prettier": "^3.0.0",
70
- "source-map-support": "^0.5.21",
71
- "supertest": "^7.0.0",
72
- "ts-jest": "^29.1.0",
73
- "ts-loader": "^9.4.3",
74
- "ts-node": "^10.9.2",
75
- "tsconfig-paths": "^4.2.0",
76
- "typescript": "^5.1.3"
77
- },
78
- "jest": {
79
- "moduleFileExtensions": [
80
- "js",
81
- "json",
82
- "ts"
83
- ],
84
- "rootDir": "src",
85
- "testRegex": ".*\\.spec\\.ts$",
86
- "transform": {
87
- "^.+\\.(t|j)s$": "ts-jest"
88
- },
89
- "collectCoverageFrom": [
90
- "**/*.(t|j)s"
91
- ],
92
- "coverageDirectory": "../coverage",
93
- "testEnvironment": "node"
94
- }
95
- }
 
1
+ {
2
+ "type": "module",
3
+ "name": "backend",
4
+ "version": "0.0.1",
5
+ "description": "",
6
+ "author": "",
7
+ "private": true,
8
+ "license": "UNLICENSED",
9
+ "scripts": {
10
+ "build": "nest build",
11
+ "format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
12
+ "start": "nest start",
13
+ "start:dev": "nest start --watch",
14
+ "start:debug": "nest start --debug --watch",
15
+ "start:prod": "node dist/main",
16
+ "lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
17
+ "test": "jest",
18
+ "test:watch": "jest --watch",
19
+ "test:cov": "jest --coverage",
20
+ "test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
21
+ "test:e2e": "jest --config ./test/jest-e2e.json",
22
+ "typeorm": "typeorm-ts-node-esm",
23
+ "db:migrate:generate": "npm run typeorm migration:generate -- -d ./src/config/typeorm.ts",
24
+ "db:migrate:up": "npm run typeorm migration:run -- -d ./src/config/typeorm.ts",
25
+ "db:migrate:down": "npm run typeorm migration:revert -- -d ./src/config/typeorm.ts"
26
+ },
27
+ "dependencies": {
28
+ "@nestjs/common": "^10.0.0",
29
+ "@nestjs/config": "^3.2.3",
30
+ "@nestjs/core": "^10.0.0",
31
+ "@nestjs/jwt": "^10.2.0",
32
+ "@nestjs/mapped-types": "*",
33
+ "@nestjs/passport": "^10.0.3",
34
+ "@nestjs/platform-express": "^10.0.0",
35
+ "@nestjs/typeorm": "^10.0.2",
36
+ "bcrypt": "^5.1.1",
37
+ "class-transformer": "^0.5.1",
38
+ "class-validator": "^0.14.1",
39
+ "dateformat": "^5.0.3",
40
+ "dotenv": "^16.4.5",
41
+ "mysql2": "^3.11.3",
42
+ "nest-access-control": "^3.1.0",
43
+ "nestjs-paginate": "^9.3.0",
44
+ "passport": "^0.7.0",
45
+ "passport-jwt": "^4.0.1",
46
+ "passport-local": "^1.0.0",
47
+ "pg": "^8.13.0",
48
+ "reflect-metadata": "^0.2.0",
49
+ "rxjs": "^7.8.1",
50
+ "typeorm": "^0.3.20",
51
+ "vnpay": "^1.6.0"
52
+ },
53
+ "devDependencies": {
54
+ "@nestjs/cli": "^10.0.0",
55
+ "@nestjs/schematics": "^10.0.0",
56
+ "@nestjs/testing": "^10.0.0",
57
+ "@types/dateformat": "^5.0.2",
58
+ "@types/express": "^4.17.17",
59
+ "@types/jest": "^29.5.2",
60
+ "@types/multer": "^1.4.12",
61
+ "@types/node": "^20.3.1",
62
+ "@types/supertest": "^6.0.0",
63
+ "@typescript-eslint/eslint-plugin": "^8.0.0",
64
+ "@typescript-eslint/parser": "^8.0.0",
65
+ "eslint": "^8.42.0",
66
+ "eslint-config-prettier": "^9.0.0",
67
+ "eslint-plugin-prettier": "^5.0.0",
68
+ "jest": "^29.5.0",
69
+ "prettier": "^3.0.0",
70
+ "source-map-support": "^0.5.21",
71
+ "supertest": "^7.0.0",
72
+ "ts-jest": "^29.1.0",
73
+ "ts-loader": "^9.4.3",
74
+ "ts-node": "^10.9.2",
75
+ "tsconfig-paths": "^4.2.0",
76
+ "typescript": "^5.1.3"
77
+ },
78
+ "jest": {
79
+ "moduleFileExtensions": [
80
+ "js",
81
+ "json",
82
+ "ts"
83
+ ],
84
+ "rootDir": "src",
85
+ "testRegex": ".*\\.spec\\.ts$",
86
+ "transform": {
87
+ "^.+\\.(t|j)s$": "ts-jest"
88
+ },
89
+ "collectCoverageFrom": [
90
+ "**/*.(t|j)s"
91
+ ],
92
+ "coverageDirectory": "../coverage",
93
+ "testEnvironment": "node"
94
+ }
95
+ }
backend/src/common/enums/VnpCardType.enum.ts CHANGED
@@ -1,5 +1,5 @@
1
- export enum VnpCardType {
2
- None = 0,
3
- ATM = 1,
4
- QRCODE = 2
5
  }
 
1
+ export enum VnpCardType {
2
+ None = 0,
3
+ ATM = 1,
4
+ QRCODE = 2
5
  }
backend/src/entities/branch.entity.ts CHANGED
@@ -2,6 +2,7 @@ import {
2
  BaseEntity,
3
  Column,
4
  CreateDateColumn,
 
5
  Entity,
6
  ManyToOne,
7
  OneToMany,
@@ -41,4 +42,7 @@ export class BranchEntity extends BaseEntity {
41
 
42
  @CreateDateColumn()
43
  create_at: Date;
 
 
 
44
  }
 
2
  BaseEntity,
3
  Column,
4
  CreateDateColumn,
5
+ DeleteDateColumn,
6
  Entity,
7
  ManyToOne,
8
  OneToMany,
 
42
 
43
  @CreateDateColumn()
44
  create_at: Date;
45
+
46
+ @DeleteDateColumn()
47
+ delete_at: Date;
48
  }
backend/src/entities/feed.entity.ts CHANGED
@@ -1,4 +1,11 @@
1
- import { Entity, Column, BaseEntity, PrimaryGeneratedColumn, CreateDateColumn } from 'typeorm';
 
 
 
 
 
 
 
2
 
3
  @Entity('feeds')
4
  export class FeedEntity extends BaseEntity {
@@ -19,4 +26,7 @@ export class FeedEntity extends BaseEntity {
19
 
20
  @CreateDateColumn()
21
  create_at: Date;
 
 
 
22
  }
 
1
+ import {
2
+ Entity,
3
+ Column,
4
+ BaseEntity,
5
+ PrimaryGeneratedColumn,
6
+ CreateDateColumn,
7
+ DeleteDateColumn,
8
+ } from 'typeorm';
9
 
10
  @Entity('feeds')
11
  export class FeedEntity extends BaseEntity {
 
26
 
27
  @CreateDateColumn()
28
  create_at: Date;
29
+
30
+ @DeleteDateColumn()
31
+ delete_at: Date;
32
  }
backend/src/entities/menu-item.entity.ts CHANGED
@@ -2,6 +2,7 @@ import {
2
  BaseEntity,
3
  Column,
4
  CreateDateColumn,
 
5
  Entity,
6
  OneToMany,
7
  PrimaryColumn,
@@ -35,4 +36,7 @@ export class MenuItemEntity extends BaseEntity {
35
 
36
  @CreateDateColumn()
37
  create_at: Date;
 
 
 
38
  }
 
2
  BaseEntity,
3
  Column,
4
  CreateDateColumn,
5
+ DeleteDateColumn,
6
  Entity,
7
  OneToMany,
8
  PrimaryColumn,
 
36
 
37
  @CreateDateColumn()
38
  create_at: Date;
39
+
40
+ @DeleteDateColumn()
41
+ delete_at: Date;
42
  }
backend/src/entities/order.entity.ts CHANGED
@@ -46,6 +46,9 @@ export class OrderEntity extends BaseEntity {
46
  @Column({ nullable: true })
47
  table_number: number;
48
 
 
 
 
49
  @Column()
50
  total_value: number;
51
 
 
46
  @Column({ nullable: true })
47
  table_number: number;
48
 
49
+ @Column({ nullable: true })
50
+ note: string;
51
+
52
  @Column()
53
  total_value: number;
54
 
backend/src/migrations/1730651201156-modify_payment.ts CHANGED
@@ -1,32 +1,32 @@
1
- import { MigrationInterface, QueryRunner } from "typeorm";
2
-
3
- export class ModifyPayment1730651201156 implements MigrationInterface {
4
- name = 'ModifyPayment1730651201156'
5
-
6
- public async up(queryRunner: QueryRunner): Promise<void> {
7
- await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "value"`);
8
- await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_amount" integer NOT NULL`);
9
- await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_bank_code" character varying`);
10
- await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_bank_tran_no" character varying`);
11
- await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_card_type" integer NOT NULL DEFAULT '0'`);
12
- await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_order_info" character varying`);
13
- await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_paydate" character varying`);
14
- await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_response_code" integer`);
15
- await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_transaction_no" character varying`);
16
- await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_transaction_status" integer`);
17
- }
18
-
19
- public async down(queryRunner: QueryRunner): Promise<void> {
20
- await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_transaction_status"`);
21
- await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_transaction_no"`);
22
- await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_response_code"`);
23
- await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_paydate"`);
24
- await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_order_info"`);
25
- await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_card_type"`);
26
- await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_bank_tran_no"`);
27
- await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_bank_code"`);
28
- await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_amount"`);
29
- await queryRunner.query(`ALTER TABLE "payments" ADD "value" integer NOT NULL`);
30
- }
31
-
32
- }
 
1
+ import { MigrationInterface, QueryRunner } from "typeorm";
2
+
3
+ export class ModifyPayment1730651201156 implements MigrationInterface {
4
+ name = 'ModifyPayment1730651201156'
5
+
6
+ public async up(queryRunner: QueryRunner): Promise<void> {
7
+ await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "value"`);
8
+ await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_amount" integer NOT NULL`);
9
+ await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_bank_code" character varying`);
10
+ await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_bank_tran_no" character varying`);
11
+ await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_card_type" integer NOT NULL DEFAULT '0'`);
12
+ await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_order_info" character varying`);
13
+ await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_paydate" character varying`);
14
+ await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_response_code" integer`);
15
+ await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_transaction_no" character varying`);
16
+ await queryRunner.query(`ALTER TABLE "payments" ADD "vnp_transaction_status" integer`);
17
+ }
18
+
19
+ public async down(queryRunner: QueryRunner): Promise<void> {
20
+ await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_transaction_status"`);
21
+ await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_transaction_no"`);
22
+ await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_response_code"`);
23
+ await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_paydate"`);
24
+ await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_order_info"`);
25
+ await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_card_type"`);
26
+ await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_bank_tran_no"`);
27
+ await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_bank_code"`);
28
+ await queryRunner.query(`ALTER TABLE "payments" DROP COLUMN "vnp_amount"`);
29
+ await queryRunner.query(`ALTER TABLE "payments" ADD "value" integer NOT NULL`);
30
+ }
31
+
32
+ }
backend/src/migrations/1730801455870-AddOrderAndPayment.ts ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { MigrationInterface, QueryRunner } from "typeorm";
2
+
3
+ export class AddOrderAndPayment1730801455870 implements MigrationInterface {
4
+ name = 'AddOrderAndPayment1730801455870'
5
+
6
+ public async up(queryRunner: QueryRunner): Promise<void> {
7
+ await queryRunner.query(`ALTER TABLE "feeds" ADD "delete_at" TIMESTAMP`);
8
+ await queryRunner.query(`ALTER TABLE "menu_items" ADD "delete_at" TIMESTAMP`);
9
+ await queryRunner.query(`ALTER TABLE "branches" ADD "delete_at" TIMESTAMP`);
10
+ await queryRunner.query(`ALTER TABLE "orders" ADD "note" character varying`);
11
+ }
12
+
13
+ public async down(queryRunner: QueryRunner): Promise<void> {
14
+ await queryRunner.query(`ALTER TABLE "orders" DROP COLUMN "note"`);
15
+ await queryRunner.query(`ALTER TABLE "branches" DROP COLUMN "delete_at"`);
16
+ await queryRunner.query(`ALTER TABLE "menu_items" DROP COLUMN "delete_at"`);
17
+ await queryRunner.query(`ALTER TABLE "feeds" DROP COLUMN "delete_at"`);
18
+ }
19
+
20
+ }
backend/src/modules/branch/branch.controller.ts CHANGED
@@ -22,6 +22,11 @@ export class BranchController {
22
  return this.branchService.create(createBranchDto);
23
  }
24
 
 
 
 
 
 
25
  @Get()
26
  async findAll() {
27
  return this.branchService.findAll();
@@ -42,7 +47,7 @@ export class BranchController {
42
 
43
  @Delete(':id')
44
  async remove(@Param('id') id: string) {
45
- return this.branchService.remove(id);
46
  }
47
 
48
  @Post(':id/menu-items')
 
22
  return this.branchService.create(createBranchDto);
23
  }
24
 
25
+ @Post(':id/restore')
26
+ restore(@Param('id') id: string) {
27
+ return this.branchService.restore(id);
28
+ }
29
+
30
  @Get()
31
  async findAll() {
32
  return this.branchService.findAll();
 
47
 
48
  @Delete(':id')
49
  async remove(@Param('id') id: string) {
50
+ return this.branchService.softRemove(id);
51
  }
52
 
53
  @Post(':id/menu-items')
backend/src/modules/branch/branch.service.ts CHANGED
@@ -46,8 +46,14 @@ export class BranchService {
46
  return await branch.save();
47
  }
48
 
49
- async remove(id: string) {
50
- let menuItem = await this.getBranchOrError(id);
51
- return await menuItem.remove();
 
 
 
 
 
 
52
  }
53
  }
 
46
  return await branch.save();
47
  }
48
 
49
+ async softRemove(id: string) {
50
+ let branch = await this.getBranchOrError(id);
51
+ return await branch.softRemove();
52
+ }
53
+
54
+ async restore(id: string) {
55
+ let result = await BranchEntity.getRepository().restore(id);
56
+ let branch = await BranchEntity.findOneBy({ id });
57
+ return branch;
58
  }
59
  }
backend/src/modules/order/order.service.ts CHANGED
@@ -17,7 +17,7 @@ export class OrderService {
17
  userId: string,
18
  createOrderDto: CreateOrderDto,
19
  ) {
20
- console.log('??');
21
  if (createOrderDto.order_type != OrderType.ONLINE) {
22
  throw new BadRequestException('customer cannot create offline order');
23
  }
@@ -124,6 +124,7 @@ export class OrderService {
124
  }
125
 
126
  findAll() {
 
127
  return `This action returns all order`;
128
  }
129
 
 
17
  userId: string,
18
  createOrderDto: CreateOrderDto,
19
  ) {
20
+ // Chưa được update đầy đủ, còn thiếu thông tin thanh toán.
21
  if (createOrderDto.order_type != OrderType.ONLINE) {
22
  throw new BadRequestException('customer cannot create offline order');
23
  }
 
124
  }
125
 
126
  findAll() {
127
+ //
128
  return `This action returns all order`;
129
  }
130
 
backend/src/modules/user/user.controller.ts CHANGED
@@ -1,46 +1,46 @@
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')
28
- async updateUser(@Request() req){
29
- const userId = req.user.sub;
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
- }
 
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')
28
+ async updateUser(@Request() req){
29
+ const userId = req.user.sub;
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
@@ -1,124 +1,124 @@
1
- import { Body, forwardRef, Inject, Injectable, NotFoundException } from '@nestjs/common';
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 { FilterOperator, paginate, PaginateConfig, PaginateQuery } from 'nestjs-paginate';
9
-
10
- export type User = any;
11
-
12
- @Injectable()
13
- export class UserService {
14
- constructor(
15
- private validateService: ValidateService,
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(field: string, value: any): Promise<UserEntity | undefined> {
33
- return UserEntity.findOne({
34
- where: { [field]: value }
35
- });
36
- }
37
-
38
- async updateUserById(userId: string, updateUserDto: UpdateUserDto){
39
-
40
- await this.validateService.checkExistField('email', updateUserDto.email);
41
- await this.validateService.checkExistField('phone_number', updateUserDto.phone_number);
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`);
48
- }
49
-
50
- Object.assign(user, updateUserDto);
51
- if (updateUserDto.hash_password) {
52
- const saltRounds = 10;
53
- user.hash_password = await bcrypt.hash(updateUserDto.hash_password, saltRounds); // Mã hóa mật khẩu
54
- }
55
- await UserEntity.save(user);
56
-
57
- const payload = { sub: user.id, username: user.full_name, roles: user.role };
58
- const token = await this.jwtService.signAsync(payload)
59
- return {
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
- }
 
1
+ import { Body, forwardRef, Inject, Injectable, NotFoundException } from '@nestjs/common';
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 { FilterOperator, paginate, PaginateConfig, PaginateQuery } from 'nestjs-paginate';
9
+
10
+ export type User = any;
11
+
12
+ @Injectable()
13
+ export class UserService {
14
+ constructor(
15
+ private validateService: ValidateService,
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(field: string, value: any): Promise<UserEntity | undefined> {
33
+ return UserEntity.findOne({
34
+ where: { [field]: value }
35
+ });
36
+ }
37
+
38
+ async updateUserById(userId: string, updateUserDto: UpdateUserDto){
39
+
40
+ await this.validateService.checkExistField('email', updateUserDto.email);
41
+ await this.validateService.checkExistField('phone_number', updateUserDto.phone_number);
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`);
48
+ }
49
+
50
+ Object.assign(user, updateUserDto);
51
+ if (updateUserDto.hash_password) {
52
+ const saltRounds = 10;
53
+ user.hash_password = await bcrypt.hash(updateUserDto.hash_password, saltRounds); // Mã hóa mật khẩu
54
+ }
55
+ await UserEntity.save(user);
56
+
57
+ const payload = { sub: user.id, username: user.full_name, roles: user.role };
58
+ const token = await this.jwtService.signAsync(payload)
59
+ return {
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
+ }