mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 00:03:33 +10:00
🚸 🚚 rename signDocument to addDigitalSignature, sign only when signatures exist
This commit is contained in:
@ -7,7 +7,7 @@ import prisma from "@documenso/prisma";
|
|||||||
import { NextApiRequest, NextApiResponse } from "next";
|
import { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { Document as PrismaDocument } from "@prisma/client";
|
import { Document as PrismaDocument } from "@prisma/client";
|
||||||
import { getDocument } from "@documenso/lib/query";
|
import { getDocument } from "@documenso/lib/query";
|
||||||
import { signDocument } from "@documenso/signing/signDocument";
|
import { addDigitalSignature } from "@documenso/signing/addDigitalSignature";
|
||||||
|
|
||||||
async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const user = await getUserFromToken(req, res);
|
const user = await getUserFromToken(req, res);
|
||||||
@ -25,9 +25,20 @@ async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
if (!document)
|
if (!document)
|
||||||
res.status(404).end(`No document with id ${documentId} found.`);
|
res.status(404).end(`No document with id ${documentId} found.`);
|
||||||
|
|
||||||
const signedDocumentAsBase64 = await signDocument(
|
const signaturesCount = await prisma.signature.count({
|
||||||
document.document.toString()
|
where: {
|
||||||
);
|
Field: {
|
||||||
|
documentId: document.id,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
let signedDocumentAsBase64 = document.document;
|
||||||
|
|
||||||
|
// No need to add a signature, if no one signed yet.
|
||||||
|
if (signaturesCount > 0) {
|
||||||
|
signedDocumentAsBase64 = await addDigitalSignature(document.document);
|
||||||
|
}
|
||||||
|
|
||||||
const buffer: Buffer = Buffer.from(signedDocumentAsBase64, "base64");
|
const buffer: Buffer = Buffer.from(signedDocumentAsBase64, "base64");
|
||||||
res.setHeader("Content-Type", "application/pdf");
|
res.setHeader("Content-Type", "application/pdf");
|
||||||
|
|||||||
@ -7,12 +7,14 @@ import prisma from "@documenso/prisma";
|
|||||||
import { NextApiRequest, NextApiResponse } from "next";
|
import { NextApiRequest, NextApiResponse } from "next";
|
||||||
import { Document as PrismaDocument } from "@prisma/client";
|
import { Document as PrismaDocument } from "@prisma/client";
|
||||||
import { getDocument } from "@documenso/lib/query";
|
import { getDocument } from "@documenso/lib/query";
|
||||||
import { signDocument } from "@documenso/signing/signDocument";
|
import { addDigitalSignature } from "@documenso/signing/addDigitalSignature";
|
||||||
|
|
||||||
async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
async function getHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const documentId = req.query.id || 1;
|
const documentId = req.query.id || 1;
|
||||||
const document: PrismaDocument = await getDocument(+documentId, req, res);
|
const document: PrismaDocument = await getDocument(+documentId, req, res);
|
||||||
const signedDocumentAsBase64 = await signDocument(document.document.toString());
|
const signedDocumentAsBase64 = await addDigitalSignature(
|
||||||
|
document.document.toString()
|
||||||
|
);
|
||||||
const buffer: Buffer = Buffer.from(signedDocumentAsBase64, "base64");
|
const buffer: Buffer = Buffer.from(signedDocumentAsBase64, "base64");
|
||||||
res.setHeader("Content-Type", "application/pdf");
|
res.setHeader("Content-Type", "application/pdf");
|
||||||
res.setHeader(
|
res.setHeader(
|
||||||
|
|||||||
@ -8,7 +8,9 @@ import {
|
|||||||
PDFString,
|
PDFString,
|
||||||
} from "pdf-lib";
|
} from "pdf-lib";
|
||||||
|
|
||||||
export const signDocument = async (documentAsBase64: string): Promise<any> => {
|
export const addDigitalSignature = async (
|
||||||
|
documentAsBase64: string
|
||||||
|
): Promise<any> => {
|
||||||
// Custom code to add Byterange to PDF
|
// Custom code to add Byterange to PDF
|
||||||
const PDFArrayCustom = require("./PDFArrayCustom");
|
const PDFArrayCustom = require("./PDFArrayCustom");
|
||||||
|
|
||||||
Reference in New Issue
Block a user