mirror of
https://github.com/documenso/documenso.git
synced 2025-11-12 07:43:16 +10:00
fix: Prevent users from bypassing document limitations (#898)
## 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
This commit is contained in:
@ -2,13 +2,16 @@
|
||||
|
||||
import { useState, useTransition } from 'react';
|
||||
|
||||
import Link from 'next/link';
|
||||
import { useRouter } from 'next/navigation';
|
||||
|
||||
import { Loader, Plus } from 'lucide-react';
|
||||
import { AlertTriangle, Loader, Plus } from 'lucide-react';
|
||||
|
||||
import { useLimits } from '@documenso/ee/server-only/limits/provider/client';
|
||||
import { useUpdateSearchParams } from '@documenso/lib/client-only/hooks/use-update-search-params';
|
||||
import type { Template } from '@documenso/prisma/client';
|
||||
import { trpc } from '@documenso/trpc/react';
|
||||
import { Alert, AlertDescription, AlertTitle } from '@documenso/ui/primitives/alert';
|
||||
import { Button } from '@documenso/ui/primitives/button';
|
||||
import { DataTable } from '@documenso/ui/primitives/data-table';
|
||||
import { DataTablePagination } from '@documenso/ui/primitives/data-table-pagination';
|
||||
@ -36,6 +39,8 @@ export const TemplatesDataTable = ({
|
||||
const [isPending, startTransition] = useTransition();
|
||||
const updateSearchParams = useUpdateSearchParams();
|
||||
|
||||
const { remaining } = useLimits();
|
||||
|
||||
const router = useRouter();
|
||||
|
||||
const { toast } = useToast();
|
||||
@ -77,6 +82,19 @@ export const TemplatesDataTable = ({
|
||||
|
||||
return (
|
||||
<div className="relative">
|
||||
{remaining.documents === 0 && (
|
||||
<Alert className="mb-4 mt-5">
|
||||
<AlertTriangle className="h-4 w-4" />
|
||||
<AlertTitle>Document Limit Exceeded!</AlertTitle>
|
||||
<AlertDescription className="mt-2">
|
||||
You have reached your document limit.{' '}
|
||||
<Link className="underline underline-offset-4" href="/settings/billing">
|
||||
Upgrade your account to continue!
|
||||
</Link>
|
||||
</AlertDescription>
|
||||
</Alert>
|
||||
)}
|
||||
|
||||
<DataTable
|
||||
columns={[
|
||||
{
|
||||
@ -102,7 +120,7 @@ export const TemplatesDataTable = ({
|
||||
return (
|
||||
<div className="flex items-center gap-x-4">
|
||||
<Button
|
||||
disabled={isRowLoading}
|
||||
disabled={isRowLoading || remaining.documents === 0}
|
||||
loading={isRowLoading}
|
||||
onClick={async () => {
|
||||
setLoadingStates((prev) => ({ ...prev, [row.original.id]: true }));
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
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';
|
||||
@ -41,6 +42,12 @@ export const templateRouter = router({
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user