import { isNull } from "../../helpers/IsNull"; import { PlansAdapterMongoose } from "./Plans.Adapter.Mongoose"; import { FindPlansParams, IPlan, IPlansManager, SysAdminUpdatePlanParams } from "./Plans.interface"; class PlansManager implements IPlansManager { plans: PlansAdapterMongoose; constructor() { this.plans = new PlansAdapterMongoose(); } private getOnePlan(plan: IPlan): IPlan { return { id: isNull(plan.id, ""), name: isNull(plan.name, ""), description: isNull(plan.description, ""), features: plan.features, code: isNull(plan.code, ""), price: isNull(plan.price, 0), annualPrice: isNull(plan.annualPrice, 0), limitOrganizations: isNull(plan.limitOrganizations, 1), limitEmployees: isNull(plan.limitEmployees, 1), limitServices: isNull(plan.limitServices, 1), limitAppointments: isNull(plan.limitAppointments, 1), limitClients: isNull(plan.limitClients, 1), limitRepeats: isNull(plan.limitRepeats, 1), mailNotifications: isNull(plan.mailNotifications, false), smsNotifications: isNull(plan.smsNotifications, false), wapNotifications: isNull(plan.wapNotifications, false), bot: isNull(plan.bot, false), active: isNull(plan.active, false), dateLimit: isNull(plan.dateLimit, false), mpPlanId: isNull(plan.mpPlanId, ""), payments: isNull(plan.payments, false), discount3Months: plan.discount3Months, discount6Months: plan.discount6Months, discount12Months: plan.discount12Months, featured: plan.featured, }; } public async find(data: FindPlansParams): Promise { 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 { const plan = await this.plans.findOne(data); if (!plan) { throw new Error("No se ha encontrado el plan"); } return this.getOnePlan(plan); } public async sysAdminUpdatePlan(data: SysAdminUpdatePlanParams): Promise { const { planId, ...planData } = data; await this.findOne({ _id: planId }); await this.plans.updateOne(planId, planData); } } const PlansList = new PlansManager(); export default PlansList;