Add 'recientes' endpoints for cuestionarios and eventos with enhanced data retrieval
This commit is contained in:
@@ -11,10 +11,7 @@ import {
|
||||
UploadedFile,
|
||||
} from '@nestjs/common';
|
||||
import { CuestionarioService } from './cuestionario.service';
|
||||
import {
|
||||
CreateCuestionarioDto,
|
||||
CreateCuestionarioEventoDto,
|
||||
} from './dto/create-cuestionario.dto';
|
||||
import { CreateCuestionarioDto } from './dto/create-cuestionario.dto';
|
||||
import { UpdateCuestionarioDto } from './dto/update-cuestionario.dto';
|
||||
import { FileInterceptor } from '@nestjs/platform-express';
|
||||
import { diskStorage } from 'multer';
|
||||
@@ -83,6 +80,12 @@ export class CuestionarioController {
|
||||
return this.cuestionarioService.findAll();
|
||||
}
|
||||
|
||||
@Get('recientes')
|
||||
@CuestionarioApiDocumentation.ApiGetAll
|
||||
findAllRecientes() {
|
||||
return this.cuestionarioService.findAllRecientes();
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@CuestionarioApiDocumentation.ApiGetOne
|
||||
findOne(@Param('id') id: string) {
|
||||
|
||||
@@ -4,7 +4,7 @@ import {
|
||||
NotFoundException,
|
||||
} from '@nestjs/common';
|
||||
import { InjectRepository } from '@nestjs/typeorm';
|
||||
import { Repository, DataSource, In } from 'typeorm';
|
||||
import { Repository, DataSource, In, MoreThan } from 'typeorm';
|
||||
import {
|
||||
CreateCuestionarioDto,
|
||||
CreateCuestionarioEventoDto,
|
||||
@@ -184,6 +184,20 @@ export class CuestionarioService {
|
||||
}
|
||||
}
|
||||
|
||||
async findAllRecientes() {
|
||||
console.log('Buscando cuestionarios recientes');
|
||||
const now = new Date();
|
||||
const pastDate = new Date();
|
||||
pastDate.setDate(now.getDate() - 30);
|
||||
|
||||
return this.cuestionarioRepository.find({
|
||||
where: {
|
||||
fecha_inicio: MoreThan(pastDate),
|
||||
},
|
||||
order: { fecha_fin: 'DESC' },
|
||||
});
|
||||
}
|
||||
|
||||
async createCuestionarioEvento(
|
||||
createCuestionarioDto: CreateEventoWithCuestionarioDto,
|
||||
) {
|
||||
|
||||
@@ -41,9 +41,9 @@ export class EventoController {
|
||||
return this.eventoService.getEventosActivosCuestionarios();
|
||||
}
|
||||
|
||||
@Get('recientes')
|
||||
getEventosRecientes(): Promise<Evento[]> {
|
||||
return this.eventoService.getEventosRecientes();
|
||||
@Get('recientes/cuestionarios')
|
||||
getEventosRecientes() {
|
||||
return this.eventoService.getEventosRecientesCuestionarios();
|
||||
}
|
||||
|
||||
@Get('activos')
|
||||
|
||||
@@ -186,19 +186,40 @@ export class EventoService {
|
||||
return this.eventoRepository.save(updateEvento);
|
||||
}
|
||||
|
||||
async getEventosRecientes(): Promise<Evento[]> {
|
||||
const now = new Date();
|
||||
const pastDate = new Date();
|
||||
pastDate.setDate(now.getDate() - 30); // Últimos 30 días
|
||||
async getEventosRecientesCuestionarios(): Promise<
|
||||
EventoConCuestionariosDto[]
|
||||
> {
|
||||
const ahora = new Date();
|
||||
const hace30Dias = new Date();
|
||||
hace30Dias.setDate(ahora.getDate() - 30);
|
||||
|
||||
return this.eventoRepository.find({
|
||||
const eventos = await this.eventoRepository.find({
|
||||
where: {
|
||||
fecha_inicio: MoreThan(pastDate),
|
||||
fecha_fin: MoreThan(hace30Dias),
|
||||
},
|
||||
order: {
|
||||
fecha_inicio: 'DESC',
|
||||
},
|
||||
take: 10, // Limitar a los 10 eventos más recientes
|
||||
relations: ['cuestionarios', 'cuestionarios.cuestionariosRespondidos'],
|
||||
});
|
||||
|
||||
const eventosConCupos = eventos.map((evento) => {
|
||||
return {
|
||||
...evento,
|
||||
cuestionarios: evento.cuestionarios.map((cuest) => {
|
||||
const cupoMaximo = cuest.cupo_maximo ?? null;
|
||||
const usados = cuest.cuestionariosRespondidos?.length || 0;
|
||||
const disponibles =
|
||||
cupoMaximo !== null ? Math.max(0, cupoMaximo - usados) : null;
|
||||
|
||||
return {
|
||||
...cuest,
|
||||
cupos_usados: usados,
|
||||
cupos_disponibles: disponibles,
|
||||
};
|
||||
}),
|
||||
};
|
||||
});
|
||||
|
||||
return plainToInstance(EventoConCuestionariosDto, eventosConCupos, {
|
||||
excludeExtraneousValues: true,
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user