feat: implement sysadmin subscription management and plan usage cycle recalculation tools

This commit is contained in:
2026-07-18 21:32:47 -03:00
parent 1c8c47cf76
commit 4f3b56784e
36 changed files with 2146 additions and 21 deletions
+22 -7
View File
@@ -305,12 +305,6 @@ class AppointmentManager implements IAppointmentsManager {
throw new Error("La compañia no existe");
}
const canAdd = await MetricsList.canAddAppointment(companyCheck.ownerId);
if (!canAdd) {
throw new Error("Ha alcanzado el limite de turnos permitidos de acuerdo a su plan.");
}
const serviceCheck = await ServicesManager.services.findOne({
_id: data.serviceId,
companyId: data.companyId,
@@ -443,6 +437,18 @@ class AppointmentManager implements IAppointmentsManager {
data.comments = isNull<string>(data.comments, "");
const appointmentReserved = await MetricsList.reserveAppointment({
userId: String(companyCheck.ownerId),
companyId: data.companyId,
quantity: 1,
});
if (!appointmentReserved) {
throw new Error("Ha alcanzado el limite de turnos permitidos de acuerdo a su plan.");
}
let appointmentCreated = false;
try {
const start = dayjs(data.start).clone();
const end = dayjs(data.start)
@@ -467,6 +473,7 @@ class AppointmentManager implements IAppointmentsManager {
comments: data.comments,
sessionUser: data.sessionUser,
});
appointmentCreated = true;
if (data.repeatId) {
const repeat = await RepeatsList.repeats.findOne({
@@ -489,8 +496,9 @@ class AppointmentManager implements IAppointmentsManager {
}
}
await MetricsList.addAppointment({
await MetricsList.metrics.addAppointment({
userId: companyCheck.ownerId,
companyId: data.companyId,
quantity: 1,
});
@@ -559,6 +567,13 @@ class AppointmentManager implements IAppointmentsManager {
return newAppointment;
} catch (err) {
console.log(err);
if (!appointmentCreated) {
await MetricsList.releaseAppointment({
userId: String(companyCheck.ownerId),
companyId: data.companyId,
quantity: 1,
});
}
throw new Error("Ha ocurrido un error al crear el turno.");
}
}