chore: added delete function

This commit is contained in:
pit
2023-10-03 10:09:40 +01:00
committed by Mythie
parent 7d21f607df
commit 9afe8731b1

View 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,
},
}),
]);
};