Trần Viết Sơn commited on
Commit
85a7484
1 Parent(s): fddef2e

feat: add entity

Browse files
backend/src/entities/branch-menu.entity.ts CHANGED
@@ -28,6 +28,9 @@ export class BranchMenuEntity extends BaseEntity {
28
  @Column({ default: true })
29
  is_open: boolean;
30
 
 
 
 
31
  @ManyToOne(() => BranchEntity, (a) => a.menu_items)
32
  @JoinColumn({ name: 'branch_id' })
33
  branch: Relation<BranchEntity>;
 
28
  @Column({ default: true })
29
  is_open: boolean;
30
 
31
+ @Column({ default: 0 })
32
+ sold_count: number;
33
+
34
  @ManyToOne(() => BranchEntity, (a) => a.menu_items)
35
  @JoinColumn({ name: 'branch_id' })
36
  branch: Relation<BranchEntity>;
backend/src/entities/branch.entity.ts CHANGED
@@ -11,19 +11,23 @@ import {
11
  } from 'typeorm';
12
  import { UserEntity } from './user.entity.js';
13
  import { BranchMenuEntity } from './branch-menu.entity.js';
 
14
 
15
  @Entity('branches')
16
  export class BranchEntity extends BaseEntity {
17
  @PrimaryColumn()
18
  id: string;
19
 
20
- @Column()
21
  name: string;
22
 
23
- @Column()
 
 
 
24
  location: string;
25
 
26
- @Column()
27
  phone_number: string;
28
 
29
  @ManyToOne(() => UserEntity, (user) => user.branches)
@@ -32,6 +36,9 @@ export class BranchEntity extends BaseEntity {
32
  @OneToMany(() => BranchMenuEntity, (a) => a.branch)
33
  menu_items: Relation<BranchMenuEntity>[];
34
 
 
 
 
35
  @CreateDateColumn()
36
  create_at: Date;
37
  }
 
11
  } from 'typeorm';
12
  import { UserEntity } from './user.entity.js';
13
  import { BranchMenuEntity } from './branch-menu.entity.js';
14
+ import { ReceiptEntity } from './receipt.entity.js';
15
 
16
  @Entity('branches')
17
  export class BranchEntity extends BaseEntity {
18
  @PrimaryColumn()
19
  id: string;
20
 
21
+ @Column({ nullable: true })
22
  name: string;
23
 
24
+ @Column({ nullable: true })
25
+ image_url: string;
26
+
27
+ @Column({ nullable: true })
28
  location: string;
29
 
30
+ @Column({ nullable: true })
31
  phone_number: string;
32
 
33
  @ManyToOne(() => UserEntity, (user) => user.branches)
 
36
  @OneToMany(() => BranchMenuEntity, (a) => a.branch)
37
  menu_items: Relation<BranchMenuEntity>[];
38
 
39
+ @OneToMany(() => ReceiptEntity, (a) => a.branch)
40
+ receipts: Relation<ReceiptEntity>[];
41
+
42
  @CreateDateColumn()
43
  create_at: Date;
44
  }
backend/src/entities/payment.entity.ts CHANGED
@@ -2,13 +2,10 @@ import {
2
  BaseEntity,
3
  Column,
4
  Entity,
5
- JoinColumn,
6
- ManyToOne,
7
  OneToOne,
8
  PrimaryGeneratedColumn,
9
  Relation,
10
  } from 'typeorm';
11
- import { BranchMenuEntity } from './branch-menu.entity.js';
12
  import { OrderEntity } from './order.entity.js';
13
  import { PaymentMethod } from '../common/enums/PaymentMethod.enum.js';
14
 
 
2
  BaseEntity,
3
  Column,
4
  Entity,
 
 
5
  OneToOne,
6
  PrimaryGeneratedColumn,
7
  Relation,
8
  } from 'typeorm';
 
9
  import { OrderEntity } from './order.entity.js';
10
  import { PaymentMethod } from '../common/enums/PaymentMethod.enum.js';
11
 
backend/src/entities/receipt.entity.ts ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import {
2
+ BaseEntity,
3
+ Column,
4
+ CreateDateColumn,
5
+ Entity,
6
+ JoinColumn,
7
+ ManyToOne,
8
+ PrimaryGeneratedColumn,
9
+ Relation,
10
+ } from 'typeorm';
11
+ import { BranchEntity } from './branch.entity.js';
12
+ import { UserEntity } from './user.entity.js';
13
+
14
+ @Entity('receipts')
15
+ export class ReceiptEntity extends BaseEntity {
16
+ @PrimaryGeneratedColumn()
17
+ id: number;
18
+
19
+ @Column()
20
+ branch_id: string;
21
+
22
+ @ManyToOne(() => BranchEntity, (a) => a.receipts)
23
+ @JoinColumn({ name: 'branch_id' })
24
+ branch: Relation<BranchEntity>;
25
+
26
+ @Column({ default: 0 })
27
+ income: number;
28
+
29
+ @Column({ default: 0 })
30
+ spend: number;
31
+
32
+ @Column({ nullable: true })
33
+ description: string;
34
+
35
+ @Column({ default: 0 })
36
+ type: number;
37
+
38
+ @Column({ default: 0 })
39
+ sub_type: number;
40
+
41
+ @Column({ nullable: true })
42
+ sender_id: string;
43
+
44
+ @Column({ nullable: true })
45
+ receiver_id: string;
46
+
47
+ @ManyToOne(() => UserEntity, (a) => a.out_receipts)
48
+ @JoinColumn({ name: 'sender_id' })
49
+ sender: Relation<UserEntity>;
50
+
51
+ @ManyToOne(() => UserEntity, (a) => a.in_receipts)
52
+ @JoinColumn({ name: 'receiver_id' })
53
+ receiver: Relation<UserEntity>;
54
+
55
+ @CreateDateColumn()
56
+ created_at: Date;
57
+ }
backend/src/entities/user.entity.ts CHANGED
@@ -12,6 +12,7 @@ import {
12
  import { BranchEntity } from './branch.entity.js';
13
  import { IsOptional } from 'class-validator';
14
  import { Role } from '../common/enums/role.enum.js';
 
15
 
16
  @Entity('users')
17
  export class UserEntity extends BaseEntity {
@@ -37,19 +38,22 @@ export class UserEntity extends BaseEntity {
37
 
38
  @Column({ type: 'enum', enum: Role, default: 'CUSTOMER' })
39
  role: Role;
40
-
41
  @Column()
42
  hash_password: string;
43
 
44
- @IsOptional()
45
  @Column({ default: true })
46
  is_valid: boolean;
47
 
48
- @IsOptional()
49
  @CreateDateColumn()
50
  create_at: Date;
51
 
52
- @IsOptional()
53
  @OneToMany(() => BranchEntity, (branch) => branch.owner)
54
  branches: Relation<BranchEntity>[];
 
 
 
 
 
 
55
  }
 
12
  import { BranchEntity } from './branch.entity.js';
13
  import { IsOptional } from 'class-validator';
14
  import { Role } from '../common/enums/role.enum.js';
15
+ import { ReceiptEntity } from './receipt.entity.js';
16
 
17
  @Entity('users')
18
  export class UserEntity extends BaseEntity {
 
38
 
39
  @Column({ type: 'enum', enum: Role, default: 'CUSTOMER' })
40
  role: Role;
41
+
42
  @Column()
43
  hash_password: string;
44
 
 
45
  @Column({ default: true })
46
  is_valid: boolean;
47
 
 
48
  @CreateDateColumn()
49
  create_at: Date;
50
 
 
51
  @OneToMany(() => BranchEntity, (branch) => branch.owner)
52
  branches: Relation<BranchEntity>[];
53
+
54
+ @OneToMany(() => ReceiptEntity, (receipt) => receipt.sender)
55
+ in_receipts: Relation<ReceiptEntity>[];
56
+
57
+ @OneToMany(() => ReceiptEntity, (receipt) => receipt.receiver)
58
+ out_receipts: Relation<ReceiptEntity>[];
59
  }
backend/src/migrations/1730547520878-AddReceipt.ts ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { MigrationInterface, QueryRunner } from "typeorm";
2
+
3
+ export class AddReceipt1730547520878 implements MigrationInterface {
4
+ name = 'AddReceipt1730547520878'
5
+
6
+ public async up(queryRunner: QueryRunner): Promise<void> {
7
+ await queryRunner.query(`CREATE TABLE "receipts" ("id" SERIAL NOT NULL, "branch_id" character varying NOT NULL, "income" integer NOT NULL DEFAULT '0', "spend" integer NOT NULL DEFAULT '0', "description" character varying, "type" integer NOT NULL DEFAULT '0', "sub_type" integer NOT NULL DEFAULT '0', "sender_id" uuid, "receiver_id" uuid, "created_at" TIMESTAMP NOT NULL DEFAULT now(), CONSTRAINT "PK_5e8182d7c29e023da6e1ff33bfe" PRIMARY KEY ("id"))`);
8
+ await queryRunner.query(`ALTER TABLE "branches" ADD "image_url" character varying`);
9
+ await queryRunner.query(`ALTER TABLE "branch_menu" ADD "sold_count" integer NOT NULL DEFAULT '0'`);
10
+ await queryRunner.query(`ALTER TABLE "branches" ALTER COLUMN "name" DROP NOT NULL`);
11
+ await queryRunner.query(`ALTER TABLE "branches" ALTER COLUMN "location" DROP NOT NULL`);
12
+ await queryRunner.query(`ALTER TABLE "branches" ALTER COLUMN "phone_number" DROP NOT NULL`);
13
+ await queryRunner.query(`ALTER TABLE "receipts" ADD CONSTRAINT "FK_82e9dee911c0e7393154d1d98ad" FOREIGN KEY ("branch_id") REFERENCES "branches"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
14
+ await queryRunner.query(`ALTER TABLE "receipts" ADD CONSTRAINT "FK_eda4c4e486a25beef4dc82a41d5" FOREIGN KEY ("sender_id") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
15
+ await queryRunner.query(`ALTER TABLE "receipts" ADD CONSTRAINT "FK_366c3d3cf125da97552f40001a1" FOREIGN KEY ("receiver_id") REFERENCES "users"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
16
+ }
17
+
18
+ public async down(queryRunner: QueryRunner): Promise<void> {
19
+ await queryRunner.query(`ALTER TABLE "receipts" DROP CONSTRAINT "FK_366c3d3cf125da97552f40001a1"`);
20
+ await queryRunner.query(`ALTER TABLE "receipts" DROP CONSTRAINT "FK_eda4c4e486a25beef4dc82a41d5"`);
21
+ await queryRunner.query(`ALTER TABLE "receipts" DROP CONSTRAINT "FK_82e9dee911c0e7393154d1d98ad"`);
22
+ await queryRunner.query(`ALTER TABLE "branches" ALTER COLUMN "phone_number" SET NOT NULL`);
23
+ await queryRunner.query(`ALTER TABLE "branches" ALTER COLUMN "location" SET NOT NULL`);
24
+ await queryRunner.query(`ALTER TABLE "branches" ALTER COLUMN "name" SET NOT NULL`);
25
+ await queryRunner.query(`ALTER TABLE "branch_menu" DROP COLUMN "sold_count"`);
26
+ await queryRunner.query(`ALTER TABLE "branches" DROP COLUMN "image_url"`);
27
+ await queryRunner.query(`DROP TABLE "receipts"`);
28
+ }
29
+
30
+ }