2023-12-02 09:38:24 +11:00
|
|
|
import { TRPCError } from '@trpc/server';
|
|
|
|
|
|
|
|
|
|
import { completeDocumentWithToken } from '@documenso/lib/server-only/document/complete-document-with-token';
|
|
|
|
|
import { setRecipientsForDocument } from '@documenso/lib/server-only/recipient/set-recipients-for-document';
|
2023-12-21 17:01:12 +11:00
|
|
|
import { setRecipientsForTemplate } from '@documenso/lib/server-only/recipient/set-recipients-for-template';
|
2024-02-12 12:04:53 +11:00
|
|
|
import { extractNextApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
|
2023-12-02 09:38:24 +11:00
|
|
|
|
|
|
|
|
import { authenticatedProcedure, procedure, router } from '../trpc';
|
2023-12-21 17:01:12 +11:00
|
|
|
import {
|
|
|
|
|
ZAddSignersMutationSchema,
|
|
|
|
|
ZAddTemplateSignersMutationSchema,
|
|
|
|
|
ZCompleteDocumentWithTokenMutationSchema,
|
|
|
|
|
} from './schema';
|
2023-12-02 09:38:24 +11:00
|
|
|
|
|
|
|
|
export const recipientRouter = router({
|
|
|
|
|
addSigners: authenticatedProcedure
|
|
|
|
|
.input(ZAddSignersMutationSchema)
|
|
|
|
|
.mutation(async ({ input, ctx }) => {
|
|
|
|
|
try {
|
2024-02-26 11:59:32 +11:00
|
|
|
const { documentId, teamId, signers } = input;
|
2023-12-02 09:38:24 +11:00
|
|
|
|
|
|
|
|
return await setRecipientsForDocument({
|
|
|
|
|
userId: ctx.user.id,
|
|
|
|
|
documentId,
|
2024-02-26 11:59:32 +11:00
|
|
|
teamId,
|
2023-12-02 09:38:24 +11:00
|
|
|
recipients: signers.map((signer) => ({
|
|
|
|
|
id: signer.nativeId,
|
|
|
|
|
email: signer.email,
|
|
|
|
|
name: signer.name,
|
2024-02-01 18:45:02 -05:00
|
|
|
role: signer.role,
|
2024-03-28 13:13:29 +08:00
|
|
|
actionAuth: signer.actionAuth,
|
2023-12-02 09:38:24 +11:00
|
|
|
})),
|
2024-02-12 12:04:53 +11:00
|
|
|
requestMetadata: extractNextApiRequestMetadata(ctx.req),
|
2023-12-02 09:38:24 +11:00
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'BAD_REQUEST',
|
2024-02-08 12:33:20 +11:00
|
|
|
message: 'We were unable to set this field. Please try again later.',
|
2023-12-02 09:38:24 +11:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
|
2023-12-21 17:01:12 +11:00
|
|
|
addTemplateSigners: authenticatedProcedure
|
|
|
|
|
.input(ZAddTemplateSignersMutationSchema)
|
|
|
|
|
.mutation(async ({ input, ctx }) => {
|
|
|
|
|
try {
|
2024-05-10 19:45:19 +07:00
|
|
|
const { templateId, signers, teamId } = input;
|
2023-12-21 17:01:12 +11:00
|
|
|
|
|
|
|
|
return await setRecipientsForTemplate({
|
|
|
|
|
userId: ctx.user.id,
|
2024-05-10 19:45:19 +07:00
|
|
|
teamId,
|
2023-12-21 17:01:12 +11:00
|
|
|
templateId,
|
|
|
|
|
recipients: signers.map((signer) => ({
|
|
|
|
|
id: signer.nativeId,
|
|
|
|
|
email: signer.email,
|
|
|
|
|
name: signer.name,
|
2024-02-15 07:01:41 +00:00
|
|
|
role: signer.role,
|
2024-05-10 19:45:19 +07:00
|
|
|
actionAuth: signer.actionAuth,
|
2023-12-21 17:01:12 +11:00
|
|
|
})),
|
|
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'BAD_REQUEST',
|
2024-02-08 12:33:20 +11:00
|
|
|
message: 'We were unable to set this field. Please try again later.',
|
2023-12-21 17:01:12 +11:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
|
2023-12-02 09:38:24 +11:00
|
|
|
completeDocumentWithToken: procedure
|
|
|
|
|
.input(ZCompleteDocumentWithTokenMutationSchema)
|
2024-02-12 12:04:53 +11:00
|
|
|
.mutation(async ({ input, ctx }) => {
|
2023-12-02 09:38:24 +11:00
|
|
|
try {
|
2024-03-28 13:13:29 +08:00
|
|
|
const { token, documentId, authOptions } = input;
|
2023-12-02 09:38:24 +11:00
|
|
|
|
|
|
|
|
return await completeDocumentWithToken({
|
|
|
|
|
token,
|
|
|
|
|
documentId,
|
2024-03-28 13:13:29 +08:00
|
|
|
authOptions,
|
|
|
|
|
userId: ctx.user?.id,
|
2024-02-12 12:04:53 +11:00
|
|
|
requestMetadata: extractNextApiRequestMetadata(ctx.req),
|
2023-12-02 09:38:24 +11:00
|
|
|
});
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.error(err);
|
|
|
|
|
|
|
|
|
|
throw new TRPCError({
|
|
|
|
|
code: 'BAD_REQUEST',
|
|
|
|
|
message: 'We were unable to sign this field. Please try again later.',
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
}),
|
|
|
|
|
});
|