refactor: modularize notification job creation and enforce plan-based channel availability in policy resolver

This commit is contained in:
2026-07-27 22:51:33 -03:00
parent dc020d94d2
commit 68d98e8b40
6 changed files with 333 additions and 58 deletions
+66 -57
View File
@@ -529,51 +529,51 @@ class AppointmentManager implements IAppointmentsManager {
type: NotificationType.APPOINTMENT,
code: String((newAppointment as any)._id)
});
}
//Create jobs for email and whatsapp notifications
if (newAppointment.id) {
const emailContent = await this.tryToSendNotification({
appointmentId: String(newAppointment.id),
sessionUser: String(companyCheck.ownerId),
type: APPOINTMENT_NOTIFICATION_TYPE.CREATION,
channel: "email",
});
const wapContent = await this.tryToSendNotification({
appointmentId: String(newAppointment.id),
sessionUser: String(companyCheck.ownerId),
type: APPOINTMENT_NOTIFICATION_TYPE.CREATION,
channel: "whatsapp",
});
const clientPhoneNumber = await this.getOptionalClientWapNumber(checkClient);
const reminderEmailContent = await this.tryToSendNotification({
appointmentId: String(newAppointment.id),
sessionUser: String(companyCheck.ownerId),
type: APPOINTMENT_NOTIFICATION_TYPE.REMINDER,
channel: "email",
});
const reminderWapContent = await this.tryToSendNotification({
appointmentId: String(newAppointment.id),
sessionUser: String(companyCheck.ownerId),
type: APPOINTMENT_NOTIFICATION_TYPE.REMINDER,
channel: "whatsapp",
});
//Create jobs for email and whatsapp notifications
if (notification && newAppointment.id) {
const emailContent = await this.tryToSendNotification({
appointmentId: String(newAppointment.id),
sessionUser: String(companyCheck.ownerId),
type: APPOINTMENT_NOTIFICATION_TYPE.CREATION,
channel: "email",
});
const wapContent = await this.tryToSendNotification({
appointmentId: String(newAppointment.id),
sessionUser: String(companyCheck.ownerId),
type: APPOINTMENT_NOTIFICATION_TYPE.CREATION,
channel: "whatsapp",
});
const clientPhoneNumber = await this.getOptionalClientWapNumber(checkClient);
const reminderEmailContent = await this.tryToSendNotification({
appointmentId: String(newAppointment.id),
sessionUser: String(companyCheck.ownerId),
type: APPOINTMENT_NOTIFICATION_TYPE.REMINDER,
channel: "email",
});
const reminderWapContent = await this.tryToSendNotification({
appointmentId: String(newAppointment.id),
sessionUser: String(companyCheck.ownerId),
type: APPOINTMENT_NOTIFICATION_TYPE.REMINDER,
channel: "whatsapp",
});
await this.createCreationNotificationJobs({
appointmentId: String(newAppointment.id),
companyId: String(companyCheck._id),
clientId: String(checkClient._id),
clientUserId: checkClient.userId ? String(checkClient.userId) : undefined,
clientEmail: checkClient.email,
clientPhoneNumber,
companyOwnerId: String(companyCheck.ownerId),
companyName: companyCheck.name,
appointmentStart: new Date(data.start),
emailMessage: emailContent.message,
wapMessage: wapContent.message,
reminderEmailMessage: reminderEmailContent.message,
reminderWapMessage: reminderWapContent.message,
});
}
await this.createCreationNotificationJobs({
appointmentId: String(newAppointment.id),
companyId: String(companyCheck._id),
clientId: String(checkClient._id),
clientUserId: checkClient.userId ? String(checkClient.userId) : undefined,
clientEmail: checkClient.email,
clientPhoneNumber,
companyOwnerId: String(companyCheck.ownerId),
companyName: companyCheck.name,
appointmentStart: new Date(data.start),
emailMessage: emailContent.message,
wapMessage: wapContent.message,
reminderEmailMessage: reminderEmailContent.message,
reminderWapMessage: reminderWapContent.message,
});
}
//Envio la notificacion al profesional.
@@ -2240,25 +2240,34 @@ public async updateAppointment(data: UpdateAppointmentParams): Promise<void> {
const systemSubject = `Turno reservado en ${data.companyName}`;
const reminderSystemSubject = `Recordatorio de turno en ${data.companyName}`;
await this.jobService.createJob({
const payload = {
email: data.clientEmail,
phoneNumber: data.clientPhoneNumber,
userId: data.clientUserId,
companyOwnerId: data.companyOwnerId,
subject: systemSubject,
message: data.emailMessage,
emailSubject: "TurnosXpress :: Alta de turno",
emailMessage: data.emailMessage,
wapMessage: data.wapMessage,
systemSubject,
systemMessage: data.emailMessage,
};
await this.jobService.createMandatoryCreationJob({
companyId: data.companyId,
clientId: data.clientId,
appointmentId: data.appointmentId,
type: "creation",
payload,
});
await this.jobService.createReminderJobs({
companyId: data.companyId,
clientId: data.clientId,
appointmentId: data.appointmentId,
type: "reminder",
appointmentStart: data.appointmentStart,
payload: {
email: data.clientEmail,
phoneNumber: data.clientPhoneNumber,
userId: data.clientUserId,
companyOwnerId: data.companyOwnerId,
subject: systemSubject,
message: data.emailMessage,
emailSubject: "TurnosXpress :: Alta de turno",
emailMessage: data.emailMessage,
wapMessage: data.wapMessage,
systemSubject,
systemMessage: data.emailMessage,
},
reminderPayload: {
email: data.clientEmail,
phoneNumber: data.clientPhoneNumber,
@@ -421,6 +421,8 @@ describe("cancellation notification jobs", () => {
};
(AppointmentsList as any).jobService = {
createJob: jest.fn().mockResolvedValue([]),
createMandatoryCreationJob: jest.fn().mockResolvedValue([]),
createReminderJobs: jest.fn().mockResolvedValue([]),
};
(CompaniesManager.companies.findOne as jest.Mock).mockResolvedValue({
@@ -563,6 +565,8 @@ describe("creation notification jobs", () => {
};
(AppointmentsList as any).jobService = {
createJob: jest.fn().mockResolvedValue([]),
createMandatoryCreationJob: jest.fn().mockResolvedValue([]),
createReminderJobs: jest.fn().mockResolvedValue([]),
};
(CompaniesManager.companies.findOne as jest.Mock).mockResolvedValue({
@@ -649,7 +653,7 @@ describe("creation notification jobs", () => {
reminderWapMessage: "WAP REMINDER Ada Lovelace",
});
expect((AppointmentsList as any).jobService.createJob).toHaveBeenCalledWith(
expect((AppointmentsList as any).jobService.createMandatoryCreationJob).toHaveBeenCalledWith(
expect.objectContaining({
type: "creation",
payload: expect.objectContaining({
@@ -659,6 +663,11 @@ describe("creation notification jobs", () => {
systemSubject: "Turno reservado en clases llavallol",
systemMessage: "EMAIL ALTA Ada Lovelace Historia clases llavallol 22/07/2026 08:00hs.",
}),
})
);
expect((AppointmentsList as any).jobService.createReminderJobs).toHaveBeenCalledWith(
expect.objectContaining({
type: "reminder",
reminderPayload: expect.objectContaining({
email: "ada@example.com",
phoneNumber: "5491112345678",