Files
formularios_api/src/evento/evento.service.ts
T

159 lines
3.9 KiB
TypeScript
Raw Normal View History

2025-06-13 22:41:20 -06:00
import {
HttpException,
HttpStatus,
Injectable,
NotFoundException,
} from '@nestjs/common';
2025-04-01 15:07:11 -06:00
import { InjectRepository } from '@nestjs/typeorm';
2025-06-13 22:41:20 -06:00
import { MoreThan, Repository } from 'typeorm';
import { Evento } from './entities/evento.entity';
2025-04-01 15:07:11 -06:00
import { CreateEventoDto } from './dto/create-evento.dto';
import { UpdateEventoDto } from './dto/update.evento.dto';
@Injectable()
export class EventoService {
2025-06-13 22:41:20 -06:00
constructor(
@InjectRepository(Evento) private eventoRepository: Repository<Evento>,
) {}
2025-04-01 15:07:11 -06:00
2025-06-13 22:41:20 -06:00
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);
2025-04-01 15:07:11 -06:00
2025-06-13 22:41:20 -06:00
evento.banner = filename;
return this.eventoRepository.save(evento);
}
2025-04-01 15:07:11 -06:00
2025-06-13 22:41:20 -06:00
getEventos() {
return this.eventoRepository.find();
}
2025-04-01 15:07:11 -06:00
2025-06-13 22:41:20 -06:00
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.`);
2025-04-01 15:07:11 -06:00
}
2025-06-13 22:41:20 -06:00
return evento;
}
async getEvento(id_evento: number) {
return await this.eventoRepository.findOne({
where: {
id_evento,
},
});
}
2025-04-01 15:07:11 -06:00
async getEventosActivosCuestionarios() {
const eventos = await this.eventoRepository.find({
where: {
fecha_fin: MoreThan(new Date()),
},
relations: ['cuestionarios'], // solo los cuestionarios, sin secciones
});
return eventos;
}
2025-06-13 22:41:20 -06:00
async getEventoOrFail(id_evento: number): Promise<Evento> {
const eventoFound = await this.eventoRepository.findOne({
where: {
id_evento,
},
});
2025-04-01 15:07:11 -06:00
2025-06-13 22:41:20 -06:00
if (!eventoFound) {
throw new HttpException(
'El evento buscado no existe',
HttpStatus.NOT_FOUND,
);
2025-04-01 15:07:11 -06:00
}
2025-06-13 22:41:20 -06:00
return eventoFound;
}
2025-04-01 15:07:11 -06:00
2025-06-13 22:41:20 -06:00
async deleteEvento(id_evento: number) {
await this.getEventoOrFail(id_evento);
2025-04-01 15:07:11 -06:00
2025-06-13 22:41:20 -06:00
const result = await this.eventoRepository.delete(id_evento);
if (result.affected === 0) {
throw new HttpException('Evento not found', HttpStatus.NOT_FOUND);
2025-04-01 15:07:11 -06:00
}
2025-06-13 22:41:20 -06:00
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,
};
}
2025-04-01 15:07:11 -06:00
}