mirror of
https://github.com/documenso/documenso.git
synced 2025-11-10 04:22:32 +10:00
Implementation of a universal upload allowing for multiple storage backends starting with `database` and `s3`. Allows clients to put and retrieve files from either client or server using a blend of client and server actions.
142 lines
4.0 KiB
TypeScript
142 lines
4.0 KiB
TypeScript
import { TRPCError } from '@trpc/server';
|
|
|
|
import { createDocument } from '@documenso/lib/server-only/document/create-document';
|
|
import { getDocumentById } from '@documenso/lib/server-only/document/get-document-by-id';
|
|
import { getDocumentAndSenderByToken } from '@documenso/lib/server-only/document/get-document-by-token';
|
|
import { sendDocument } from '@documenso/lib/server-only/document/send-document';
|
|
import { setFieldsForDocument } from '@documenso/lib/server-only/field/set-fields-for-document';
|
|
import { setRecipientsForDocument } from '@documenso/lib/server-only/recipient/set-recipients-for-document';
|
|
|
|
import { authenticatedProcedure, procedure, router } from '../trpc';
|
|
import {
|
|
ZCreateDocumentMutationSchema,
|
|
ZGetDocumentByIdQuerySchema,
|
|
ZGetDocumentByTokenQuerySchema,
|
|
ZSendDocumentMutationSchema,
|
|
ZSetFieldsForDocumentMutationSchema,
|
|
ZSetRecipientsForDocumentMutationSchema,
|
|
} from './schema';
|
|
|
|
export const documentRouter = router({
|
|
getDocumentById: authenticatedProcedure
|
|
.input(ZGetDocumentByIdQuerySchema)
|
|
.query(async ({ input, ctx }) => {
|
|
try {
|
|
const { id } = input;
|
|
|
|
return await getDocumentById({
|
|
id,
|
|
userId: ctx.user.id,
|
|
});
|
|
} catch (err) {
|
|
console.error(err);
|
|
|
|
throw new TRPCError({
|
|
code: 'BAD_REQUEST',
|
|
message: 'We were unable to find this document. Please try again later.',
|
|
});
|
|
}
|
|
}),
|
|
|
|
getDocumentByToken: procedure.input(ZGetDocumentByTokenQuerySchema).query(async ({ input }) => {
|
|
try {
|
|
const { token } = input;
|
|
|
|
return await getDocumentAndSenderByToken({
|
|
token,
|
|
});
|
|
} catch (err) {
|
|
console.error(err);
|
|
|
|
throw new TRPCError({
|
|
code: 'BAD_REQUEST',
|
|
message: 'We were unable to find this document. Please try again later.',
|
|
});
|
|
}
|
|
}),
|
|
|
|
createDocument: authenticatedProcedure
|
|
.input(ZCreateDocumentMutationSchema)
|
|
.mutation(async ({ input, ctx }) => {
|
|
try {
|
|
const { title, documentDataId } = input;
|
|
|
|
return await createDocument({
|
|
userId: ctx.user.id,
|
|
title,
|
|
documentDataId,
|
|
});
|
|
} catch (err) {
|
|
console.error(err);
|
|
|
|
throw new TRPCError({
|
|
code: 'BAD_REQUEST',
|
|
message: 'We were unable to create this document. Please try again later.',
|
|
});
|
|
}
|
|
}),
|
|
|
|
setRecipientsForDocument: authenticatedProcedure
|
|
.input(ZSetRecipientsForDocumentMutationSchema)
|
|
.mutation(async ({ input, ctx }) => {
|
|
try {
|
|
const { documentId, recipients } = input;
|
|
|
|
return await setRecipientsForDocument({
|
|
userId: ctx.user.id,
|
|
documentId,
|
|
recipients,
|
|
});
|
|
} catch (err) {
|
|
console.error(err);
|
|
|
|
throw new TRPCError({
|
|
code: 'BAD_REQUEST',
|
|
message:
|
|
'We were unable to set the recipients for this document. Please try again later.',
|
|
});
|
|
}
|
|
}),
|
|
|
|
setFieldsForDocument: authenticatedProcedure
|
|
.input(ZSetFieldsForDocumentMutationSchema)
|
|
.mutation(async ({ input, ctx }) => {
|
|
try {
|
|
const { documentId, fields } = input;
|
|
|
|
return await setFieldsForDocument({
|
|
userId: ctx.user.id,
|
|
documentId,
|
|
fields,
|
|
});
|
|
} catch (err) {
|
|
console.error(err);
|
|
|
|
throw new TRPCError({
|
|
code: 'BAD_REQUEST',
|
|
message: 'We were unable to set the fields for this document. Please try again later.',
|
|
});
|
|
}
|
|
}),
|
|
|
|
sendDocument: authenticatedProcedure
|
|
.input(ZSendDocumentMutationSchema)
|
|
.mutation(async ({ input, ctx }) => {
|
|
try {
|
|
const { documentId } = input;
|
|
|
|
return await sendDocument({
|
|
userId: ctx.user.id,
|
|
documentId,
|
|
});
|
|
} catch (err) {
|
|
console.error(err);
|
|
|
|
throw new TRPCError({
|
|
code: 'BAD_REQUEST',
|
|
message: 'We were unable to send this document. Please try again later.',
|
|
});
|
|
}
|
|
}),
|
|
});
|