chore: tidy code and extract alert-dialog

This commit is contained in:
Lucas Smith
2024-02-25 11:12:18 +00:00
parent 7226d5ac53
commit 9cf72e1442
7 changed files with 156 additions and 111 deletions

View File

@ -1,11 +1,13 @@
import { prisma } from '@documenso/prisma';
import { DocumentStatus } from '@documenso/prisma/client';
import { deletedAccountServiceAccount } from './service-accounts/deleted-account';
export type DeleteUserOptions = {
email: string;
};
export const deletedServiceAccount = async ({ email }: DeleteUserOptions) => {
export const deleteUser = async ({ email }: DeleteUserOptions) => {
const user = await prisma.user.findFirst({
where: {
email: {
@ -14,20 +16,13 @@ export const deletedServiceAccount = async ({ email }: DeleteUserOptions) => {
},
});
const defaultDeleteUser = await prisma.user.findFirst({
where: {
email: 'deleted@documenso.com',
},
});
if (!user) {
throw new Error(`User with email ${email} not found`);
}
if (!defaultDeleteUser) {
throw new Error(`Default delete account not found`);
}
const serviceAccount = await deletedAccountServiceAccount();
// TODO: Send out cancellations for all pending docs
await prisma.document.updateMany({
where: {
userId: user.id,
@ -36,7 +31,7 @@ export const deletedServiceAccount = async ({ email }: DeleteUserOptions) => {
},
},
data: {
userId: defaultDeleteUser.id,
userId: serviceAccount.id,
deletedAt: new Date(),
},
});

View File

@ -0,0 +1,17 @@
import { prisma } from '@documenso/prisma';
export const deletedAccountServiceAccount = async () => {
const serviceAccount = await prisma.user.findFirst({
where: {
email: 'deleted-account@documenso.com',
},
});
if (!serviceAccount) {
throw new Error(
'Deleted account service account not found, have you ran the appropriate migrations?',
);
}
return serviceAccount;
};