Skip to content

fix(api): handle organzation and profile deletion #813

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class JournalEntry extends BaseEntity {
@Column("text")
commandName!: string;

@ManyToOne(() => Organization)
@ManyToOne(() => Organization, { onDelete: "CASCADE" })
organization!: Organization;

@Column()
Expand All @@ -18,7 +18,7 @@ export class JournalEntry extends BaseEntity {
@Column("json")
payload!: unknown;

@ManyToOne(() => Profile)
@ManyToOne(() => Profile, { onDelete: "CASCADE" })
profile!: Profile;

@Column()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AddJournalDeleteCascadeHandlers1720640713138 implements MigrationInterface {
name = 'AddJournalDeleteCascadeHandlers1720640713138'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "journal_entry" DROP CONSTRAINT "FK_6747365996c833749ba53dd39f8"`);
await queryRunner.query(`ALTER TABLE "journal_entry" DROP CONSTRAINT "FK_8ab2ce48d25de7470897d4970f3"`);
await queryRunner.query(`ALTER TABLE "journal_entry" ADD CONSTRAINT "FK_8ab2ce48d25de7470897d4970f3" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "journal_entry" ADD CONSTRAINT "FK_6747365996c833749ba53dd39f8" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "journal_entry" DROP CONSTRAINT "FK_6747365996c833749ba53dd39f8"`);
await queryRunner.query(`ALTER TABLE "journal_entry" DROP CONSTRAINT "FK_8ab2ce48d25de7470897d4970f3"`);
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`);
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`);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ export class Notification extends BaseEntity {
@Column({ type: "varchar", length: 2048 })
message!: string;

@ManyToOne(() => Organization)
@ManyToOne(() => Organization, { onDelete: "CASCADE" })
organization!: Organization;

@Column()
organizationId!: number;

@ManyToOne(() => Profile)
@ManyToOne(() => Profile, { onDelete: "CASCADE" })
profile!: Profile;

@Column()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AddNotificationDeleteCascadeHandlers1720640853966 implements MigrationInterface {
name = 'AddNotificationDeleteCascadeHandlers1720640853966'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "notification" DROP CONSTRAINT "FK_4dd039be3d37179110ff3e14901"`);
await queryRunner.query(`ALTER TABLE "notification" DROP CONSTRAINT "FK_4bb0507e70fc50c02e221326f8e"`);
await queryRunner.query(`ALTER TABLE "notification" ADD CONSTRAINT "FK_4bb0507e70fc50c02e221326f8e" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "notification" ADD CONSTRAINT "FK_4dd039be3d37179110ff3e14901" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "notification" DROP CONSTRAINT "FK_4dd039be3d37179110ff3e14901"`);
await queryRunner.query(`ALTER TABLE "notification" DROP CONSTRAINT "FK_4bb0507e70fc50c02e221326f8e"`);
await queryRunner.query(`ALTER TABLE "notification" ADD CONSTRAINT "FK_4bb0507e70fc50c02e221326f8e" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "notification" ADD CONSTRAINT "FK_4dd039be3d37179110ff3e14901" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE NO ACTION ON UPDATE NO ACTION`);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import { Organization } from "./organization.entity";

@Entity()
export class ActiveOrganization extends BaseEntity {
@ManyToOne(() => Organization, { onDelete: "SET NULL" })
organization!: Organization;

@Column()
organizationId!: number;

@ManyToOne(() => Organization)
organization!: Organization;
@OneToOne(() => Profile, { onDelete: "CASCADE" })
profile!: Profile;

@Column()
profileId!: number;

@OneToOne(() => Profile)
profile!: Profile;
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,22 @@ export class OrganizationMembership {
@CreateDateColumn()
created!: Date;

@ManyToOne(() => Organization, (organization) => organization.memberships, {
onDelete: "CASCADE",
})
organization!: Organization;

@Column()
organizationId!: number;

@ManyToOne(() => Organization, (organization) => organization.memberships)
organization!: Organization;
@ManyToOne(() => Profile, (profile) => profile.organizationMemberships, {
onDelete: "CASCADE",
})
profile!: Profile;

@Column()
profileId!: number;

@ManyToOne(() => Profile, (profile) => profile.organizationMemberships)
profile!: Profile;

@Column({
type: "enum",
enum: OrganizationRole,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AddDeleteCascadeHandlers1720639767923 implements MigrationInterface {
name = 'AddDeleteCascadeHandlers1720639767923'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "organization_membership" DROP CONSTRAINT "FK_ee8dc62205b4c3acb1483da4142"`);
await queryRunner.query(`ALTER TABLE "organization_membership" DROP CONSTRAINT "FK_827c4eb64b3510234ee23866f81"`);
await queryRunner.query(`ALTER TABLE "active_organization" DROP CONSTRAINT "FK_60b7353926f26cb16ccc4235408"`);
await queryRunner.query(`ALTER TABLE "organization_membership" ADD CONSTRAINT "FK_ee8dc62205b4c3acb1483da4142" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "organization_membership" ADD CONSTRAINT "FK_827c4eb64b3510234ee23866f81" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
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`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "active_organization" DROP CONSTRAINT "FK_60b7353926f26cb16ccc4235408"`);
await queryRunner.query(`ALTER TABLE "organization_membership" DROP CONSTRAINT "FK_827c4eb64b3510234ee23866f81"`);
await queryRunner.query(`ALTER TABLE "organization_membership" DROP CONSTRAINT "FK_ee8dc62205b4c3acb1483da4142"`);
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`);
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`);
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`);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ import { Profile } from "./profile.entity";

@Entity()
export class ProfilePhoto extends BasePhoto {
@OneToOne(() => Profile, (profile) => profile.profilePhoto, {
onDelete: "CASCADE",
})
profile!: Profile;

@Column()
profileId!: number;

@OneToOne(() => Profile, (profile) => profile.profilePhoto)
profile!: Profile;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AddProfileDeleteCascadeHandlers1720640142267 implements MigrationInterface {
name = 'AddProfileDeleteCascadeHandlers1720640142267'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "profile" ADD CONSTRAINT "UQ_e93f4cc04459521af4d5da5a2cc" UNIQUE ("profilePhotoId")`);
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`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "profile" DROP CONSTRAINT "FK_e93f4cc04459521af4d5da5a2cc"`);
await queryRunner.query(`ALTER TABLE "profile" DROP CONSTRAINT "UQ_e93f4cc04459521af4d5da5a2cc"`);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class BlogPostComment extends BaseEntity {
@Column({ type: "varchar", length: 2048, default: "" })
content!: string;

@ManyToOne(() => Profile)
@ManyToOne(() => Profile, { onDelete: "CASCADE" })
profile!: Profile;

@Column()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ export class BlogPostPhoto extends BasePhoto {
blogPost!: BlogPost;

@Column()
profileId!: number;
blogPostId!: string;

@ManyToOne(() => Profile)
@ManyToOne(() => Profile, { onDelete: "CASCADE" })
profile!: Profile;

@Column()
profileId!: number;
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class BlogPostReaction extends BaseEntity {
@Column()
blogPostId!: string;

@ManyToOne(() => Profile)
@ManyToOne(() => Profile, { onDelete: "CASCADE" })
profile!: Profile;

@Column()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export class BlogPost extends BaseEntity {
})
comments!: BlogPostComment[];

@ManyToOne(() => Organization)
@ManyToOne(() => Organization, { onDelete: "CASCADE" })
organization!: Organization;

@Column()
Expand All @@ -32,7 +32,7 @@ export class BlogPost extends BaseEntity {
)
photos!: BlogPostPhoto[];

@ManyToOne(() => Profile)
@ManyToOne(() => Profile, { onDelete: "CASCADE" })
profile!: Profile;

@Column()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { MigrationInterface, QueryRunner } from "typeorm";

export class AddBlogPostDeleteCascadeHandlers1720640445245 implements MigrationInterface {
name = 'AddBlogPostDeleteCascadeHandlers1720640445245'

public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "blog_post_photo" DROP CONSTRAINT "FK_663877f57c851f804fc6bb9a881"`);
await queryRunner.query(`ALTER TABLE "blog_post_reaction" DROP CONSTRAINT "FK_11e0938003df1680e02080fca6a"`);
await queryRunner.query(`ALTER TABLE "blog_post" DROP CONSTRAINT "FK_5fc410996103f64f5d16c423918"`);
await queryRunner.query(`ALTER TABLE "blog_post" DROP CONSTRAINT "FK_5e268dc00df2ba14bfb0381dc99"`);
await queryRunner.query(`ALTER TABLE "blog_post_comment" DROP CONSTRAINT "FK_182a1cd7b3453180dafe47d0bff"`);
await queryRunner.query(`ALTER TABLE "blog_post_photo" DROP CONSTRAINT "FK_c6412cba4a5c0a36761adaef490"`);
await queryRunner.query(`ALTER TABLE "blog_post_photo" ALTER COLUMN "blogPostId" SET NOT NULL`);
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`);
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`);
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`);
await queryRunner.query(`ALTER TABLE "blog_post" ADD CONSTRAINT "FK_5e268dc00df2ba14bfb0381dc99" FOREIGN KEY ("organizationId") REFERENCES "organization"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
await queryRunner.query(`ALTER TABLE "blog_post" ADD CONSTRAINT "FK_5fc410996103f64f5d16c423918" FOREIGN KEY ("profileId") REFERENCES "profile"("id") ON DELETE CASCADE ON UPDATE NO ACTION`);
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`);
}

public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "blog_post_comment" DROP CONSTRAINT "FK_182a1cd7b3453180dafe47d0bff"`);
await queryRunner.query(`ALTER TABLE "blog_post" DROP CONSTRAINT "FK_5fc410996103f64f5d16c423918"`);
await queryRunner.query(`ALTER TABLE "blog_post" DROP CONSTRAINT "FK_5e268dc00df2ba14bfb0381dc99"`);
await queryRunner.query(`ALTER TABLE "blog_post_reaction" DROP CONSTRAINT "FK_11e0938003df1680e02080fca6a"`);
await queryRunner.query(`ALTER TABLE "blog_post_photo" DROP CONSTRAINT "FK_663877f57c851f804fc6bb9a881"`);
await queryRunner.query(`ALTER TABLE "blog_post_photo" DROP CONSTRAINT "FK_c6412cba4a5c0a36761adaef490"`);
await queryRunner.query(`ALTER TABLE "blog_post_photo" ALTER COLUMN "blogPostId" DROP NOT NULL`);
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`);
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`);
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`);
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`);
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`);
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`);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export class Settings {
@Column("boolean")
darkmode!: boolean;

@OneToOne(() => Profile)
@OneToOne(() => Profile, { onDelete: "CASCADE" })
profile!: Profile;

@Column()
Expand Down
Loading