AnhLedger commited on
Commit
184b88f
1 Parent(s): c0f9d1c

update user, create migration, change role to enum

Browse files
backend/src/{modules/authentication/authorization → common}/enums/role.enum.ts RENAMED
File without changes
backend/src/entities/role.entity.ts DELETED
@@ -1,18 +0,0 @@
1
- import{
2
- Entity,
3
- Column,
4
- BaseEntity,
5
- PrimaryGeneratedColumn,
6
- } from 'typeorm'
7
-
8
- @Entity('role')
9
- export class RoleEntity extends BaseEntity{
10
- @PrimaryGeneratedColumn('uuid')
11
- id: string;
12
-
13
- @Column({ nullable: false})
14
- role_name: string;
15
-
16
- @Column({ nullable: true })
17
- description: string;
18
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
backend/src/entities/user.entity.ts CHANGED
@@ -9,8 +9,8 @@ import {
9
  JoinColumn,
10
  } from 'typeorm';
11
  import { BranchEntity } from './branch.entity.js';
12
- import { RoleEntity } from './role.entity.js';
13
  import { IsOptional } from 'class-validator';
 
14
 
15
  @Entity('users')
16
  export class UserEntity extends BaseEntity {
@@ -34,8 +34,8 @@ export class UserEntity extends BaseEntity {
34
  @Column({ nullable: true, unique: true })
35
  email: string;
36
 
37
- @Column({ default: 'f3750930-48ab-4c30-8681-d50e68e2bda7' })
38
- role_id: string;
39
 
40
  @Column()
41
  hash_password: string;
@@ -51,9 +51,4 @@ export class UserEntity extends BaseEntity {
51
  @IsOptional()
52
  @OneToMany(() => BranchEntity, (branch) => branch.owner)
53
  branches: Relation<BranchEntity>[];
54
-
55
- @IsOptional()
56
- @ManyToOne(() => RoleEntity)
57
- @JoinColumn({ name: 'role_id'})
58
- role: RoleEntity;
59
  }
 
9
  JoinColumn,
10
  } from 'typeorm';
11
  import { BranchEntity } from './branch.entity.js';
 
12
  import { IsOptional } from 'class-validator';
13
+ import { Role } from '../common/enums/role.enum.js';
14
 
15
  @Entity('users')
16
  export class UserEntity extends BaseEntity {
 
34
  @Column({ nullable: true, unique: true })
35
  email: string;
36
 
37
+ @Column({ type: 'enum', enum: Role, default: 'CUSTOMER' })
38
+ role: Role;
39
 
40
  @Column()
41
  hash_password: string;
 
51
  @IsOptional()
52
  @OneToMany(() => BranchEntity, (branch) => branch.owner)
53
  branches: Relation<BranchEntity>[];
 
 
 
 
 
54
  }
backend/src/migrations/1728840582079-Addf.ts DELETED
@@ -1,18 +0,0 @@
1
- import { MigrationInterface, QueryRunner } from "typeorm";
2
-
3
- export class Addf1728840582079 implements MigrationInterface {
4
- name = 'Addf1728840582079'
5
-
6
- public async up(queryRunner: QueryRunner): Promise<void> {
7
- await queryRunner.query(`CREATE TABLE "branches" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "name" character varying NOT NULL, "location" character varying NOT NULL, "phone_number" character varying NOT NULL, "create_at" TIMESTAMP NOT NULL DEFAULT now(), "ownerId" uuid, CONSTRAINT "PK_7f37d3b42defea97f1df0d19535" PRIMARY KEY ("id"))`);
8
- await queryRunner.query(`CREATE TABLE "users" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "avatar" character varying, "full_name" character varying, "phone_number" character varying, "address" character varying, "email" character varying, "role_id" integer, "hash_password" character varying, "is_valid" boolean NOT NULL DEFAULT true, "create_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_a3ffb1c0c8416b9fc6f907b7433" PRIMARY KEY ("id"))`);
9
- await queryRunner.query(`ALTER TABLE "branches" ADD CONSTRAINT "FK_8c6ae9f9c654c4fac71bccbb7ed" FOREIGN KEY ("ownerId") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
10
- }
11
-
12
- public async down(queryRunner: QueryRunner): Promise<void> {
13
- await queryRunner.query(`ALTER TABLE "branches" DROP CONSTRAINT "FK_8c6ae9f9c654c4fac71bccbb7ed"`);
14
- await queryRunner.query(`DROP TABLE "users"`);
15
- await queryRunner.query(`DROP TABLE "branches"`);
16
- }
17
-
18
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
backend/src/migrations/1729703705445-AddBranchMenus.ts DELETED
@@ -1,20 +0,0 @@
1
- import { MigrationInterface, QueryRunner } from "typeorm";
2
-
3
- export class AddBranchMenus1729703705445 implements MigrationInterface {
4
- name = 'AddBranchMenus1729703705445'
5
-
6
- public async up(queryRunner: QueryRunner): Promise<void> {
7
- await queryRunner.query(`CREATE TABLE "menu_items" ("id" character varying NOT NULL, "item_name" character varying NOT NULL, "image_url" character varying, "item_group_id" character varying, "description" character varying NOT NULL, "price" integer NOT NULL, "create_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_57e6188f929e5dc6919168620c8" PRIMARY KEY ("id"))`);
8
- await queryRunner.query(`CREATE TABLE "branch_menu" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "branch_id" character varying NOT NULL, "menu_id" character varying NOT NULL, "description" character varying NOT NULL, "is_open" boolean NOT NULL, "branchId" uuid, "menuItemId" character varying, CONSTRAINT "PK_977becffe98bbc626a56031b9e7" PRIMARY KEY ("id"))`);
9
- await queryRunner.query(`ALTER TABLE "branch_menu" ADD CONSTRAINT "FK_e0c721a124fa03ea4cef6f28f42" FOREIGN KEY ("branchId") REFERENCES "branches"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
10
- await queryRunner.query(`ALTER TABLE "branch_menu" ADD CONSTRAINT "FK_cbfb42df5887653593974e3e285" FOREIGN KEY ("menuItemId") REFERENCES "menu_items"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
11
- }
12
-
13
- public async down(queryRunner: QueryRunner): Promise<void> {
14
- await queryRunner.query(`ALTER TABLE "branch_menu" DROP CONSTRAINT "FK_cbfb42df5887653593974e3e285"`);
15
- await queryRunner.query(`ALTER TABLE "branch_menu" DROP CONSTRAINT "FK_e0c721a124fa03ea4cef6f28f42"`);
16
- await queryRunner.query(`DROP TABLE "branch_menu"`);
17
- await queryRunner.query(`DROP TABLE "menu_items"`);
18
- }
19
-
20
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
backend/src/migrations/1729780814885-AddFeeds.ts DELETED
@@ -1,14 +0,0 @@
1
- import { MigrationInterface, QueryRunner } from "typeorm";
2
-
3
- export class AddFeeds1729780814885 implements MigrationInterface {
4
- name = 'AddFeeds1729780814885'
5
-
6
- public async up(queryRunner: QueryRunner): Promise<void> {
7
- await queryRunner.query(`CREATE TABLE "feeds" ("id" SERIAL NOT NULL, "author_id" character varying, "image_url" character varying, "title" character varying NOT NULL, "description" character varying, "create_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_3dafbf766ecbb1eb2017732153f" PRIMARY KEY ("id"))`);
8
- }
9
-
10
- public async down(queryRunner: QueryRunner): Promise<void> {
11
- await queryRunner.query(`DROP TABLE "feeds"`);
12
- }
13
-
14
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
backend/src/migrations/1729846207793-unique-email-phone.ts DELETED
@@ -1,19 +0,0 @@
1
- import { MigrationInterface, QueryRunner } from "typeorm";
2
-
3
- export class UniqueEmailPhone1729846207793 implements MigrationInterface {
4
- name = 'UniqueEmailPhone1729846207793'
5
-
6
- public async up(queryRunner: QueryRunner): Promise<void> {
7
- await queryRunner.query(`ALTER TABLE "users" ADD CONSTRAINT "UQ_17d1817f241f10a3dbafb169fd2" UNIQUE ("phone_number")`);
8
- await queryRunner.query(`ALTER TABLE "users" ADD CONSTRAINT "UQ_97672ac88f789774dd47f7c8be3" UNIQUE ("email")`);
9
- await queryRunner.query(`ALTER TABLE "users" ALTER COLUMN "hash_password" SET NOT NULL`);
10
- }
11
-
12
- public async down(queryRunner: QueryRunner): Promise<void> {
13
- await queryRunner.query(`ALTER TABLE "users" ALTER COLUMN "hash_password" DROP NOT NULL`);
14
- await queryRunner.query(`ALTER TABLE "users" DROP CONSTRAINT "UQ_97672ac88f789774dd47f7c8be3"`);
15
- await queryRunner.query(`ALTER TABLE "users" DROP CONSTRAINT "UQ_17d1817f241f10a3dbafb169fd2"`);
16
- await queryRunner.query(`DROP TABLE "feeds"`);
17
- }
18
-
19
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
backend/src/migrations/1729864559594-name-phone-not-nullable.ts DELETED
@@ -1,16 +0,0 @@
1
- import { MigrationInterface, QueryRunner } from "typeorm";
2
-
3
- export class NamePhoneNotNullable1729864559594 implements MigrationInterface {
4
- name = 'NamePhoneNotNullable1729864559594'
5
-
6
- public async up(queryRunner: QueryRunner): Promise<void> {
7
- await queryRunner.query(`ALTER TABLE "users" ALTER COLUMN "full_name" SET NOT NULL`);
8
- await queryRunner.query(`ALTER TABLE "users" ALTER COLUMN "phone_number" SET NOT NULL`);
9
- }
10
-
11
- public async down(queryRunner: QueryRunner): Promise<void> {
12
- await queryRunner.query(`ALTER TABLE "users" ALTER COLUMN "phone_number" DROP NOT NULL`);
13
- await queryRunner.query(`ALTER TABLE "users" ALTER COLUMN "full_name" DROP NOT NULL`);
14
- }
15
-
16
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
backend/src/migrations/1729877042750-MoveMenuItemType.ts DELETED
@@ -1,20 +0,0 @@
1
- import { MigrationInterface, QueryRunner } from "typeorm";
2
-
3
- export class MoveMenuItemType1729877042750 implements MigrationInterface {
4
- name = 'MoveMenuItemType1729877042750'
5
-
6
- public async up(queryRunner: QueryRunner): Promise<void> {
7
- await queryRunner.query(`ALTER TABLE "menu_items" RENAME COLUMN "item_group_id" TO "item_type"`);
8
- await queryRunner.query(`ALTER TABLE "menu_items" DROP COLUMN "item_type"`);
9
- await queryRunner.query(`CREATE TYPE "public"."menu_items_item_type_enum" AS ENUM('monchinh', 'trangmieng', 'giaikhat', 'khac')`);
10
- await queryRunner.query(`ALTER TABLE "menu_items" ADD "item_type" "public"."menu_items_item_type_enum" NOT NULL DEFAULT 'khac'`);
11
- }
12
-
13
- public async down(queryRunner: QueryRunner): Promise<void> {
14
- await queryRunner.query(`ALTER TABLE "menu_items" DROP COLUMN "item_type"`);
15
- await queryRunner.query(`DROP TYPE "public"."menu_items_item_type_enum"`);
16
- await queryRunner.query(`ALTER TABLE "menu_items" ADD "item_type" character varying`);
17
- await queryRunner.query(`ALTER TABLE "menu_items" RENAME COLUMN "item_type" TO "item_group_id"`);
18
- }
19
-
20
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
backend/src/migrations/1729952137958-Change-role-id-string-default.ts DELETED
@@ -1,20 +0,0 @@
1
- import { MigrationInterface, QueryRunner } from "typeorm";
2
-
3
- export class ChangeRoleIdStringDefault1729952137958 implements MigrationInterface {
4
- name = 'ChangeRoleIdStringDefault1729952137958'
5
-
6
- public async up(queryRunner: QueryRunner): Promise<void> {
7
- await queryRunner.query(`ALTER TABLE "users" DROP CONSTRAINT "FK_a2cecd1a3531c0b041e29ba46e1"`);
8
- await queryRunner.query(`ALTER TABLE "users" ALTER COLUMN "role_id" SET NOT NULL`);
9
- await queryRunner.query(`ALTER TABLE "users" ALTER COLUMN "role_id" SET DEFAULT 'f3750930-48ab-4c30-8681-d50e68e2bda7'`);
10
- await queryRunner.query(`ALTER TABLE "users" ADD CONSTRAINT "FK_a2cecd1a3531c0b041e29ba46e1" FOREIGN KEY ("role_id") REFERENCES "role"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
11
- }
12
-
13
- public async down(queryRunner: QueryRunner): Promise<void> {
14
- await queryRunner.query(`ALTER TABLE "users" DROP CONSTRAINT "FK_a2cecd1a3531c0b041e29ba46e1"`);
15
- await queryRunner.query(`ALTER TABLE "users" ALTER COLUMN "role_id" DROP DEFAULT`);
16
- await queryRunner.query(`ALTER TABLE "users" ALTER COLUMN "role_id" DROP NOT NULL`);
17
- await queryRunner.query(`ALTER TABLE "users" ADD CONSTRAINT "FK_a2cecd1a3531c0b041e29ba46e1" FOREIGN KEY ("role_id") REFERENCES "role"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
18
- }
19
-
20
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
backend/src/migrations/{1729070527478-Create-Role-Entity.ts → 1729963419864-enum-role.ts} RENAMED
@@ -1,20 +1,22 @@
1
  import { MigrationInterface, QueryRunner } from "typeorm";
2
 
3
- export class CreateRoleEntity1729070527478 implements MigrationInterface {
4
- name = 'CreateRoleEntity1729070527478'
5
 
6
  public async up(queryRunner: QueryRunner): Promise<void> {
7
- await queryRunner.query(`CREATE TABLE "role" ("id" uuid NOT NULL DEFAULT uuid_generate_v4(), "role_name" character varying NOT NULL, "description" character varying, CONSTRAINT "PK_b36bcfe02fc8de3c57a8b2391c2" PRIMARY KEY ("id"))`);
8
- await queryRunner.query(`ALTER TABLE "users" DROP COLUMN "role_id"`);
9
- await queryRunner.query(`ALTER TABLE "users" ADD "role_id" uuid`);
10
- await queryRunner.query(`ALTER TABLE "users" ADD CONSTRAINT "FK_a2cecd1a3531c0b041e29ba46e1" FOREIGN KEY ("role_id") REFERENCES "role"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
 
11
  }
12
 
13
  public async down(queryRunner: QueryRunner): Promise<void> {
14
- await queryRunner.query(`ALTER TABLE "users" DROP CONSTRAINT "FK_a2cecd1a3531c0b041e29ba46e1"`);
15
- await queryRunner.query(`ALTER TABLE "users" DROP COLUMN "role_id"`);
16
- await queryRunner.query(`ALTER TABLE "users" ADD "role_id" integer`);
17
- await queryRunner.query(`DROP TABLE "role"`);
 
18
  }
19
 
20
  }
 
1
  import { MigrationInterface, QueryRunner } from "typeorm";
2
 
3
+ export class EnumRole1729963419864 implements MigrationInterface {
4
+ name = 'EnumRole1729963419864'
5
 
6
  public async up(queryRunner: QueryRunner): Promise<void> {
7
+ await queryRunner.query(`ALTER TABLE "users" DROP CONSTRAINT "FK_a2cecd1a3531c0b041e29ba46e1"`);
8
+ await queryRunner.query(`ALTER TABLE "users" RENAME COLUMN "role_id" TO "role"`);
9
+ await queryRunner.query(`ALTER TABLE "users" DROP COLUMN "role"`);
10
+ await queryRunner.query(`CREATE TYPE "public"."users_role_enum" AS ENUM('CUSTOMER', 'ADMIN', 'BRANCH_MANAGER', 'AREA_MANAGER', 'STAFF', 'SHIPPER')`);
11
+ await queryRunner.query(`ALTER TABLE "users" ADD "role" "public"."users_role_enum" NOT NULL DEFAULT 'CUSTOMER'`);
12
  }
13
 
14
  public async down(queryRunner: QueryRunner): Promise<void> {
15
+ await queryRunner.query(`ALTER TABLE "users" DROP COLUMN "role"`);
16
+ await queryRunner.query(`DROP TYPE "public"."users_role_enum"`);
17
+ await queryRunner.query(`ALTER TABLE "users" ADD "role" uuid NOT NULL DEFAULT 'f3750930-48ab-4c30-8681-d50e68e2bda7'`);
18
+ await queryRunner.query(`ALTER TABLE "users" RENAME COLUMN "role" TO "role_id"`);
19
+ await queryRunner.query(`ALTER TABLE "users" ADD CONSTRAINT "FK_a2cecd1a3531c0b041e29ba46e1" FOREIGN KEY ("role_id") REFERENCES "role"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
20
  }
21
 
22
  }
backend/src/modules/authentication/authentication.controller.ts CHANGED
@@ -12,7 +12,7 @@ import { Public } from './authentication.decorator.js';
12
  import { SignInDto } from './dto/sign-in.dto.js';
13
  import { SignUpDto } from './dto/sign-up.dto.js';
14
  import { Roles } from './authorization/roles.decorator.js';
15
- import { Role } from './authorization/enums/role.enum.js';
16
  @Controller('authentication')
17
 
18
  export class AuthenticationController {
 
12
  import { SignInDto } from './dto/sign-in.dto.js';
13
  import { SignUpDto } from './dto/sign-up.dto.js';
14
  import { Roles } from './authorization/roles.decorator.js';
15
+ import { Role } from '../../common/enums/role.enum.js';
16
  @Controller('authentication')
17
 
18
  export class AuthenticationController {
backend/src/modules/authentication/authentication.service.ts CHANGED
@@ -23,11 +23,10 @@ export class AuthenticationService {
23
  else
24
  user = await this.usersService.findOneByField('phone_number', username);
25
  const compare = await bcrypt.compare(pass, user.hash_password)
26
- console.log(compare);
27
  if (( !user || !compare )) {
28
  throw new UnauthorizedException();
29
  }
30
- const payload = { sub: user.id, username: user.full_name, roles: user.role.role_name};
31
  return {
32
  access_token: await this.jwtService.signAsync(payload),
33
  };
@@ -55,7 +54,7 @@ export class AuthenticationService {
55
  })
56
  await this.usersService.save(user);
57
 
58
- const payload = { sub: user.id, username: user.id, role: user.role };
59
  const token = await this.jwtService.signAsync(payload)
60
  return {
61
  access_token: token
 
23
  else
24
  user = await this.usersService.findOneByField('phone_number', username);
25
  const compare = await bcrypt.compare(pass, user.hash_password)
 
26
  if (( !user || !compare )) {
27
  throw new UnauthorizedException();
28
  }
29
+ const payload = { sub: user.id, username: user.full_name, roles: user.role};
30
  return {
31
  access_token: await this.jwtService.signAsync(payload),
32
  };
 
54
  })
55
  await this.usersService.save(user);
56
 
57
+ const payload = { sub: user.id, username: user.full_name, role: user.role };
58
  const token = await this.jwtService.signAsync(payload)
59
  return {
60
  access_token: token
backend/src/modules/user/user.service.ts CHANGED
@@ -34,8 +34,7 @@ export class UserService {
34
 
35
  async findOneByField(field: string, value: any): Promise<UserEntity | undefined> {
36
  return UserEntity.findOne({
37
- where: { [field]: value },
38
- relations: ['role']
39
  });
40
  }
41
 
@@ -45,8 +44,7 @@ export class UserService {
45
  await this.validateService.checkExistField('phone_number', updateUserDto.phone_number);
46
 
47
  const user = await UserEntity.findOne({
48
- where: { id: userId },
49
- relations: ['role']
50
  });
51
  if (!user) {
52
  throw new NotFoundException(`User with ID ${userId} not found`);
@@ -59,7 +57,7 @@ export class UserService {
59
  }
60
  await UserEntity.save(user);
61
 
62
- const payload = { sub: user.id, username: user.full_name, roles: user.role.role_name };
63
  const token = await this.jwtService.signAsync(payload)
64
  return {
65
  access_token: token
 
34
 
35
  async findOneByField(field: string, value: any): Promise<UserEntity | undefined> {
36
  return UserEntity.findOne({
37
+ where: { [field]: value }
 
38
  });
39
  }
40
 
 
44
  await this.validateService.checkExistField('phone_number', updateUserDto.phone_number);
45
 
46
  const user = await UserEntity.findOne({
47
+ where: { id: userId }
 
48
  });
49
  if (!user) {
50
  throw new NotFoundException(`User with ID ${userId} not found`);
 
57
  }
58
  await UserEntity.save(user);
59
 
60
+ const payload = { sub: user.id, username: user.full_name, roles: user.role };
61
  const token = await this.jwtService.signAsync(payload)
62
  return {
63
  access_token: token
backend/src/validate/validate.service.ts CHANGED
@@ -14,8 +14,7 @@ export class ValidateService {
14
 
15
  // Tìm trong database theo tên và giá trị của trường
16
  const existingUser = await UserEntity.findOne({
17
- where: { [fieldName]: fieldValue },
18
- relations: ['role']
19
  });
20
 
21
  if (existingUser) {
 
14
 
15
  // Tìm trong database theo tên và giá trị của trường
16
  const existingUser = await UserEntity.findOne({
17
+ where: { [fieldName]: fieldValue }
 
18
  });
19
 
20
  if (existingUser) {