import { Injectable, NotFoundException } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { Repository } from 'typeorm'; import { CreateSubscriptionPlanDto } from './dto/create-subscription-plan.dto'; import { UpdateSubscriptionPlanDto } from './dto/update-subscription-plan.dto'; import { SubscriptionPlan } from './entities/subscription-plan.entity'; @Injectable() export class SubscriptionPlansService { constructor( @InjectRepository(SubscriptionPlan) private readonly plansRepository: Repository, ) {} create(dto: CreateSubscriptionPlanDto): Promise { const plan = this.plansRepository.create({ ...dto, price: String(dto.price), }); return this.plansRepository.save(plan); } findAll(activeOnly = false): Promise { return this.plansRepository.find({ where: activeOnly ? { isActive: true } : undefined, order: { durationMonths: 'ASC' }, }); } async findOne(id: string): Promise { const plan = await this.plansRepository.findOne({ where: { id } }); if (!plan) { throw new NotFoundException(`Subscription plan #${id} not found`); } return plan; } async findActiveById(id: string): Promise { const plan = await this.findOne(id); if (!plan.isActive) { throw new NotFoundException(`Subscription plan #${id} is not available`); } return plan; } async update( id: string, dto: UpdateSubscriptionPlanDto, ): Promise { const plan = await this.findOne(id); if (dto.price !== undefined) { plan.price = String(dto.price); } if (dto.name !== undefined) plan.name = dto.name; if (dto.code !== undefined) plan.code = dto.code; if (dto.durationMonths !== undefined) plan.durationMonths = dto.durationMonths; if (dto.freeSmsCount !== undefined) plan.freeSmsCount = dto.freeSmsCount; if (dto.isActive !== undefined) plan.isActive = dto.isActive; return this.plansRepository.save(plan); } async remove(id: string): Promise { const plan = await this.findOne(id); await this.plansRepository.remove(plan); } }