Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
191 changes: 72 additions & 119 deletions apps/OpenSignServer/cloud/customRoute/autoReminder.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,34 +22,83 @@ function replaceMailVaribles(subject, body, variables) {
return result;
}

export default async function autoReminder(request, response) {
// `sendmail` is used to signing reminder mail to signer
async function sendMail(doc, signer, mailcount) {
const subject = `{{sender_name}} has requested you to sign "{{document_title}}"`;
const body = `<html><head><meta http-equiv='Content-Type' content='text/html; charset=UTF-8' /></head><body><p>Hi {{receiver_name}},</p><br><p>We hope this email finds you well. {{sender_name}}&nbsp;has requested you to review and sign&nbsp;<b>"{{document_title}}"</b>.</p><p>Your signature is crucial to proceed with the next steps as it signifies your agreement and authorization.</p><br><p>{{signing_url}}</p><br><p>If you have any questions or need further clarification regarding the document or the signing process, please contact the sender.</p><br><p>Thanks</p><p> Team OpenSign™</p><br></body> </html>`;
const url = `${process.env.SERVER_URL}/functions/sendmailv3`;
const headers = {
'Content-Type': 'application/json',
'X-Parse-Application-Id': process.env.APP_ID,
};
const limit = request.query.limit || 2000;
const skip = request.query.skip || 0;

const baseUrl = new URL(process.env.PUBLIC_URL);
const encodeBase64 = btoa(`${doc.objectId}/${signer.Email}/${signer.objectId}`);
const expireDate = doc?.ExpiryDate?.iso;
const newDate = new Date(expireDate);
const localExpireDate = newDate.toLocaleDateString('en-US', {
day: 'numeric',
month: 'long',
year: 'numeric',
});
const signPdf = `${baseUrl.origin}/login/${encodeBase64}`;
const variables = {
document_title: doc.Name,
sender_name: doc.ExtUserPtr.Name,
sender_mail: doc.ExtUserPtr.Email,
sender_phone: doc.ExtUserPtr.Phone,
receiver_name: signer.Name,
receiver_email: signer.Email,
receiver_phone: signer.Phone,
expiry_date: localExpireDate,
company_name: doc?.ExtUserPtr?.Company || '',
signing_url: `<a href=${signPdf}>Sign here</a>`,
};
const mail = replaceMailVaribles(subject, body, variables);

let params = {
mailProvider: doc?.ExtUserPtr?.active_mail_adapter,
extUserId: doc?.ExtUserPtr?.objectId,
recipient: signer.Email,
subject: mail.subject,
from: doc?.ExtUserPtr?.Email,
html: mail.body,
};
try {
// The axios request is used to send a signing reminder email.
const res = await axios.post(url, params, { headers: headers });
// console.log('res ', res.data.result);
if (res.data.result.status === 'success') {
mailcount += 1;
return { status: 'success', count: mailcount };
}
} catch (err) {
console.log('err in mail of sendreminder api', err);
return { status: 'error' };
}
}
export default async function autoReminder(request, response) {
// The query below is used to find documents where the reminder date is less than or equal to the current date, and which have existing signers and a signed URL.
try {
const docQuery = new Parse.Query('contracts_Document');
docQuery.limit(limit);
docQuery.skip(skip);
docQuery.limit(2000);
docQuery.lessThanOrEqualTo('NextReminderDate', new Date());
docQuery.equalTo('AutomaticReminders', true);
docQuery.exists('NextReminderDate');
docQuery.exists('Signers');
docQuery.exists('SignedUrl');
docQuery.descending('createdAt');
docQuery.include('Signers,AuditTrail.UserPtr,ExtUserPtr');
docQuery.notEqualTo('IsCompleted', true);
docQuery.notEqualTo('IsDeclined', true);
docQuery.notEqualTo('IsArchive', true);
docQuery.greaterThanOrEqualTo('ExpiryDate', new Date());

const docsArr = await docQuery.find({ useMasterKey: true });

if (docsArr && docsArr.length > 0) {
const _docsArr = JSON.parse(JSON.stringify(docsArr));
let mailCount = 0;
for (const doc of _docsArr) {
// The reminderDate variable is used to calculate the next reminder date.
const RemindOnceInEvery = doc?.RemindOnceInEvery || 5;
Expand All @@ -63,51 +112,19 @@ export default async function autoReminder(request, response) {
const count = auditTrail?.length || 0;
const signer = doc?.Signers?.[count];
if (signer) {
const encodeBase64 = btoa(`${doc.objectId}/${signer.Email}/${signer.objectId}`);
const expireDate = doc?.ExpiryDate?.iso;
const newDate = new Date(expireDate);
const localExpireDate = newDate.toLocaleDateString('en-US', {
day: 'numeric',
month: 'long',
year: 'numeric',
});
const signPdf = `${baseUrl.origin}/login/${encodeBase64}`;
const variables = {
document_title: doc.Name,
sender_name: doc.ExtUserPtr.Name,
sender_mail: doc.ExtUserPtr.Email,
sender_phone: doc.ExtUserPtr.Phone,
receiver_name: signer.Name,
receiver_email: signer.Email,
receiver_phone: signer.Phone,
expiry_date: localExpireDate,
company_name: doc?.ExtUserPtr?.Company || '',
signing_url: `<a href=${signPdf}>Sign here</a>`,
};
const mail = replaceMailVaribles(subject, body, variables);

let params = {
mailProvider: doc?.ExtUserPtr?.active_mail_adapter,
extUserId: doc?.ExtUserPtr?.objectId,
recipient: signer.Email,
subject: mail.subject,
from: doc?.ExtUserPtr?.Email,
html: mail.body,
};
const mailRes = await sendMail(doc, signer, mailCount);
if (mailRes && mailRes.status === 'success') {
mailCount += mailRes.count;
}
try {
// The axios request is used to send a signing reminder email.
const res = await axios.post(url, params, { headers: headers });
// console.log('res ', res.data.result);
if (res.data.result.status === 'success') {
// The code below is used to update the next reminder date of the document based on the "remind once every X days" setting.
const updateDoc = new Parse.Object('contracts_Document');
updateDoc.id = doc.objectId;
updateDoc.set('NextReminderDate', ReminderDate);
const updateRes = await updateDoc.save(null, { useMasterKey: true });
// console.log('updateRes ', updateRes);
}
// The code below is used to update the next reminder date of the document based on the "remind once every X days" setting.
const updateDoc = new Parse.Object('contracts_Document');
updateDoc.id = doc.objectId;
updateDoc.set('NextReminderDate', ReminderDate);
const updateRes = await updateDoc.save(null, { useMasterKey: true });
// console.log('updateRes ', updateRes);
} catch (err) {
console.log('err in sendmail', err);
console.log('err in update document', err);
}
}
} else {
Expand All @@ -124,41 +141,9 @@ export default async function autoReminder(request, response) {
if (signers?.length > 0) {
// The for...of loop below is used to send a signing reminder to every signer who hasn't signed the document yet.
for (const signer of signers) {
const encodeBase64 = btoa(`${doc.objectId}/${signer.Email}/${signer.objectId}`);
const expireDate = doc?.ExpiryDate?.iso;
const newDate = new Date(expireDate);
const localExpireDate = newDate.toLocaleDateString('en-US', {
day: 'numeric',
month: 'long',
year: 'numeric',
});
const signPdf = `${baseUrl.origin}/login/${encodeBase64}`;
const variables = {
document_title: doc.Name,
sender_name: doc.ExtUserPtr.Name,
sender_mail: doc.ExtUserPtr.Email,
sender_phone: doc.ExtUserPtr.Phone,
receiver_name: signer.Name,
receiver_email: signer.Email,
receiver_phone: signer.Phone,
expiry_date: localExpireDate,
company_name: doc?.ExtUserPtr?.Company || '',
signing_url: `<a href=${signPdf}>Sign here</a>`,
};
const mail = replaceMailVaribles(subject, body, variables);
let params = {
mailProvider: doc?.ExtUserPtr?.active_mail_adapter,
extUserId: doc?.ExtUserPtr?.objectId,
recipient: signer.Email,
subject: mail.subject,
from: doc?.ExtUserPtr?.Email,
html: mail.body,
};
try {
const res = await axios.post(url, params, { headers: headers });
// console.log('res ', res.data.result);
} catch (err) {
console.log('err in sendmail', err);
const mailRes = await sendMail(doc, signer, mailCount);
if (mailRes && mailRes.status === 'success') {
mailCount += mailRes.count;
}
}
}
Expand All @@ -167,41 +152,9 @@ export default async function autoReminder(request, response) {
const signers = doc?.Signers;
if (signers?.length > 0) {
for (const signer of signers) {
const encodeBase64 = btoa(`${doc.objectId}/${signer.Email}/${signer.objectId}`);
const expireDate = doc?.ExpiryDate?.iso;
const newDate = new Date(expireDate);
const localExpireDate = newDate.toLocaleDateString('en-US', {
day: 'numeric',
month: 'long',
year: 'numeric',
});
const signPdf = `${baseUrl.origin}/login/${encodeBase64}`;
const variables = {
document_title: doc.Name,
sender_name: doc.ExtUserPtr.Name,
sender_mail: doc.ExtUserPtr.Email,
sender_phone: doc.ExtUserPtr.Phone,
receiver_name: signer.Name,
receiver_email: signer.Email,
receiver_phone: signer.Phone,
expiry_date: localExpireDate,
company_name: doc?.ExtUserPtr?.Company || '',
signing_url: `<a href=${signPdf}>Sign here</a>`,
};
const mail = replaceMailVaribles(subject, body, variables);
let params = {
mailProvider: doc?.ExtUserPtr?.active_mail_adapter,
extUserId: doc?.ExtUserPtr?.objectId,
recipient: signer.Email,
subject: mail.subject,
from: doc?.ExtUserPtr?.Email,
html: mail.body,
};
try {
const res = await axios.post(url, params, { headers: headers });
// console.log('res ', res.data.result);
} catch (err) {
console.log('err in sendmail', err);
const mailRes = await sendMail(doc, signer, mailCount);
if (mailRes && mailRes.status === 'success') {
mailCount += mailRes.count;
}
}
}
Expand All @@ -218,7 +171,7 @@ export default async function autoReminder(request, response) {
}
}
}
response.json({ status: 'success' });
response.json({ status: 'success', document_count: docsArr.length, mail_count: mailCount });
} else {
response.json({ status: 'no record found' });
}
Expand Down
2 changes: 1 addition & 1 deletion apps/OpenSignServer/cloud/customRoute/customApp.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ app.post('/savesubscription', saveSubscription);
app.post('/saveinvoice', saveInvoice);
app.post('/savepayment', savePayments);
app.post('/googleauth', gooogleauth);
app.post('/autoreminder', autoReminder);
app.post('/sendreminder', autoReminder);