feat: implement sysadmin subscription management and plan usage cycle recalculation tools
This commit is contained in:
@@ -14,6 +14,7 @@ import AppointmentList from "../Appointments/Appointments";
|
||||
import ClientsList from "../Clients/Clients";
|
||||
import RepeatsList from "../Repeats/Repeats";
|
||||
import { isNull } from "../../helpers/IsNull";
|
||||
import PlanUsageCycleList from "../PlanUsageCycle/PlanUsageCycle";
|
||||
|
||||
class MetricsManager implements IMetricsManager {
|
||||
metrics: MetricsAdapterMongoose;
|
||||
@@ -231,17 +232,64 @@ class MetricsManager implements IMetricsManager {
|
||||
if (plan.limitAppointments < 0) {
|
||||
return true;
|
||||
}
|
||||
const metrics = await this.getMetrics(userId);
|
||||
if (metrics.appointmentsCount >= plan.limitAppointments) {
|
||||
const appointmentsCount = await PlanUsageCycleList.getAppointmentsCount({ userId });
|
||||
if (appointmentsCount >= plan.limitAppointments) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
public async addAppointment(data: MetricsParams): Promise<void> {
|
||||
if (data.userId) {
|
||||
await PlanUsageCycleList.addAppointment({
|
||||
userId: data.userId,
|
||||
quantity: data.quantity,
|
||||
});
|
||||
}
|
||||
await this.metrics.addAppointment(data);
|
||||
}
|
||||
|
||||
public async reserveAppointment(data: MetricsParams): Promise<boolean> {
|
||||
if (!data.userId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
const subscription = await PlanSubscriptionsList.findOne({
|
||||
sessionUser: data.userId,
|
||||
});
|
||||
if (!subscription) {
|
||||
return false;
|
||||
}
|
||||
const plan = await PlansList.plans.findOne({
|
||||
_id: subscription.planId,
|
||||
});
|
||||
if (!plan) {
|
||||
return false;
|
||||
}
|
||||
if (plan.limitAppointments < 0) {
|
||||
await PlanUsageCycleList.addAppointment({
|
||||
userId: data.userId,
|
||||
quantity: data.quantity,
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
return await PlanUsageCycleList.reserveAppointment({
|
||||
userId: data.userId,
|
||||
quantity: data.quantity,
|
||||
limit: plan.limitAppointments,
|
||||
});
|
||||
}
|
||||
|
||||
public async releaseAppointment(data: MetricsParams): Promise<void> {
|
||||
if (data.userId) {
|
||||
await PlanUsageCycleList.releaseAppointment({
|
||||
userId: data.userId,
|
||||
quantity: data.quantity,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public async addClient(data: MetricsParams): Promise<void> {
|
||||
await this.metrics.addClient(data);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user