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

202 lines
5.7 KiB
TypeScript
Raw Normal View History

2023-06-09 18:21:18 +10:00
import { TRPCError } from '@trpc/server';
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';
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) {
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) {
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 {
const { profileURL } = input;
return await updatePublicProfile({
id: ctx.user.id,
profileURL,
});
} catch (err) {
console.error(err);
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) {
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) {
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) {
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 {
const user = ctx.user;
return await deleteUser(user);
} catch (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
});