first commit
This commit is contained in:
@@ -0,0 +1,48 @@
|
||||
import { FindPlansParams, IPlansAdapter, IPlan } from "./Plans.interface";
|
||||
import { Document, Model, Schema, model } from "mongoose";
|
||||
|
||||
export interface IPlanDocument extends Omit<IPlan, "id">, Document {}
|
||||
|
||||
export class PlansAdapterMongoose implements IPlansAdapter {
|
||||
schema: Schema;
|
||||
planList: Model<IPlanDocument>;
|
||||
|
||||
constructor() {
|
||||
this.schema = new Schema({
|
||||
name: { type: String, required: true, unique: true },
|
||||
description: { type: String, required: true },
|
||||
features: { type: Array, required: true, default: [] },
|
||||
code: { type: String, required: true, unique: true },
|
||||
price: { type: Number, required: true },
|
||||
annualPrice: { type: Number, required: true },
|
||||
limitOrganizations: { type: Number, required: true },
|
||||
limitEmployees: { type: Number, required: true },
|
||||
limitServices: { type: Number, required: true },
|
||||
limitAppointments: { type: Number, required: true },
|
||||
limitClients: { type: Number, required: true },
|
||||
limitRepeats: { type: Number, required: true },
|
||||
mailNotifications: { type: Boolean, required: true },
|
||||
smsNotifications: { type: Boolean, required: true },
|
||||
wapNotifications: { type: Boolean, required: true },
|
||||
payments: { type: Boolean, required: false },
|
||||
bot: { type: Boolean, required: true },
|
||||
active: { type: Boolean, required: true },
|
||||
mpPlanId: { type: String, required: false },
|
||||
dateLimit: { type: Boolean, required: true },
|
||||
discount3Months: { type: Number, required: false },
|
||||
discount6Months: { type: Number, required: false },
|
||||
discount12Months: { type: Number, required: false },
|
||||
featured: { type: Boolean, required: false },
|
||||
});
|
||||
|
||||
this.planList = model<IPlanDocument>("Plan", this.schema);
|
||||
}
|
||||
|
||||
public async find(filters: FindPlansParams): Promise<IPlan[]> {
|
||||
return this.planList.find(filters).exec();
|
||||
}
|
||||
|
||||
public async findOne(filters: FindPlansParams): Promise<IPlanDocument | null> {
|
||||
return this.planList.findOne(filters).exec();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
export enum PlanFeatures {
|
||||
MAIL_NOTIFICATIONS = "mail_notifications",
|
||||
SMS_NOTIFICATIONS = "sms_notifications",
|
||||
WAP_NOTIFICATIONS = "wap_notifications",
|
||||
HAS_BOT = "has_bot",
|
||||
PAYMENTS = "payments",
|
||||
}
|
||||
|
||||
export type FindPlansParams = {
|
||||
_id?: string;
|
||||
code?: string;
|
||||
price?: number;
|
||||
};
|
||||
|
||||
export interface IPlan {
|
||||
id?: string;
|
||||
name: string;
|
||||
description: string;
|
||||
features: string[];
|
||||
code: string;
|
||||
price: number;
|
||||
annualPrice: number;
|
||||
limitOrganizations: number;
|
||||
limitEmployees: number;
|
||||
limitServices: number;
|
||||
limitAppointments: number; //Cantidad de turnos por día.
|
||||
limitClients: number;
|
||||
limitRepeats: number;
|
||||
mailNotifications: boolean;
|
||||
smsNotifications: boolean;
|
||||
wapNotifications: boolean;
|
||||
payments: boolean;
|
||||
bot: boolean;
|
||||
active: boolean; //Indica si es posible contratar el plan.
|
||||
dateLimit: boolean; //Indica se el plan tiene un limite de fecha.
|
||||
//Por ejemplo, el plan gratuito no tiene un limite de fecha.
|
||||
mpPlanId?: string;
|
||||
discount3Months?: number;
|
||||
discount6Months?: number;
|
||||
discount12Months?: number;
|
||||
featured?: boolean;
|
||||
}
|
||||
|
||||
export interface IPlansAdapter {
|
||||
find(filters: FindPlansParams): Promise<IPlan[]>;
|
||||
findOne(filters: FindPlansParams): Promise<IPlan | null>;
|
||||
}
|
||||
|
||||
export interface IPlansManager {
|
||||
plans: IPlansAdapter;
|
||||
find(data: FindPlansParams): Promise<IPlan[]>;
|
||||
findOne(data: FindPlansParams): Promise<IPlan>;
|
||||
}
|
||||
@@ -0,0 +1,66 @@
|
||||
import { isNull } from "../../helpers/IsNull";
|
||||
import { PlansAdapterMongoose } from "./Plans.Adapter.Mongoose";
|
||||
import { FindPlansParams, IPlan, IPlansManager } from "./Plans.interface";
|
||||
|
||||
class PlansManager implements IPlansManager {
|
||||
plans: PlansAdapterMongoose;
|
||||
|
||||
constructor() {
|
||||
this.plans = new PlansAdapterMongoose();
|
||||
}
|
||||
|
||||
private getOnePlan(plan: IPlan): IPlan {
|
||||
return {
|
||||
id: isNull<string>(plan.id, ""),
|
||||
name: isNull<string>(plan.name, ""),
|
||||
description: isNull<string>(plan.description, ""),
|
||||
features: plan.features,
|
||||
code: isNull<string>(plan.code, ""),
|
||||
price: isNull<number>(plan.price, 0),
|
||||
annualPrice: isNull<number>(plan.annualPrice, 0),
|
||||
limitOrganizations: isNull<number>(plan.limitOrganizations, 1),
|
||||
limitEmployees: isNull<number>(plan.limitEmployees, 1),
|
||||
limitServices: isNull<number>(plan.limitServices, 1),
|
||||
limitAppointments: isNull<number>(plan.limitAppointments, 1),
|
||||
limitClients: isNull<number>(plan.limitClients, 1),
|
||||
limitRepeats: isNull<number>(plan.limitRepeats, 1),
|
||||
mailNotifications: isNull<boolean>(plan.mailNotifications, false),
|
||||
smsNotifications: isNull<boolean>(plan.smsNotifications, false),
|
||||
wapNotifications: isNull<boolean>(plan.wapNotifications, false),
|
||||
bot: isNull<boolean>(plan.bot, false),
|
||||
active: isNull<boolean>(plan.active, false),
|
||||
dateLimit: isNull<boolean>(plan.dateLimit, false),
|
||||
mpPlanId: isNull<string>(plan.mpPlanId, ""),
|
||||
payments: isNull<boolean>(plan.payments, false),
|
||||
discount3Months: plan.discount3Months,
|
||||
discount6Months: plan.discount6Months,
|
||||
discount12Months: plan.discount12Months,
|
||||
featured: plan.featured,
|
||||
};
|
||||
}
|
||||
|
||||
public async find(data: FindPlansParams): Promise<IPlan[]> {
|
||||
const plans = await this.plans.find(data);
|
||||
const returnPlans: IPlan[] = [];
|
||||
|
||||
for (const plan of plans) {
|
||||
returnPlans.push(this.getOnePlan(plan));
|
||||
}
|
||||
|
||||
return returnPlans;
|
||||
}
|
||||
|
||||
public async findOne(data: FindPlansParams): Promise<IPlan> {
|
||||
const plan = await this.plans.findOne(data);
|
||||
|
||||
if (!plan) {
|
||||
throw new Error("No se ha encontrado el plan");
|
||||
}
|
||||
|
||||
return this.getOnePlan(plan);
|
||||
}
|
||||
}
|
||||
|
||||
const PlansList = new PlansManager();
|
||||
|
||||
export default PlansList;
|
||||
Reference in New Issue
Block a user