Files
sign/packages/lib/server-only/user/delete-user.ts

45 lines
971 B
TypeScript
Raw Normal View History

2023-10-05 11:38:51 +03:00
import { prisma } from '@documenso/prisma';
import { DocumentStatus } from '@documenso/prisma/client';
2023-10-05 11:38:51 +03:00
import { deletedAccountServiceAccount } from './service-accounts/deleted-account';
2023-10-14 00:20:11 +11:00
export type DeleteUserOptions = {
email: string;
};
2023-10-14 00:20:11 +11:00
export const deleteUser = async ({ email }: DeleteUserOptions) => {
2023-10-05 11:38:51 +03:00
const user = await prisma.user.findFirst({
where: {
2023-10-12 12:19:23 +03:00
email: {
contains: email,
2023-10-05 11:38:51 +03:00
},
},
});
if (!user) {
2023-10-12 12:19:23 +03:00
throw new Error(`User with email ${email} not found`);
2023-10-05 11:38:51 +03:00
}
const serviceAccount = await deletedAccountServiceAccount();
// TODO: Send out cancellations for all pending docs
await prisma.document.updateMany({
where: {
userId: user.id,
status: {
in: [DocumentStatus.PENDING, DocumentStatus.COMPLETED],
},
},
data: {
userId: serviceAccount.id,
2024-02-05 13:06:36 +00:00
deletedAt: new Date(),
},
});
2023-10-05 11:38:51 +03:00
return await prisma.user.delete({
where: {
id: user.id,
},
});
};