-
Notifications
You must be signed in to change notification settings - Fork 73
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: Add user_organizations migration and update related entities
- Loading branch information
Showing
10 changed files
with
162 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import type { MigrationInterface, QueryRunner } from 'typeorm'; | ||
|
||
export class UserOrganization1738679234042 implements MigrationInterface { | ||
name = 'UserOrganization1738679234042'; | ||
|
||
public async up(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`CREATE TABLE "user_organizations" ("id" SERIAL NOT NULL, "name" character varying(255), "role" integer NOT NULL, "status" integer NOT NULL, "created_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "updated_at" TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(), "userId" integer NOT NULL, "organizationId" integer NOT NULL, CONSTRAINT "UQ_user_organizations" UNIQUE ("userId", "organizationId"), CONSTRAINT "PK_UO_id" PRIMARY KEY ("id"))`, | ||
); | ||
await queryRunner.query( | ||
`CREATE INDEX "idx_UO_role_status" ON "user_organizations" ("role", "status")`, | ||
); | ||
await queryRunner.query( | ||
`CREATE INDEX "idx_UO_name" ON "user_organizations" ("name")`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "user_organizations" ADD CONSTRAINT "FK_UO_user_id" FOREIGN KEY ("userId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "user_organizations" ADD CONSTRAINT "FK_UO_organization_id" FOREIGN KEY ("organizationId") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE NO ACTION`, | ||
); | ||
await queryRunner.query(` | ||
CREATE TRIGGER update_updated_at | ||
BEFORE UPDATE | ||
ON | ||
user_organizations | ||
FOR EACH ROW | ||
EXECUTE PROCEDURE update_updated_at(); | ||
`); | ||
} | ||
|
||
public async down(queryRunner: QueryRunner): Promise<void> { | ||
await queryRunner.query( | ||
`DROP TRIGGER IF EXISTS update_updated_at ON user_organizations;`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "user_organizations" DROP CONSTRAINT "FK_UO_organization_id"`, | ||
); | ||
await queryRunner.query( | ||
`ALTER TABLE "user_organizations" DROP CONSTRAINT "FK_UO_user_id"`, | ||
); | ||
await queryRunner.query(`DROP INDEX "public"."idx_UO_name"`); | ||
await queryRunner.query(`DROP INDEX "public"."idx_UO_role_status"`); | ||
await queryRunner.query(`DROP TABLE "user_organizations"`); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
src/datasources/users/entities/user-organizations.entity.db.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
import { Organization } from '@/datasources/organizations/entities/organizations.entity.db'; | ||
import { User } from '@/datasources/users/entities/users.entity.db'; | ||
import { | ||
UserOrganizationRole, | ||
UserOrganizationStatus, | ||
} from '@/domain/users/entities/user-organization.entity'; | ||
import { | ||
Column, | ||
Entity, | ||
Index, | ||
JoinColumn, | ||
ManyToOne, | ||
PrimaryGeneratedColumn, | ||
Unique, | ||
} from 'typeorm'; | ||
|
||
@Entity('user_organizations') | ||
@Unique('UQ_user_organizations', ['user', 'organization']) | ||
@Index('idx_UO_name', ['name']) | ||
@Index('idx_UO_role_status', ['role', 'status']) | ||
export class UserOrganization { | ||
@PrimaryGeneratedColumn({ | ||
primaryKeyConstraintName: 'PK_UO_id', | ||
}) | ||
id!: number; | ||
|
||
@ManyToOne(() => User, (user: User) => user.id, { | ||
cascade: true, | ||
nullable: false, | ||
}) | ||
@JoinColumn({ | ||
foreignKeyConstraintName: 'FK_UO_user_id', | ||
}) | ||
user!: User; | ||
|
||
@ManyToOne( | ||
() => Organization, | ||
(organization: Organization) => organization.id, | ||
{ | ||
cascade: true, | ||
nullable: false, | ||
}, | ||
) | ||
@JoinColumn({ | ||
foreignKeyConstraintName: 'FK_UO_organization_id', | ||
}) | ||
organization!: Organization; | ||
|
||
@Column({ type: 'varchar', length: 255, nullable: true }) | ||
name!: string | null; | ||
|
||
// Postgres enums are string therefore we use integer | ||
@Column({ | ||
type: 'integer', | ||
}) | ||
role!: UserOrganizationRole; | ||
|
||
// Postgres enums are string therefore we use integer | ||
@Column({ | ||
type: 'integer', | ||
}) | ||
status!: UserOrganizationStatus; | ||
|
||
@Column({ | ||
type: 'timestamp with time zone', | ||
default: () => 'CURRENT_TIMESTAMP', | ||
}) | ||
created_at!: Date; | ||
|
||
@Column({ | ||
type: 'timestamp with time zone', | ||
default: () => 'CURRENT_TIMESTAMP', | ||
}) | ||
updated_at!: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export enum UserOrganizationRole { | ||
ADMIN = 1, | ||
MEMBER = 2, | ||
} | ||
|
||
export enum UserOrganizationStatus { | ||
PENDING = 0, | ||
ACTIVE = 1, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters