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