mirror of
https://github.com/documenso/documenso.git
synced 2025-11-12 15:53:02 +10:00
chore: added delete function
This commit is contained in:
41
packages/lib/server-only/user/delete-user-and-data.ts
Normal file
41
packages/lib/server-only/user/delete-user-and-data.ts
Normal file
@ -0,0 +1,41 @@
|
||||
import { prisma } from '@documenso/prisma';
|
||||
|
||||
export const deleteUserAndItsData = async (name: string) => {
|
||||
const user = await prisma.user.findFirst({
|
||||
where: {
|
||||
name: {
|
||||
contains: name,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
throw new Error(`User with name ${name} not found`);
|
||||
}
|
||||
|
||||
const document = await prisma.document.findMany({
|
||||
where: {
|
||||
userId: user.id,
|
||||
},
|
||||
select: {
|
||||
documentData: {
|
||||
select: {
|
||||
data: true,
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
return prisma.$transaction([
|
||||
prisma.user.delete({
|
||||
where: {
|
||||
id: user.id,
|
||||
},
|
||||
}),
|
||||
prisma.documentData.deleteMany({
|
||||
where: {
|
||||
data: document[0]?.documentData.data,
|
||||
},
|
||||
}),
|
||||
]);
|
||||
};
|
||||
Reference in New Issue
Block a user