-
Notifications
You must be signed in to change notification settings - Fork 0
/
cronJobs.js
93 lines (84 loc) · 3.36 KB
/
cronJobs.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
const cron = require("node-cron");
const Task = require("./models/Task");
const User = require("./models/User");
const twilio = require("twilio");
require("dotenv/config");
const accountSid = process.env.ACCOUNT_SID;
const authToken = process.env.AUTH_TOKEN;
const client = new twilio(accountSid, authToken);
// Cron job to change task priorities based on due_date
const cronJobs = {
start: () => {
// Cron job to change task priorities based on due_date
cron.schedule("0 0 * * *", async () => {
try {
const tasksToUpdate = await Task.find({
due_date: { $lt: new Date() }, // Find tasks with due dates in the past
});
tasksToUpdate.forEach(async (task) => {
// Logic to update task priorities based on due_date
// You can adjust this logic based on your requirements
// Example: Update priority to 2 if due_date is more than 5 days ago
let priority = 0; // Default priority
// Calculate the difference in days between the due date and the current date
const currentDate = new Date();
const differenceInDays = Math.floor(
(dueDate - currentDate) / (1000 * 60 * 60 * 24)
);
// Assign priority based on the difference in days
if (differenceInDays === 0) {
priority = 0; // Due date is today
} else if (differenceInDays === 1 || differenceInDays === 2) {
priority = 1; // Due date is between tomorrow and day after tomorrow
} else if (differenceInDays <= 4) {
priority = 2; // Due date is within the next 4 days
} else {
priority = 3; // Due date is 5 or more days away
}
try {
// Update the priority of the task in the database
await Task.findByIdAndUpdate(taskId, { priority });
console.log(`Priority updated for task ${taskId}: ${priority}`);
} catch (error) {
console.error(`Error updating priority for task ${taskId}:`, error);
}
});
} catch (error) {
console.error("Error updating task priorities:", error);
}
});
// Cron job for voice calling using Twilio
cron.schedule("33 15 * * *", async () => {
try {
// Fetch users based on priority order
const users = await User.find().sort({ priority: 1 });
// Iterate through users and attempt to make voice calls
for (const user of users) {
try {
// Logic to check if the user has overdue tasks and make a voice call
const overdueTasks = await Task.find({
user: user._id,
due_date: { $lt: new Date() },
});
if (overdueTasks.length > 0) {
// Make voice call using Twilio
await client.calls.create({
to: user.phone_number,
from: process.env.PHONE_NO,
url: "http://demo.twilio.com/docs/voice.xml",
method: "POST",
});
// Break the loop if a call is successfully made
break;
}
} catch (callError) {
console.error("Error making Twilio voice call:", callError);
}
}
} catch (error) {
console.error("Error fetching users for voice calls:", error);
}
});
},
};
module.exports = cronJobs;