first commit

This commit is contained in:
2026-07-16 20:48:43 -03:00
commit 5696cee264
1111 changed files with 322270 additions and 0 deletions
@@ -0,0 +1,163 @@
import { Document, Model, Schema, model } from "mongoose";
import { IMetrics, IMetricsAdapter, MetricsParams } from "./Metrics.Interface";
import dayjs from "dayjs";
import { isNull } from "../../helpers/IsNull";
export interface IMetricsDocument extends Omit<IMetrics, "id">, Document {}
export class MetricsAdapterMongoose implements IMetricsAdapter {
schema: Schema;
metricsList: Model<IMetricsDocument>;
constructor() {
this.schema = new Schema({
userId: { type: Schema.Types.ObjectId, required: true, ref: "User" },
organizationsCount: { type: Number, required: true },
employeesCount: { type: Number, required: true },
servicesCount: { type: Number, required: true },
appointmentsCount: { type: Number, required: true },
clientsCount: { type: Number, required: true },
repeatsCount: { type: Number, required: false },
month: { type: Number, required: true },
year: { type: Number, required: true },
creationDate: { type: Date, required: true, default: Date.now },
});
this.metricsList = model<IMetricsDocument>("Metric", this.schema);
}
private getDate(): { month: number; year: number } {
const dateNow = dayjs(new Date());
const month = dateNow.month();
const year = dateNow.year();
return { month, year };
}
public async create(userId: string): Promise<IMetricsDocument> {
const { month, year } = this.getDate();
return await this.metricsList.create({
userId,
month,
year,
organizationsCount: 0,
employeesCount: 0,
servicesCount: 0,
appointmentsCount: 0,
clientsCount: 0,
repeatsCount: 0,
});
}
public async reset(userId: string): Promise<IMetricsDocument> {
const { month, year } = this.getDate();
let metrics: IMetricsDocument | null = await this.metricsList.findOne({ userId });
if (metrics) {
metrics.appointmentsCount = 0;
// metrics.organizationsCount = 0;
// metrics.employeesCount = 0;
// metrics.servicesCount = 0;
// metrics.clientsCount = 0;
// metrics.repeatsCount = 0;
metrics.year = year;
metrics.month = month;
await metrics.save();
}
if (!metrics) {
metrics = await this.create(userId);
}
return metrics;
}
public async getMetrics(userId: string): Promise<IMetricsDocument> {
let metrics: IMetricsDocument | null = await this.metricsList.findOne({ userId });
if (!metrics) {
metrics = await this.create(userId);
}
return metrics;
}
public async addOrganization(data: MetricsParams): Promise<void> {
const { month, year } = this.getDate();
if (!data.userId) {
return;
}
let metrics = await this.getMetrics(data.userId);
if (metrics.year !== year || metrics.month !== month) {
metrics = await this.reset(data.userId);
}
metrics.organizationsCount += isNull<number>(data.quantity, 1);
await metrics.save();
}
public async addEmployee(data: MetricsParams): Promise<void> {
const { month, year } = this.getDate();
if (!data.userId) {
return;
}
let metrics = await this.getMetrics(data.userId);
if (metrics.year !== year || metrics.month !== month) {
metrics = await this.reset(data.userId);
}
metrics.employeesCount += isNull<number>(data.quantity, 1);
await metrics.save();
}
public async addService(data: MetricsParams): Promise<void> {
const { month, year } = this.getDate();
if (!data.userId) {
return;
}
let metrics = await this.getMetrics(data.userId);
if (metrics.year !== year || metrics.month !== month) {
metrics = await this.reset(data.userId);
}
metrics.servicesCount += isNull<number>(data.quantity, 1);
await metrics.save();
}
public async addAppointment(data: MetricsParams): Promise<void> {
const { month, year } = this.getDate();
if (!data.userId) {
return;
}
let metrics = await this.getMetrics(data.userId);
if (metrics.year !== year || metrics.month !== month) {
metrics = await this.reset(data.userId);
}
metrics.appointmentsCount += isNull<number>(data.quantity, 1);
await metrics.save();
}
public async addClient(data: MetricsParams): Promise<void> {
const { month, year } = this.getDate();
if (!data.userId) {
return;
}
let metrics = await this.getMetrics(data.userId);
if (metrics.year !== year || metrics.month !== month) {
metrics = await this.reset(data.userId);
}
metrics.clientsCount += isNull<number>(data.quantity, 1);
await metrics.save();
}
public async addRepeat(data: MetricsParams): Promise<void> {
const { month, year } = this.getDate();
if (!data.userId) {
return;
}
let metrics = await this.getMetrics(data.userId);
if (metrics.year !== year || metrics.month !== month) {
metrics = await this.reset(data.userId);
}
if (!metrics.repeatsCount) {
metrics.repeatsCount = 0;
}
metrics.repeatsCount += isNull<number>(data.quantity, 1);
await metrics.save();
}
}
@@ -0,0 +1,56 @@
import { IMetricsDocument } from "./Metrics.Adapter.Mongoose";
export type MetricsParams = {
userId?: string;
quantity?: number;
};
export type DeleteMetricsByCompany = {
companyId: string;
};
export type CalculateMetricsParams = {
userId: string;
};
export interface IMetrics {
id?: string;
userId: string;
organizationsCount: number;
employeesCount: number;
servicesCount: number;
appointmentsCount: number;
clientsCount: number;
repeatsCount: number;
month: number;
year: number;
}
export interface IMetricsAdapter {
create(userId: string): Promise<IMetricsDocument>;
reset(userId: string): Promise<IMetricsDocument>;
getMetrics(userId: string): Promise<IMetricsDocument>;
addOrganization(data: MetricsParams): Promise<void>;
addEmployee(data: MetricsParams): Promise<void>;
addService(data: MetricsParams): Promise<void>;
addAppointment(data: MetricsParams): Promise<void>;
addClient(data: MetricsParams): Promise<void>;
}
export interface IMetricsManager {
metrics: IMetricsAdapter;
reset(userId: string): Promise<void>;
getMetrics(userId: string): Promise<IMetricsDocument>;
addOrganization(data: MetricsParams): Promise<void>;
addEmployee(data: MetricsParams): Promise<void>;
addService(data: MetricsParams): Promise<void>;
addAppointment(data: MetricsParams): Promise<void>;
addClient(data: MetricsParams): Promise<void>;
canAddOrganization(userId: string): Promise<boolean>;
canAddEmployee(userId: string): Promise<boolean>;
canAddService(userId: string): Promise<boolean>;
canAddAppointment(userId: string): Promise<boolean>;
canAddClient(userId: string): Promise<boolean>;
deleteMetricsByCompany(data: DeleteMetricsByCompany): Promise<void>;
calculateMetrics(data: CalculateMetricsParams): Promise<void>;
}
+268
View File
@@ -0,0 +1,268 @@
import {
CalculateMetricsParams,
DeleteMetricsByCompany,
IMetricsManager,
MetricsParams,
} from "./Metrics.Interface";
import { IMetricsDocument, MetricsAdapterMongoose } from "./Metrics.Adapter.Mongoose";
import PlansList from "../../Models/Plans/Plans";
import PlanSubscriptionsList from "../../Models/PlanSubscriptions/PlanSubscriptons";
import CompaniesList from "../Companies/Companies";
import EmployeesList from "../Employees/Employee";
import ServiceList from "../Services/Service";
import AppointmentList from "../Appointments/Appointments";
import ClientsList from "../Clients/Clients";
import RepeatsList from "../Repeats/Repeats";
import { isNull } from "../../helpers/IsNull";
class MetricsManager implements IMetricsManager {
metrics: MetricsAdapterMongoose;
constructor() {
this.metrics = new MetricsAdapterMongoose();
}
public async calculateMetrics(data: CalculateMetricsParams): Promise<void> {
//TODO: calcular las metricas...
const metrics = await this.getMetrics(data.userId);
const organizations = await CompaniesList.companies.find({
ownerId: data.userId,
});
let employeesLength = 0;
let servicesLength = 0;
let appointmentsLength = 0;
let clientsLength = 0;
let repeatsLength = 0;
for (const org of organizations) {
const employees = await EmployeesList.employees.find({
companyId: String(org.id),
});
employeesLength += employees.length;
const services = await ServiceList.services.find({
companyId: String(org.id),
});
servicesLength += services.length;
//TODO: Revisar si el parametro del mes esta bien o necesita incrementar en uno.
//ya que no recuerdo si empieza en 0 o en 1.
appointmentsLength += await AppointmentList.countAppointmentsByMonth({
year: metrics.year,
month: metrics.month,
companyId: String(org.id),
});
const clients = await ClientsList.clients.find({
companyId: String(org.id),
});
clientsLength += clients.length;
const repeats = await RepeatsList.repeats.find({
companyId: String(org.id),
});
repeatsLength += repeats.length;
}
metrics.organizationsCount = organizations.length;
metrics.employeesCount = employeesLength;
metrics.servicesCount = servicesLength;
metrics.appointmentsCount = appointmentsLength;
metrics.clientsCount = clientsLength;
metrics.repeatsCount = repeatsLength;
await metrics.save();
}
public async deleteMetricsByCompany(data: DeleteMetricsByCompany): Promise<void> {
const companyCheck = await CompaniesList.companies.findOne({ _id: data.companyId });
if (!companyCheck) {
throw new Error("La compañia no existe");
}
await this.metrics.reset(String(companyCheck.ownerId));
//await this.metrics.metricsList.deleteMany({ userId: String(companyCheck.ownerId) });
}
public async reset(userId: string): Promise<void> {
await this.metrics.reset(userId);
}
public async getMetrics(userId: string): Promise<IMetricsDocument> {
return await this.metrics.getMetrics(userId);
}
public async canAddOrganization(userId: string): Promise<boolean> {
const subscription = await PlanSubscriptionsList.findOne({
sessionUser: userId,
});
if (!subscription) {
return false;
}
const plan = await PlansList.plans.findOne({
_id: subscription.planId,
});
if (!plan) {
return false;
}
if (plan.limitOrganizations < 0) {
return true;
}
const metrics = await this.getMetrics(userId);
if (metrics.organizationsCount >= plan.limitOrganizations) {
return false;
}
return true;
}
public async canAddService(userId: string): Promise<boolean> {
const subscription = await PlanSubscriptionsList.findOne({
sessionUser: userId,
});
if (!subscription) {
return false;
}
const plan = await PlansList.plans.findOne({
_id: subscription.planId,
});
if (!plan) {
return false;
}
if (plan.limitServices < 0) {
return true;
}
const metrics = await this.getMetrics(userId);
if (metrics.servicesCount >= plan.limitServices) {
return false;
}
return true;
}
public async canAddEmployee(userId: string): Promise<boolean> {
const subscription = await PlanSubscriptionsList.findOne({
sessionUser: userId,
});
if (!subscription) {
return false;
}
const plan = await PlansList.plans.findOne({
_id: subscription.planId,
});
if (!plan) {
return false;
}
if (plan.limitEmployees < 0) {
return true;
}
const metrics = await this.getMetrics(userId);
if (metrics.employeesCount >= plan.limitEmployees) {
return false;
}
return true;
}
public async canAddClient(userId: string): Promise<boolean> {
const subscription = await PlanSubscriptionsList.findOne({
sessionUser: userId,
});
if (!subscription) {
return false;
}
const plan = await PlansList.plans.findOne({
_id: subscription.planId,
});
if (!plan) {
return false;
}
if (plan.limitClients < 0) {
return true;
}
const metrics = await this.getMetrics(userId);
if (metrics.clientsCount >= plan.limitClients) {
return false;
}
return true;
}
public async canAddRepeat(userId: string): Promise<boolean> {
const subscription = await PlanSubscriptionsList.findOne({
sessionUser: userId,
});
if (!subscription) {
return false;
}
const plan = await PlansList.plans.findOne({
_id: subscription.planId,
});
if (!plan) {
return false;
}
if (plan.limitRepeats < 0) {
return true;
}
const metrics = await this.getMetrics(userId);
if (isNull<number>(metrics.repeatsCount, 0) >= plan.limitRepeats) {
return false;
}
return true;
}
public async canAddAppointment(userId: string): Promise<boolean> {
const subscription = await PlanSubscriptionsList.findOne({
sessionUser: userId,
});
if (!subscription) {
return false;
}
const plan = await PlansList.plans.findOne({
_id: subscription.planId,
});
if (!plan) {
return false;
}
if (plan.limitAppointments < 0) {
return true;
}
const metrics = await this.getMetrics(userId);
if (metrics.appointmentsCount >= plan.limitAppointments) {
return false;
}
return true;
}
public async addAppointment(data: MetricsParams): Promise<void> {
await this.metrics.addAppointment(data);
}
public async addClient(data: MetricsParams): Promise<void> {
await this.metrics.addClient(data);
}
public async addRepeat(data: MetricsParams): Promise<void> {
await this.metrics.addRepeat(data);
}
public async addEmployee(data: MetricsParams): Promise<void> {
await this.metrics.addEmployee(data);
}
public async addOrganization(data: MetricsParams): Promise<void> {
await this.metrics.addOrganization(data);
}
public async addService(data: MetricsParams): Promise<void> {
await this.metrics.addService(data);
}
}
const MetricsList = new MetricsManager();
export default MetricsList;