Skip to content

Commit 302f38d

Browse files
authored
Merge pull request #808 from OpenSignLabs/autoreminder
2 parents 0be1816 + ba1c314 commit 302f38d

File tree

1 file changed

+18
-11
lines changed

1 file changed

+18
-11
lines changed

apps/OpenSignServer/cloud/customRoute/autoReminder.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ function replaceMailVaribles(subject, body, variables) {
2323
}
2424

2525
// `sendmail` is used to signing reminder mail to signer
26-
async function sendMail(doc, signer, mailcount) {
26+
async function sendMail(doc, signer) {
2727
const subject = `{{sender_name}} has requested you to sign "{{document_title}}"`;
2828
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>`;
2929
const url = `${process.env.SERVER_URL}/functions/sendmailv3`;
@@ -69,8 +69,7 @@ async function sendMail(doc, signer, mailcount) {
6969
const res = await axios.post(url, params, { headers: headers });
7070
// console.log('res ', res.data.result);
7171
if (res.data.result.status === 'success') {
72-
mailcount += 1;
73-
return { status: 'success', count: mailcount };
72+
return { status: 'success' };
7473
}
7574
} catch (err) {
7675
console.log('err in mail of sendreminder api', err);
@@ -99,10 +98,11 @@ export default async function autoReminder(request, response) {
9998
if (docsArr && docsArr.length > 0) {
10099
const _docsArr = JSON.parse(JSON.stringify(docsArr));
101100
let mailCount = 0;
101+
let docCount = 0;
102102
for (const doc of _docsArr) {
103103
// The reminderDate variable is used to calculate the next reminder date.
104104
const RemindOnceInEvery = doc?.RemindOnceInEvery || 5;
105-
const ReminderDate = new Date(doc?.NextReminderDate?.iso);
105+
const ReminderDate = new Date();
106106
ReminderDate.setDate(ReminderDate.getDate() + RemindOnceInEvery);
107107
// The sendInOrder variable is used to determine whether to send emails in order or not.
108108
const SendInOrder = doc?.SendinOrder || false;
@@ -112,9 +112,10 @@ export default async function autoReminder(request, response) {
112112
const count = auditTrail?.length || 0;
113113
const signer = doc?.Signers?.[count];
114114
if (signer) {
115-
const mailRes = await sendMail(doc, signer, mailCount);
115+
docCount += 1;
116+
const mailRes = await sendMail(doc, signer);
116117
if (mailRes && mailRes.status === 'success') {
117-
mailCount += mailRes.count;
118+
mailCount += 1;
118119
}
119120
try {
120121
// The code below is used to update the next reminder date of the document based on the "remind once every X days" setting.
@@ -139,22 +140,24 @@ export default async function autoReminder(request, response) {
139140
}
140141
});
141142
if (signers?.length > 0) {
143+
docCount += 1;
142144
// The for...of loop below is used to send a signing reminder to every signer who hasn't signed the document yet.
143145
for (const signer of signers) {
144-
const mailRes = await sendMail(doc, signer, mailCount);
146+
const mailRes = await sendMail(doc, signer);
145147
if (mailRes && mailRes.status === 'success') {
146-
mailCount += mailRes.count;
148+
mailCount += 1;
147149
}
148150
}
149151
}
150152
} else {
151153
// The for...of loop below is used to send a signing reminder to every signer who hasn't signed the document yet.
152154
const signers = doc?.Signers;
153155
if (signers?.length > 0) {
156+
docCount += 1;
154157
for (const signer of signers) {
155-
const mailRes = await sendMail(doc, signer, mailCount);
158+
const mailRes = await sendMail(doc, signer);
156159
if (mailRes && mailRes.status === 'success') {
157-
mailCount += mailRes.count;
160+
mailCount += 1;
158161
}
159162
}
160163
}
@@ -171,7 +174,11 @@ export default async function autoReminder(request, response) {
171174
}
172175
}
173176
}
174-
response.json({ status: 'success', document_count: docsArr.length, mail_count: mailCount });
177+
if (docCount > 0) {
178+
response.json({ status: 'success', document_count: docCount, mail_count: mailCount });
179+
} else {
180+
response.json({ status: 'no record found' });
181+
}
175182
} else {
176183
response.json({ status: 'no record found' });
177184
}

0 commit comments

Comments
 (0)