|
1 | 1 | const emailService = require("../services/emailService"); |
2 | | -// const Submission = require("../models/InternshipRequest"); // ❌ Remove this |
3 | 2 | const NotificationLog = require("../models/NotifLog"); |
4 | 3 | const User = require("../models/User"); |
5 | 4 | const UserTokenRequest = require("../models/TokenRequest"); |
6 | 5 | const logger = require("../utils/logger"); |
7 | 6 | const dayjs = require("dayjs"); |
8 | 7 |
|
9 | 8 | // ================= Coordinator Reminder ================= |
| 9 | +// Sends reminders for |
| 10 | +// A1 (InternshipRequest) every 5 working days |
| 11 | +// A2 (Weekly Reports) every 5 working days |
| 12 | +// A3 (Evaluations) every 3 days |
10 | 13 | const coordinatorReminder = async () => { |
11 | 14 | const now = dayjs(); |
12 | 15 | const fiveWorkingDays = now.subtract(7, "day").toDate(); |
| 16 | + const threeDays = now.subtract(3, 'day').toDate(); |
13 | 17 |
|
14 | 18 | try { |
15 | 19 | const pendingSubs = await getAllForms({ |
16 | 20 | coordinator_status: "pending", |
17 | 21 | supervisor_status: "approved", |
18 | 22 | createdAt: { $lt: fiveWorkingDays }, |
| 23 | + evaluations: { $size: 0 }, |
19 | 24 | }); |
20 | 25 |
|
21 | | - for (const submission of pendingSubs) { |
| 26 | + const pendingEvals = await getAllForms({ |
| 27 | + coordinator_status: "pending", |
| 28 | + supervisor_status: "approved", |
| 29 | + createdAt: { $lt: threeDays }, |
| 30 | + evaluations: { $ne: [] }, |
| 31 | + }); |
| 32 | + |
| 33 | + await sendCoordinatorReminder(pendingSubs, 5, now); |
| 34 | + await sendCoordinatorReminder(pendingEvals, 3, now); |
| 35 | + } catch (err) { |
| 36 | + logger.error("❌ Error in coordinatorReminder:", err.message); |
| 37 | + } |
| 38 | +}; |
| 39 | + |
| 40 | +const sendCoordinatorReminder = async (subs, nextDueIn, now) => { |
| 41 | + try { |
| 42 | + for (const submission of subs) { |
22 | 43 | const student = await User.findById(submission.student_id); |
23 | 44 | const coordinator = await User.findById(submission.coordinator_id); |
24 | 45 |
|
25 | 46 | const reminderCount = submission.coordinator_reminder_count || 0; |
26 | 47 | const lastReminded = submission.last_coordinator_reminder_at || submission.createdAt; |
27 | | - const nextReminderDue = dayjs(lastReminded).add(5, "day"); |
| 48 | + const nextReminderDue = dayjs(lastReminded).add(nextDueIn, "day"); |
28 | 49 | const shouldRemindAgain = now.isAfter(nextReminderDue); |
29 | 50 |
|
30 | 51 | if (reminderCount >= 2 && shouldRemindAgain && !submission.studentNotified) { |
31 | 52 | await emailService.sendEmail({ |
32 | 53 | to: student.email, |
33 | 54 | subject: `Coordinator Not Responding for "${submission.name}"`, |
34 | 55 | html: `<p>Your submission "${submission.name}" has not been approved by the coordinator even after 2 reminders.</p> |
35 | | - <p>You can now choose to <strong>resend</strong> or <strong>delete</strong> the request.</p>`, |
| 56 | + <p>You can now choose to <strong>resend</strong> or <strong>delete</strong> the request.</p>`, |
36 | 57 | text: `Your submission "${submission.name}" is still awaiting coordinator approval.`, |
37 | 58 | }); |
38 | 59 |
|
@@ -63,7 +84,7 @@ const coordinatorReminder = async () => { |
63 | 84 | } |
64 | 85 | } |
65 | 86 | } catch (err) { |
66 | | - logger.error("❌ Error in coordinatorReminder:", err.message); |
| 87 | + logger.error("❌ Error in sendCoordinatorReminder:", err.message); |
67 | 88 | } |
68 | 89 | }; |
69 | 90 |
|
|
0 commit comments