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

168 lines
5.0 KiB
TypeScript
Raw Normal View History

2023-08-17 19:56:18 +10:00
import { TRPCError } from '@trpc/server';
import { getFieldById } from '@documenso/lib/server-only/field/get-field-by-id';
2023-08-17 19:56:18 +10:00
import { removeSignedFieldWithToken } from '@documenso/lib/server-only/field/remove-signed-field-with-token';
2023-12-02 09:38:24 +11:00
import { setFieldsForDocument } from '@documenso/lib/server-only/field/set-fields-for-document';
import { setFieldsForTemplate } from '@documenso/lib/server-only/field/set-fields-for-template';
2023-08-17 19:56:18 +10:00
import { signFieldWithToken } from '@documenso/lib/server-only/field/sign-field-with-token';
import { extractNextApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
2023-08-17 19:56:18 +10:00
2023-12-02 09:38:24 +11:00
import { authenticatedProcedure, procedure, router } from '../trpc';
2023-08-17 19:56:18 +10:00
import {
2023-12-02 09:38:24 +11:00
ZAddFieldsMutationSchema,
ZAddTemplateFieldsMutationSchema,
ZGetFieldQuerySchema,
2023-08-17 19:56:18 +10:00
ZRemovedSignedFieldWithTokenMutationSchema,
ZSignFieldWithTokenMutationSchema,
} from './schema';
export const fieldRouter = router({
2023-12-02 09:38:24 +11:00
addFields: authenticatedProcedure
.input(ZAddFieldsMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
const { documentId, fields } = input;
return await setFieldsForDocument({
documentId,
userId: ctx.user.id,
fields: fields.map((field) => ({
id: field.nativeId,
signerEmail: field.signerEmail,
type: field.type,
pageNumber: field.pageNumber,
pageX: field.pageX,
pageY: field.pageY,
pageWidth: field.pageWidth,
pageHeight: field.pageHeight,
fieldMeta: field.fieldMeta,
2023-12-02 09:38:24 +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
});
}
}),
addTemplateFields: authenticatedProcedure
.input(ZAddTemplateFieldsMutationSchema)
.mutation(async ({ input, ctx }) => {
const { templateId, fields } = input;
2024-03-30 14:00:34 +08:00
try {
return await setFieldsForTemplate({
2024-03-30 14:00:34 +08:00
userId: ctx.user.id,
templateId,
fields: fields.map((field) => ({
id: field.nativeId,
signerEmail: field.signerEmail,
type: field.type,
pageNumber: field.pageNumber,
pageX: field.pageX,
pageY: field.pageY,
pageWidth: field.pageWidth,
pageHeight: field.pageHeight,
fieldMeta: field.fieldMeta,
2024-03-30 14:00:34 +08:00
})),
});
} catch (err) {
console.error(err);
throw err;
}
}),
2023-08-17 19:56:18 +10:00
signFieldWithToken: procedure
.input(ZSignFieldWithTokenMutationSchema)
.mutation(async ({ input, ctx }) => {
2023-08-17 19:56:18 +10:00
try {
2024-03-28 13:13:29 +08:00
const { token, fieldId, value, isBase64, authOptions } = input;
2023-08-17 19:56:18 +10:00
return await signFieldWithToken({
token,
fieldId,
value,
isBase64,
2024-03-28 13:13:29 +08:00
userId: ctx.user?.id,
authOptions,
requestMetadata: extractNextApiRequestMetadata(ctx.req),
2023-08-17 19:56:18 +10:00
});
} catch (err) {
console.error(err);
throw err;
2023-08-17 19:56:18 +10:00
}
}),
removeSignedFieldWithToken: procedure
.input(ZRemovedSignedFieldWithTokenMutationSchema)
.mutation(async ({ input, ctx }) => {
2023-08-17 19:56:18 +10:00
try {
const { token, fieldId } = input;
return await removeSignedFieldWithToken({
token,
fieldId,
requestMetadata: extractNextApiRequestMetadata(ctx.req),
2023-08-17 19:56:18 +10:00
});
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to remove the signature for this field. Please try again later.',
});
}
}),
getField: authenticatedProcedure.input(ZGetFieldQuerySchema).query(async ({ input, ctx }) => {
try {
const { fieldId, teamId } = input;
return await getFieldById({
userId: ctx.user.id,
teamId,
fieldId,
});
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to find this field. Please try again.',
});
}
}),
// This doesn't appear to be used anywhere, and it doesn't seem to support updating template fields
// so commenting this out for now.
// updateField: authenticatedProcedure
// .input(ZUpdateFieldMutationSchema)
// .mutation(async ({ input, ctx }) => {
// try {
// const { documentId, fieldId, fieldMeta, teamId } = input;
// return await updateField({
// userId: ctx.user.id,
// teamId,
// fieldId,
// documentId,
// requestMetadata: extractNextApiRequestMetadata(ctx.req),
// fieldMeta: fieldMeta,
// });
// } catch (err) {
// console.error(err);
// throw new TRPCError({
// code: 'BAD_REQUEST',
// message: 'We were unable to set this field. Please try again later.',
// });
// }
// }),
2023-08-17 19:56:18 +10:00
});