Skip to content

Commit 428419e

Browse files
johanbookJohan Book
and
Johan Book
authored
fix(api): handle organzation and profile deletion (#813)
Co-authored-by: Johan Book <{ID}+{username}@users.noreply.github.com>
1 parent 245dec2 commit 428419e

File tree

15 files changed

+151
-24
lines changed

15 files changed

+151
-24
lines changed

services/api/src/core/journal/infrastructure/entities/journal-entry.entity.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class JournalEntry extends BaseEntity {
99
@Column("text")
1010
commandName!: string;
1111

12-
@ManyToOne(() => Organization)
12+
@ManyToOne(() => Organization, { onDelete: "CASCADE" })
1313
organization!: Organization;
1414

1515
@Column()
@@ -18,7 +18,7 @@ export class JournalEntry extends BaseEntity {
1818
@Column("json")
1919
payload!: unknown;
2020

21-
@ManyToOne(() => Profile)
21+
@ManyToOne(() => Profile, { onDelete: "CASCADE" })
2222
profile!: Profile;
2323

2424
@Column()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class AddJournalDeleteCascadeHandlers1720640713138 implements MigrationInterface {
4+
name = 'AddJournalDeleteCascadeHandlers1720640713138'
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`ALTER TABLE "journal_entry" DROP CONSTRAINT "FK_6747365996c833749ba53dd39f8"`);
8+
await queryRunner.query(`ALTER TABLE "journal_entry" DROP CONSTRAINT "FK_8ab2ce48d25de7470897d4970f3"`);
9+
await queryRunner.query(`ALTER TABLE "journal_entry" ADD CONSTRAINT "FK_8ab2ce48d25de7470897d4970f3" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
10+
await queryRunner.query(`ALTER TABLE "journal_entry" ADD CONSTRAINT "FK_6747365996c833749ba53dd39f8" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
11+
}
12+
13+
public async down(queryRunner: QueryRunner): Promise<void> {
14+
await queryRunner.query(`ALTER TABLE "journal_entry" DROP CONSTRAINT "FK_6747365996c833749ba53dd39f8"`);
15+
await queryRunner.query(`ALTER TABLE "journal_entry" DROP CONSTRAINT "FK_8ab2ce48d25de7470897d4970f3"`);
16+
await queryRunner.query(`ALTER TABLE "journal_entry" ADD CONSTRAINT "FK_8ab2ce48d25de7470897d4970f3" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
17+
await queryRunner.query(`ALTER TABLE "journal_entry" ADD CONSTRAINT "FK_6747365996c833749ba53dd39f8" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
18+
}
19+
20+
}

services/api/src/core/notifications/infrastructure/entities/notification.entity.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,13 @@ export class Notification extends BaseEntity {
1212
@Column({ type: "varchar", length: 2048 })
1313
message!: string;
1414

15-
@ManyToOne(() => Organization)
15+
@ManyToOne(() => Organization, { onDelete: "CASCADE" })
1616
organization!: Organization;
1717

1818
@Column()
1919
organizationId!: number;
2020

21-
@ManyToOne(() => Profile)
21+
@ManyToOne(() => Profile, { onDelete: "CASCADE" })
2222
profile!: Profile;
2323

2424
@Column()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class AddNotificationDeleteCascadeHandlers1720640853966 implements MigrationInterface {
4+
name = 'AddNotificationDeleteCascadeHandlers1720640853966'
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`ALTER TABLE "notification" DROP CONSTRAINT "FK_4dd039be3d37179110ff3e14901"`);
8+
await queryRunner.query(`ALTER TABLE "notification" DROP CONSTRAINT "FK_4bb0507e70fc50c02e221326f8e"`);
9+
await queryRunner.query(`ALTER TABLE "notification" ADD CONSTRAINT "FK_4bb0507e70fc50c02e221326f8e" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
10+
await queryRunner.query(`ALTER TABLE "notification" ADD CONSTRAINT "FK_4dd039be3d37179110ff3e14901" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
11+
}
12+
13+
public async down(queryRunner: QueryRunner): Promise<void> {
14+
await queryRunner.query(`ALTER TABLE "notification" DROP CONSTRAINT "FK_4dd039be3d37179110ff3e14901"`);
15+
await queryRunner.query(`ALTER TABLE "notification" DROP CONSTRAINT "FK_4bb0507e70fc50c02e221326f8e"`);
16+
await queryRunner.query(`ALTER TABLE "notification" ADD CONSTRAINT "FK_4bb0507e70fc50c02e221326f8e" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
17+
await queryRunner.query(`ALTER TABLE "notification" ADD CONSTRAINT "FK_4dd039be3d37179110ff3e14901" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
18+
}
19+
20+
}

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

+5-5
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

+9-5
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,
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

+5-3
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+
}

services/api/src/features/blogs/infrastructure/entities/blog-post-comment.entity.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class BlogPostComment extends BaseEntity {
1818
@Column({ type: "varchar", length: 2048, default: "" })
1919
content!: string;
2020

21-
@ManyToOne(() => Profile)
21+
@ManyToOne(() => Profile, { onDelete: "CASCADE" })
2222
profile!: Profile;
2323

2424
@Column()

services/api/src/features/blogs/infrastructure/entities/blog-post-photo.entity.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ export class BlogPostPhoto extends BasePhoto {
1414
blogPost!: BlogPost;
1515

1616
@Column()
17-
profileId!: number;
17+
blogPostId!: string;
1818

19-
@ManyToOne(() => Profile)
19+
@ManyToOne(() => Profile, { onDelete: "CASCADE" })
2020
profile!: Profile;
21+
22+
@Column()
23+
profileId!: number;
2124
}

services/api/src/features/blogs/infrastructure/entities/blog-post-reaction.entity.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export class BlogPostReaction extends BaseEntity {
1616
@Column()
1717
blogPostId!: string;
1818

19-
@ManyToOne(() => Profile)
19+
@ManyToOne(() => Profile, { onDelete: "CASCADE" })
2020
profile!: Profile;
2121

2222
@Column()

services/api/src/features/blogs/infrastructure/entities/blog-post.entity.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ export class BlogPost extends BaseEntity {
1818
})
1919
comments!: BlogPostComment[];
2020

21-
@ManyToOne(() => Organization)
21+
@ManyToOne(() => Organization, { onDelete: "CASCADE" })
2222
organization!: Organization;
2323

2424
@Column()
@@ -32,7 +32,7 @@ export class BlogPost extends BaseEntity {
3232
)
3333
photos!: BlogPostPhoto[];
3434

35-
@ManyToOne(() => Profile)
35+
@ManyToOne(() => Profile, { onDelete: "CASCADE" })
3636
profile!: Profile;
3737

3838
@Column()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { MigrationInterface, QueryRunner } from "typeorm";
2+
3+
export class AddBlogPostDeleteCascadeHandlers1720640445245 implements MigrationInterface {
4+
name = 'AddBlogPostDeleteCascadeHandlers1720640445245'
5+
6+
public async up(queryRunner: QueryRunner): Promise<void> {
7+
await queryRunner.query(`ALTER TABLE "blog_post_photo" DROP CONSTRAINT "FK_663877f57c851f804fc6bb9a881"`);
8+
await queryRunner.query(`ALTER TABLE "blog_post_reaction" DROP CONSTRAINT "FK_11e0938003df1680e02080fca6a"`);
9+
await queryRunner.query(`ALTER TABLE "blog_post" DROP CONSTRAINT "FK_5fc410996103f64f5d16c423918"`);
10+
await queryRunner.query(`ALTER TABLE "blog_post" DROP CONSTRAINT "FK_5e268dc00df2ba14bfb0381dc99"`);
11+
await queryRunner.query(`ALTER TABLE "blog_post_comment" DROP CONSTRAINT "FK_182a1cd7b3453180dafe47d0bff"`);
12+
await queryRunner.query(`ALTER TABLE "blog_post_photo" DROP CONSTRAINT "FK_c6412cba4a5c0a36761adaef490"`);
13+
await queryRunner.query(`ALTER TABLE "blog_post_photo" ALTER COLUMN "blogPostId" SET NOT NULL`);
14+
await queryRunner.query(`ALTER TABLE "blog_post_photo" ADD CONSTRAINT "FK_c6412cba4a5c0a36761adaef490" FOREIGN KEY ("blogPostId") REFERENCES "blog_post"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
15+
await queryRunner.query(`ALTER TABLE "blog_post_photo" ADD CONSTRAINT "FK_663877f57c851f804fc6bb9a881" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
16+
await queryRunner.query(`ALTER TABLE "blog_post_reaction" ADD CONSTRAINT "FK_11e0938003df1680e02080fca6a" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
17+
await queryRunner.query(`ALTER TABLE "blog_post" ADD CONSTRAINT "FK_5e268dc00df2ba14bfb0381dc99" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
18+
await queryRunner.query(`ALTER TABLE "blog_post" ADD CONSTRAINT "FK_5fc410996103f64f5d16c423918" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
19+
await queryRunner.query(`ALTER TABLE "blog_post_comment" ADD CONSTRAINT "FK_182a1cd7b3453180dafe47d0bff" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
20+
}
21+
22+
public async down(queryRunner: QueryRunner): Promise<void> {
23+
await queryRunner.query(`ALTER TABLE "blog_post_comment" DROP CONSTRAINT "FK_182a1cd7b3453180dafe47d0bff"`);
24+
await queryRunner.query(`ALTER TABLE "blog_post" DROP CONSTRAINT "FK_5fc410996103f64f5d16c423918"`);
25+
await queryRunner.query(`ALTER TABLE "blog_post" DROP CONSTRAINT "FK_5e268dc00df2ba14bfb0381dc99"`);
26+
await queryRunner.query(`ALTER TABLE "blog_post_reaction" DROP CONSTRAINT "FK_11e0938003df1680e02080fca6a"`);
27+
await queryRunner.query(`ALTER TABLE "blog_post_photo" DROP CONSTRAINT "FK_663877f57c851f804fc6bb9a881"`);
28+
await queryRunner.query(`ALTER TABLE "blog_post_photo" DROP CONSTRAINT "FK_c6412cba4a5c0a36761adaef490"`);
29+
await queryRunner.query(`ALTER TABLE "blog_post_photo" ALTER COLUMN "blogPostId" DROP NOT NULL`);
30+
await queryRunner.query(`ALTER TABLE "blog_post_photo" ADD CONSTRAINT "FK_c6412cba4a5c0a36761adaef490" FOREIGN KEY ("blogPostId") REFERENCES "blog_post"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
31+
await queryRunner.query(`ALTER TABLE "blog_post_comment" ADD CONSTRAINT "FK_182a1cd7b3453180dafe47d0bff" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
32+
await queryRunner.query(`ALTER TABLE "blog_post" ADD CONSTRAINT "FK_5e268dc00df2ba14bfb0381dc99" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
33+
await queryRunner.query(`ALTER TABLE "blog_post" ADD CONSTRAINT "FK_5fc410996103f64f5d16c423918" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
34+
await queryRunner.query(`ALTER TABLE "blog_post_reaction" ADD CONSTRAINT "FK_11e0938003df1680e02080fca6a" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
35+
await queryRunner.query(`ALTER TABLE "blog_post_photo" ADD CONSTRAINT "FK_663877f57c851f804fc6bb9a881" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
36+
}
37+
38+
}

services/api/src/features/settings/infrastructure/entities/settings.entity.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export class Settings {
1919
@Column("boolean")
2020
darkmode!: boolean;
2121

22-
@OneToOne(() => Profile)
22+
@OneToOne(() => Profile, { onDelete: "CASCADE" })
2323
profile!: Profile;
2424

2525
@Column()

0 commit comments

Comments
 (0)