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

333 lines
10 KiB
TypeScript
Raw Normal View History

2023-10-06 22:54:24 +00:00
import { TRPCError } from '@trpc/server';
import { getServerLimits } from '@documenso/ee/server-only/limits/server';
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import { sendDocument } from '@documenso/lib/server-only/document/send-document';
import { createDocumentFromDirectTemplate } from '@documenso/lib/server-only/template/create-document-from-direct-template';
2023-10-06 22:54:24 +00:00
import { createDocumentFromTemplate } from '@documenso/lib/server-only/template/create-document-from-template';
import { createTemplate } from '@documenso/lib/server-only/template/create-template';
import { createTemplateDirectLink } from '@documenso/lib/server-only/template/create-template-direct-link';
2023-10-06 22:54:24 +00:00
import { deleteTemplate } from '@documenso/lib/server-only/template/delete-template';
import { deleteTemplateDirectLink } from '@documenso/lib/server-only/template/delete-template-direct-link';
2023-10-06 22:54:24 +00:00
import { duplicateTemplate } from '@documenso/lib/server-only/template/duplicate-template';
2024-06-06 14:46:48 +10:00
import { findTemplates } from '@documenso/lib/server-only/template/find-templates';
2024-07-19 13:16:26 +10:00
import { getTemplateById } from '@documenso/lib/server-only/template/get-template-by-id';
import { getTemplateWithDetailsById } from '@documenso/lib/server-only/template/get-template-with-details-by-id';
import { moveTemplateToTeam } from '@documenso/lib/server-only/template/move-template-to-team';
import { toggleTemplateDirectLink } from '@documenso/lib/server-only/template/toggle-template-direct-link';
import { updateTemplateSettings } from '@documenso/lib/server-only/template/update-template-settings';
import { extractNextApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
import type { Document } from '@documenso/prisma/client';
2023-10-06 22:54:24 +00:00
import { authenticatedProcedure, maybeAuthenticatedProcedure, router } from '../trpc';
2023-10-06 22:54:24 +00:00
import {
ZCreateDocumentFromDirectTemplateMutationSchema,
2023-10-06 22:54:24 +00:00
ZCreateDocumentFromTemplateMutationSchema,
ZCreateTemplateDirectLinkMutationSchema,
2023-10-06 22:54:24 +00:00
ZCreateTemplateMutationSchema,
ZDeleteTemplateDirectLinkMutationSchema,
2023-10-06 22:54:24 +00:00
ZDeleteTemplateMutationSchema,
ZDuplicateTemplateMutationSchema,
2024-06-06 14:46:48 +10:00
ZFindTemplatesQuerySchema,
ZGetTemplateWithDetailsByIdQuerySchema,
ZMoveTemplatesToTeamSchema,
ZToggleTemplateDirectLinkMutationSchema,
ZUpdateTemplateSettingsMutationSchema,
2023-10-06 22:54:24 +00:00
} from './schema';
export const templateRouter = router({
createTemplate: authenticatedProcedure
.input(ZCreateTemplateMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
2024-02-08 12:33:20 +11:00
const { teamId, title, templateDocumentDataId } = input;
2023-10-06 22:54:24 +00:00
return await createTemplate({
userId: ctx.user.id,
2024-02-08 12:33:20 +11:00
teamId,
title,
2023-10-06 22:54:24 +00:00
templateDocumentDataId,
});
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to create this template. Please try again later.',
});
}
}),
createDocumentFromDirectTemplate: maybeAuthenticatedProcedure
.input(ZCreateDocumentFromDirectTemplateMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
const {
directRecipientName,
directRecipientEmail,
directTemplateToken,
directTemplateExternalId,
signedFieldValues,
templateUpdatedAt,
} = input;
const requestMetadata = extractNextApiRequestMetadata(ctx.req);
return await createDocumentFromDirectTemplate({
directRecipientName,
directRecipientEmail,
directTemplateToken,
directTemplateExternalId,
signedFieldValues,
templateUpdatedAt,
user: ctx.user
? {
id: ctx.user.id,
name: ctx.user.name || undefined,
email: ctx.user.email,
}
: undefined,
requestMetadata,
});
} catch (err) {
console.error(err);
throw AppError.parseErrorToTRPCError(err);
}
}),
2023-10-06 22:54:24 +00:00
createDocumentFromTemplate: authenticatedProcedure
.input(ZCreateDocumentFromTemplateMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
2024-02-26 10:31:24 +11:00
const { templateId, teamId } = input;
2023-10-06 22:54:24 +00:00
2024-07-19 13:16:26 +10:00
const limits = await getServerLimits({ email: ctx.user.email, teamId });
if (limits.remaining.documents === 0) {
throw new Error('You have reached your document limit.');
}
const requestMetadata = extractNextApiRequestMetadata(ctx.req);
let document: Document = await createDocumentFromTemplate({
2023-10-06 22:54:24 +00:00
templateId,
2024-02-26 10:31:24 +11:00
teamId,
2023-10-06 22:54:24 +00:00
userId: ctx.user.id,
recipients: input.recipients,
requestMetadata,
2023-10-06 22:54:24 +00:00
});
if (input.sendDocument) {
document = await sendDocument({
documentId: document.id,
userId: ctx.user.id,
teamId,
requestMetadata,
}).catch((err) => {
console.error(err);
throw new AppError('DOCUMENT_SEND_FAILED');
});
}
return document;
2023-10-06 22:54:24 +00:00
} catch (err) {
2024-03-30 14:00:34 +08:00
console.error(err);
throw AppError.parseErrorToTRPCError(err);
2023-10-06 22:54:24 +00:00
}
}),
duplicateTemplate: authenticatedProcedure
.input(ZDuplicateTemplateMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
2024-02-08 12:33:20 +11:00
const { teamId, templateId } = input;
2023-10-06 22:54:24 +00:00
return await duplicateTemplate({
userId: ctx.user.id,
2024-02-08 12:33:20 +11:00
teamId,
templateId,
2023-10-06 22:54:24 +00:00
});
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to duplicate the template. Please try again later.',
});
}
}),
deleteTemplate: authenticatedProcedure
.input(ZDeleteTemplateMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
feat: add more template API endpoints (#1198) ## Description Update the API endpoint to support more actions for templates ## Changes Made Add the following endpoints for templates: - Get template - Get templates - Delete template Get template(s) returns associated recipients and fields. UI: - Updated template delete button to have the destructive delete variant ## Testing Performed Tested endpoints via /api/v1/openapi Tested deleting templates via UI manually ## Test data <details> <summary>Delete template response</summary> ```json { "id": 32, "type": "PRIVATE", "title": "documenso-supporter-pledge.pdf", "userId": 3, "teamId": null, "templateDocumentDataId": "clxva9b4h0001rrh7v0wdw97h", "createdAt": "2024-06-26T03:35:45.065Z", "updatedAt": "2024-06-26T03:35:45.065Z" } ``` </details> <details> <summary>Get template response</summary> ```json { "id": 28, "type": "PRIVATE", "title": "blank_long.pdf", "userId": 3, "teamId": null, "templateDocumentDataId": "clxu4vyty0003rrr52ue5ee4d", "createdAt": "2024-06-25T08:17:38.418Z", "updatedAt": "2024-06-26T03:36:33.890Z", "templateMeta": { "id": "clxvaacte0004rrh7s2k910nw", "subject": "", "message": "", "timezone": "Australia/Melbourne", "dateFormat": "yyyy-MM-dd hh:mm a", "templateId": 28, "redirectUrl": "" }, "directLink": { "token": "tBJHVFR75sC8m6hPfBTZd", "enabled": true }, "templateDocumentData": { "id": "clxu4vyty0003rrr52ue5ee4d", "type": "BYTES_64", "data": "<PDF DATA>" }, "Field": [ { "id": 327, "recipientId": 357, "type": "SIGNATURE", "page": 1, "positionX": "55.8431952662722", "positionY": "21.39588100686499", "width": "29.58579881656805", "height": "6.864988558352403" }, { "id": 328, "recipientId": 357, "type": "EMAIL", "page": 1, "positionX": "28.03254437869823", "positionY": "72.99771167048056", "width": "29.58579881656805", "height": "6.864988558352403" } ], "Recipient": [ { "id": 357, "email": "direct.link@documenso.com", "name": "Direct link recipient", "authOptions": { "accessAuth": null, "actionAuth": null }, "role": "SIGNER" }, { "id": 359, "email": "example@documenso.com", "name": "Example User", "authOptions": { "accessAuth": null, "actionAuth": null }, "role": "SIGNER" } ] } ``` </details> <details> <summary>Get templates response</summary> ```json { "templates": [ { "id": 33, "type": "PRIVATE", "title": "documenso-supporter-pledge.pdf", "userId": 3, "teamId": null, "templateDocumentDataId": "clxva9oaj0003rrh7hwdyg60o", "createdAt": "2024-06-26T03:36:02.130Z", "updatedAt": "2024-06-26T03:36:02.130Z", "directLink": null, "Field": [], "Recipient": [] }, { "id": 28, "type": "PRIVATE", "title": "blank_long.pdf", "userId": 3, "teamId": null, "templateDocumentDataId": "clxu4vyty0003rrr52ue5ee4d", "createdAt": "2024-06-25T08:17:38.418Z", "updatedAt": "2024-06-26T03:36:33.890Z", "directLink": { "token": "tBJHVFR75sC8m6hPfBTZd", "enabled": true }, "Field": [ { "id": 327, "recipientId": 357, "type": "SIGNATURE", "page": 1, "positionX": "55.8431952662722", "positionY": "21.39588100686499", "width": "29.58579881656805", "height": "6.864988558352403" }, { "id": 328, "recipientId": 357, "type": "EMAIL", "page": 1, "positionX": "28.03254437869823", "positionY": "72.99771167048056", "width": "29.58579881656805", "height": "6.864988558352403" } ], "Recipient": [ { "id": 357, "email": "direct.link@documenso.com", "name": "Direct link recipient", "authOptions": { "accessAuth": null, "actionAuth": null }, "role": "SIGNER" }, { "id": 359, "email": "example@documenso.com", "name": "Example User", "authOptions": { "accessAuth": null, "actionAuth": null }, "role": "SIGNER" } ] } ], "totalPages": 2 } ``` </details> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added support for team-based template deletion in the dashboard. - Enhanced API to manage templates, including fetching and deleting templates by team ID. - **Bug Fixes** - Improved error handling for template operations, ensuring better feedback when templates are not found. - **Refactor** - Updated various components and functions to include `teamId` for more robust template management. - **Documentation** - Expanded schema definitions to detail new structures for template and team interactions. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-06-27 15:44:16 +10:00
const { id, teamId } = input;
2023-10-06 22:54:24 +00:00
const userId = ctx.user.id;
feat: add more template API endpoints (#1198) ## Description Update the API endpoint to support more actions for templates ## Changes Made Add the following endpoints for templates: - Get template - Get templates - Delete template Get template(s) returns associated recipients and fields. UI: - Updated template delete button to have the destructive delete variant ## Testing Performed Tested endpoints via /api/v1/openapi Tested deleting templates via UI manually ## Test data <details> <summary>Delete template response</summary> ```json { "id": 32, "type": "PRIVATE", "title": "documenso-supporter-pledge.pdf", "userId": 3, "teamId": null, "templateDocumentDataId": "clxva9b4h0001rrh7v0wdw97h", "createdAt": "2024-06-26T03:35:45.065Z", "updatedAt": "2024-06-26T03:35:45.065Z" } ``` </details> <details> <summary>Get template response</summary> ```json { "id": 28, "type": "PRIVATE", "title": "blank_long.pdf", "userId": 3, "teamId": null, "templateDocumentDataId": "clxu4vyty0003rrr52ue5ee4d", "createdAt": "2024-06-25T08:17:38.418Z", "updatedAt": "2024-06-26T03:36:33.890Z", "templateMeta": { "id": "clxvaacte0004rrh7s2k910nw", "subject": "", "message": "", "timezone": "Australia/Melbourne", "dateFormat": "yyyy-MM-dd hh:mm a", "templateId": 28, "redirectUrl": "" }, "directLink": { "token": "tBJHVFR75sC8m6hPfBTZd", "enabled": true }, "templateDocumentData": { "id": "clxu4vyty0003rrr52ue5ee4d", "type": "BYTES_64", "data": "<PDF DATA>" }, "Field": [ { "id": 327, "recipientId": 357, "type": "SIGNATURE", "page": 1, "positionX": "55.8431952662722", "positionY": "21.39588100686499", "width": "29.58579881656805", "height": "6.864988558352403" }, { "id": 328, "recipientId": 357, "type": "EMAIL", "page": 1, "positionX": "28.03254437869823", "positionY": "72.99771167048056", "width": "29.58579881656805", "height": "6.864988558352403" } ], "Recipient": [ { "id": 357, "email": "direct.link@documenso.com", "name": "Direct link recipient", "authOptions": { "accessAuth": null, "actionAuth": null }, "role": "SIGNER" }, { "id": 359, "email": "example@documenso.com", "name": "Example User", "authOptions": { "accessAuth": null, "actionAuth": null }, "role": "SIGNER" } ] } ``` </details> <details> <summary>Get templates response</summary> ```json { "templates": [ { "id": 33, "type": "PRIVATE", "title": "documenso-supporter-pledge.pdf", "userId": 3, "teamId": null, "templateDocumentDataId": "clxva9oaj0003rrh7hwdyg60o", "createdAt": "2024-06-26T03:36:02.130Z", "updatedAt": "2024-06-26T03:36:02.130Z", "directLink": null, "Field": [], "Recipient": [] }, { "id": 28, "type": "PRIVATE", "title": "blank_long.pdf", "userId": 3, "teamId": null, "templateDocumentDataId": "clxu4vyty0003rrr52ue5ee4d", "createdAt": "2024-06-25T08:17:38.418Z", "updatedAt": "2024-06-26T03:36:33.890Z", "directLink": { "token": "tBJHVFR75sC8m6hPfBTZd", "enabled": true }, "Field": [ { "id": 327, "recipientId": 357, "type": "SIGNATURE", "page": 1, "positionX": "55.8431952662722", "positionY": "21.39588100686499", "width": "29.58579881656805", "height": "6.864988558352403" }, { "id": 328, "recipientId": 357, "type": "EMAIL", "page": 1, "positionX": "28.03254437869823", "positionY": "72.99771167048056", "width": "29.58579881656805", "height": "6.864988558352403" } ], "Recipient": [ { "id": 357, "email": "direct.link@documenso.com", "name": "Direct link recipient", "authOptions": { "accessAuth": null, "actionAuth": null }, "role": "SIGNER" }, { "id": 359, "email": "example@documenso.com", "name": "Example User", "authOptions": { "accessAuth": null, "actionAuth": null }, "role": "SIGNER" } ] } ], "totalPages": 2 } ``` </details> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit - **New Features** - Added support for team-based template deletion in the dashboard. - Enhanced API to manage templates, including fetching and deleting templates by team ID. - **Bug Fixes** - Improved error handling for template operations, ensuring better feedback when templates are not found. - **Refactor** - Updated various components and functions to include `teamId` for more robust template management. - **Documentation** - Expanded schema definitions to detail new structures for template and team interactions. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
2024-06-27 15:44:16 +10:00
return await deleteTemplate({ userId, id, teamId });
2023-10-06 22:54:24 +00:00
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to delete this template. Please try again later.',
});
}
}),
getTemplateWithDetailsById: authenticatedProcedure
.input(ZGetTemplateWithDetailsByIdQuerySchema)
.query(async ({ input, ctx }) => {
try {
return await getTemplateWithDetailsById({
id: input.id,
userId: ctx.user.id,
});
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to find this template. Please try again later.',
});
}
}),
// Todo: Add API
updateTemplateSettings: authenticatedProcedure
.input(ZUpdateTemplateSettingsMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
const { templateId, teamId, data, meta } = input;
const userId = ctx.user.id;
const requestMetadata = extractNextApiRequestMetadata(ctx.req);
return await updateTemplateSettings({
userId,
teamId,
templateId,
data,
meta,
requestMetadata,
});
} catch (err) {
console.error(err);
throw new TRPCError({
code: 'BAD_REQUEST',
message:
'We were unable to update the settings for this template. Please try again later.',
});
}
}),
2024-06-06 14:46:48 +10:00
findTemplates: authenticatedProcedure
.input(ZFindTemplatesQuerySchema)
.query(async ({ input, ctx }) => {
try {
return await findTemplates({
userId: ctx.user.id,
...input,
});
} catch (err) {
console.error(err);
throw AppError.parseErrorToTRPCError(err);
}
}),
createTemplateDirectLink: authenticatedProcedure
.input(ZCreateTemplateDirectLinkMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
2024-07-22 20:52:17 +10:00
const { templateId, teamId, directRecipientId } = input;
const userId = ctx.user.id;
2024-07-22 20:52:17 +10:00
const template = await getTemplateById({ id: templateId, teamId, userId: ctx.user.id });
2024-07-19 13:16:26 +10:00
const limits = await getServerLimits({ email: ctx.user.email, teamId: template.teamId });
if (limits.remaining.directTemplates === 0) {
throw new AppError(
AppErrorCode.LIMIT_EXCEEDED,
'You have reached your direct templates limit.',
);
}
return await createTemplateDirectLink({ userId, templateId, directRecipientId });
} catch (err) {
console.error(err);
const error = AppError.parseError(err);
throw AppError.parseErrorToTRPCError(error);
}
}),
deleteTemplateDirectLink: authenticatedProcedure
.input(ZDeleteTemplateDirectLinkMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
const { templateId } = input;
const userId = ctx.user.id;
return await deleteTemplateDirectLink({ userId, templateId });
} catch (err) {
console.error(err);
const error = AppError.parseError(err);
throw AppError.parseErrorToTRPCError(error);
}
}),
toggleTemplateDirectLink: authenticatedProcedure
.input(ZToggleTemplateDirectLinkMutationSchema)
.mutation(async ({ input, ctx }) => {
try {
const { templateId, enabled } = input;
const userId = ctx.user.id;
return await toggleTemplateDirectLink({ userId, templateId, enabled });
} catch (err) {
console.error(err);
const error = AppError.parseError(err);
throw AppError.parseErrorToTRPCError(error);
}
}),
moveTemplateToTeam: authenticatedProcedure
.input(ZMoveTemplatesToTeamSchema)
.mutation(async ({ input, ctx }) => {
try {
const { templateId, teamId } = input;
const userId = ctx.user.id;
return await moveTemplateToTeam({
templateId,
teamId,
userId,
});
} catch (err) {
console.error(err);
if (err instanceof TRPCError) {
throw err;
}
throw new TRPCError({
code: 'BAD_REQUEST',
message: 'We were unable to move this template. Please try again later.',
});
}
}),
2023-10-06 22:54:24 +00:00
});