'Cron job scheduling on heroku . Please go thru my code below and tell me if there is a solution to this
I am making an appointment system so every time an appointment is booked i want to run a cron job so the code goes as follows
const Appointment = new appointment({
p_id: req.user._id,
d_id: req.params.id,
date,
time,
type,
relative,
phoneNo1,
phoneNo2,
payment,
});
await Appointment.save();
await checkStatus(req.params.id, Appointment.time, date);
// the above function schedules a cron job res.status(200).json({ status: "success", message: "Appointment placed successfully", Appointment, });
CheckStatus Function is as below
export const checkStatus = async (id, time, date) => {
if (todayDate === date) {
const job = Cron("*/5 * * * *", async () => {
const Appointment = await appointment
.find({
$and: [
{ d_id: mongoose.Types.ObjectId(id) },
{ date: date },
{ time: time },
{ Status: "incoming" },
],
})
.populate("p_id d_id", "name email cardCollections.doctorName phoneNumber");
console.log("Appointment", Appointment);
console.log("diff", diff);
if (Appointment.length === 0) {
job.stop();
}
if (diff <= 4 && diff > 3) {
SendEmail(
Appointment[0].p_id.email,
`Reminder to accept appointment 1nd cond at ${now} `,
"Appointments waiting for you"
);
} else if (diff <= 3 && diff > 1) {
// send sms to doctor
// SendSms(
// Appointment[0].d_id.phoneNumber,
// `Reminder to accept appointment 2nd cond at ${now} `
// );
SendEmail(
Appointment[0].p_id.email,
`Reminder to accept appointment 2nd cond at ${now} `,
"Appointments waiting for you"
);
} else if (diff <= 1) {
// send sms to doctor
Appointment.forEach(async (apt) => {
apt.Status = "dismissed";
apt.cancellationStatus.reason = `Dismissed by the doctor`;
apt.cancellationStatus.cancelledAt = Date.now();
await apt.save();
});
SendEmail(
Appointment[0].p_id.email,
`All appointment are cancelled `,
"Cancellation due to no response"
);
job.stop();
}
});
} }
So the code works perfectly during localhost but i cant find a way to do the same in heroku as i m confused how do i send parameters and especially i want to run the code for when a appointment is booked .I tried looking various blogs but all of them they show cron scheduling independently without any params . So basically i want to chedule a cron job (checkStatus) everytime an appointment is booked Can anyone help me with this???
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
