mirror of
https://github.com/documenso/documenso.git
synced 2025-11-09 20:12:31 +10:00
## Description **Fixed document limitation bypassing issues through templates.** Previously, users could bypass document restrictions by utilizing templates even after reaching their limitations. This fix ensures that templates will no longer function as a workaround when users reach their document limits. ## Changes 1. imported `useLimits` hook on `data-table-templates.tsx` 2. Disabled the 'Use Template' button when the user reaches their limit. 3. Added an Alert Component on top of the templates page to notify users that they can't use templates anymore because they have reached their limit. 4. Used `getServerLimits` hook on `template-router` to a condition on the server. ## Example  ## Issue Closes #883
102 lines
3.0 KiB
TypeScript
102 lines
3.0 KiB
TypeScript
import { TRPCError } from '@trpc/server';
|
|
|
|
import { getServerLimits } from '@documenso/ee/server-only/limits/server';
|
|
import { createDocumentFromTemplate } from '@documenso/lib/server-only/template/create-document-from-template';
|
|
import { createTemplate } from '@documenso/lib/server-only/template/create-template';
|
|
import { deleteTemplate } from '@documenso/lib/server-only/template/delete-template';
|
|
import { duplicateTemplate } from '@documenso/lib/server-only/template/duplicate-template';
|
|
|
|
import { authenticatedProcedure, router } from '../trpc';
|
|
import {
|
|
ZCreateDocumentFromTemplateMutationSchema,
|
|
ZCreateTemplateMutationSchema,
|
|
ZDeleteTemplateMutationSchema,
|
|
ZDuplicateTemplateMutationSchema,
|
|
} from './schema';
|
|
|
|
export const templateRouter = router({
|
|
createTemplate: authenticatedProcedure
|
|
.input(ZCreateTemplateMutationSchema)
|
|
.mutation(async ({ input, ctx }) => {
|
|
try {
|
|
const { title, templateDocumentDataId } = input;
|
|
|
|
return await createTemplate({
|
|
title,
|
|
userId: ctx.user.id,
|
|
templateDocumentDataId,
|
|
});
|
|
} catch (err) {
|
|
console.error(err);
|
|
|
|
throw new TRPCError({
|
|
code: 'BAD_REQUEST',
|
|
message: 'We were unable to create this template. Please try again later.',
|
|
});
|
|
}
|
|
}),
|
|
|
|
createDocumentFromTemplate: authenticatedProcedure
|
|
.input(ZCreateDocumentFromTemplateMutationSchema)
|
|
.mutation(async ({ input, ctx }) => {
|
|
try {
|
|
const { templateId } = input;
|
|
|
|
const limits = await getServerLimits({ email: ctx.user.email });
|
|
|
|
if (limits.remaining.documents === 0) {
|
|
throw new Error('You have reached your document limit.');
|
|
}
|
|
|
|
return await createDocumentFromTemplate({
|
|
templateId,
|
|
userId: ctx.user.id,
|
|
});
|
|
} catch (err) {
|
|
throw new TRPCError({
|
|
code: 'BAD_REQUEST',
|
|
message: 'We were unable to create this document. Please try again later.',
|
|
});
|
|
}
|
|
}),
|
|
|
|
duplicateTemplate: authenticatedProcedure
|
|
.input(ZDuplicateTemplateMutationSchema)
|
|
.mutation(async ({ input, ctx }) => {
|
|
try {
|
|
const { templateId } = input;
|
|
|
|
return await duplicateTemplate({
|
|
templateId,
|
|
userId: ctx.user.id,
|
|
});
|
|
} 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 {
|
|
const { id } = input;
|
|
|
|
const userId = ctx.user.id;
|
|
|
|
return await deleteTemplate({ id, userId });
|
|
} catch (err) {
|
|
console.error(err);
|
|
|
|
throw new TRPCError({
|
|
code: 'BAD_REQUEST',
|
|
message: 'We were unable to delete this template. Please try again later.',
|
|
});
|
|
}
|
|
}),
|
|
});
|