Skip to content

Commit 2d7571a

Browse files
fix: change response of createdocumentwithtemplate API for signing document
1 parent fee3d8f commit 2d7571a

File tree

1 file changed

+93
-13
lines changed

1 file changed

+93
-13
lines changed

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

Lines changed: 93 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default async function createDocumentWithTemplate(request, response) {
66
const folderId = request.body.folderId;
77
const templateId = request.params.template_id;
88
const protocol = customAPIurl();
9-
9+
const baseUrl = new URL(process.env.SERVER_URL);
1010
try {
1111
const reqToken = request.headers['x-api-token'];
1212
if (!reqToken) {
@@ -19,8 +19,9 @@ export default async function createDocumentWithTemplate(request, response) {
1919
// Valid Token then proceed request
2020
const userPtr = token.get('userId');
2121

22-
const templateQyuery = new Parse.Query('contracts_Template');
23-
const templateRes = await templateQyuery.get(templateId, { useMasterKey: true });
22+
const templateQuery = new Parse.Query('contracts_Template');
23+
templateQuery.include('ExtUserPtr');
24+
const templateRes = await templateQuery.get(templateId, { useMasterKey: true });
2425
if (templateRes) {
2526
const template = JSON.parse(JSON.stringify(templateRes));
2627
if (template?.Placeholders?.length > 0) {
@@ -30,7 +31,7 @@ export default async function createDocumentWithTemplate(request, response) {
3031
signers.length <= template?.Placeholders?.length;
3132
const placeholder =
3233
signers.length > emptyplaceholder.length ? template.Placeholders : emptyplaceholder;
33-
const updateSigners = placeholder.every(y => signers?.some(x => x.Role === y.Role));
34+
const updateSigners = placeholder.every(y => signers?.some(x => x.role === y.Role));
3435
// console.log('isValid ', isValid);
3536
if (isValid && updateSigners) {
3637
const folderPtr = {
@@ -48,16 +49,16 @@ export default async function createDocumentWithTemplate(request, response) {
4849
object.set('Description', template.Description);
4950
}
5051
let templateSigner = template?.Signers ? template?.Signers : [];
52+
let contact = [];
5153
if (signers && signers.length > 0) {
5254
let parseSigners = [...signers];
5355
let createContactUrl = protocol + '/v1/createcontact';
5456

55-
let contact = [];
5657
for (const obj of parseSigners) {
5758
const body = {
58-
name: obj?.Name || '',
59-
email: obj?.Email || '',
60-
phone: obj?.Phone || '',
59+
name: obj?.name || '',
60+
email: obj?.email || '',
61+
phone: obj?.phone || '',
6162
};
6263
try {
6364
const res = await axios.post(createContactUrl, body, {
@@ -89,7 +90,7 @@ export default async function createDocumentWithTemplate(request, response) {
8990
object.set('Signers', [...templateSigner, ...contactPtrs]);
9091

9192
let updatedPlaceholder = template?.Placeholders?.map(x => {
92-
let matchingSigner = contact.find(y => x.Role && x.Role === y.Role);
93+
let matchingSigner = contact.find(y => x.Role && x.Role === y.role);
9394

9495
if (matchingSigner) {
9596
return {
@@ -109,7 +110,11 @@ export default async function createDocumentWithTemplate(request, response) {
109110
}
110111
object.set('URL', template.URL);
111112
object.set('CreatedBy', template.CreatedBy);
112-
object.set('ExtUserPtr', template.ExtUserPtr);
113+
object.set('ExtUserPtr', {
114+
__type: 'Pointer',
115+
className: 'contracts_Users',
116+
objectId: template.ExtUserPtr.objectId,
117+
});
113118
if (folderId) {
114119
object.set('Folder', folderPtr);
115120
}
@@ -120,10 +125,85 @@ export default async function createDocumentWithTemplate(request, response) {
120125
newACL.setWriteAccess(userPtr.id, true);
121126
object.setACL(newACL);
122127
const res = await object.save(null, { useMasterKey: true });
123-
return response.json({
124-
objectId: res.id,
125-
url: protocol + '/load/signmicroapp/placeholdersign/' + res.id,
128+
129+
const newDate = new Date();
130+
newDate.setDate(newDate.getDate() + 15);
131+
const localExpireDate = newDate.toLocaleDateString('en-US', {
132+
day: 'numeric',
133+
month: 'long',
134+
year: 'numeric',
126135
});
136+
let sender = template.ExtUserPtr.Email;
137+
let sendMail;
138+
const serverUrl = process.env.SERVER_URL;
139+
const newServer = serverUrl.replaceAll('/', '%2F');
140+
const serverParams = `${newServer}%2F&${process.env.APP_ID}&contracts`;
141+
142+
for (let i = 0; i < contact.length; i++) {
143+
try {
144+
const imgPng = 'https://qikinnovation.ams3.digitaloceanspaces.com/logo.png';
145+
let url = `${process.env.SERVER_URL}/functions/sendmailv3/`;
146+
const headers = {
147+
'Content-Type': 'application/json',
148+
'X-Parse-Application-Id': process.env.APP_ID,
149+
'X-Parse-Master-Key': process.env.MASTER_KEY,
150+
};
151+
152+
const objectId = contact[i].contactPtr.objectId;
153+
154+
const hostUrl = baseUrl.origin + '/loadmf/signmicroapp';
155+
let signPdf = `${hostUrl}/login/${res.id}/${contact[i].email}/${objectId}/${serverParams}`;
156+
const openSignUrl = 'https://www.opensignlabs.com/contact-us';
157+
const orgName = template.ExtUserPtr.Company ? template.ExtUserPtr.Company : '';
158+
const themeBGcolor = '#47a3ad';
159+
let params = {
160+
recipient: contact[i].email,
161+
subject: `${template.ExtUserPtr.Name} has requested you to sign ${template.ExtUserPtr.Name}`,
162+
from: sender,
163+
html:
164+
"<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=" +
165+
imgPng +
166+
" height='50' style='padding: 20px,width:170px,height:40px' /></div> <div style=' padding: 2px;font-family: system-ui;background-color:" +
167+
themeBGcolor +
168+
";'><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;'> " +
169+
template.ExtUserPtr.Name +
170+
' has requested you to review and sign <strong> ' +
171+
template.Name +
172+
"</strong>.</p><div style='padding: 5px 0px 5px 25px;display: flex;flex-direction: row;justify-content: space-around;'><table> <tr> <td style='font-weight:bold;font-family:sans-serif;font-size:15px'>Sender</td> <td> </td> <td style='color:#626363;font-weight:bold'>" +
173+
sender +
174+
"</td></tr><tr><td style='font-weight:bold;font-family:sans-serif;font-size:15px'>Organization</td> <td> </td><td style='color:#626363;font-weight:bold'> " +
175+
orgName +
176+
"</td></tr> <tr> <td style='font-weight:bold;font-family:sans-serif;font-size:15px'>Expire on</td><td> </td> <td style='color:#626363;font-weight:bold'>" +
177+
localExpireDate +
178+
"</td></tr><tr> <td></td> <td> </td></tr></table> </div> <div style='margin-left:70px'><a href=" +
179+
signPdf +
180+
"> <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 " +
181+
sender +
182+
' directly.If you think this email is inappropriate or spam, you may file a complaint with OpenSign™ <a href= ' +
183+
openSignUrl +
184+
' target=_blank>here</a>.</p> </div></div></body> </html>',
185+
};
186+
sendMail = await axios.post(url, params, { headers: headers });
187+
} catch (error) {
188+
console.log('error', error);
189+
}
190+
}
191+
192+
if (sendMail.data.result.status === 'success') {
193+
const user = contact.find(x => x.email === template.ExtUserPtr.Email);
194+
if (user && user.email) {
195+
return response.json({
196+
objectId: res.id,
197+
url: `${baseUrl.origin}/loadmf/signmicroapp/login/${res.id}/${user.email}/${user.contactPtr.objectId}/${serverParams}`,
198+
message: 'Document sent successfully!',
199+
});
200+
} else {
201+
return response.json({
202+
objectId: res.id,
203+
message: 'Document sent successfully!',
204+
});
205+
}
206+
}
127207
} else {
128208
return response.status(400).json({ error: 'Please provide signers properly!' });
129209
}

0 commit comments

Comments
 (0)