mirror of
https://github.com/documenso/documenso.git
synced 2025-11-15 01:01:49 +10:00
Merge branch 'main' of https://github.com/ElTimuro/documenso into doc-107
This commit is contained in:
@ -13,12 +13,13 @@ import { insertImageInPDF, insertTextInPDF } from "@documenso/pdf";
|
|||||||
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
||||||
const existingUser = await getUserFromToken(req, res);
|
const existingUser = await getUserFromToken(req, res);
|
||||||
const { token: recipientToken } = req.query;
|
const { token: recipientToken } = req.query;
|
||||||
const { signatures: signatures }: { signatures: any[] } = req.body;
|
const { signatures: signaturesFromBody }: { signatures: any[] } = req.body;
|
||||||
|
|
||||||
if (!recipientToken) {
|
if (!recipientToken) {
|
||||||
return res.status(401).send("Missing recipient token.");
|
return res.status(401).send("Missing recipient token.");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The recipient who received the signing request
|
||||||
const recipient = await prisma.recipient.findFirstOrThrow({
|
const recipient = await prisma.recipient.findFirstOrThrow({
|
||||||
where: { token: recipientToken?.toString() },
|
where: { token: recipientToken?.toString() },
|
||||||
});
|
});
|
||||||
@ -34,28 +35,24 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (!document) res.status(404).end(`No document found.`);
|
if (!document) res.status(404).end(`No document found.`);
|
||||||
// todo insert if not exits
|
|
||||||
signatures.forEach(async (signature) => {
|
|
||||||
if (!signature.signatureImage && !signature.typedSignature)
|
|
||||||
throw new Error("Cant't save invalid signature.");
|
|
||||||
|
|
||||||
await prisma.signature.upsert({
|
// todo rename .document to documentImageAsBase64 or sth. like that
|
||||||
where: {
|
let documentWithSignatureImages = document.document;
|
||||||
fieldId: signature.fieldId,
|
for (const signature of signaturesFromBody) {
|
||||||
},
|
if (!signature.signatureImage && !signature.typedSignature) {
|
||||||
update: {},
|
documentWithSignatureImages = document.document;
|
||||||
create: {
|
throw new Error("Cant't save invalid signature.");
|
||||||
recipientId: recipient.id,
|
}
|
||||||
fieldId: signature.fieldId,
|
|
||||||
signatureImageAsBase64: signature.signatureImage
|
await saveSignature(signature);
|
||||||
? signature.signatureImage
|
|
||||||
: null,
|
const signedField = await prisma.field.findFirstOrThrow({
|
||||||
typedSignature: signature.typedSignature
|
where: { id: signature.fieldId },
|
||||||
? signature.typedSignature
|
include: { Signature: true },
|
||||||
: null,
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
});
|
|
||||||
|
await insertSignatureInDocument(signedField);
|
||||||
|
}
|
||||||
|
|
||||||
await prisma.recipient.update({
|
await prisma.recipient.update({
|
||||||
where: {
|
where: {
|
||||||
@ -73,63 +70,64 @@ async function postHandler(req: NextApiRequest, res: NextApiResponse) {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
console.log("unsignedRecipients.length.." + unsignedRecipients.length);
|
await prisma.document.update({
|
||||||
if (unsignedRecipients.length === 0) {
|
where: {
|
||||||
// todo if everybody signed insert images and create signature
|
id: recipient.documentId,
|
||||||
const signedFields = await prisma.field.findMany({
|
},
|
||||||
where: { documentId: document.id },
|
data: {
|
||||||
include: { Signature: true },
|
document: documentWithSignatureImages,
|
||||||
});
|
status:
|
||||||
// todo rename .document to documentImageAsBase64 or sth. like that
|
unsignedRecipients.length > 0
|
||||||
let documentWithSignatureImages = document.document;
|
? DocumentStatus.PENDING
|
||||||
let signaturesInserted = 0;
|
: DocumentStatus.COMPLETED,
|
||||||
signedFields.forEach(async (signedField) => {
|
},
|
||||||
if (!signedField.Signature) {
|
});
|
||||||
documentWithSignatureImages = document.document;
|
|
||||||
throw new Error("Invalid Signature in Field");
|
|
||||||
}
|
|
||||||
|
|
||||||
if (signedField.Signature.signatureImageAsBase64) {
|
|
||||||
documentWithSignatureImages = await insertImageInPDF(
|
|
||||||
documentWithSignatureImages,
|
|
||||||
signedField.Signature
|
|
||||||
? signedField.Signature?.signatureImageAsBase64
|
|
||||||
: "",
|
|
||||||
signedField.positionX,
|
|
||||||
signedField.positionY,
|
|
||||||
signedField.page
|
|
||||||
);
|
|
||||||
} else if (signedField.Signature.typedSignature) {
|
|
||||||
console.log("inserting text");
|
|
||||||
documentWithSignatureImages = await insertTextInPDF(
|
|
||||||
documentWithSignatureImages,
|
|
||||||
signedField.Signature.typedSignature,
|
|
||||||
signedField.positionX,
|
|
||||||
signedField.positionY,
|
|
||||||
signedField.page
|
|
||||||
);
|
|
||||||
} else {
|
|
||||||
documentWithSignatureImages = document.document;
|
|
||||||
throw new Error("Invalid signature could not be inserted.");
|
|
||||||
}
|
|
||||||
|
|
||||||
signaturesInserted++;
|
|
||||||
if (signaturesInserted == signedFields.length) {
|
|
||||||
await prisma.document.update({
|
|
||||||
where: {
|
|
||||||
id: recipient.documentId,
|
|
||||||
},
|
|
||||||
data: {
|
|
||||||
status: DocumentStatus.COMPLETED,
|
|
||||||
document: documentWithSignatureImages,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
}
|
|
||||||
// todo send notifications
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return res.status(200).end();
|
return res.status(200).end();
|
||||||
|
|
||||||
|
async function insertSignatureInDocument(signedField: any) {
|
||||||
|
if (signedField?.Signature?.signatureImageAsBase64) {
|
||||||
|
documentWithSignatureImages = await insertImageInPDF(
|
||||||
|
documentWithSignatureImages,
|
||||||
|
signedField.Signature
|
||||||
|
? signedField.Signature?.signatureImageAsBase64
|
||||||
|
: "",
|
||||||
|
signedField.positionX,
|
||||||
|
signedField.positionY,
|
||||||
|
signedField.page
|
||||||
|
);
|
||||||
|
} else if (signedField?.Signature?.typedSignature) {
|
||||||
|
documentWithSignatureImages = await insertTextInPDF(
|
||||||
|
documentWithSignatureImages,
|
||||||
|
signedField.Signature.typedSignature,
|
||||||
|
signedField.positionX,
|
||||||
|
signedField.positionY,
|
||||||
|
signedField.page
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
documentWithSignatureImages = document.document;
|
||||||
|
throw new Error("Invalid signature could not be inserted.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
async function saveSignature(signature: any) {
|
||||||
|
await prisma.signature.upsert({
|
||||||
|
where: {
|
||||||
|
fieldId: signature.fieldId,
|
||||||
|
},
|
||||||
|
update: {},
|
||||||
|
create: {
|
||||||
|
recipientId: recipient.id,
|
||||||
|
fieldId: signature.fieldId,
|
||||||
|
signatureImageAsBase64: signature.signatureImage
|
||||||
|
? signature.signatureImage
|
||||||
|
: null,
|
||||||
|
typedSignature: signature.typedSignature
|
||||||
|
? signature.typedSignature
|
||||||
|
: null,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export default defaultHandler({
|
export default defaultHandler({
|
||||||
|
|||||||
Reference in New Issue
Block a user