Skip to content

The Open Source DocuSign Alternative.

feat: certificate qrcode (#1755)

Ephraim Duncanbdb0b0ea88be
diff --git a/apps/remix/app/components/dialogs/document-resend-dialog.tsx b/apps/remix/app/components/dialogs/document-resend-dialog.tsx
index bcd1d61a3..0847eca0f 100644
--- a/apps/remix/app/components/dialogs/document-resend-dialog.tsx
+++ b/apps/remix/app/components/dialogs/document-resend-dialog.tsx
@@ -4,14 +4,14 @@ import { zodResolver } from '@hookform/resolvers/zod';
import { msg } from '@lingui/core/macro';
import { useLingui } from '@lingui/react';
import { Trans } from '@lingui/react/macro';
-import type { Team } from '@prisma/client';
-import { type Document, type Recipient, SigningStatus } from '@prisma/client';
+import { type Recipient, SigningStatus } from '@prisma/client';
import { History } from 'lucide-react';
import { useForm } from 'react-hook-form';
import * as z from 'zod';
import { useSession } from '@documenso/lib/client-only/providers/session';
import { getRecipientType } from '@documenso/lib/client-only/recipient-type';
+import type { TDocumentMany as TDocumentRow } from '@documenso/lib/types/document';
import { recipientAbbreviation } from '@documenso/lib/utils/recipient-formatter';
import { trpc as trpcReact } from '@documenso/trpc/react';
import { cn } from '@documenso/ui/lib/utils';
@@ -43,9 +43,7 @@ import { StackAvatar } from '../general/stack-avatar';
const FORM_ID = 'resend-email';
export type DocumentResendDialogProps = {
- document: Document & {
- team: Pick<Team, 'id' | 'url'> | null;
- };
+ document: TDocumentRow;
recipients: Recipient[];
};
diff --git a/apps/remix/app/components/general/document/document-certificate-qr-view.tsx b/apps/remix/app/components/general/document/document-certificate-qr-view.tsx
new file mode 100644
index 000000000..20d13a007
--- /dev/null
+++ b/apps/remix/app/components/general/document/document-certificate-qr-view.tsx
@@ -0,0 +1,106 @@
+import { useEffect, useState } from 'react';
+
+import { Trans } from '@lingui/react/macro';
+import type { DocumentData } from '@prisma/client';
+import { DateTime } from 'luxon';
+
+import { trpc } from '@documenso/trpc/react';
+import { Button } from '@documenso/ui/primitives/button';
+import {
+ Dialog,
+ DialogContent,
+ DialogDescription,
+ DialogFooter,
+ DialogHeader,
+ DialogTitle,
+} from '@documenso/ui/primitives/dialog';
+import { PDFViewer } from '@documenso/ui/primitives/pdf-viewer';
+
+import { ShareDocumentDownloadButton } from '../share-document-download-button';
+
+export type DocumentCertificateQRViewProps = {
+ documentId: number;
+ title: string;
+ documentData: DocumentData;
+ password?: string | null;
+ recipientCount?: number;
+ completedDate?: Date;
+};
+
+export const DocumentCertificateQRView = ({
+ documentId,
+ title,
+ documentData,
+ password,
+ recipientCount = 0,
+ completedDate,
+}: DocumentCertificateQRViewProps) => {
+ const { data: documentUrl } = trpc.shareLink.getDocumentInternalUrlForQRCode.useQuery({
+ documentId,
+ });
+
+ const [isDialogOpen, setIsDialogOpen] = useState(() => !!documentUrl);
+
+ const formattedDate = completedDate
+ ? DateTime.fromJSDate(completedDate).toLocaleString(DateTime.DATETIME_MED)
+ : '';
+
+ useEffect(() => {
+ if (documentUrl) {
+ setIsDialogOpen(true);
+ }
+ }, [documentUrl]);
+
+ return (
+ <div className="mx-auto w-full max-w-screen-md">
+ {/* Dialog for internal document link */}
+ {documentUrl && (
+ <Dialog open={isDialogOpen} onOpenChange={setIsDialogOpen}>
+ <DialogContent>
+ <DialogHeader>
+ <DialogTitle>
+ <Trans>Document found in your account</Trans>
+ </DialogTitle>
+
+ <DialogDescription>
+ <Trans>
+ This document is available in your Documenso account. You can view more details,
+ recipients, and audit logs there.
+ </Trans>
+ </DialogDescription>
+ </DialogHeader>
+
+ <DialogFooter className="flex flex-row justify-end gap-2">
+ <Button asChild>
+ <a href={documentUrl} target="_blank" rel="noopener noreferrer">
+ <Trans>Go to document</Trans>
+ </a>
+ </Button>
+ </DialogFooter>
+ </DialogContent>
+ </Dialog>
+ )}
+
+ <div className="flex w-full flex-col justify-between gap-4 md:flex-row md:items-end">
+ <div className="space-y-1">
+ <h1 className="text-xl font-medium">{title}</h1>
+ <div className="text-muted-foreground flex flex-col gap-0.5 text-sm">
+ <p>
+ <Trans>{recipientCount} recipients</Trans>
+ </p>
+
+ <p>
+ <Trans>Completed on {formattedDate}</Trans>
+ </p>
+ </div>
+ </div>
+
+ <ShareDocumentDownloadButton title={title} documentData={documentData} />
+ </div>
+
+ <div className="mt-12 w-full">
+ <PDFViewer key={documentData.id} documentData={documentData} password={password} />
+ </div>
+ </div>
+ );
+};
diff --git a/apps/remix/app/components/general/share-document-download-button.tsx b/apps/remix/app/components/general/share-document-download-button.tsx
new file mode 100644
index 000000000..c79ac4f51
--- /dev/null
+++ b/apps/remix/app/components/general/share-document-download-button.tsx
@@ -0,0 +1,53 @@
+import { useState } from 'react';
+
+import { msg } from '@lingui/core/macro';
+import { useLingui } from '@lingui/react';
+import { Trans } from '@lingui/react/macro';
+import { Download } from 'lucide-react';
+
+import { downloadPDF } from '@documenso/lib/client-only/download-pdf';
+import type { DocumentData } from '@documenso/prisma/client';
+import { Button } from '@documenso/ui/primitives/button';
+import { useToast } from '@documenso/ui/primitives/use-toast';
+
+export type ShareDocumentDownloadButtonProps = {
+ title: string;
+ documentData: DocumentData;
+};
+
+export const ShareDocumentDownloadButton = ({
+ title,
+ documentData,
+}: ShareDocumentDownloadButtonProps) => {
+ const { _ } = useLingui();
+ const { toast } = useToast();
+
+ const [isDownloading, setIsDownloading] = useState(false);
+
+ const onDownloadClick = async () => {
+ try {
+ setIsDownloading(true);
+
+ await new Promise((resolve) => {
+ setTimeout(resolve, 4000);
+ });
+
+ await downloadPDF({ documentData, fileName: title });
+ } catch (err) {
+ toast({
+ title: _(msg`Something went wrong`),
+ description: _(msg`An error occurred while downloading your document.`),
+ variant: 'destructive',
+ });
+ } finally {
+ setIsDownloading(false);
+ }
+ };
+
+ return (
+ <Button loading={isDownloading} onClick={onDownloadClick}>
+ {!isDownloading && <Download className="mr-2 h-4 w-4" />}
+ <Trans>Download</Trans>
+ </Button>
+ );
+};
diff --git a/apps/remix/app/components/tables/documents-table-action-button.tsx b/apps/remix/app/components/tables/documents-table-action-button.tsx
index 97230c359..20331de97 100644
--- a/apps/remix/app/components/tables/documents-table-action-button.tsx
+++ b/apps/remix/app/components/tables/documents-table-action-button.tsx
@@ -1,7 +1,6 @@
import { msg } from '@lingui/core/macro';
import { useLingui } from '@lingui/react';
import { Trans } from '@lingui/react/macro';
-import type { Document, Recipient, Team, User } from '@prisma/client';
import { DocumentStatus, RecipientRole, SigningStatus } from '@prisma/client';
import { CheckCircle, Download, Edit, EyeIcon, Pencil } from 'lucide-react';
import { Link } from 'react-router';
@@ -9,6 +8,7 @@ import { match } from 'ts-pattern';
import { downloadPDF } from '@documenso/lib/client-only/download-pdf';
import { useSession } from '@documenso/lib/client-only/providers/session';
+import type { TDocumentMany as TDocumentRow } from '@documenso/lib/types/document';
import { isDocumentCompleted } from '@documenso/lib/utils/document';
import { formatDocumentsPath } from '@documenso/lib/utils/teams';
import { trpc as trpcClient } from '@documenso/trpc/client';
@@ -18,11 +18,7 @@ import { useToast } from '@documenso/ui/primitives/use-toast';
import { useOptionalCurrentTeam } from '~/providers/team';
export type DocumentsTableActionButtonProps = {
- row: Document & {
- user: Pick<User, 'id' | 'name' | 'email'>;
- recipients: Recipient[];
- team: Pick<Team, 'id' | 'url'> | null;
- };
+ row: TDocumentRow;
};
export const DocumentsTableActionButton = ({ row }: DocumentsTableActionButtonProps) => {
diff --git a/apps/remix/app/components/tables/documents-table-action-dropdown.tsx b/apps/remix/app/components/tables/documents-table-action-dropdown.tsx
index 21e203a50..73bf010bc 100644
--- a/apps/remix/app/components/tables/documents-table-action-dropdown.tsx
+++ b/apps/remix/app/components/tables/documents-table-action-dropdown.tsx
@@ -3,7 +3,6 @@ import { useState } from 'react';
import { msg } from '@lingui/core/macro';
import { useLingui } from '@lingui/react';
import { Trans } from '@lingui/react/macro';
-import type { Document, Recipient, Team, User } from '@prisma/client';
import { DocumentStatus, RecipientRole } from '@prisma/client';
import {
CheckCircle,
@@ -22,6 +21,7 @@ import { Link } from 'react-router';
import { downloadPDF } from '@documenso/lib/client-only/download-pdf';
import { useSession } from '@documenso/lib/client-only/providers/session';
+import type { TDocumentMany as TDocumentRow } from '@documenso/lib/types/document';
import { isDocumentCompleted } from '@documenso/lib/utils/document';
import { formatDocumentsPath } from '@documenso/lib/utils/teams';
import { trpc as trpcClient } from '@documenso/trpc/client';
@@ -43,11 +43,7 @@ import { DocumentRecipientLinkCopyDialog } from '~/components/general/document/d
import { useOptionalCurrentTeam } from '~/providers/team';
export type DocumentsTableActionDropdownProps = {
- row: Document & {
- user: Pick<User, 'id' | 'name' | 'email'>;
- recipients: Recipient[];
- team: Pick<Team, 'id' | 'url'> | null;
- };
+ row: TDocumentRow;
};
export const DocumentsTableActionDropdown = ({ row }: DocumentsTableActionDropdownProps) => {
diff --git a/apps/remix/app/components/tables/documents-table-title.tsx b/apps/remix/app/components/tables/documents-table-title.tsx
index f31a8dd94..7946b4ea9 100644
--- a/apps/remix/app/components/tables/documents-table-title.tsx
+++ b/apps/remix/app/components/tables/documents-table-title.tsx
@@ -1,16 +1,12 @@
-import type { Document, Recipient, Team, User } from '@prisma/client';
import { Link } from 'react-router';
import { match } from 'ts-pattern';
import { useSession } from '@documenso/lib/client-only/providers/session';
+import type { TDocumentMany as TDocumentRow } from '@documenso/lib/types/document';
import { formatDocumentsPath } from '@documenso/lib/utils/teams';
export type DataTableTitleProps = {
- row: Document & {
- user: Pick<User, 'id' | 'name' | 'email'>;
- team: Pick<Team, 'url'> | null;
- recipients: Recipient[];
- };
+ row: TDocumentRow;
teamUrl?: string;
};
diff --git a/apps/remix/app/routes/_internal+/[__htmltopdf]+/certificate.tsx b/apps/remix/app/routes/_internal+/[__htmltopdf]+/certificate.tsx
index acf11e935..951e6b582 100644
--- a/apps/remix/app/routes/_internal+/[__htmltopdf]+/certificate.tsx
+++ b/apps/remix/app/routes/_internal+/[__htmltopdf]+/certificate.tsx
@@ -5,8 +5,10 @@ import { DateTime } from 'luxon';
import { redirect } from 'react-router';
import { match } from 'ts-pattern';
import { UAParser } from 'ua-parser-js';
+import { renderSVG } from 'uqr';
import { isDocumentPlatform } from '@documenso/ee/server-only/util/is-document-platform';
+import { NEXT_PUBLIC_WEBAPP_URL } from '@documenso/lib/constants/app';
import { APP_I18N_OPTIONS, ZSupportedLanguageCodeSchema } from '@documenso/lib/constants/i18n';
import {
RECIPIENT_ROLES_DESCRIPTION,
@@ -342,7 +344,18 @@ export default function SigningCertificate({ loaderData }: Route.ComponentProps)
</Card>
{isPlatformDocument && (
- <div className="my-8 flex-row-reverse">
+ <div className="my-8 flex-row-reverse space-y-4">
+ <div className="flex items-end justify-end gap-x-4">
+ <div
+ className="flex h-24 w-24 justify-center"
+ dangerouslySetInnerHTML={{
+ __html: renderSVG(`${NEXT_PUBLIC_WEBAPP_URL()}/share/${document.qrToken}`, {
+ ecc: 'Q',
+ }),
+ }}
+ />
+ </div>
+
<div className="flex items-end justify-end gap-x-4">
<p className="flex-shrink-0 text-sm font-medium print:text-xs">
{_(msg`Signing certificate provided by`)}:
diff --git a/apps/remix/app/routes/_share+/_layout.tsx b/apps/remix/app/routes/_share+/_layout.tsx
new file mode 100644
index 000000000..39b519864
--- /dev/null
+++ b/apps/remix/app/routes/_share+/_layout.tsx
@@ -0,0 +1,11 @@
+import { Outlet } from 'react-router';
+
+export default function Layout() {
+ return (
+ <main className="relative flex min-h-screen flex-col items-center justify-center overflow-hidden px-4 py-12 md:p-12 lg:p-24">
+ <div className="relative w-full">
+ <Outlet />
+ </div>
+ </main>
+ );
+}
diff --git a/apps/remix/app/routes/_unauthenticated+/share.$slug.opengraph.tsx b/apps/remix/app/routes/_share+/share.$slug.opengraph.tsx
similarity index 97%
rename from apps/remix/app/routes/_unauthenticated+/share.$slug.opengraph.tsx
rename to apps/remix/app/routes/_share+/share.$slug.opengraph.tsx
index 2a6f7c6de..7e4cbd636 100644
--- a/apps/remix/app/routes/_unauthenticated+/share.$slug.opengraph.tsx
+++ b/apps/remix/app/routes/_share+/share.$slug.opengraph.tsx
@@ -22,6 +22,11 @@ const IMAGE_SIZE = {
export const loader = async ({ params }: Route.LoaderArgs) => {
const { slug } = params;
+ // QR codes are not supported for OpenGraph images
+ if (slug.startsWith('qr_')) {
+ return new Response('Not found', { status: 404 });
+ }
+
const baseUrl = NEXT_PUBLIC_WEBAPP_URL();
const [interSemiBold, interRegular, caveatRegular] = await Promise.all([
diff --git a/apps/remix/app/routes/_unauthenticated+/share.$slug.tsx b/apps/remix/app/routes/_share+/share.$slug.tsx
similarity index 57%
rename from apps/remix/app/routes/_unauthenticated+/share.$slug.tsx
rename to apps/remix/app/routes/_share+/share.$slug.tsx
index e82ab6676..c0a3f75b2 100644
--- a/apps/remix/app/routes/_unauthenticated+/share.$slug.tsx
+++ b/apps/remix/app/routes/_share+/share.$slug.tsx
@@ -1,10 +1,17 @@
-import { redirect } from 'react-router';
+import { redirect, useLoaderData } from 'react-router';
import { NEXT_PUBLIC_WEBAPP_URL } from '@documenso/lib/constants/app';
+import { getDocumentByAccessToken } from '@documenso/lib/server-only/document/get-document-by-access-token';
+
+import { DocumentCertificateQRView } from '~/components/general/document/document-certificate-qr-view';
import type { Route } from './+types/share.$slug';
export function meta({ params: { slug } }: Route.MetaArgs) {
+ if (slug.startsWith('qr_')) {
+ return undefined;
+ }
+
return [
{ title: 'Documenso - Share' },
{ description: 'I just signed a document in style with Documenso!' },
@@ -43,11 +50,23 @@ export function meta({ params: { slug } }: Route.MetaArgs) {
];
}
-export const loader = ({ request }: Route.LoaderArgs) => {
+export const loader = async ({ request, params: { slug } }: Route.LoaderArgs) => {
+ if (slug.startsWith('qr_')) {
+ const document = await getDocumentByAccessToken({ token: slug });
+
+ if (!document) {
+ throw redirect('/');
+ }
+
+ return {
+ document,
+ };
+ }
+
const userAgent = request.headers.get('User-Agent') ?? '';
if (/bot|facebookexternalhit|WhatsApp|google|bing|duckduckbot|MetaInspector/i.test(userAgent)) {
- return null;
+ return {};
}
// Is hardcoded because this whole meta is hardcoded anyway for Documenso.
@@ -55,5 +74,20 @@ export const loader = ({ request }: Route.LoaderArgs) => {
};
export default function SharePage() {
+ const { document } = useLoaderData<typeof loader>();
+
+ if (document) {
+ return (
+ <DocumentCertificateQRView
+ documentId={document.id}
+ title={document.title}
+ documentData={document.documentData}
+ password={document.documentMeta?.password}
+ recipientCount={document.recipients?.length ?? 0}
+ completedDate={document.completedAt ?? undefined}
+ />
+ );
+ }
+
return <div></div>;
}
diff --git a/packages/lib/jobs/definitions/internal/seal-document.handler.ts b/packages/lib/jobs/definitions/internal/seal-document.handler.ts
index 716d4737b..5726dcf2a 100644
--- a/packages/lib/jobs/definitions/internal/seal-document.handler.ts
+++ b/packages/lib/jobs/definitions/internal/seal-document.handler.ts
@@ -22,6 +22,7 @@ import {
ZWebhookDocumentSchema,
mapDocumentToWebhookDocumentPayload,
} from '../../../types/webhook-payload';
+import { prefixedId } from '../../../universal/id';
import { getFileServerSide } from '../../../universal/upload/get-file.server';
import { putPdfFileServerSide } from '../../../universal/upload/put-file.server';
import { fieldsContainUnsignedRequiredField } from '../../../utils/advanced-fields-helpers';
@@ -130,6 +131,17 @@ export const run = async ({
documentData.data = documentData.initialData;
}
+ if (!document.qrToken) {
+ await prisma.document.update({
+ where: {
+ id: document.id,
+ },
+ data: {
+ qrToken: prefixedId('qr'),
+ },
+ });
+ }
+
const pdfData = await getFileServerSide(documentData);
const certificateData =
diff --git a/packages/lib/server-only/document/create-document-v2.ts b/packages/lib/server-only/document/create-document-v2.ts
index 9e6aed167..77babbba4 100644
--- a/packages/lib/server-only/document/create-document-v2.ts
+++ b/packages/lib/server-only/document/create-document-v2.ts
@@ -13,7 +13,7 @@ import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
import { normalizePdf as makeNormalizedPdf } from '@documenso/lib/server-only/pdf/normalize-pdf';
import { DOCUMENT_AUDIT_LOG_TYPE } from '@documenso/lib/types/document-audit-logs';
import type { ApiRequestMetadata } from '@documenso/lib/universal/extract-request-metadata';
-import { nanoid } from '@documenso/lib/universal/id';
+import { nanoid, prefixedId } from '@documenso/lib/universal/id';
import { createDocumentAuditLogData } from '@documenso/lib/utils/document-audit-logs';
import { prisma } from '@documenso/prisma';
import type { TCreateDocumentV2Request } from '@documenso/trpc/server/document-router/schema';
@@ -142,6 +142,7 @@ export const createDocumentV2 = async ({
const document = await tx.document.create({
data: {
title,
+ qrToken: prefixedId('qr'),
externalId: data.externalId,
documentDataId,
userId,
diff --git a/packages/lib/server-only/document/create-document.ts b/packages/lib/server-only/document/create-document.ts
index 992e3c685..8ca98ca29 100644
--- a/packages/lib/server-only/document/create-document.ts
+++ b/packages/lib/server-only/document/create-document.ts
@@ -13,6 +13,7 @@ import {
ZWebhookDocumentSchema,
mapDocumentToWebhookDocumentPayload,
} from '../../types/webhook-payload';
+import { prefixedId } from '../../universal/id';
import { getFileServerSide } from '../../universal/upload/get-file.server';
import { putPdfFileServerSide } from '../../universal/upload/put-file.server';
import { determineDocumentVisibility } from '../../utils/document-visibility';
@@ -115,6 +116,7 @@ export const createDocument = async ({
const document = await tx.document.create({
data: {
title,
+ qrToken: prefixedId('qr'),
externalId,
documentDataId,
userId,
diff --git a/packages/lib/server-only/document/duplicate-document-by-id.ts b/packages/lib/server-only/document/duplicate-document-by-id.ts
index d91f0deb6..5599b0b4d 100644
--- a/packages/lib/server-only/document/duplicate-document-by-id.ts
+++ b/packages/lib/server-only/document/duplicate-document-by-id.ts
@@ -3,6 +3,7 @@ import { DocumentSource, type Prisma } from '@prisma/client';
import { prisma } from '@documenso/prisma';
import { AppError, AppErrorCode } from '../../errors/app-error';
+import { prefixedId } from '../../universal/id';
import { getDocumentWhereInput } from './get-document-by-id';
export interface DuplicateDocumentOptions {
@@ -56,6 +57,7 @@ export const duplicateDocument = async ({
const createDocumentArguments: Prisma.DocumentCreateArgs = {
data: {
title: document.title,
+ qrToken: prefixedId('qr'),
user: {
connect: {
id: document.userId,
diff --git a/packages/lib/server-only/document/get-document-by-access-token.ts b/packages/lib/server-only/document/get-document-by-access-token.ts
new file mode 100644
index 000000000..e7ccdfbf8
--- /dev/null
+++ b/packages/lib/server-only/document/get-document-by-access-token.ts
@@ -0,0 +1,38 @@
+import { prisma } from '@documenso/prisma';
+
+export type GetDocumentByAccessTokenOptions = {
+ token: string;
+};
+
+export const getDocumentByAccessToken = async ({ token }: GetDocumentByAccessTokenOptions) => {
+ if (!token) {
+ throw new Error('Missing token');
+ }
+
+ const result = await prisma.document.findFirstOrThrow({
+ where: {
+ qrToken: token,
+ },
+ select: {
+ id: true,
+ title: true,
+ completedAt: true,
+ documentData: {
+ select: {
+ id: true,
+ type: true,
+ data: true,
+ initialData: true,
+ },
+ },
+ documentMeta: {
+ select: {
+ password: true,
+ },
+ },
+ recipients: true,
+ },
+ });
+
+ return result;
+};
diff --git a/packages/lib/server-only/template/create-document-from-direct-template.ts b/packages/lib/server-only/template/create-document-from-direct-template.ts
index 0b8784417..9d71f3297 100644
--- a/packages/lib/server-only/template/create-document-from-direct-template.ts
+++ b/packages/lib/server-only/template/create-document-from-direct-template.ts
@@ -19,7 +19,7 @@ import { z } from 'zod';
import { mailer } from '@documenso/email/mailer';
import { DocumentCreatedFromDirectTemplateEmailTemplate } from '@documenso/email/templates/document-created-from-direct-template';
-import { nanoid } from '@documenso/lib/universal/id';
+import { nanoid, prefixedId } from '@documenso/lib/universal/id';
import { prisma } from '@documenso/prisma';
import type { TSignFieldWithTokenMutationSchema } from '@documenso/trpc/server/field-router/schema';
@@ -276,6 +276,7 @@ export const createDocumentFromDirectTemplate = async ({
// Create the document and non direct template recipients.
const document = await tx.document.create({
data: {
+ qrToken: prefixedId('qr'),
source: DocumentSource.TEMPLATE_DIRECT_LINK,
templateId: template.id,
userId: template.userId,
diff --git a/packages/lib/server-only/template/create-document-from-template-legacy.ts b/packages/lib/server-only/template/create-document-from-template-legacy.ts
index ea1659a1d..06aaa3c64 100644
--- a/packages/lib/server-only/template/create-document-from-template-legacy.ts
+++ b/packages/lib/server-only/template/create-document-from-template-legacy.ts
@@ -1,6 +1,6 @@
import { DocumentSource, type RecipientRole } from '@prisma/client';
-import { nanoid } from '@documenso/lib/universal/id';
+import { nanoid, prefixedId } from '@documenso/lib/universal/id';
import { prisma } from '@documenso/prisma';
export type CreateDocumentFromTemplateLegacyOptions = {
@@ -70,6 +70,7 @@ export const createDocumentFromTemplateLegacy = async ({
const document = await prisma.document.create({
data: {
+ qrToken: prefixedId('qr'),
source: DocumentSource.TEMPLATE,
templateId: template.id,
userId,
diff --git a/packages/lib/server-only/template/create-document-from-template.ts b/packages/lib/server-only/template/create-document-from-template.ts
index b17a36835..ebefd10cb 100644
--- a/packages/lib/server-only/template/create-document-from-template.ts
+++ b/packages/lib/server-only/template/create-document-from-template.ts
@@ -11,7 +11,7 @@ import {
} from '@prisma/client';
import { match } from 'ts-pattern';
-import { nanoid } from '@documenso/lib/universal/id';
+import { nanoid, prefixedId } from '@documenso/lib/universal/id';
import { prisma } from '@documenso/prisma';
import type { SupportedLanguageCodes } from '../../constants/i18n';
@@ -372,6 +372,7 @@ export const createDocumentFromTemplate = async ({
return await prisma.$transaction(async (tx) => {
const document = await tx.document.create({
data: {
+ qrToken: prefixedId('qr'),
source: DocumentSource.TEMPLATE,
externalId: externalId || template.externalId,
templateId: template.id,
diff --git a/packages/lib/types/document.ts b/packages/lib/types/document.ts
index 3709b140c..0835f8ce7 100644
--- a/packages/lib/types/document.ts
+++ b/packages/lib/types/document.ts
@@ -86,6 +86,8 @@ export const ZDocumentLiteSchema = DocumentSchema.pick({
useLegacyFieldInsertion: true,
});
+export type TDocumentLite = z.infer<typeof ZDocumentLiteSchema>;
+
/**
* A version of the document response schema when returning multiple documents at once from a single API endpoint.
*/
@@ -119,3 +121,5 @@ export const ZDocumentManySchema = DocumentSchema.pick({
url: true,
}).nullable(),
});
+
+export type TDocumentMany = z.infer<typeof ZDocumentManySchema>;
diff --git a/packages/lib/universal/id.ts b/packages/lib/universal/id.ts
index 0d40dd088..e2f01b044 100644
--- a/packages/lib/universal/id.ts
+++ b/packages/lib/universal/id.ts
@@ -3,3 +3,9 @@ import { customAlphabet } from 'nanoid';
export const alphaid = customAlphabet('0123456789abcdefghijklmnopqrstuvwxyz', 21);
export { nanoid } from 'nanoid';
+
+export const fancyId = customAlphabet('abcdefhiklmnorstuvwxyz', 16);
+
+export const prefixedId = (prefix: string, length = 16) => {
+ return `${prefix}_${fancyId(length)}`;
+};
diff --git a/packages/prisma/index.ts b/packages/prisma/index.ts
index b9c1600b7..cffa7a451 100644
--- a/packages/prisma/index.ts
+++ b/packages/prisma/index.ts
@@ -31,4 +31,38 @@ export const kyselyPrisma = remember('kyselyPrisma', () =>
),
);
+export const prismaWithLogging = remember('prismaWithLogging', () => {
+ const client = new PrismaClient({
+ datasourceUrl: getDatabaseUrl(),
+ log: [
+ {
+ emit: 'event',
+ level: 'query',
+ },
+ ],
+ });
+
+ client.$on('query', (e) => {
+ console.log('query:', e.query);
+ console.log('params:', e.params);
+ console.log('duration:', e.duration);
+
+ const params = JSON.parse(e.params) as unknown[];
+
+ const query = e.query.replace(/\$\d+/g, (match) => {
+ const index = Number(match.replace('$', ''));
+
+ if (index > params.length) {
+ return match;
+ }
+
+ return String(params[index - 1]);
+ });
+
+ console.log('formatted query:', query);
+ });
+
+ return client;
+});
+
export { sql } from 'kysely';
diff --git a/packages/prisma/migrations/20250425015123_add_qr_token_to_document/migration.sql b/packages/prisma/migrations/20250425015123_add_qr_token_to_document/migration.sql
new file mode 100644
index 000000000..7965ef1f2
--- /dev/null
+++ b/packages/prisma/migrations/20250425015123_add_qr_token_to_document/migration.sql
@@ -0,0 +1,2 @@
+-- AlterTable
+ALTER TABLE "Document" ADD COLUMN "qrToken" TEXT;
diff --git a/packages/prisma/schema.prisma b/packages/prisma/schema.prisma
index 4eec0bbc7..0ba161545 100644
--- a/packages/prisma/schema.prisma
+++ b/packages/prisma/schema.prisma
@@ -315,6 +315,7 @@ enum DocumentVisibility {
/// @zod.import(["import { ZDocumentAuthOptionsSchema } from '@documenso/lib/types/document-auth';", "import { ZDocumentFormValuesSchema } from '@documenso/lib/types/document-form-values';"])
model Document {
id Int @id @default(autoincrement())
+ qrToken String? /// @zod.string.describe("The token for viewing the document using the QR code on the certificate.")
externalId String? /// @zod.string.describe("A custom external ID you can use to identify the document.")
userId Int /// @zod.number.describe("The ID of the user that created this document.")
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
diff --git a/packages/trpc/server/share-link-router/get-document-internal-url-for-qr-code.ts b/packages/trpc/server/share-link-router/get-document-internal-url-for-qr-code.ts
new file mode 100644
index 000000000..d1d9cdcbc
--- /dev/null
+++ b/packages/trpc/server/share-link-router/get-document-internal-url-for-qr-code.ts
@@ -0,0 +1,61 @@
+import { NEXT_PUBLIC_WEBAPP_URL } from '@documenso/lib/constants/app';
+import { prisma } from '@documenso/prisma';
+
+import { procedure } from '../trpc';
+import {
+ ZGetDocumentInternalUrlForQRCodeInput,
+ ZGetDocumentInternalUrlForQRCodeOutput,
+} from './get-document-internal-url-for-qr-code.types';
+
+export const getDocumentInternalUrlForQRCodeRoute = procedure
+ .input(ZGetDocumentInternalUrlForQRCodeInput)
+ .output(ZGetDocumentInternalUrlForQRCodeOutput)
+ .query(async ({ input, ctx }) => {
+ const { documentId } = input;
+
+ if (!ctx.user) {
+ return null;
+ }
+
+ const document = await prisma.document.findFirst({
+ where: {
+ OR: [
+ {
+ id: documentId,
+ userId: ctx.user.id,
+ },
+ {
+ id: documentId,
+ team: {
+ members: {
+ some: {
+ userId: ctx.user.id,
+ },
+ },
+ },
+ },
+ ],
+ },
+ include: {
+ team: {
+ where: {
+ members: {
+ some: {
+ userId: ctx.user.id,
+ },
+ },
+ },
+ },
+ },
+ });
+
+ if (!document) {
+ return null;
+ }
+
+ if (document.team) {
+ return `${NEXT_PUBLIC_WEBAPP_URL()}/t/${document.team.url}/documents/${document.id}`;
+ }
+
+ return `${NEXT_PUBLIC_WEBAPP_URL()}/documents/${document.id}`;
+ });
diff --git a/packages/trpc/server/share-link-router/get-document-internal-url-for-qr-code.types.ts b/packages/trpc/server/share-link-router/get-document-internal-url-for-qr-code.types.ts
new file mode 100644
index 000000000..cc7a56c1d
--- /dev/null
+++ b/packages/trpc/server/share-link-router/get-document-internal-url-for-qr-code.types.ts
@@ -0,0 +1,15 @@
+import { z } from 'zod';
+
+export const ZGetDocumentInternalUrlForQRCodeInput = z.object({
+ documentId: z.number(),
+});
+
+export type TGetDocumentInternalUrlForQRCodeInput = z.infer<
+ typeof ZGetDocumentInternalUrlForQRCodeInput
+>;
+
+export const ZGetDocumentInternalUrlForQRCodeOutput = z.string().nullable();
+
+export type TGetDocumentInternalUrlForQRCodeOutput = z.infer<
+ typeof ZGetDocumentInternalUrlForQRCodeOutput
+>;
diff --git a/packages/trpc/server/share-link-router/router.ts b/packages/trpc/server/share-link-router/router.ts
index 517e207e8..f10424cae 100644
--- a/packages/trpc/server/share-link-router/router.ts
+++ b/packages/trpc/server/share-link-router/router.ts
@@ -1,6 +1,7 @@
import { createOrGetShareLink } from '@documenso/lib/server-only/share/create-or-get-share-link';
import { procedure, router } from '../trpc';
+import { getDocumentInternalUrlForQRCodeRoute } from './get-document-internal-url-for-qr-code';
import { ZCreateOrGetShareLinkMutationSchema } from './schema';
export const shareLinkRouter = router({
@@ -21,4 +22,6 @@ export const shareLinkRouter = router({
return await createOrGetShareLink({ documentId, userId: ctx.user.id });
}),
+
+ getDocumentInternalUrlForQRCode: getDocumentInternalUrlForQRCodeRoute,
});