import { HttpException, HttpStatus, Injectable, NotFoundException, } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { MoreThan, Repository } from 'typeorm'; import { Evento } from './entities/evento.entity'; import { CreateEventoDto } from './dto/create-evento.dto'; import { UpdateEventoDto } from './dto/update.evento.dto'; @Injectable() export class EventoService { constructor( @InjectRepository(Evento) private eventoRepository: Repository, ) {} async createEvento(evento: CreateEventoDto) { const eventoFound = await this.eventoRepository.findOne({ where: { nombre_evento: evento.nombre_evento, tipo_evento: evento.tipo_evento, }, }); if (eventoFound) throw new HttpException( 'Ya existe un evento con el mismo nombre y tipo, puede agregar el periodo al nombre del evento para diferenciarlos', HttpStatus.BAD_REQUEST, ); const createEvento = this.eventoRepository.create(evento); return this.eventoRepository.save(createEvento); } // evento.service.ts async asociarBanner(id: number, filename: string) { const evento = await this.getEventoOrFail(id); evento.banner = filename; return this.eventoRepository.save(evento); } getEventos() { return this.eventoRepository.find(); } async getCuestionariosEvento(id_evento: number) { const evento = await this.eventoRepository.findOne({ where: { id_evento }, relations: ['cuestionarios'], // solo los cuestionarios, sin secciones }); if (!evento) { throw new NotFoundException(`Evento con ID ${id_evento} no encontrado.`); } return evento; } async getEvento(id_evento: number) { return await this.eventoRepository.findOne({ where: { id_evento, }, }); } async getEventosActivosCuestionarios() { const eventos = await this.eventoRepository.find({ where: { fecha_fin: MoreThan(new Date()), }, relations: ['cuestionarios'], // solo los cuestionarios, sin secciones }); return eventos; } async getEventoOrFail(id_evento: number): Promise { const eventoFound = await this.eventoRepository.findOne({ where: { id_evento, }, }); if (!eventoFound) { throw new HttpException( 'El evento buscado no existe', HttpStatus.NOT_FOUND, ); } return eventoFound; } async deleteEvento(id_evento: number) { await this.getEventoOrFail(id_evento); const result = await this.eventoRepository.delete(id_evento); if (result.affected === 0) { throw new HttpException('Evento not found', HttpStatus.NOT_FOUND); } return { message: 'Evento eliminado exitosamente' }; } async updateEvento(id_evento: number, evento: UpdateEventoDto) { const eventoFound = await this.getEventoOrFail(id_evento); const updateEvento = Object.assign(eventoFound, evento); return this.eventoRepository.save(updateEvento); } async getEventosActivos() { const now = new Date(); return this.eventoRepository.find({ where: { fecha_fin: MoreThan(now), }, }); } async getCuestionarioEvento( id_evento: number, id_cuestionario: number, ) { const evento = await this.eventoRepository.findOne({ where: { id_evento }, relations: ['cuestionarios'], }); if (!evento) { throw new NotFoundException(`Evento con ID ${id_evento} no encontrado.`); } const cuestionario = evento.cuestionarios.find( (c) => c.id_cuestionario === id_cuestionario, ); if (!cuestionario) { throw new NotFoundException( `Cuestionario no asignado al evento ${evento.nombre_evento}.`, ); } // Return evento info, but only with the searched cuestionario return { ...evento, cuestionarios: undefined, cuestionario: cuestionario, }; } }