Skip to content

Commit 354a528

Browse files
Johan BookJohan Book
Johan Book
authored and
Johan Book
committed
fix(api): handle organzation and profile deletion
1 parent 245dec2 commit 354a528

File tree

5 files changed

+59
-13
lines changed

5 files changed

+59
-13
lines changed

services/api/src/core/organizations/infrastructure/entities/active-organization.entity.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,15 @@ import { Organization } from "./organization.entity";
77

88
@Entity()
99
export class ActiveOrganization extends BaseEntity {
10+
@ManyToOne(() => Organization, { onDelete: "SET NULL" })
11+
organization!: Organization;
12+
1013
@Column()
1114
organizationId!: number;
1215

13-
@ManyToOne(() => Organization)
14-
organization!: Organization;
16+
@OneToOne(() => Profile, { onDelete: "CASCADE" })
17+
profile!: Profile;
1518

1619
@Column()
1720
profileId!: number;
18-
19-
@OneToOne(() => Profile)
20-
profile!: Profile;
2121
}

services/api/src/core/organizations/infrastructure/entities/organization-membership.entity.ts

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,22 @@ export class OrganizationMembership {
2121
@CreateDateColumn()
2222
created!: Date;
2323

24+
@ManyToOne(() => Organization, (organization) => organization.memberships, {
25+
onDelete: "CASCADE",
26+
})
27+
organization!: Organization;
28+
2429
@Column()
2530
organizationId!: number;
2631

27-
@ManyToOne(() => Organization, (organization) => organization.memberships)
28-
organization!: Organization;
32+
@ManyToOne(() => Profile, (profile) => profile.organizationMemberships, {
33+
onDelete: "CASCADE",
34+
})
35+
profile!: Profile;
2936

3037
@Column()
3138
profileId!: number;
3239

33-
@ManyToOne(() => Profile, (profile) => profile.organizationMemberships)
34-
profile!: Profile;
35-
3640
@Column({
3741
type: "enum",
3842
enum: OrganizationRole,
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class AddDeleteCascadeHandlers1720639767923 implements MigrationInterface {
4+
name = 'AddDeleteCascadeHandlers1720639767923'
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`ALTER TABLE "organization_membership" DROP CONSTRAINT "FK_ee8dc62205b4c3acb1483da4142"`);
8+
await queryRunner.query(`ALTER TABLE "organization_membership" DROP CONSTRAINT "FK_827c4eb64b3510234ee23866f81"`);
9+
await queryRunner.query(`ALTER TABLE "active_organization" DROP CONSTRAINT "FK_60b7353926f26cb16ccc4235408"`);
10+
await queryRunner.query(`ALTER TABLE "organization_membership" ADD CONSTRAINT "FK_ee8dc62205b4c3acb1483da4142" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
11+
await queryRunner.query(`ALTER TABLE "organization_membership" ADD CONSTRAINT "FK_827c4eb64b3510234ee23866f81" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
12+
await queryRunner.query(`ALTER TABLE "active_organization" ADD CONSTRAINT "FK_60b7353926f26cb16ccc4235408" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE SET NULL ON UPDATE NO ACTION`);
13+
}
14+
15+
public async down(queryRunner: QueryRunner): Promise<void> {
16+
await queryRunner.query(`ALTER TABLE "active_organization" DROP CONSTRAINT "FK_60b7353926f26cb16ccc4235408"`);
17+
await queryRunner.query(`ALTER TABLE "organization_membership" DROP CONSTRAINT "FK_827c4eb64b3510234ee23866f81"`);
18+
await queryRunner.query(`ALTER TABLE "organization_membership" DROP CONSTRAINT "FK_ee8dc62205b4c3acb1483da4142"`);
19+
await queryRunner.query(`ALTER TABLE "active_organization" ADD CONSTRAINT "FK_60b7353926f26cb16ccc4235408" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
20+
await queryRunner.query(`ALTER TABLE "organization_membership" ADD CONSTRAINT "FK_827c4eb64b3510234ee23866f81" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
21+
await queryRunner.query(`ALTER TABLE "organization_membership" ADD CONSTRAINT "FK_ee8dc62205b4c3acb1483da4142" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
22+
}
23+
24+
}

services/api/src/core/profiles/infrastructure/entities/profile-photo.entity.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,11 @@ import { Profile } from "./profile.entity";
66

77
@Entity()
88
export class ProfilePhoto extends BasePhoto {
9+
@OneToOne(() => Profile, (profile) => profile.profilePhoto, {
10+
onDelete: "CASCADE",
11+
})
12+
profile!: Profile;
13+
914
@Column()
1015
profileId!: number;
11-
12-
@OneToOne(() => Profile, (profile) => profile.profilePhoto)
13-
profile!: Profile;
1416
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class AddProfileDeleteCascadeHandlers1720640142267 implements MigrationInterface {
4+
name = 'AddProfileDeleteCascadeHandlers1720640142267'
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`ALTER TABLE "profile" ADD CONSTRAINT "UQ_e93f4cc04459521af4d5da5a2cc" UNIQUE ("profilePhotoId")`);
8+
await queryRunner.query(`ALTER TABLE "profile" ADD CONSTRAINT "FK_e93f4cc04459521af4d5da5a2cc" FOREIGN KEY ("profilePhotoId") REFERENCES "profile_photo"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
9+
}
10+
11+
public async down(queryRunner: QueryRunner): Promise<void> {
12+
await queryRunner.query(`ALTER TABLE "profile" DROP CONSTRAINT "FK_e93f4cc04459521af4d5da5a2cc"`);
13+
await queryRunner.query(`ALTER TABLE "profile" DROP CONSTRAINT "UQ_e93f4cc04459521af4d5da5a2cc"`);
14+
}
15+
16+
}

0 commit comments

Comments
 (0)