diff --git a/server/src/Models/Appointments/Appointments.ts b/server/src/Models/Appointments/Appointments.ts index 0637590..c801e42 100644 --- a/server/src/Models/Appointments/Appointments.ts +++ b/server/src/Models/Appointments/Appointments.ts @@ -687,6 +687,47 @@ class AppointmentManager implements IAppointmentsManager { // Update both heatmaps await this.updateHeatMap(originalConfig); await this.updateHeatMap(newConfig); + + // Reprogram pending notification jobs for the new date/time. The appointment + // was already saved, so tryToSendNotification reads the new start date and + // builds the reminder content (email template included) from it. + try { + // Cancel the jobs scheduled for the original date/time so they don't fire late. + await this.jobService.cancelByAppointment(String(appointment._id)); + + const reminderEmailContent = await this.tryToSendNotification({ + appointmentId: String(appointment._id), + sessionUser: data.sessionUser || "", + type: APPOINTMENT_NOTIFICATION_TYPE.REMINDER, + channel: "email", + }); + const reminderWapContent = await this.tryToSendNotification({ + appointmentId: String(appointment._id), + sessionUser: data.sessionUser || "", + type: APPOINTMENT_NOTIFICATION_TYPE.REMINDER, + channel: "whatsapp", + }); + + await this.createReminderNotificationJobs({ + appointmentId: String(appointment._id), + companyId: String(appointment.companyId), + clientId: String(appointment.clientId), + clientUserId: reminderEmailContent.checkClient.userId + ? String(reminderEmailContent.checkClient.userId) + : undefined, + clientEmail: reminderEmailContent.checkClient.email, + clientPhoneNumber: await this.getOptionalClientWapNumber(reminderEmailContent.checkClient), + companyOwnerId: String(reminderEmailContent.companyCheck.ownerId), + companyName: reminderEmailContent.companyCheck.name, + appointmentStart: appointment.start, + reminderEmailMessage: reminderEmailContent.message, + reminderWapMessage: reminderWapContent.message, + reminderEmailTemplateId: reminderEmailContent.emailTemplateId, + reminderEmailContext: reminderEmailContent.emailContext, + }); + } catch (error) { + console.log("Error reprogramming notification jobs after move:", error); + } } public async changeServiceForce(data: ChangeServiceParams): Promise {