Add foreign key constraints and perform on Cascade Delete (#1219)

This commit is contained in:
Charles Bochet
2023-08-15 20:52:23 +02:00
committed by GitHub
parent 83b900e016
commit 7a330b4a02
3 changed files with 68 additions and 43 deletions

View File

@ -99,15 +99,7 @@ export class UserService {
workspaceId: string;
userId: string;
}) {
const {
workspaceMember,
company,
comment,
attachment,
refreshToken,
activity,
activityTarget,
} = this.prismaService.client;
const { workspaceMember, refreshToken } = this.prismaService.client;
const user = await this.findUnique({
where: {
id: userId,
@ -141,35 +133,15 @@ export class UserService {
select: { id: true },
});
} else {
const where = { authorId: userId };
const activities = await activity.findMany({
where,
});
await this.prismaService.client.$transaction([
workspaceMember.deleteMany({
where: { userId },
}),
company.deleteMany({
where: { accountOwnerId: userId },
}),
comment.deleteMany({
where,
}),
attachment.deleteMany({
where,
}),
refreshToken.deleteMany({
where: { userId },
}),
...activities.map(({ id: activityId }) =>
activityTarget.deleteMany({
where: { activityId },
}),
),
activity.deleteMany({
where,
}),
this.delete({ where: { id: userId } }),
]);
}