From 85c0eb157f00c14caff141d6695be6a93c67975f Mon Sep 17 00:00:00 2001 From: vygandas Date: Fri, 8 Dec 2023 10:41:52 +0200 Subject: [PATCH] first steps --- apps/api/src/entities/organization.entity.ts | 13 +++++ apps/api/src/entities/user.entity.ts | 4 ++ ...701953109158-create-organizations-table.ts | 47 +++++++++++++++++++ libs/interfaces/src/index.ts | 1 + .../organization/organization.interface.ts | 6 +++ libs/interfaces/src/user/User.interface.ts | 2 + 6 files changed, 73 insertions(+) create mode 100644 apps/api/src/entities/organization.entity.ts create mode 100644 apps/api/src/migrations/1701953109158-create-organizations-table.ts create mode 100644 libs/interfaces/src/organization/organization.interface.ts diff --git a/apps/api/src/entities/organization.entity.ts b/apps/api/src/entities/organization.entity.ts new file mode 100644 index 0000000..56d5779 --- /dev/null +++ b/apps/api/src/entities/organization.entity.ts @@ -0,0 +1,13 @@ +import { OrganizationInterface } from '@isomera/interfaces' +import { PrimaryGeneratedColumn, JoinColumn, ManyToOne } from 'typeorm' + +import { UserEntity } from './user.entity' + +export class OrganizationEntity implements OrganizationInterface { + @PrimaryGeneratedColumn('increment') + id: number + + @ManyToOne(() => UserEntity, user => user.id) + @JoinColumn() + user: UserEntity +} diff --git a/apps/api/src/entities/user.entity.ts b/apps/api/src/entities/user.entity.ts index db4e780..70a011e 100644 --- a/apps/api/src/entities/user.entity.ts +++ b/apps/api/src/entities/user.entity.ts @@ -11,6 +11,7 @@ import { import { UserInterface } from '@isomera/interfaces' import bcrypt from 'bcryptjs' import { ConfirmCodeEntity } from './confirm-code.entity' +import { OrganizationEntity } from './organization.entity' @Entity({ name: 'users' }) export class UserEntity implements UserInterface { @@ -50,6 +51,9 @@ export class UserEntity implements UserInterface { @OneToMany(() => ConfirmCodeEntity, confirmCode => confirmCode.user) confirmationCodes: ConfirmCodeEntity[] + @OneToMany(() => OrganizationEntity, organization => organization.user) + organizations: OrganizationEntity[] + @BeforeInsert() @BeforeUpdate() async hashPassword(): Promise { diff --git a/apps/api/src/migrations/1701953109158-create-organizations-table.ts b/apps/api/src/migrations/1701953109158-create-organizations-table.ts new file mode 100644 index 0000000..35e9316 --- /dev/null +++ b/apps/api/src/migrations/1701953109158-create-organizations-table.ts @@ -0,0 +1,47 @@ +import { + MigrationInterface, + QueryRunner, + Table, + TableColumn, + TableForeignKey +} from 'typeorm' + +export class CreateOrganizationsTable1701953109158 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.createTable( + new Table({ + name: 'organizations', + columns: [ + new TableColumn({ + name: 'id', + type: 'int', + isPrimary: true, + isGenerated: true, + generationStrategy: 'increment' + }), + new TableColumn({ + name: 'userId', + type: 'int' + }) + ] + }) + ) + + await queryRunner.createForeignKey( + 'organizations', + new TableForeignKey({ + name: 'FK_user_of_organization', + columnNames: ['userId'], + referencedColumnNames: ['id'], + referencedTableName: 'users', + onDelete: 'CASCADE' + }) + ) + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.dropTable('organizations') + } +} diff --git a/libs/interfaces/src/index.ts b/libs/interfaces/src/index.ts index 92c3b8e..7b858d7 100644 --- a/libs/interfaces/src/index.ts +++ b/libs/interfaces/src/index.ts @@ -8,3 +8,4 @@ export * from './auth/passwordResetRequest.interface' export * from './auth/passwordResetPerform.interface' export * from './auth/signUpResponse.interface' export * from './auth/confirmationCode.interface' +export * from './organization/organization.interface' diff --git a/libs/interfaces/src/organization/organization.interface.ts b/libs/interfaces/src/organization/organization.interface.ts new file mode 100644 index 0000000..8fea1e0 --- /dev/null +++ b/libs/interfaces/src/organization/organization.interface.ts @@ -0,0 +1,6 @@ +import { UserInterface } from '../user/User.interface' + +export interface OrganizationInterface { + id?: number + user: UserInterface +} diff --git a/libs/interfaces/src/user/User.interface.ts b/libs/interfaces/src/user/User.interface.ts index 9d81cf1..a780c0c 100644 --- a/libs/interfaces/src/user/User.interface.ts +++ b/libs/interfaces/src/user/User.interface.ts @@ -1,4 +1,5 @@ import { ConfirmationCodeInterface } from '../auth/confirmationCode.interface' +import { OrganizationInterface } from '../organization/organization.interface' export interface UserInterface { id?: number @@ -11,4 +12,5 @@ export interface UserInterface { passwordResetCode?: string | null active: boolean confirmationCodes?: ConfirmationCodeInterface[] + organizations: OrganizationInterface[] }