Add foreign key constraints and perform on Cascade Delete (#1219)
This commit is contained in:
@ -99,15 +99,7 @@ export class UserService {
|
|||||||
workspaceId: string;
|
workspaceId: string;
|
||||||
userId: string;
|
userId: string;
|
||||||
}) {
|
}) {
|
||||||
const {
|
const { workspaceMember, refreshToken } = this.prismaService.client;
|
||||||
workspaceMember,
|
|
||||||
company,
|
|
||||||
comment,
|
|
||||||
attachment,
|
|
||||||
refreshToken,
|
|
||||||
activity,
|
|
||||||
activityTarget,
|
|
||||||
} = this.prismaService.client;
|
|
||||||
const user = await this.findUnique({
|
const user = await this.findUnique({
|
||||||
where: {
|
where: {
|
||||||
id: userId,
|
id: userId,
|
||||||
@ -141,35 +133,15 @@ export class UserService {
|
|||||||
select: { id: true },
|
select: { id: true },
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
const where = { authorId: userId };
|
|
||||||
const activities = await activity.findMany({
|
|
||||||
where,
|
|
||||||
});
|
|
||||||
|
|
||||||
await this.prismaService.client.$transaction([
|
await this.prismaService.client.$transaction([
|
||||||
workspaceMember.deleteMany({
|
workspaceMember.deleteMany({
|
||||||
where: { userId },
|
where: { userId },
|
||||||
}),
|
}),
|
||||||
company.deleteMany({
|
|
||||||
where: { accountOwnerId: userId },
|
|
||||||
}),
|
|
||||||
comment.deleteMany({
|
|
||||||
where,
|
|
||||||
}),
|
|
||||||
attachment.deleteMany({
|
|
||||||
where,
|
|
||||||
}),
|
|
||||||
refreshToken.deleteMany({
|
refreshToken.deleteMany({
|
||||||
where: { userId },
|
where: { userId },
|
||||||
}),
|
}),
|
||||||
...activities.map(({ id: activityId }) =>
|
|
||||||
activityTarget.deleteMany({
|
|
||||||
where: { activityId },
|
|
||||||
}),
|
|
||||||
),
|
|
||||||
activity.deleteMany({
|
|
||||||
where,
|
|
||||||
}),
|
|
||||||
this.delete({ where: { id: userId } }),
|
this.delete({ where: { id: userId } }),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -0,0 +1,53 @@
|
|||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "activities" DROP CONSTRAINT "activities_authorId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "attachments" DROP CONSTRAINT "attachments_activityId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "attachments" DROP CONSTRAINT "attachments_authorId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "favorites" DROP CONSTRAINT "favorites_companyId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "favorites" DROP CONSTRAINT "favorites_personId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "pipeline_progresses" DROP CONSTRAINT "pipeline_progresses_pipelineId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "pipeline_progresses" DROP CONSTRAINT "pipeline_progresses_pipelineStageId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "pipeline_stages" DROP CONSTRAINT "pipeline_stages_pipelineId_fkey";
|
||||||
|
|
||||||
|
-- DropForeignKey
|
||||||
|
ALTER TABLE "comments" DROP CONSTRAINT "comments_authorId_fkey";
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "activities" ADD CONSTRAINT "activities_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "pipeline_stages" ADD CONSTRAINT "pipeline_stages_pipelineId_fkey" FOREIGN KEY ("pipelineId") REFERENCES "pipelines"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "pipeline_progresses" ADD CONSTRAINT "pipeline_progresses_pipelineId_fkey" FOREIGN KEY ("pipelineId") REFERENCES "pipelines"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "pipeline_progresses" ADD CONSTRAINT "pipeline_progresses_pipelineStageId_fkey" FOREIGN KEY ("pipelineStageId") REFERENCES "pipeline_stages"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "attachments" ADD CONSTRAINT "attachments_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "attachments" ADD CONSTRAINT "attachments_activityId_fkey" FOREIGN KEY ("activityId") REFERENCES "activities"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "favorites" ADD CONSTRAINT "favorites_personId_fkey" FOREIGN KEY ("personId") REFERENCES "people"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "favorites" ADD CONSTRAINT "favorites_companyId_fkey" FOREIGN KEY ("companyId") REFERENCES "companies"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
|
|
||||||
|
-- AddForeignKey
|
||||||
|
ALTER TABLE "comments" ADD CONSTRAINT "comments_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
|
||||||
@ -233,7 +233,7 @@ model Company {
|
|||||||
employees Int?
|
employees Int?
|
||||||
|
|
||||||
people Person[]
|
people Person[]
|
||||||
accountOwner User? @relation(fields: [accountOwnerId], references: [id])
|
accountOwner User? @relation(fields: [accountOwnerId], references: [id], onDelete: SetNull)
|
||||||
accountOwnerId String?
|
accountOwnerId String?
|
||||||
/// @TypeGraphQL.omit(input: true, output: true)
|
/// @TypeGraphQL.omit(input: true, output: true)
|
||||||
workspace Workspace @relation(fields: [workspaceId], references: [id])
|
workspace Workspace @relation(fields: [workspaceId], references: [id])
|
||||||
@ -346,9 +346,9 @@ model Activity {
|
|||||||
activityTargets ActivityTarget[]
|
activityTargets ActivityTarget[]
|
||||||
comments Comment[]
|
comments Comment[]
|
||||||
attachments Attachment[]
|
attachments Attachment[]
|
||||||
author User @relation(fields: [authorId], references: [id], name: "authoredActivities")
|
author User @relation(fields: [authorId], references: [id], name: "authoredActivities", onDelete: Cascade)
|
||||||
authorId String
|
authorId String
|
||||||
assignee User? @relation(fields: [assigneeId], references: [id], name: "assignedActivities")
|
assignee User? @relation(fields: [assigneeId], references: [id], name: "assignedActivities", onDelete: SetNull)
|
||||||
assigneeId String?
|
assigneeId String?
|
||||||
/// @TypeGraphQL.omit(input: true, output: true)
|
/// @TypeGraphQL.omit(input: true, output: true)
|
||||||
workspace Workspace @relation(fields: [workspaceId], references: [id])
|
workspace Workspace @relation(fields: [workspaceId], references: [id])
|
||||||
@ -371,7 +371,7 @@ model Comment {
|
|||||||
/// @Validator.IsString()
|
/// @Validator.IsString()
|
||||||
body String
|
body String
|
||||||
|
|
||||||
author User @relation(fields: [authorId], references: [id])
|
author User @relation(fields: [authorId], references: [id], onDelete: Cascade)
|
||||||
authorId String
|
authorId String
|
||||||
activity Activity? @relation(fields: [activityId], references: [id], onDelete: Cascade)
|
activity Activity? @relation(fields: [activityId], references: [id], onDelete: Cascade)
|
||||||
activityId String?
|
activityId String?
|
||||||
@ -462,7 +462,7 @@ model PipelineStage {
|
|||||||
|
|
||||||
pipelineProgresses PipelineProgress[]
|
pipelineProgresses PipelineProgress[]
|
||||||
///
|
///
|
||||||
pipeline Pipeline @relation(fields: [pipelineId], references: [id])
|
pipeline Pipeline @relation(fields: [pipelineId], references: [id], onDelete: Cascade)
|
||||||
pipelineId String
|
pipelineId String
|
||||||
/// @TypeGraphQL.omit(input: true, output: true)
|
/// @TypeGraphQL.omit(input: true, output: true)
|
||||||
workspace Workspace @relation(fields: [workspaceId], references: [id])
|
workspace Workspace @relation(fields: [workspaceId], references: [id])
|
||||||
@ -491,11 +491,11 @@ model PipelineProgress {
|
|||||||
closeDate DateTime?
|
closeDate DateTime?
|
||||||
probability Int?
|
probability Int?
|
||||||
|
|
||||||
pipeline Pipeline @relation(fields: [pipelineId], references: [id])
|
pipeline Pipeline @relation(fields: [pipelineId], references: [id], onDelete: Cascade)
|
||||||
pipelineId String
|
pipelineId String
|
||||||
pipelineStage PipelineStage @relation(fields: [pipelineStageId], references: [id])
|
pipelineStage PipelineStage @relation(fields: [pipelineStageId], references: [id], onDelete: Cascade)
|
||||||
pipelineStageId String
|
pipelineStageId String
|
||||||
pointOfContact Person? @relation("PointOfContactPipelineProgress", fields: [pointOfContactId], references: [id])
|
pointOfContact Person? @relation("PointOfContactPipelineProgress", fields: [pointOfContactId], references: [id], onDelete: SetNull)
|
||||||
pointOfContactId String?
|
pointOfContactId String?
|
||||||
|
|
||||||
/// @TypeGraphQL.omit(input: true, output: true)
|
/// @TypeGraphQL.omit(input: true, output: true)
|
||||||
@ -540,9 +540,9 @@ model Attachment {
|
|||||||
/// @TypeGraphQL.omit(input: true, output: true)
|
/// @TypeGraphQL.omit(input: true, output: true)
|
||||||
workspaceId String
|
workspaceId String
|
||||||
|
|
||||||
author User @relation(fields: [authorId], references: [id], name: "authoredAttachments")
|
author User @relation(fields: [authorId], references: [id], name: "authoredAttachments", onDelete: Cascade)
|
||||||
authorId String
|
authorId String
|
||||||
activity Activity @relation(fields: [activityId], references: [id])
|
activity Activity @relation(fields: [activityId], references: [id], onDelete: Cascade)
|
||||||
activityId String
|
activityId String
|
||||||
|
|
||||||
/// @TypeGraphQL.omit(input: true, output: false)
|
/// @TypeGraphQL.omit(input: true, output: false)
|
||||||
@ -559,10 +559,10 @@ model Favorite {
|
|||||||
id String @id @default(uuid())
|
id String @id @default(uuid())
|
||||||
workspaceId String?
|
workspaceId String?
|
||||||
/// @TypeGraphQL.omit(input: true, output: false)
|
/// @TypeGraphQL.omit(input: true, output: false)
|
||||||
person Person? @relation(fields: [personId], references: [id])
|
person Person? @relation(fields: [personId], references: [id], onDelete: Cascade)
|
||||||
personId String?
|
personId String?
|
||||||
/// @TypeGraphQL.omit(input: true, output: false)
|
/// @TypeGraphQL.omit(input: true, output: false)
|
||||||
company Company? @relation(fields: [companyId], references: [id])
|
company Company? @relation(fields: [companyId], references: [id], onDelete: Cascade)
|
||||||
companyId String?
|
companyId String?
|
||||||
/// @TypeGraphQL.omit(input: true, output: false)
|
/// @TypeGraphQL.omit(input: true, output: false)
|
||||||
workspaceMember WorkspaceMember? @relation(fields: [workspaceMemberId], references: [id])
|
workspaceMember WorkspaceMember? @relation(fields: [workspaceMemberId], references: [id])
|
||||||
|
|||||||
Reference in New Issue
Block a user