mirror of
https://github.com/documenso/documenso.git
synced 2026-08-23 14:52:23 +10:00
Recipients can download a PDF that contains the inserted fields while the envelope is in the PENDING state. The token file route accepts the `pending` version. The PDF contains only the fields from recipients who signed and the fields of the recipient who makes the request. This is the same set of fields that the signing page shows to the recipient. The signing page and the completion page show the download dialog for pending envelopes. The dialog does not show the "Partial" button for legacy envelopes.
93 lines
3.1 KiB
TypeScript
93 lines
3.1 KiB
TypeScript
import type { DocumentDataVersion } from '@documenso/lib/types/document';
|
|
import type { EnvelopeItem } from '@prisma/client';
|
|
|
|
import { NEXT_PUBLIC_WEBAPP_URL } from '../constants/app';
|
|
|
|
/**
|
|
* `pending` downloads a PDF with the currently-inserted fields burned in. Supported
|
|
* for both session-authed and recipient-token downloads while the envelope is PENDING.
|
|
*/
|
|
export type EnvelopeItemPdfUrlOptions =
|
|
| {
|
|
type: 'download';
|
|
envelopeItem: Pick<EnvelopeItem, 'id' | 'envelopeId'>;
|
|
token: string | undefined;
|
|
version: 'original' | 'signed' | 'pending';
|
|
presignToken?: undefined;
|
|
}
|
|
| {
|
|
type: 'view';
|
|
envelopeItem: Pick<EnvelopeItem, 'id' | 'envelopeId'>;
|
|
token: string | undefined;
|
|
presignToken?: string | undefined;
|
|
};
|
|
|
|
export const getEnvelopeItemPdfUrl = (options: EnvelopeItemPdfUrlOptions) => {
|
|
const { envelopeItem, token, type, presignToken } = options;
|
|
|
|
const { id, envelopeId } = envelopeItem;
|
|
|
|
if (type === 'download') {
|
|
const version = options.version;
|
|
|
|
return token
|
|
? `${NEXT_PUBLIC_WEBAPP_URL()}/api/files/token/${token}/envelopeItem/${id}/download/${version}${presignToken ? `?presignToken=${presignToken}` : ''}`
|
|
: `${NEXT_PUBLIC_WEBAPP_URL()}/api/files/envelope/${envelopeId}/envelopeItem/${id}/download/${version}`;
|
|
}
|
|
|
|
return token
|
|
? `${NEXT_PUBLIC_WEBAPP_URL()}/api/files/token/${token}/envelopeItem/${id}${presignToken ? `?presignToken=${presignToken}` : ''}`
|
|
: `${NEXT_PUBLIC_WEBAPP_URL()}/api/files/envelope/${envelopeId}/envelopeItem/${id}${presignToken ? `?token=${presignToken}` : ''}`;
|
|
};
|
|
|
|
export type DocumentDataUrlOptions = {
|
|
envelopeId: string;
|
|
envelopeItemId: string;
|
|
documentDataId: string;
|
|
token: string | undefined;
|
|
presignToken?: string | undefined;
|
|
version: DocumentDataVersion;
|
|
};
|
|
|
|
/**
|
|
* The difference between this and `getEnvelopeItemPdfUrl` is that this will
|
|
* hard cache since we add the `documentDataId` to the URL.
|
|
*
|
|
* Since `documentDataId` should change when the document is changed/signed, this is a
|
|
* good way to cache an envelope item by.
|
|
*/
|
|
export const getDocumentDataUrl = (options: DocumentDataUrlOptions) => {
|
|
const { envelopeId, envelopeItemId, documentDataId, token, presignToken, version } = options;
|
|
|
|
const partialUrl = `envelope/${envelopeId}/envelopeItem/${envelopeItemId}/dataId/${documentDataId}/${version}/item.pdf`;
|
|
|
|
// Recipient token endpoint.
|
|
if (token) {
|
|
return `${NEXT_PUBLIC_WEBAPP_URL()}/api/files/token/${token}/${partialUrl}`;
|
|
}
|
|
|
|
// Endpoint authenticated by session or presigned token.
|
|
const baseUrl = `${NEXT_PUBLIC_WEBAPP_URL()}/api/files/${partialUrl}`;
|
|
|
|
if (presignToken) {
|
|
return `${baseUrl}?presignToken=${presignToken}`;
|
|
}
|
|
|
|
return baseUrl;
|
|
};
|
|
|
|
/**
|
|
* Gets a PDF url for the PDF viewer.
|
|
*
|
|
* Returns `null` if invalid.
|
|
*/
|
|
export const getDocumentDataUrlForPdfViewer = (options: DocumentDataUrlOptions): string | null => {
|
|
const { envelopeId, envelopeItemId, documentDataId } = options;
|
|
|
|
if (!envelopeId || !envelopeItemId || !documentDataId) {
|
|
return null;
|
|
}
|
|
|
|
return getDocumentDataUrl(options);
|
|
};
|