mirror of
https://github.com/OpenSignLabs/OpenSign.git
synced 2026-08-17 21:25:54 +02:00
303 lines
12 KiB
JavaScript
303 lines
12 KiB
JavaScript
import SignPDF from './SignPDF.min.cjs';
|
|
import fs from 'node:fs';
|
|
import axios from 'axios';
|
|
import { plainAddPlaceholder } from 'node-signpdf/dist/helpers/index.js';
|
|
const serverUrl = process.env.SERVER_URL,
|
|
APPID = process.env.APP_ID,
|
|
masterKEY = process.env.MASTER_KEY;
|
|
async function uploadFile(e, a) {
|
|
try {
|
|
var t = fs.readFileSync(a),
|
|
s = new Parse.File(e, [...t]),
|
|
r = (await s.save({ useMasterKey: !0 }), s.url());
|
|
return { imageUrl: r };
|
|
} catch (e) {
|
|
console.log('Err ', e), fs.unlinkSync(a);
|
|
}
|
|
}
|
|
async function updateDoc(t, s, r, i, n, o) {
|
|
try {
|
|
var d = {
|
|
UserPtr: { __type: 'Pointer', className: o, objectId: r },
|
|
SignedUrl: s,
|
|
Activity: 'Signed',
|
|
ipAddress: i,
|
|
};
|
|
let e;
|
|
var l = (e = n.AuditTrail && 0 < n.AuditTrail.length ? [...n.AuditTrail, d] : [d]).filter(
|
|
e => 'Signed' === e.Activity
|
|
);
|
|
let a = !1;
|
|
!((n.Signers && 0 < n.Signers.length && l.length !== n.Signers.length) || !(a = !0));
|
|
var c = { SignedUrl: s, AuditTrail: e, IsCompleted: a };
|
|
await axios.put(serverUrl + '/classes/contracts_Document/' + t, c, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-Parse-Application-Id': APPID,
|
|
'X-Parse-Master-Key': masterKEY,
|
|
},
|
|
});
|
|
return { isCompleted: a, message: 'success' };
|
|
} catch (e) {
|
|
return console.log('update doc err ', e), 'err';
|
|
}
|
|
}
|
|
async function sendMail(e) {
|
|
var a = e.url,
|
|
t = e.sender,
|
|
s = e.pdfName,
|
|
a = {
|
|
url: a,
|
|
from: 'OpenSign™',
|
|
recipient: e.receiver,
|
|
subject: 'You have signed the doc - ' + s,
|
|
pdfName: s,
|
|
html:
|
|
"<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /></head><body> <div style='background-color:#f5f5f5;padding:20px'> <div style='box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 12px;background-color:white;'> <div><img src=https://qikinnovation.ams3.digitaloceanspaces.com/logo.png height='50' style='padding:20px'/> </div><div style='padding:2px;font-family:system-ui; background-color: #47a3ad;'> <p style='font-size:20px;font-weight:400;color:white;padding-left:20px',> Document Copy</p></div><div><p style='padding:20px;font-family:system-ui;font-size:14px'>A copy of the document " +
|
|
s +
|
|
' Standard is attached to this email. Kindly download the document from the attachment.</p></div> </div><div><p>This is an automated email from Open Sign. For any queries regarding this email, please contact the sender ' +
|
|
t.Mail +
|
|
' directly. If you think this email is inappropriate or spam, you may file a complaint with Open Sign <a href=www.opensignlabs.com target=_blank>here</a>.</p></div></div></body></html>',
|
|
};
|
|
await axios.post(serverUrl + '/functions/sendmailv3', a, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-Parse-Application-Id': APPID,
|
|
'X-Parse-Master-Key': masterKEY,
|
|
},
|
|
});
|
|
}
|
|
async function sendCompletedMail(e) {
|
|
var a = e.url,
|
|
t = e.sender,
|
|
s = e.pdfName,
|
|
a = {
|
|
url: a,
|
|
from: 'OpenSign™',
|
|
recipient: e.receiver,
|
|
subject: `Document ${s} has been signed by all parties`,
|
|
pdfName: s,
|
|
html:
|
|
"<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /></head><body> <div style='background-color:#f5f5f5;padding:20px'> <div style='box-shadow: rgba(0, 0, 0, 0.1) 0px 4px 12px;background-color:white;'> <div><img src=https://qikinnovation.ams3.digitaloceanspaces.com/logo.png height='50' style='padding:20px'/> </div><div style='padding:2px;font-family:system-ui; background-color: #47a3ad;'> <p style='font-size:20px;font-weight:400;color:white;padding-left:20px',> Document sign successfully</p></div><div><p style='padding:20px;font-family:system-ui;font-size:14px'>All parties have successfully signed the document" +
|
|
s +
|
|
'. Kindly download the document from the attachment.</p></div> </div><div><p>This is an automated email from Open Sign. For any queries regarding this email, please contact the sender ' +
|
|
t.Mail +
|
|
' directly. If you think this email is inappropriate or spam, you may file a complaint with Open Sign <a href=www.opensignlabs.com target=_blank>here</a>.</p></div></div></body></html>',
|
|
};
|
|
await axios.post(serverUrl + '/functions/sendmailv3', a, {
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-Parse-Application-Id': APPID,
|
|
'X-Parse-Master-Key': masterKEY,
|
|
},
|
|
});
|
|
}
|
|
async function sendDoctoWebhook(t, e, a, s) {
|
|
let r = [];
|
|
(r = s
|
|
? { name: s?.Name, email: s?.Email, phone: s?.Phone }
|
|
: t?.data?.Signers?.map(e => ({ name: e.Name, email: e.Email, phone: e.Phone })) || [
|
|
{
|
|
name: t?.data?.ExtUserPtr?.Name,
|
|
email: t?.data?.ExtUserPtr?.Email,
|
|
phone: t?.data?.ExtUserPtr?.Phone,
|
|
},
|
|
]),
|
|
t.data.ExtUserPtr?.Webhook &&
|
|
((s =
|
|
'signed' === a
|
|
? { signer: r, signedAt: new Date() }
|
|
: { signers: r, completedAt: new Date() }),
|
|
(a = {
|
|
event: a,
|
|
objectId: t?.data?.objectId,
|
|
file: e || '',
|
|
name: t?.data?.Name,
|
|
note: t?.data?.Note || '',
|
|
description: t?.data?.Description || '',
|
|
...s,
|
|
createdAt: t?.data?.createdAt,
|
|
}),
|
|
await axios
|
|
.post(t?.data?.ExtUserPtr?.Webhook, a, { headers: { 'Content-Type': 'application/json' } })
|
|
.then(e => {
|
|
try {
|
|
var a = new Parse.Object('contracts_Webhook');
|
|
a.set('Log', e?.status),
|
|
a.set('UserId', {
|
|
__type: 'Pointer',
|
|
className: '_User',
|
|
objectId: t.data.ExtUserPtr.UserId.objectId,
|
|
}),
|
|
a.save(null, { useMasterKey: !0 });
|
|
} catch (e) {
|
|
console.log('err save in contracts_Webhook', e);
|
|
}
|
|
})
|
|
.catch(e => {
|
|
console.log('Err send data to webhook', e);
|
|
try {
|
|
var a = new Parse.Object('contracts_Webhook');
|
|
a.set('Log', e?.status),
|
|
a.set('UserId', {
|
|
__type: 'Pointer',
|
|
className: '_User',
|
|
objectId: t.data.ExtUserPtr.UserId.objectId,
|
|
}),
|
|
a.save(null, { useMasterKey: !0 });
|
|
} catch (e) {
|
|
console.log('err save in contracts_Webhook', e);
|
|
}
|
|
}));
|
|
}
|
|
async function PDF(i, n) {
|
|
try {
|
|
i.params.sign;
|
|
var e = i.params.docId,
|
|
a = i.params.userId,
|
|
o = await axios.get(
|
|
serverUrl + '/classes/contracts_Document/' + e + '?include=ExtUserPtr,Signers',
|
|
{
|
|
headers: {
|
|
'Content-Type': 'application/json',
|
|
'X-Parse-Application-Id': APPID,
|
|
'X-Parse-Master-Key': masterKEY,
|
|
},
|
|
}
|
|
),
|
|
t = await axios.get(serverUrl + '/users/me', {
|
|
headers: {
|
|
'X-Parse-Application-Id': APPID,
|
|
'X-Parse-Session-Token': i.headers.sessiontoken,
|
|
},
|
|
});
|
|
if (!t.data || !t.data.objectId) return { status: 'error', message: 'this user not allowed!' };
|
|
{
|
|
var d,
|
|
l,
|
|
c,
|
|
p = JSON.stringify({ objectId: a });
|
|
let s, r;
|
|
r = a
|
|
? (d = await axios.get(serverUrl + '/classes/contracts_Contactbook?where=' + p, {
|
|
headers: {
|
|
'X-Parse-Application-Id': APPID,
|
|
'X-Parse-Session-Token': i.headers.sessiontoken,
|
|
},
|
|
})).data && 0 < d.data.results.length
|
|
? ((s = d), 'contracts_Contactbook')
|
|
: ((s = await axios.get(serverUrl + '/classes/contracts_Users?where=' + p, {
|
|
headers: { 'X-Parse-Application-Id': APPID, 'X-Parse-Master-Key': masterKEY },
|
|
})),
|
|
'contracts_Users')
|
|
: ((l = JSON.stringify({
|
|
UserId: { __type: 'Pointer', className: '_User', objectId: t.data.objectId },
|
|
})),
|
|
(c = await axios.get(serverUrl + '/classes/contracts_Users?where=' + l, {
|
|
headers: { 'X-Parse-Application-Id': APPID, 'X-Parse-Master-Key': masterKEY },
|
|
})).data && 0 < c.data.results.length
|
|
? ((s = c), 'contracts_Users')
|
|
: ((s = await axios.get(serverUrl + '/classes/contracts_Contactbook?where=' + l, {
|
|
headers: {
|
|
'X-Parse-Application-Id': APPID,
|
|
'X-Parse-Session-Token': i.headers.sessiontoken,
|
|
},
|
|
})),
|
|
'contracts_Contactbook'));
|
|
var m = s.data.results[0].Name,
|
|
g = s.data.results[0].Email;
|
|
if (!i.params.pdfFile) return { status: 'error', message: 'pdf file not present!' };
|
|
{
|
|
let e = Buffer.from(i.params.pdfFile, 'base64');
|
|
var h = process.env.PFX_BASE64,
|
|
u = Buffer.from(h, 'base64'),
|
|
f = {
|
|
UserPtr: { __type: 'Pointer', className: r, objectId: s.data.results[0].objectId },
|
|
SignedUrl: '',
|
|
Activity: 'Signed',
|
|
ipAddress: i.headers['x-real-ip'],
|
|
};
|
|
let a;
|
|
var y = (a =
|
|
o.data.AuditTrail && 0 < o.data.AuditTrail.length
|
|
? [...o.data.AuditTrail, f]
|
|
: [f]).filter(e => 'Signed' === e.Activity);
|
|
let t = !1;
|
|
!(
|
|
(o.data.Signers && 0 < o.data.Signers.length && y.length !== o.data.Signers.length) ||
|
|
!(t = !0)
|
|
);
|
|
var v,
|
|
P,
|
|
b = `exported_file_${Math.floor(5e3 * Math.random())}.pdf`,
|
|
x = './exports/' + b,
|
|
U =
|
|
(t
|
|
? ((v = o.data.Signers?.map(e => e.Name + ' <' + e.Email + '>')),
|
|
(e =
|
|
v && 0 < v.length
|
|
? plainAddPlaceholder({
|
|
pdfBuffer: e,
|
|
reason: 'Digitally signed by Open sign for ' + v?.join(', '),
|
|
location: 'location',
|
|
signatureLength: 1e4,
|
|
})
|
|
: plainAddPlaceholder({
|
|
pdfBuffer: e,
|
|
reason: 'Digitally signed by Open sign for ' + m + ' <' + g + '>',
|
|
location: 'location',
|
|
signatureLength: 1e4,
|
|
})),
|
|
(P = await new SignPDF(e, u).signPDF()),
|
|
fs.writeFileSync(x, P))
|
|
: fs.writeFileSync(x, e),
|
|
await uploadFile(b, x));
|
|
if (U && U.imageUrl) {
|
|
const n = await updateDoc(
|
|
i.params.docId,
|
|
U.imageUrl,
|
|
s.data.results[0].objectId,
|
|
i.headers['x-real-ip'],
|
|
o.data,
|
|
r
|
|
);
|
|
var I,
|
|
w = {
|
|
url: U.imageUrl,
|
|
sender: { Mail: o.data.ExtUserPtr.Email, Name: o.data.ExtUserPtr.Name },
|
|
pdfName: o.data.Name,
|
|
receiver: g,
|
|
};
|
|
return (
|
|
o.data.IsSendMail && sendMail(w),
|
|
sendDoctoWebhook(o, U.imageUrl, 'signed', s?.data.results?.[0]),
|
|
n &&
|
|
n.isCompleted &&
|
|
((I = {
|
|
url: U.imageUrl,
|
|
sender: { Mail: o.data.ExtUserPtr.Email, Name: 'OpenSign™' },
|
|
pdfName: o.data.Name,
|
|
receiver: o.data.ExtUserPtr.Email,
|
|
}),
|
|
o.data.IsSendMail && sendCompletedMail(I),
|
|
sendDoctoWebhook(o, U.imageUrl, 'completed')),
|
|
fs.unlinkSync(x),
|
|
console.log('New Signed PDF created called: ' + x),
|
|
'success' === n.message
|
|
? { status: 'success', data: U.imageUrl }
|
|
: { status: 'error', message: 'please provide required parameters!' }
|
|
);
|
|
}
|
|
}
|
|
}
|
|
} catch (e) {
|
|
return (
|
|
console.log('Err ', e),
|
|
'ERR_BAD_REQUEST' === e.code
|
|
? { status: 'error', message: 'Invalid session token!' }
|
|
: { status: 'error', message: 'Encrypted files are currently not supported!' }
|
|
);
|
|
}
|
|
}
|
|
export default PDF;
|