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

122 lines
3.6 KiB
TypeScript
Raw Normal View History

2023-09-29 17:26:37 +01:00
import { TRPCError } from '@trpc/server';
2024-03-03 01:55:33 +11:00
import { findDocuments } from '@documenso/lib/server-only/admin/get-all-documents';
import { updateRecipient } from '@documenso/lib/server-only/admin/update-recipient';
2023-09-29 17:26:37 +01:00
import { updateUser } from '@documenso/lib/server-only/admin/update-user';
2024-03-03 01:55:33 +11:00
import { sealDocument } from '@documenso/lib/server-only/document/seal-document';
2024-02-23 10:47:01 +00:00
import { upsertSiteSetting } from '@documenso/lib/server-only/site-settings/upsert-site-setting';
2024-03-03 01:55:33 +11:00
import { deleteUser } from '@documenso/lib/server-only/user/delete-user';
import { getUserById } from '@documenso/lib/server-only/user/get-user-by-id';
2023-09-29 17:26:37 +01:00
2023-10-11 12:32:33 +03:00
import { adminProcedure, router } from '../trpc';
2024-03-03 01:55:33 +11:00
import {
ZAdminDeleteUserMutationSchema,
ZAdminFindDocumentsQuerySchema,
ZAdminResealDocumentMutationSchema,
ZAdminUpdateProfileMutationSchema,
ZAdminUpdateRecipientMutationSchema,
ZAdminUpdateSiteSettingMutationSchema,
} from './schema';
2023-09-29 17:26:37 +01:00
export const adminRouter = router({
2024-03-03 01:55:33 +11:00
findDocuments: adminProcedure.input(ZAdminFindDocumentsQuerySchema).query(async ({ input }) => {
const { term, page, perPage } = input;
try {
return await findDocuments({ term, page, perPage });
} catch (err) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to retrieve the documents. Please try again.',
});
}
}),
2023-10-11 12:32:33 +03:00
updateUser: adminProcedure
2024-03-03 01:55:33 +11:00
.input(ZAdminUpdateProfileMutationSchema)
2023-10-11 12:32:33 +03:00
.mutation(async ({ input }) => {
2023-09-29 17:26:37 +01:00
const { id, name, email, roles } = input;
try {
return await updateUser({ id, name, email, roles });
} catch (err) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to retrieve the specified account. Please try again.',
});
}
}),
2024-02-23 10:47:01 +00:00
2024-03-03 01:55:33 +11:00
updateRecipient: adminProcedure
.input(ZAdminUpdateRecipientMutationSchema)
.mutation(async ({ input }) => {
const { id, name, email } = input;
try {
return await updateRecipient({ id, name, email });
} catch (err) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to update the recipient provided.',
});
}
}),
2024-02-23 10:47:01 +00:00
updateSiteSetting: adminProcedure
2024-03-03 01:55:33 +11:00
.input(ZAdminUpdateSiteSettingMutationSchema)
2024-02-23 10:47:01 +00:00
.mutation(async ({ ctx, input }) => {
try {
const { id, enabled, data } = input;
return await upsertSiteSetting({
id,
enabled,
data,
userId: ctx.user.id,
});
} catch (err) {
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to update the site setting provided.',
});
}
}),
2024-03-03 01:55:33 +11:00
resealDocument: adminProcedure
.input(ZAdminResealDocumentMutationSchema)
.mutation(async ({ input }) => {
const { id } = input;
try {
return await sealDocument({ documentId: id, isResealing: true });
} catch (err) {
console.log('resealDocument error', err);
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to reseal the document provided.',
});
}
}),
deleteUser: adminProcedure.input(ZAdminDeleteUserMutationSchema).mutation(async ({ input }) => {
const { id, email } = input;
try {
const user = await getUserById({ id });
if (user.email !== email) {
throw new Error('Email does not match');
}
return await deleteUser({ id });
} catch (err) {
console.log(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to delete the specified account. Please try again.',
});
}
}),
2023-09-29 17:26:37 +01:00
});