Skip to content

Commit bd60f91

Browse files
authored
Merge pull request #383 from OpenSignLabs/api-v1-beta
2 parents cb1ebc6 + b58c3fd commit bd60f91

File tree

3 files changed

+245
-92
lines changed

3 files changed

+245
-92
lines changed

apps/OpenSignServer/Utils.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,24 @@ export const color = [
1717
'#66ccff',
1818
'#ffffcc',
1919
];
20+
21+
export function replaceMailVaribles(subject, body, variables) {
22+
let replacedSubject = subject;
23+
let replacedBody = body;
24+
25+
for (const variable in variables) {
26+
const regex = new RegExp(`{{${variable}}}`, 'g');
27+
if (subject) {
28+
replacedSubject = replacedSubject.replace(regex, variables[variable]);
29+
}
30+
if (body) {
31+
replacedBody = replacedBody.replace(regex, variables[variable]);
32+
}
33+
}
34+
35+
const result = {
36+
subject: replacedSubject,
37+
body: replacedBody,
38+
};
39+
return result;
40+
}

apps/OpenSignServer/cloud/customRoute/v1/routes/CreateDocumentWithTemplate.js

Lines changed: 141 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,59 @@
11
import axios from 'axios';
2-
import { customAPIurl } from '../../../../Utils.js';
2+
import { customAPIurl, replaceMailVaribles } from '../../../../Utils.js';
33

4+
// `sendDoctoWebhook` is used to send res data of document on webhook
5+
async function sendDoctoWebhook(doc, WebhookUrl, userId) {
6+
if (WebhookUrl) {
7+
const params = {
8+
event: 'created',
9+
...doc,
10+
};
11+
await axios
12+
.post(WebhookUrl, params, {
13+
headers: { 'Content-Type': 'application/json' },
14+
})
15+
.then(res => {
16+
try {
17+
// console.log('res ', res);
18+
const webhook = new Parse.Object('contracts_Webhook');
19+
webhook.set('Log', res?.status);
20+
webhook.set('UserId', {
21+
__type: 'Pointer',
22+
className: '_User',
23+
objectId: userId,
24+
});
25+
webhook.save(null, { useMasterKey: true });
26+
} catch (err) {
27+
console.log('err save in contracts_Webhook', err);
28+
}
29+
})
30+
.catch(err => {
31+
console.log('Err send data to webhook', err);
32+
try {
33+
const webhook = new Parse.Object('contracts_Webhook');
34+
webhook.set('Log', err?.status);
35+
webhook.set('UserId', {
36+
__type: 'Pointer',
37+
className: '_User',
38+
objectId: userId,
39+
});
40+
webhook.save(null, { useMasterKey: true });
41+
} catch (err) {
42+
console.log('err save in contracts_Webhook', err);
43+
}
44+
});
45+
// console.log('res ', res.data);
46+
}
47+
}
448
export default async function createDocumentWithTemplate(request, response) {
549
const signers = request.body.signers;
650
const folderId = request.body.folderId;
751
const templateId = request.params.template_id;
852
const protocol = customAPIurl();
953
const baseUrl = new URL(process.env.SERVER_URL);
10-
const send_email = request.body.send_email || true;
11-
54+
const send_email = request.body.send_email;
55+
const email_subject = request.body.email_subject;
56+
const email_body = request.body.email_body;
1257
try {
1358
const reqToken = request.headers['x-api-token'];
1459
if (!reqToken) {
@@ -142,34 +187,32 @@ export default async function createDocumentWithTemplate(request, response) {
142187
const serverUrl = process.env.SERVER_URL;
143188
const newServer = serverUrl.replaceAll('/', '%2F');
144189
const serverParams = `${newServer}%2F&${process.env.APP_ID}&contracts`;
190+
if (send_email === false) {
191+
console.log("don't send mail");
192+
} else {
193+
for (let i = 0; i < contact.length; i++) {
194+
try {
195+
const imgPng = 'https://qikinnovation.ams3.digitaloceanspaces.com/logo.png';
196+
let url = `${process.env.SERVER_URL}/functions/sendmailv3/`;
197+
const headers = {
198+
'Content-Type': 'application/json',
199+
'X-Parse-Application-Id': process.env.APP_ID,
200+
'X-Parse-Master-Key': process.env.MASTER_KEY,
201+
};
145202

146-
for (let i = 0; i < contact.length; i++) {
147-
try {
148-
const imgPng = 'https://qikinnovation.ams3.digitaloceanspaces.com/logo.png';
149-
let url = `${process.env.SERVER_URL}/functions/sendmailv3/`;
150-
const headers = {
151-
'Content-Type': 'application/json',
152-
'X-Parse-Application-Id': process.env.APP_ID,
153-
'X-Parse-Master-Key': process.env.MASTER_KEY,
154-
};
155-
156-
const objectId = contact[i].contactPtr.objectId;
203+
const objectId = contact[i].contactPtr.objectId;
157204

158-
const hostUrl = baseUrl.origin + '/loadmf/signmicroapp';
159-
let signPdf = `${hostUrl}/login/${res.id}/${contact[i].email}/${objectId}/${serverParams}`;
160-
const openSignUrl = 'https://www.opensignlabs.com/contact-us';
161-
const orgName = template.ExtUserPtr.Company ? template.ExtUserPtr.Company : '';
162-
const themeBGcolor = '#47a3ad';
163-
let params = {
164-
recipient: contact[i].email,
165-
subject: `${template.ExtUserPtr.Name} has requested you to sign ${template.ExtUserPtr.Name}`,
166-
from: sender,
167-
html:
168-
"<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: white;padding-bottom: 20px;'> <div style='padding:10px 10px 0 10px'><img src=" +
205+
const hostUrl = baseUrl.origin + '/loadmf/signmicroapp';
206+
let signPdf = `${hostUrl}/login/${res.id}/${contact[i].email}/${objectId}/${serverParams}`;
207+
const openSignUrl = 'https://www.opensignlabs.com/contact-us';
208+
const orgName = template.ExtUserPtr.Company ? template.ExtUserPtr.Company : '';
209+
const themeBGcolor = '#47a3ad';
210+
const email_html =
211+
"<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: white;padding-bottom: 20px;'> <div style='padding:10px 10px 0 10px'><img src='" +
169212
imgPng +
170-
" height='50' style='padding: 20px,width:170px,height:40px' /></div> <div style=' padding: 2px;font-family: system-ui;background-color:" +
213+
"' height='50' style='padding:20px; width:170px; height:40px;' /></div><div style='padding: 2px;font-family: system-ui;background-color:" +
171214
themeBGcolor +
172-
";'><p style='font-size: 20px;font-weight: 400;color: white;padding-left: 20px;' > Digital Signature Request</p></div><div><p style='padding: 20px;font-family: system-ui;font-size: 14px; margin-bottom: 10px;'> " +
215+
";'><p style='font-size: 20px;font-weight: 400;color: white;padding-left: 20px;' > Digital Signature Request</p></div><div><p style='padding: 20px;font-family: system-ui;font-size: 14px; margin-bottom: 10px;'> " +
173216
template.ExtUserPtr.Name +
174217
' has requested you to review and sign <strong> ' +
175218
template.Name +
@@ -181,33 +224,83 @@ export default async function createDocumentWithTemplate(request, response) {
181224
localExpireDate +
182225
"</td></tr><tr> <td></td> <td> </td></tr></table> </div> <div style='margin-left:70px'><a href=" +
183226
signPdf +
184-
"> <button style='padding: 12px 12px 12px 12px;background-color: #d46b0f;color: white; border: 0px;box-shadow: rgba(0, 0, 0, 0.05) 0px 6px 24px 0px,rgba(0, 0, 0, 0.08) 0px 0px 0px 1px;font-weight:bold;margin-top:30px'>Sign here</button></a> </div> <div style='display: flex; justify-content: center;margin-top: 10px;'> </div></div></div><div><p> This is an automated email from OpenSign™. For any queries regarding this email, please contact the sender " +
227+
"> <button style='padding: 12px 12px 12px 12px;background-color: #d46b0f;color: white; border: 0px;box-shadow: rgba(0, 0, 0, 0.05) 0px 6px 24px 0px,rgba(0, 0, 0, 0.08) 0px 0px 0px 1px;font-weight:bold;margin-top:30px;'>Sign here</button></a> </div> <div style='display: flex; justify-content: center;margin-top: 10px;'> </div></div></div><div><p> This is an automated email from OpenSign™. For any queries regarding this email, please contact the sender " +
185228
sender +
186-
' directly.If you think this email is inappropriate or spam, you may file a complaint with OpenSign™ <a href= ' +
229+
' directly.If you think this email is inappropriate or spam, you may file a complaint with OpenSign™ <a href=' +
187230
openSignUrl +
188-
' target=_blank>here</a>.</p> </div></div></body> </html>',
189-
};
190-
sendMail = await axios.post(url, params, { headers: headers });
191-
} catch (error) {
192-
console.log('error', error);
231+
' target=_blank>here</a>.</p> </div></div></body> </html>';
232+
233+
let replaceVar;
234+
const variables = {
235+
document_title: template.Name,
236+
sender_name: template.ExtUserPtr.Name,
237+
sender_mail: template.ExtUserPtr.Email,
238+
sender_phone: template.ExtUserPtr.Phone,
239+
receiver_name: contact[i].name,
240+
receiver_email: contact[i].email,
241+
receiver_phone: contact[i].phone,
242+
expiry_date: localExpireDate,
243+
company_name: orgName,
244+
signing_url: signPdf,
245+
};
246+
if (email_subject && email_body) {
247+
replaceVar = replaceMailVaribles(email_subject, email_body, variables);
248+
} else if (email_subject) {
249+
replaceVar = replaceMailVaribles(email_subject, '', variables);
250+
replaceVar = { subject: replaceVar.subject, body: email_html };
251+
} else if (email_body) {
252+
replaceVar = replaceMailVaribles(
253+
`${template.ExtUserPtr.Name} has requested you to sign ${template.Name}`,
254+
email_body,
255+
variables
256+
);
257+
} else {
258+
replaceVar = {
259+
subject: `${template.ExtUserPtr.Name} has requested you to sign ${template.Name}`,
260+
body: email_html,
261+
};
262+
}
263+
264+
const subject = replaceVar.subject;
265+
const html = replaceVar.body;
266+
let params = {
267+
recipient: contact[i].email,
268+
subject: subject,
269+
from: sender,
270+
html: html,
271+
};
272+
sendMail = await axios.post(url, params, { headers: headers });
273+
} catch (error) {
274+
console.log('error', error);
275+
}
193276
}
194277
}
195-
196-
if (sendMail.data.result.status === 'success') {
197-
const user = contact.find(x => x.email === template.ExtUserPtr.Email);
198-
if (user && user.email) {
199-
return response.json({
200-
objectId: res.id,
201-
url: `${baseUrl.origin}/loadmf/signmicroapp/login/${res.id}/${user.email}/${user.contactPtr.objectId}/${serverParams}`,
202-
message: 'Document sent successfully!',
203-
});
204-
} else {
205-
return response.json({
206-
objectId: res.id,
207-
message: 'Document sent successfully!',
208-
});
278+
// if (sendMail.data.result.status === 'success') {
279+
try {
280+
const doc = {
281+
objectId: res.id,
282+
file: template?.URL,
283+
name: template?.Name,
284+
note: template?.Note || '',
285+
description: template?.Description || '',
286+
signers: contact?.map(x => ({ name: x.name, email: x.email, phone: x.phone })),
287+
createdAt: res.createdAt,
288+
};
289+
if (template.ExtUserPtr && template.ExtUserPtr?.Webhook) {
290+
sendDoctoWebhook(doc, template.ExtUserPtr?.Webhook, userPtr?.id);
209291
}
292+
} catch (err) {
293+
console.log('Err', err);
210294
}
295+
return response.json({
296+
objectId: res.id,
297+
signurl: contact.map(x => ({
298+
email: x.email,
299+
url: `${baseUrl.origin}/loadmf/signmicroapp/login/${res.id}/${x.email}/${x.contactPtr.objectId}/${serverParams}`,
300+
})),
301+
message: 'Document sent successfully!',
302+
});
303+
// }
211304
} else {
212305
return response.status(400).json({ error: 'Please provide signers properly!' });
213306
}

0 commit comments

Comments
 (0)