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

43 lines
920 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 = {
2024-03-03 01:55:33 +11:00
id: number;
};
2023-10-14 00:20:11 +11:00
2024-03-03 01:55:33 +11:00
export const deleteUser = async ({ id }: DeleteUserOptions) => {
2023-10-05 11:38:51 +03:00
const user = await prisma.user.findFirst({
where: {
2024-03-03 01:55:33 +11:00
id,
2023-10-05 11:38:51 +03:00
},
});
if (!user) {
2024-03-03 01:55:33 +11:00
throw new Error(`User with ID ${id} 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,
},
});
};