From fddd860d15a69a804493babc70120f9ccc2d6499 Mon Sep 17 00:00:00 2001 From: Ephraim Atta-Duncan Date: Thu, 15 Feb 2024 11:33:43 +0000 Subject: [PATCH] chore: code refactor to avoid repetitions --- apps/web/src/components/forms/profile.tsx | 32 +++++++++----------- packages/lib/server-only/user/delete-user.ts | 3 +- 2 files changed, 16 insertions(+), 19 deletions(-) diff --git a/apps/web/src/components/forms/profile.tsx b/apps/web/src/components/forms/profile.tsx index a44e70940..23861c9fc 100644 --- a/apps/web/src/components/forms/profile.tsx +++ b/apps/web/src/components/forms/profile.tsx @@ -113,18 +113,22 @@ export const ProfileForm = ({ className, user }: ProfileFormProps) => { } }; + const deleteAccoutAndSignOut = async () => { + await deleteAccount(); + + toast({ + title: 'Account deleted', + description: 'Your account has been deleted successfully.', + duration: 5000, + }); + + return await signOut({ callbackUrl: '/' }); + }; + const onDeleteAccount = async (hasTwoFactorAuthentication: boolean) => { try { if (!hasTwoFactorAuthentication) { - await deleteAccount(); - - toast({ - title: 'Account deleted', - description: 'Your account has been deleted successfully.', - duration: 5000, - }); - - return await signOut({ callbackUrl: '/' }); + return await deleteAccoutAndSignOut(); } const { token } = deleteAccountTwoFactorTokenForm.getValues(); @@ -140,15 +144,7 @@ export const ProfileForm = ({ className, user }: ProfileFormProps) => { throw new Error('We were unable to validate your Two Factor Authentication token.'); }); - await deleteAccount(); - - toast({ - title: 'Account deleted', - description: 'Your account has been deleted successfully.', - duration: 5000, - }); - - await signOut({ callbackUrl: '/' }); + await deleteAccoutAndSignOut(); } catch (err) { if (err instanceof TRPCClientError && err.data?.code === 'BAD_REQUEST') { toast({ diff --git a/packages/lib/server-only/user/delete-user.ts b/packages/lib/server-only/user/delete-user.ts index 02d811b12..65a74ac42 100644 --- a/packages/lib/server-only/user/delete-user.ts +++ b/packages/lib/server-only/user/delete-user.ts @@ -1,4 +1,5 @@ import { prisma } from '@documenso/prisma'; +import { DocumentStatus } from '@documenso/prisma/client'; export type DeleteUserOptions = { email: string; @@ -31,7 +32,7 @@ export const deletedServiceAccount = async ({ email }: DeleteUserOptions) => { where: { userId: user.id, status: { - in: ['PENDING', 'COMPLETED'], + in: [DocumentStatus.PENDING, DocumentStatus.COMPLETED], }, }, data: {