Skip to content

Commit 50b72f4

Browse files
authored
Merge pull request #755 from OpenSignLabs/feat_resendmail
2 parents 562a7d6 + 44639f6 commit 50b72f4

File tree

2 files changed

+73
-120
lines changed

2 files changed

+73
-120
lines changed

apps/OpenSignServer/cloud/customRoute/autoReminder.js

Lines changed: 72 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,83 @@ function replaceMailVaribles(subject, body, variables) {
2222
return result;
2323
}
2424

25-
export default async function autoReminder(request, response) {
25+
// `sendmail` is used to signing reminder mail to signer
26+
async function sendMail(doc, signer, mailcount) {
2627
const subject = `{{sender_name}} has requested you to sign "{{document_title}}"`;
2728
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>`;
2829
const url = `${process.env.SERVER_URL}/functions/sendmailv3`;
2930
const headers = {
3031
'Content-Type': 'application/json',
3132
'X-Parse-Application-Id': process.env.APP_ID,
3233
};
33-
const limit = request.query.limit || 2000;
34-
const skip = request.query.skip || 0;
34+
3535
const baseUrl = new URL(process.env.PUBLIC_URL);
36+
const encodeBase64 = btoa(`${doc.objectId}/${signer.Email}/${signer.objectId}`);
37+
const expireDate = doc?.ExpiryDate?.iso;
38+
const newDate = new Date(expireDate);
39+
const localExpireDate = newDate.toLocaleDateString('en-US', {
40+
day: 'numeric',
41+
month: 'long',
42+
year: 'numeric',
43+
});
44+
const signPdf = `${baseUrl.origin}/login/${encodeBase64}`;
45+
const variables = {
46+
document_title: doc.Name,
47+
sender_name: doc.ExtUserPtr.Name,
48+
sender_mail: doc.ExtUserPtr.Email,
49+
sender_phone: doc.ExtUserPtr.Phone,
50+
receiver_name: signer.Name,
51+
receiver_email: signer.Email,
52+
receiver_phone: signer.Phone,
53+
expiry_date: localExpireDate,
54+
company_name: doc?.ExtUserPtr?.Company || '',
55+
signing_url: `<a href=${signPdf}>Sign here</a>`,
56+
};
57+
const mail = replaceMailVaribles(subject, body, variables);
3658

59+
let params = {
60+
mailProvider: doc?.ExtUserPtr?.active_mail_adapter,
61+
extUserId: doc?.ExtUserPtr?.objectId,
62+
recipient: signer.Email,
63+
subject: mail.subject,
64+
from: doc?.ExtUserPtr?.Email,
65+
html: mail.body,
66+
};
67+
try {
68+
// The axios request is used to send a signing reminder email.
69+
const res = await axios.post(url, params, { headers: headers });
70+
// console.log('res ', res.data.result);
71+
if (res.data.result.status === 'success') {
72+
mailcount += 1;
73+
return { status: 'success', count: mailcount };
74+
}
75+
} catch (err) {
76+
console.log('err in mail of sendreminder api', err);
77+
return { status: 'error' };
78+
}
79+
}
80+
export default async function autoReminder(request, response) {
3781
// 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.
3882
try {
3983
const docQuery = new Parse.Query('contracts_Document');
40-
docQuery.limit(limit);
41-
docQuery.skip(skip);
84+
docQuery.limit(2000);
4285
docQuery.lessThanOrEqualTo('NextReminderDate', new Date());
4386
docQuery.equalTo('AutomaticReminders', true);
4487
docQuery.exists('NextReminderDate');
4588
docQuery.exists('Signers');
4689
docQuery.exists('SignedUrl');
4790
docQuery.descending('createdAt');
4891
docQuery.include('Signers,AuditTrail.UserPtr,ExtUserPtr');
92+
docQuery.notEqualTo('IsCompleted', true);
93+
docQuery.notEqualTo('IsDeclined', true);
94+
docQuery.notEqualTo('IsArchive', true);
95+
docQuery.greaterThanOrEqualTo('ExpiryDate', new Date());
96+
4997
const docsArr = await docQuery.find({ useMasterKey: true });
5098

5199
if (docsArr && docsArr.length > 0) {
52100
const _docsArr = JSON.parse(JSON.stringify(docsArr));
101+
let mailCount = 0;
53102
for (const doc of _docsArr) {
54103
// The reminderDate variable is used to calculate the next reminder date.
55104
const RemindOnceInEvery = doc?.RemindOnceInEvery || 5;
@@ -63,51 +112,19 @@ export default async function autoReminder(request, response) {
63112
const count = auditTrail?.length || 0;
64113
const signer = doc?.Signers?.[count];
65114
if (signer) {
66-
const encodeBase64 = btoa(`${doc.objectId}/${signer.Email}/${signer.objectId}`);
67-
const expireDate = doc?.ExpiryDate?.iso;
68-
const newDate = new Date(expireDate);
69-
const localExpireDate = newDate.toLocaleDateString('en-US', {
70-
day: 'numeric',
71-
month: 'long',
72-
year: 'numeric',
73-
});
74-
const signPdf = `${baseUrl.origin}/login/${encodeBase64}`;
75-
const variables = {
76-
document_title: doc.Name,
77-
sender_name: doc.ExtUserPtr.Name,
78-
sender_mail: doc.ExtUserPtr.Email,
79-
sender_phone: doc.ExtUserPtr.Phone,
80-
receiver_name: signer.Name,
81-
receiver_email: signer.Email,
82-
receiver_phone: signer.Phone,
83-
expiry_date: localExpireDate,
84-
company_name: doc?.ExtUserPtr?.Company || '',
85-
signing_url: `<a href=${signPdf}>Sign here</a>`,
86-
};
87-
const mail = replaceMailVaribles(subject, body, variables);
88-
89-
let params = {
90-
mailProvider: doc?.ExtUserPtr?.active_mail_adapter,
91-
extUserId: doc?.ExtUserPtr?.objectId,
92-
recipient: signer.Email,
93-
subject: mail.subject,
94-
from: doc?.ExtUserPtr?.Email,
95-
html: mail.body,
96-
};
115+
const mailRes = await sendMail(doc, signer, mailCount);
116+
if (mailRes && mailRes.status === 'success') {
117+
mailCount += mailRes.count;
118+
}
97119
try {
98-
// The axios request is used to send a signing reminder email.
99-
const res = await axios.post(url, params, { headers: headers });
100-
// console.log('res ', res.data.result);
101-
if (res.data.result.status === 'success') {
102-
// The code below is used to update the next reminder date of the document based on the "remind once every X days" setting.
103-
const updateDoc = new Parse.Object('contracts_Document');
104-
updateDoc.id = doc.objectId;
105-
updateDoc.set('NextReminderDate', ReminderDate);
106-
const updateRes = await updateDoc.save(null, { useMasterKey: true });
107-
// console.log('updateRes ', updateRes);
108-
}
120+
// The code below is used to update the next reminder date of the document based on the "remind once every X days" setting.
121+
const updateDoc = new Parse.Object('contracts_Document');
122+
updateDoc.id = doc.objectId;
123+
updateDoc.set('NextReminderDate', ReminderDate);
124+
const updateRes = await updateDoc.save(null, { useMasterKey: true });
125+
// console.log('updateRes ', updateRes);
109126
} catch (err) {
110-
console.log('err in sendmail', err);
127+
console.log('err in update document', err);
111128
}
112129
}
113130
} else {
@@ -124,41 +141,9 @@ export default async function autoReminder(request, response) {
124141
if (signers?.length > 0) {
125142
// The for...of loop below is used to send a signing reminder to every signer who hasn't signed the document yet.
126143
for (const signer of signers) {
127-
const encodeBase64 = btoa(`${doc.objectId}/${signer.Email}/${signer.objectId}`);
128-
const expireDate = doc?.ExpiryDate?.iso;
129-
const newDate = new Date(expireDate);
130-
const localExpireDate = newDate.toLocaleDateString('en-US', {
131-
day: 'numeric',
132-
month: 'long',
133-
year: 'numeric',
134-
});
135-
const signPdf = `${baseUrl.origin}/login/${encodeBase64}`;
136-
const variables = {
137-
document_title: doc.Name,
138-
sender_name: doc.ExtUserPtr.Name,
139-
sender_mail: doc.ExtUserPtr.Email,
140-
sender_phone: doc.ExtUserPtr.Phone,
141-
receiver_name: signer.Name,
142-
receiver_email: signer.Email,
143-
receiver_phone: signer.Phone,
144-
expiry_date: localExpireDate,
145-
company_name: doc?.ExtUserPtr?.Company || '',
146-
signing_url: `<a href=${signPdf}>Sign here</a>`,
147-
};
148-
const mail = replaceMailVaribles(subject, body, variables);
149-
let params = {
150-
mailProvider: doc?.ExtUserPtr?.active_mail_adapter,
151-
extUserId: doc?.ExtUserPtr?.objectId,
152-
recipient: signer.Email,
153-
subject: mail.subject,
154-
from: doc?.ExtUserPtr?.Email,
155-
html: mail.body,
156-
};
157-
try {
158-
const res = await axios.post(url, params, { headers: headers });
159-
// console.log('res ', res.data.result);
160-
} catch (err) {
161-
console.log('err in sendmail', err);
144+
const mailRes = await sendMail(doc, signer, mailCount);
145+
if (mailRes && mailRes.status === 'success') {
146+
mailCount += mailRes.count;
162147
}
163148
}
164149
}
@@ -167,41 +152,9 @@ export default async function autoReminder(request, response) {
167152
const signers = doc?.Signers;
168153
if (signers?.length > 0) {
169154
for (const signer of signers) {
170-
const encodeBase64 = btoa(`${doc.objectId}/${signer.Email}/${signer.objectId}`);
171-
const expireDate = doc?.ExpiryDate?.iso;
172-
const newDate = new Date(expireDate);
173-
const localExpireDate = newDate.toLocaleDateString('en-US', {
174-
day: 'numeric',
175-
month: 'long',
176-
year: 'numeric',
177-
});
178-
const signPdf = `${baseUrl.origin}/login/${encodeBase64}`;
179-
const variables = {
180-
document_title: doc.Name,
181-
sender_name: doc.ExtUserPtr.Name,
182-
sender_mail: doc.ExtUserPtr.Email,
183-
sender_phone: doc.ExtUserPtr.Phone,
184-
receiver_name: signer.Name,
185-
receiver_email: signer.Email,
186-
receiver_phone: signer.Phone,
187-
expiry_date: localExpireDate,
188-
company_name: doc?.ExtUserPtr?.Company || '',
189-
signing_url: `<a href=${signPdf}>Sign here</a>`,
190-
};
191-
const mail = replaceMailVaribles(subject, body, variables);
192-
let params = {
193-
mailProvider: doc?.ExtUserPtr?.active_mail_adapter,
194-
extUserId: doc?.ExtUserPtr?.objectId,
195-
recipient: signer.Email,
196-
subject: mail.subject,
197-
from: doc?.ExtUserPtr?.Email,
198-
html: mail.body,
199-
};
200-
try {
201-
const res = await axios.post(url, params, { headers: headers });
202-
// console.log('res ', res.data.result);
203-
} catch (err) {
204-
console.log('err in sendmail', err);
155+
const mailRes = await sendMail(doc, signer, mailCount);
156+
if (mailRes && mailRes.status === 'success') {
157+
mailCount += mailRes.count;
205158
}
206159
}
207160
}
@@ -218,7 +171,7 @@ export default async function autoReminder(request, response) {
218171
}
219172
}
220173
}
221-
response.json({ status: 'success' });
174+
response.json({ status: 'success', document_count: docsArr.length, mail_count: mailCount });
222175
} else {
223176
response.json({ status: 'no record found' });
224177
}

apps/OpenSignServer/cloud/customRoute/customApp.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,4 @@ app.post('/savesubscription', saveSubscription);
2121
app.post('/saveinvoice', saveInvoice);
2222
app.post('/savepayment', savePayments);
2323
app.post('/googleauth', gooogleauth);
24-
app.post('/autoreminder', autoReminder);
24+
app.post('/sendreminder', autoReminder);

0 commit comments

Comments
 (0)