From 95b3e3698b2229b999bcece4c91090b0e4b4518b Mon Sep 17 00:00:00 2001 From: Horacio Daniel Ros Date: Sat, 29 Aug 2026 11:28:10 -0300 Subject: [PATCH] =?UTF-8?q?feat:=20reprogramar=20trabajos=20de=20notificac?= =?UTF-8?q?i=C3=B3n=20tras=20el=20cambio=20de=20cita=20y=20manejar=20error?= =?UTF-8?q?es?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../src/Models/Appointments/Appointments.ts | 41 +++++++++++++++++++ 1 file changed, 41 insertions(+) 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 {