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

153 lines
4.8 KiB
TypeScript
Raw Normal View History

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-05-15 18:55:05 +10:00
import { jobsClient } from '@documenso/lib/jobs/client';
2024-06-27 21:50:42 +10:00
import { setAvatarImage } from '@documenso/lib/server-only/profile/set-avatar-image';
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';
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,
2024-06-27 21:50:42 +10:00
ZSetProfileImageMutationSchema,
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 }) => {
2024-12-06 16:01:24 +09:00
return await findUserSecurityAuditLogs({
userId: ctx.user.id,
...input,
});
2024-01-30 17:31:27 +11:00
}),
2023-10-11 12:32:33 +03:00
getUser: adminProcedure.input(ZRetrieveUserByIdQuerySchema).query(async ({ input }) => {
2024-12-06 16:01:24 +09:00
const { id } = input;
2024-03-30 14:00:34 +08:00
2024-12-06 16:01:24 +09:00
return await getUserById({ id });
2023-10-11 12:32:33 +03:00
}),
2023-09-21 15:10:20 +01:00
2023-06-09 18:21:18 +10:00
updateProfile: authenticatedProcedure
.input(ZUpdateProfileMutationSchema)
.mutation(async ({ input, ctx }) => {
2024-12-06 16:01:24 +09:00
const { name, signature } = input;
2023-06-09 18:21:18 +10:00
2024-12-06 16:01:24 +09:00
return await updateProfile({
userId: ctx.user.id,
name,
signature,
requestMetadata: ctx.metadata.requestMetadata,
2024-12-06 16:01:24 +09:00
});
2023-06-09 18:21:18 +10:00
}),
updatePublicProfile: authenticatedProcedure
.input(ZUpdatePublicProfileMutationSchema)
.mutation(async ({ input, ctx }) => {
2024-12-06 16:01:24 +09:00
const { url, bio, enabled } = input;
2024-12-06 16:01:24 +09:00
if (IS_BILLING_ENABLED() && url !== undefined && url.length < 6) {
const subscriptions = await getSubscriptionsByUserId({
2024-02-28 14:43:09 +11:00
userId: ctx.user.id,
2024-12-06 16:01:24 +09:00
}).then((subscriptions) =>
subscriptions.filter((s) => s.status === SubscriptionStatus.ACTIVE),
);
if (subscriptions.length === 0) {
throw new AppError(AppErrorCode.PREMIUM_PROFILE_URL, {
message: 'Only subscribers can have a username shorter than 6 characters',
});
2024-02-28 14:43:09 +11:00
}
}
2024-12-06 16:01:24 +09:00
const user = await updatePublicProfile({
userId: ctx.user.id,
data: {
url,
bio,
enabled,
},
});
return { success: true, url: user.url };
}),
2023-06-09 18:21:18 +10:00
updatePassword: authenticatedProcedure
.input(ZUpdatePasswordMutationSchema)
.mutation(async ({ input, ctx }) => {
2024-12-06 16:01:24 +09:00
const { password, currentPassword } = input;
2024-03-30 14:00:34 +08:00
2024-12-06 16:01:24 +09:00
return await updatePassword({
userId: ctx.user.id,
password,
currentPassword,
requestMetadata: ctx.metadata.requestMetadata,
2024-12-06 16:01:24 +09:00
});
2023-06-09 18:21:18 +10:00
}),
2023-09-18 11:15:29 +00:00
forgotPassword: procedure.input(ZForgotPasswordFormSchema).mutation(async ({ input }) => {
2024-12-06 16:01:24 +09:00
const { email } = input;
2023-09-18 11:15:29 +00:00
2024-12-06 16:01:24 +09:00
return await forgotPassword({
email,
});
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 }) => {
2024-12-06 16:01:24 +09:00
const { password, token } = input;
2023-09-18 14:03:33 +00:00
2024-12-06 16:01:24 +09:00
return await resetPassword({
token,
password,
requestMetadata: extractNextApiRequestMetadata(ctx.req),
});
2023-09-18 14:03:33 +00:00
}),
sendConfirmationEmail: procedure
.input(ZConfirmEmailMutationSchema)
.mutation(async ({ input }) => {
2024-12-06 16:01:24 +09:00
const { email } = input;
2024-12-06 16:01:24 +09:00
await jobsClient.triggerJob({
name: 'send.signup.confirmation.email',
payload: {
email,
},
});
}),
deleteAccount: authenticatedProcedure.mutation(async ({ ctx }) => {
2024-12-06 16:01:24 +09:00
return await deleteUser({
id: ctx.user.id,
});
}),
2024-06-27 21:50:42 +10:00
setProfileImage: authenticatedProcedure
.input(ZSetProfileImageMutationSchema)
.mutation(async ({ input, ctx }) => {
2024-12-06 16:01:24 +09:00
const { bytes, teamId } = input;
2024-06-27 21:50:42 +10:00
2024-12-06 16:01:24 +09:00
return await setAvatarImage({
userId: ctx.user.id,
teamId,
bytes,
requestMetadata: ctx.metadata,
2024-12-06 16:01:24 +09:00
});
2024-06-27 21:50:42 +10:00
}),
2023-06-09 18:21:18 +10:00
});