Skip to content

Commit 3c0db94

Browse files
feat: send custom completion mail if available
1 parent 4f2b72d commit 3c0db94

File tree

1 file changed

+118
-86
lines changed
  • apps/OpenSignServer/cloud/parsefunction/pdf

1 file changed

+118
-86
lines changed

apps/OpenSignServer/cloud/parsefunction/pdf/PDF.min.js

Lines changed: 118 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import fs from 'node:fs';
33
import axios from 'axios';
44
import { pdflibAddPlaceholder } from './customSignPdf/pdflibplaceholder.min.js';
55
import { PDFDocument } from 'pdf-lib';
6-
import { saveFileUsage } from '../../../Utils.js';
6+
import { replaceMailVaribles, saveFileUsage } from '../../../Utils.js';
77
import GenerateCertificate from './GenerateCertificate.js';
88
const serverUrl = process.env.SERVER_URL,
99
APPID = process.env.APP_ID,
@@ -20,18 +20,18 @@ async function uploadFile(e, a) {
2020
}
2121
async function updateDoc(t, s, r, i, o, n) {
2222
try {
23-
var d = {
23+
var l = {
2424
UserPtr: { __type: 'Pointer', className: n, objectId: r },
2525
SignedUrl: s,
2626
Activity: 'Signed',
2727
ipAddress: i,
2828
};
2929
let e;
30-
var l = (e = o.AuditTrail && 0 < o.AuditTrail.length ? [...o.AuditTrail, d] : [d]).filter(
30+
var d = (e = o.AuditTrail && 0 < o.AuditTrail.length ? [...o.AuditTrail, l] : [l]).filter(
3131
e => 'Signed' === e.Activity
3232
);
3333
let a = !1;
34-
!((o.Signers && 0 < o.Signers.length && l.length !== o.Signers.length) || !(a = !0));
34+
!((o.Signers && 0 < o.Signers.length && d.length !== o.Signers.length) || !(a = !0));
3535
var c = { SignedUrl: s, AuditTrail: e, IsCompleted: a };
3636
await axios.put(serverUrl + '/classes/contracts_Document/' + t, c, {
3737
headers: {
@@ -47,23 +47,62 @@ async function updateDoc(t, s, r, i, o, n) {
4747
}
4848
async function sendCompletedMail(e) {
4949
var a = e.url,
50-
t = e.sender,
51-
s = e.pdfName,
52-
r = e.receiver,
53-
e = {
54-
extUserId: e.extUserId,
55-
url: a,
56-
from: 'OpenSign™',
57-
recipient: r,
58-
subject: `Document ${s} has been signed by all parties`,
59-
pdfName: s,
60-
html:
61-
"<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" +
62-
s +
63-
'. Kindly download the document from the attachment.</p></div> </div><div><p>This is an automated email from OpenSign™. For any queries regarding this email, please contact the sender ' +
64-
t.Mail +
65-
' directly. If you think this email is inappropriate or spam, you may file a complaint with OpenSign™ <a href=www.opensignlabs.com target=_blank>here</a>.</p></div></div></body></html>',
66-
};
50+
t = e.doc,
51+
s = e.doc.ExtUserPtr,
52+
r = t.Name,
53+
i = s.Email;
54+
let o = `Document "${r}" has been signed by all parties`,
55+
n =
56+
"<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 signed successfully</p></div><div><p style='padding:20px;font-family:system-ui;font-size:14px'>All parties have successfully signed the document " +
57+
`<b>"${r}"</b>` +
58+
'. Kindly download the document from the attachment.</p></div> </div><div><p>This is an automated email from OpenSign™. For any queries regarding this email, please contact the sender ' +
59+
s.Email +
60+
' directly. If you think this email is inappropriate or spam, you may file a complaint with OpenSign™ <a href=www.opensignlabs.com target=_blank>here</a>.</p></div></div></body></html>';
61+
if (e?.isCustomMail)
62+
try {
63+
var l,
64+
d,
65+
c,
66+
p,
67+
m,
68+
g = new Parse.Query('partners_Tenant');
69+
g.equalTo('UserId', { __type: 'Pointer', className: '_User', objectId: s.UserId.objectId });
70+
const u = await g.first();
71+
u &&
72+
((l = JSON.parse(JSON.stringify(u)))?.CompletionSubject && (o = l?.CompletionSubject),
73+
l?.CompletionBody && (n = l?.CompletionBody),
74+
(d = t.ExpiryDate.iso),
75+
(c = new Date(d).toLocaleDateString('en-US', {
76+
day: 'numeric',
77+
month: 'long',
78+
year: 'numeric',
79+
})),
80+
(p = {
81+
document_title: r,
82+
sender_name: s.Name,
83+
sender_mail: s.Email,
84+
sender_phone: s.Phone,
85+
receiver_name: s.Name,
86+
receiver_email: s.Email,
87+
receiver_phone: s.Phone,
88+
expiry_date: c,
89+
company_name: s.Company,
90+
}),
91+
(m = replaceMailVaribles(o, n, p)),
92+
(o = m.subject),
93+
(n = m.body));
94+
} catch (e) {
95+
console.log('error in fetch tenant in signpdf', e.message);
96+
}
97+
e = {
98+
extUserId: s.objectId,
99+
url: a,
100+
from: 'OpenSign™',
101+
recipient: i,
102+
subject: o,
103+
pdfName: r,
104+
html: n,
105+
};
67106
await axios.post(serverUrl + '/functions/sendmailv3', e, {
68107
headers: {
69108
'Content-Type': 'application/json',
@@ -134,6 +173,7 @@ async function PDF(o) {
134173
try {
135174
var n = o.params.docId,
136175
e = o.params.userId,
176+
l = o.params.isCustomCompletionMail,
137177
d = await axios.get(
138178
serverUrl + '/classes/contracts_Document/' + n + '?include=ExtUserPtr,Signers',
139179
{
@@ -144,33 +184,33 @@ async function PDF(o) {
144184
},
145185
}
146186
),
147-
l = await axios.get(serverUrl + '/users/me', {
187+
c = await axios.get(serverUrl + '/users/me', {
148188
headers: {
149189
'X-Parse-Application-Id': APPID,
150190
'X-Parse-Session-Token': o.headers.sessiontoken,
151191
},
152192
});
153-
if (!l.data || !l.data.objectId) return { status: 'error', message: 'This user not allowed!' };
193+
if (!c.data || !c.data.objectId) return { status: 'error', message: 'This user not allowed!' };
154194
{
155195
var a,
156196
t,
157197
s,
158-
c = JSON.stringify({ objectId: e });
198+
p = JSON.stringify({ objectId: e });
159199
let r, i;
160200
i = e
161-
? (a = await axios.get(serverUrl + '/classes/contracts_Contactbook?where=' + c, {
201+
? (a = await axios.get(serverUrl + '/classes/contracts_Contactbook?where=' + p, {
162202
headers: {
163203
'X-Parse-Application-Id': APPID,
164204
'X-Parse-Session-Token': o.headers.sessiontoken,
165205
},
166206
})).data && 0 < a.data.results.length
167207
? ((r = a), 'contracts_Contactbook')
168-
: ((r = await axios.get(serverUrl + '/classes/contracts_Users?where=' + c, {
208+
: ((r = await axios.get(serverUrl + '/classes/contracts_Users?where=' + p, {
169209
headers: { 'X-Parse-Application-Id': APPID, 'X-Parse-Master-Key': masterKEY },
170210
})),
171211
'contracts_Users')
172212
: ((t = JSON.stringify({
173-
UserId: { __type: 'Pointer', className: '_User', objectId: l.data.objectId },
213+
UserId: { __type: 'Pointer', className: '_User', objectId: c.data.objectId },
174214
})),
175215
(s = await axios.get(serverUrl + '/classes/contracts_Users?where=' + t, {
176216
headers: { 'X-Parse-Application-Id': APPID, 'X-Parse-Master-Key': masterKEY },
@@ -183,118 +223,110 @@ async function PDF(o) {
183223
},
184224
})),
185225
'contracts_Contactbook'));
186-
var p = r.data.results[0].Name,
187-
m = r.data.results[0].Email;
226+
var m = r.data.results[0].Name,
227+
g = r.data.results[0].Email;
188228
if (!o.params.pdfFile) return { status: 'error', message: 'Pdf file not present!' };
189229
{
190230
let e = Buffer.from(o.params.pdfFile, 'base64');
191-
var g = process.env.PFX_BASE64,
192-
u = Buffer.from(g, 'base64'),
231+
var u = process.env.PFX_BASE64,
232+
h = Buffer.from(u, 'base64'),
193233
f = {
194234
UserPtr: { __type: 'Pointer', className: i, objectId: r.data.results[0].objectId },
195235
SignedUrl: '',
196236
Activity: 'Signed',
197237
ipAddress: o.headers['x-real-ip'],
198238
};
199239
let a;
200-
var h = (a =
240+
var P = (a =
201241
d.data.AuditTrail && 0 < d.data.AuditTrail.length
202242
? [...d.data.AuditTrail, f]
203243
: [f]).filter(e => 'Signed' === e.Activity);
204244
let t = !1;
205245
!(
206-
(d.data.Signers && 0 < d.data.Signers.length && h.length !== d.data.Signers.length) ||
246+
(d.data.Signers && 0 < d.data.Signers.length && P.length !== d.data.Signers.length) ||
207247
!(t = !0)
208248
);
209-
var P,
210-
y,
249+
var y,
250+
b,
211251
v,
212252
U,
213-
b,
214253
I,
215-
w = `exported_file_${Math.floor(5e3 * Math.random())}.pdf`,
216-
A = './exports/' + w;
254+
w,
255+
D = `exported_file_${Math.floor(5e3 * Math.random())}.pdf`,
256+
S = './exports/' + D;
217257
let s = e.length;
218258
s = (
219259
t
220-
? ((P = d.data.Signers?.map(e => e.Name + ' <' + e.Email + '>')),
260+
? ((y = d.data.Signers?.map(e => e.Name + ' <' + e.Email + '>')),
221261
(e =
222-
P && 0 < P.length
223-
? ((y = await PDFDocument.load(e)),
262+
y && 0 < y.length
263+
? ((b = await PDFDocument.load(e)),
224264
pdflibAddPlaceholder({
225-
pdfDoc: y,
226-
reason: 'Digitally signed by OpenSign for ' + P?.join(', '),
265+
pdfDoc: b,
266+
reason: 'Digitally signed by OpenSign for ' + y?.join(', '),
227267
location: 'n/a',
228268
signatureLength: 15e3,
229269
}),
230-
(v = await y.save()),
270+
(v = await b.save()),
231271
Buffer.from(v))
232272
: ((U = await PDFDocument.load(e)),
233273
pdflibAddPlaceholder({
234274
pdfDoc: U,
235-
reason: 'Digitally signed by OpenSign for ' + p + ' <' + m + '>',
275+
reason: 'Digitally signed by OpenSign for ' + m + ' <' + g + '>',
236276
location: 'n/a',
237277
signatureLength: 15e3,
238278
}),
239-
(b = await U.save()),
240-
Buffer.from(b))),
241-
(I = await new SignPDF(e, u).signPDF()),
242-
fs.writeFileSync(A, I),
243-
I)
244-
: (fs.writeFileSync(A, e), e)
279+
(I = await U.save()),
280+
Buffer.from(I))),
281+
(w = await new SignPDF(e, h).signPDF()),
282+
fs.writeFileSync(S, w),
283+
w)
284+
: (fs.writeFileSync(S, e), e)
245285
).length;
246-
var D,
247-
S,
286+
var _,
287+
A,
248288
x,
249289
E,
250-
_,
251290
j,
252291
k,
253292
F,
293+
C,
254294
T,
255-
N,
256-
C = await uploadFile(w, A);
257-
if (C && C.imageUrl)
295+
N = await uploadFile(D, S);
296+
if (N && N.imageUrl)
258297
return (
259-
(D = await updateDoc(
298+
(_ = await updateDoc(
260299
o.params.docId,
261-
C.imageUrl,
300+
N.imageUrl,
262301
r.data.results[0].objectId,
263302
o.headers['x-real-ip'],
264303
d.data,
265304
i
266305
)),
267-
sendDoctoWebhook(d, C.imageUrl, 'signed', r?.data.results?.[0]),
268-
saveFileUsage(s, C.imageUrl, l.data.objectId),
269-
D &&
270-
D.isCompleted &&
271-
((S = {
272-
url: C.imageUrl,
273-
sender: { Mail: d.data.ExtUserPtr.Email, Name: 'OpenSign™' },
274-
pdfName: d.data.Name,
275-
receiver: d.data.ExtUserPtr.Email,
276-
extUserId: d.data.ExtUserPtr.objectId,
277-
}),
278-
(x = { ...d.data, AuditTrail: D.AuditTrail }),
279-
(E = await GenerateCertificate(x)),
280-
(_ = await PDFDocument.load(E)),
306+
sendDoctoWebhook(d, N.imageUrl, 'signed', r?.data.results?.[0]),
307+
saveFileUsage(s, N.imageUrl, c.data.objectId),
308+
_ &&
309+
_.isCompleted &&
310+
((A = { ...d.data, AuditTrail: _.AuditTrail }),
311+
(x = await GenerateCertificate(A)),
312+
(E = await PDFDocument.load(x)),
281313
pdflibAddPlaceholder({
282-
pdfDoc: _,
314+
pdfDoc: E,
283315
reason: 'Digitally signed by OpenSign.',
284316
location: 'n/a',
285317
signatureLength: 15e3,
286318
}),
287-
(j = await _.save()),
319+
(j = await E.save()),
288320
(k = Buffer.from(j)),
289-
(F = await new SignPDF(k, u).signPDF()),
321+
(F = await new SignPDF(k, h).signPDF()),
290322
fs.writeFileSync('./exports/certificate.pdf', F),
291-
(N = {
292-
CertificateUrl: (T = await uploadFile(
323+
(T = {
324+
CertificateUrl: (C = await uploadFile(
293325
'certificate.pdf',
294326
'./exports/certificate.pdf'
295327
)).imageUrl,
296328
}),
297-
await axios.put(serverUrl + '/classes/contracts_Document/' + n, N, {
329+
await axios.put(serverUrl + '/classes/contracts_Document/' + n, T, {
298330
headers: {
299331
'Content-Type': 'application/json',
300332
'X-Parse-Application-Id': APPID,
@@ -303,13 +335,13 @@ async function PDF(o) {
303335
}),
304336
d.data.IsSendMail && !1 === d.data.IsSendMail
305337
? console.log("don't send mail")
306-
: sendCompletedMail(S),
307-
saveFileUsage(k.length, T.imageUrl, l.data.objectId),
308-
sendDoctoWebhook(d, C.imageUrl, 'completed')),
309-
fs.unlinkSync(A),
310-
console.log('New Signed PDF created called: ' + A),
311-
'success' === D.message
312-
? { status: 'success', data: C.imageUrl }
338+
: sendCompletedMail({ url: N.imageUrl, isCustomMail: l, doc: d.data }),
339+
saveFileUsage(k.length, C.imageUrl, c.data.objectId),
340+
sendDoctoWebhook(d, N.imageUrl, 'completed')),
341+
fs.unlinkSync(S),
342+
console.log('New Signed PDF created called: ' + S),
343+
'success' === _.message
344+
? { status: 'success', data: N.imageUrl }
313345
: { status: 'error', message: 'Please provide required parameters!' }
314346
);
315347
}

0 commit comments

Comments
 (0)