mirror of
https://github.com/documenso/documenso.git
synced 2025-11-16 01:32:06 +10:00
feat: update audit log and certificate generation process
This commit is contained in:
@ -29,39 +29,30 @@ export const DocumentCertificateDownloadButton = ({
|
|||||||
|
|
||||||
const onDownloadCertificatesClick = async () => {
|
const onDownloadCertificatesClick = async () => {
|
||||||
try {
|
try {
|
||||||
const { url } = await downloadCertificate({ documentId });
|
const { pdfData, filename } = await downloadCertificate({ documentId });
|
||||||
|
|
||||||
const iframe = Object.assign(document.createElement('iframe'), {
|
const byteCharacters = atob(pdfData);
|
||||||
src: url,
|
const byteNumbers = new Array(byteCharacters.length);
|
||||||
});
|
|
||||||
|
|
||||||
Object.assign(iframe.style, {
|
for (let i = 0; i < byteCharacters.length; i++) {
|
||||||
position: 'fixed',
|
byteNumbers[i] = byteCharacters.charCodeAt(i);
|
||||||
top: '0',
|
|
||||||
left: '0',
|
|
||||||
width: '0',
|
|
||||||
height: '0',
|
|
||||||
});
|
|
||||||
|
|
||||||
const onLoaded = () => {
|
|
||||||
if (iframe.contentDocument?.readyState === 'complete') {
|
|
||||||
iframe.contentWindow?.print();
|
|
||||||
|
|
||||||
iframe.contentWindow?.addEventListener('afterprint', () => {
|
|
||||||
document.body.removeChild(iframe);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
// When the iframe has loaded, print the iframe and remove it from the dom
|
const byteArray = new Uint8Array(byteNumbers);
|
||||||
iframe.addEventListener('load', onLoaded);
|
const blob = new Blob([byteArray], { type: 'application/pdf' });
|
||||||
|
|
||||||
document.body.appendChild(iframe);
|
const url = URL.createObjectURL(blob);
|
||||||
|
const link = document.createElement('a');
|
||||||
|
|
||||||
onLoaded();
|
link.href = url;
|
||||||
|
link.download = filename;
|
||||||
|
|
||||||
|
document.body.appendChild(link);
|
||||||
|
link.click();
|
||||||
|
|
||||||
|
document.body.removeChild(link);
|
||||||
|
URL.revokeObjectURL(url);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
|
||||||
|
|
||||||
toast({
|
toast({
|
||||||
title: _(msg`Something went wrong`),
|
title: _(msg`Something went wrong`),
|
||||||
description: _(
|
description: _(
|
||||||
|
|||||||
@ -1,9 +1,6 @@
|
|||||||
import { DateTime } from 'luxon';
|
import { AppError, AppErrorCode } from '@documenso/lib/errors/app-error';
|
||||||
|
|
||||||
import { NEXT_PUBLIC_WEBAPP_URL } from '@documenso/lib/constants/app';
|
|
||||||
import { AppError } from '@documenso/lib/errors/app-error';
|
|
||||||
import { encryptSecondaryData } from '@documenso/lib/server-only/crypto/encrypt';
|
|
||||||
import { getDocumentById } from '@documenso/lib/server-only/document/get-document-by-id';
|
import { getDocumentById } from '@documenso/lib/server-only/document/get-document-by-id';
|
||||||
|
import { getCertificatePdf } from '@documenso/lib/server-only/htmltopdf/get-certificate-pdf';
|
||||||
import { isDocumentCompleted } from '@documenso/lib/utils/document';
|
import { isDocumentCompleted } from '@documenso/lib/utils/document';
|
||||||
|
|
||||||
import { authenticatedProcedure } from '../trpc';
|
import { authenticatedProcedure } from '../trpc';
|
||||||
@ -16,7 +13,7 @@ export const downloadDocumentCertificateRoute = authenticatedProcedure
|
|||||||
.input(ZDownloadDocumentCertificateRequestSchema)
|
.input(ZDownloadDocumentCertificateRequestSchema)
|
||||||
.output(ZDownloadDocumentCertificateResponseSchema)
|
.output(ZDownloadDocumentCertificateResponseSchema)
|
||||||
.mutation(async ({ input, ctx }) => {
|
.mutation(async ({ input, ctx }) => {
|
||||||
const { teamId } = ctx;
|
const { teamId, user } = ctx;
|
||||||
const { documentId } = input;
|
const { documentId } = input;
|
||||||
|
|
||||||
ctx.logger.info({
|
ctx.logger.info({
|
||||||
@ -27,20 +24,43 @@ export const downloadDocumentCertificateRoute = authenticatedProcedure
|
|||||||
|
|
||||||
const document = await getDocumentById({
|
const document = await getDocumentById({
|
||||||
documentId,
|
documentId,
|
||||||
userId: ctx.user.id,
|
userId: user.id,
|
||||||
teamId,
|
teamId,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
if (!document || (teamId && document.teamId !== teamId)) {
|
||||||
|
throw new AppError(AppErrorCode.UNAUTHORIZED, {
|
||||||
|
message: 'You do not have access to this document.',
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!isDocumentCompleted(document.status)) {
|
if (!isDocumentCompleted(document.status)) {
|
||||||
throw new AppError('DOCUMENT_NOT_COMPLETE');
|
throw new AppError('DOCUMENT_NOT_COMPLETE');
|
||||||
}
|
}
|
||||||
|
|
||||||
const encrypted = encryptSecondaryData({
|
try {
|
||||||
data: document.id.toString(),
|
const pdfBuffer = await getCertificatePdf({
|
||||||
expiresAt: DateTime.now().plus({ minutes: 5 }).toJSDate().valueOf(),
|
documentId: document.id,
|
||||||
|
language: document.documentMeta?.language,
|
||||||
});
|
});
|
||||||
|
|
||||||
|
const base64Pdf = pdfBuffer.toString('base64');
|
||||||
|
const filename = `${document.title.replace(/\.pdf$/, '')}_certificate.pdf`;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
url: `${NEXT_PUBLIC_WEBAPP_URL()}/__htmltopdf/certificate?d=${encrypted}`,
|
pdfData: base64Pdf,
|
||||||
|
filename,
|
||||||
|
contentType: 'application/pdf',
|
||||||
};
|
};
|
||||||
|
} catch (error) {
|
||||||
|
ctx.logger.error({
|
||||||
|
error,
|
||||||
|
message: 'Failed to generate certificate PDF',
|
||||||
|
documentId,
|
||||||
|
});
|
||||||
|
|
||||||
|
throw new AppError(AppErrorCode.UNKNOWN_ERROR, {
|
||||||
|
message: 'Failed to generate certificate PDF',
|
||||||
|
});
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
@ -5,7 +5,9 @@ export const ZDownloadDocumentCertificateRequestSchema = z.object({
|
|||||||
});
|
});
|
||||||
|
|
||||||
export const ZDownloadDocumentCertificateResponseSchema = z.object({
|
export const ZDownloadDocumentCertificateResponseSchema = z.object({
|
||||||
url: z.string(),
|
pdfData: z.string(),
|
||||||
|
filename: z.string(),
|
||||||
|
contentType: z.string(),
|
||||||
});
|
});
|
||||||
|
|
||||||
export type TDownloadDocumentCertificateRequest = z.infer<
|
export type TDownloadDocumentCertificateRequest = z.infer<
|
||||||
|
|||||||
Reference in New Issue
Block a user