Files
sign/packages/trpc/server/profile-router/router.ts

241 lines
6.9 KiB
TypeScript
Raw Normal View History

2023-06-09 18:21:18 +10:00
import { TRPCError } from '@trpc/server';
2024-02-29 13:22:21 +11:00
import { IS_BILLING_ENABLED } from '@documenso/lib/constants/app';
2024-02-28 14:43:09 +11:00
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
2024-02-29 13:22:21 +11:00
import { getSubscriptionsByUserId } from '@documenso/lib/server-only/subscription/get-subscriptions-by-user-id';
import { deleteUser } from '@documenso/lib/server-only/user/delete-user';
2024-01-30 17:31:27 +11:00
import { findUserSecurityAuditLogs } from '@documenso/lib/server-only/user/find-user-security-audit-logs';
2023-09-18 11:15:29 +00:00
import { forgotPassword } from '@documenso/lib/server-only/user/forgot-password';
2023-09-21 15:10:20 +01:00
import { getUserById } from '@documenso/lib/server-only/user/get-user-by-id';
2023-09-18 14:03:33 +00:00
import { resetPassword } from '@documenso/lib/server-only/user/reset-password';
import { sendConfirmationToken } from '@documenso/lib/server-only/user/send-confirmation-token';
2023-06-09 18:21:18 +10:00
import { updatePassword } from '@documenso/lib/server-only/user/update-password';
import { updateProfile } from '@documenso/lib/server-only/user/update-profile';
import { updatePublicProfile } from '@documenso/lib/server-only/user/update-public-profile';
2024-01-31 12:27:40 +11:00
import { extractNextApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
2024-02-29 13:22:21 +11:00
import { SubscriptionStatus } from '@documenso/prisma/client';
2023-06-09 18:21:18 +10:00
2023-10-11 12:32:33 +03:00
import { adminProcedure, authenticatedProcedure, procedure, router } from '../trpc';
2023-09-18 11:15:29 +00:00
import {
ZConfirmEmailMutationSchema,
2024-01-30 17:31:27 +11:00
ZFindUserSecurityAuditLogsSchema,
2023-09-18 11:15:29 +00:00
ZForgotPasswordFormSchema,
2023-09-18 14:03:33 +00:00
ZResetPasswordFormSchema,
2023-09-21 15:10:20 +01:00
ZRetrieveUserByIdQuerySchema,
2023-09-18 11:15:29 +00:00
ZUpdatePasswordMutationSchema,
ZUpdateProfileMutationSchema,
ZUpdatePublicProfileMutationSchema,
2023-09-18 11:15:29 +00:00
} from './schema';
2023-06-09 18:21:18 +10:00
export const profileRouter = router({
2024-01-30 17:31:27 +11:00
findUserSecurityAuditLogs: authenticatedProcedure
.input(ZFindUserSecurityAuditLogsSchema)
.query(async ({ input, ctx }) => {
try {
return await findUserSecurityAuditLogs({
userId: ctx.user.id,
...input,
});
} catch (err) {
2024-03-30 14:00:34 +08:00
console.error(err);
2024-01-30 17:31:27 +11:00
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to find user security audit logs. Please try again.',
});
}
}),
2023-10-11 12:32:33 +03:00
getUser: adminProcedure.input(ZRetrieveUserByIdQuerySchema).query(async ({ input }) => {
try {
const { id } = input;
2023-09-21 15:10:20 +01:00
2023-10-11 12:32:33 +03:00
return await getUserById({ id });
} catch (err) {
2024-03-30 14:00:34 +08:00
console.error(err);
2023-10-11 12:32:33 +03:00
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to retrieve the specified account. Please try again.',
});
}
}),
2023-09-21 15:10:20 +01:00
2023-06-09 18:21:18 +10:00
updateProfile: authenticatedProcedure
.input(ZUpdateProfileMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
const { name, signature } = input;
return await updateProfile({
userId: ctx.user.id,
name,
signature,
2024-01-31 12:27:40 +11:00
requestMetadata: extractNextApiRequestMetadata(ctx.req),
2023-06-09 18:21:18 +10:00
});
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message:
'We were unable to update your profile. Please review the information you provided and try again.',
});
}
}),
updatePublicProfile: authenticatedProcedure
.input(ZUpdatePublicProfileMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
2024-02-28 14:43:09 +11:00
const { url } = input;
if (IS_BILLING_ENABLED() && url.length < 6) {
2024-02-29 13:22:21 +11:00
const subscriptions = await getSubscriptionsByUserId({
userId: ctx.user.id,
}).then((subscriptions) =>
subscriptions.filter((s) => s.status === SubscriptionStatus.ACTIVE),
);
if (subscriptions.length === 0) {
throw new AppError(
AppErrorCode.PREMIUM_PROFILE_URL,
'Only subscribers can have a username shorter than 6 characters',
);
}
}
2024-02-28 14:43:09 +11:00
const user = await updatePublicProfile({
userId: ctx.user.id,
url,
});
2024-02-28 14:43:09 +11:00
return { success: true, url: user.url };
} catch (err) {
2024-03-30 14:00:34 +08:00
console.error(err);
2024-02-28 14:43:09 +11:00
const error = AppError.parseError(err);
if (error.code !== AppErrorCode.UNKNOWN_ERROR) {
throw AppError.parseErrorToTRPCError(error);
}
throw new TRPCError({
code: 'BAD_REQUEST',
message:
'We were unable to update your public profile. Please review the information you provided and try again.',
});
}
}),
2023-06-09 18:21:18 +10:00
updatePassword: authenticatedProcedure
.input(ZUpdatePasswordMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
const { password, currentPassword } = input;
2023-06-09 18:21:18 +10:00
return await updatePassword({
userId: ctx.user.id,
password,
currentPassword,
2024-01-31 12:27:40 +11:00
requestMetadata: extractNextApiRequestMetadata(ctx.req),
2023-06-09 18:21:18 +10:00
});
} catch (err) {
2024-03-30 14:00:34 +08:00
console.error(err);
2023-08-30 14:01:30 +10:00
let message =
'We were unable to update your profile. Please review the information you provided and try again.';
if (err instanceof Error) {
message = err.message;
}
2023-06-09 18:21:18 +10:00
throw new TRPCError({
code: 'BAD_REQUEST',
2023-08-30 14:01:30 +10:00
message,
2023-06-09 18:21:18 +10:00
});
}
}),
2023-09-18 11:15:29 +00:00
forgotPassword: procedure.input(ZForgotPasswordFormSchema).mutation(async ({ input }) => {
try {
const { email } = input;
return await forgotPassword({
email,
});
} catch (err) {
2023-09-19 13:34:54 +00:00
console.error(err);
2023-09-18 11:15:29 +00:00
}
}),
2023-09-18 14:03:33 +00:00
2024-01-30 17:31:27 +11:00
resetPassword: procedure.input(ZResetPasswordFormSchema).mutation(async ({ input, ctx }) => {
2023-09-18 14:03:33 +00:00
try {
const { password, token } = input;
return await resetPassword({
token,
password,
2024-01-31 12:27:40 +11:00
requestMetadata: extractNextApiRequestMetadata(ctx.req),
2023-09-18 14:03:33 +00:00
});
} catch (err) {
2024-03-30 14:00:34 +08:00
console.error(err);
let message = 'We were unable to reset your password. Please try again.';
2023-09-18 14:03:33 +00:00
if (err instanceof Error) {
message = err.message;
}
throw new TRPCError({
code: 'BAD_REQUEST',
message,
});
}
}),
sendConfirmationEmail: procedure
.input(ZConfirmEmailMutationSchema)
.mutation(async ({ input }) => {
try {
const { email } = input;
2024-01-30 12:54:48 +02:00
return await sendConfirmationToken({ email });
} catch (err) {
2024-03-30 14:00:34 +08:00
console.error(err);
let message = 'We were unable to send a confirmation email. Please try again.';
if (err instanceof Error) {
message = err.message;
}
throw new TRPCError({
code: 'BAD_REQUEST',
message,
});
}
}),
deleteAccount: authenticatedProcedure.mutation(async ({ ctx }) => {
try {
2024-03-03 01:55:33 +11:00
return await deleteUser({
id: ctx.user.id,
});
} catch (err) {
2024-03-30 14:00:34 +08:00
console.error(err);
let message = 'We were unable to delete your account. Please try again.';
if (err instanceof Error) {
message = err.message;
}
throw new TRPCError({
code: 'BAD_REQUEST',
message,
});
}
}),
2023-06-09 18:21:18 +10:00
});