Refactor code structure for improved readability and maintainability

This commit is contained in:
miguel
2025-06-18 12:41:30 -06:00
parent 6c85847bf4
commit 282f921e43
9 changed files with 46 additions and 19 deletions
@@ -102,13 +102,13 @@ export const ejemploCuestionarioTrabajador = {
validacion: TiposValidacion.COMUNIDAD_TRABAJADOR,
},
{
titulo: 'Número de trabajador',
titulo: 'RFC',
tipo: 'Abierta (Respuesta corta)',
obligatoria: false,
validacion: TiposValidacion.CUENTA_TRABAJADOR,
validacion: TiposValidacion.RFC,
},
{
titulo: 'Correo electrónico institucional',
titulo: 'Correo electrónico',
tipo: 'Abierta (Respuesta corta)',
obligatoria: true,
validacion: TiposValidacion.CORREO_INSTITUCIONAL,
@@ -450,7 +450,7 @@ export class CuestionarioRespondidoService {
return `This action removes a #${id} cuestionarioRespondido`;
}
async generarReporteRespuestas(idCuestionario: number): Promise<string> {
/* async generarReporteRespuestas(idCuestionario: number): Promise<string> {
const cuestionario = await this.cuestionarioRepository.findOne({
where: { id_cuestionario: idCuestionario },
relations: ['evento'],
@@ -543,9 +543,9 @@ export class CuestionarioRespondidoService {
}
return csv;
}
} */
/* async generarReporteRespuestas(idCuestionario: number): Promise<string> {
async generarReporteRespuestas(idCuestionario: number): Promise<string> {
// Buscar el cuestionario para validar que existe
const cuestionario = await this.cuestionarioRepository.findOne({
where: { id_cuestionario: idCuestionario },
@@ -586,12 +586,12 @@ export class CuestionarioRespondidoService {
pe.fecha_asistencia
FROM cuestionario_respondido cr
JOIN participante p ON cr.id_participante = p.id_participante
LEFT JOIN participante_evento pe ON p.id_participante = pe.id_participante AND pe.id_evento = ?
LEFT JOIN participante_evento pe ON p.id_participante = pe.id_participante AND pe.id_cuestionario = ?
WHERE cr.id_cuestionario = ?
ORDER BY cr.fecha_respuesta DESC
`,
[
cuestionario.evento ? cuestionario.evento.id_evento : null,
cuestionario.evento ? cuestionario.id_cuestionario : null,
idCuestionario,
],
);
@@ -711,5 +711,5 @@ export class CuestionarioRespondidoService {
.join('\n');
return csvContent;
} */
}
}
+5 -1
View File
@@ -8,9 +8,13 @@ export class UpdateEventoDto {
tipo_evento?: string;
@IsString()
@IsNotEmpty({ message: 'El nombre del evento es obligatorio' })
@IsOptional()
nombre_evento: string;
@IsString()
@IsOptional()
descripcion_evento: string;
@IsOptional()
@Type(() => Date)
@IsDate({ message: 'La fecha de inicio debe ser una fecha válida' })
+4 -1
View File
@@ -7,6 +7,7 @@ import {
ParseIntPipe,
Patch,
Post,
UnprocessableEntityException,
UploadedFile,
UseInterceptors,
} from '@nestjs/common';
@@ -87,7 +88,9 @@ export class EventoController {
@UploadedFile() file: Express.Multer.File,
) {
if (!file) {
throw new Error('No se ha recibido un archivo válido');
throw new UnprocessableEntityException(
'No se ha subido ningún archivo o el archivo no es válido.',
);
}
return this.eventoService.asociarBanner(id, file.filename);
+25 -6
View File
@@ -9,6 +9,8 @@ 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';
import { join } from 'path';
import { existsSync, unlinkSync } from 'fs';
@Injectable()
export class EventoService {
@@ -40,6 +42,26 @@ export class EventoService {
async asociarBanner(id: number, filename: string) {
const evento = await this.getEventoOrFail(id);
// Si ya tiene un banner anterior, eliminarlo
if (evento.banner) {
const bannerPath = join(
__dirname,
'..',
'..',
'uploads',
'banners',
evento.banner,
);
if (existsSync(bannerPath)) {
try {
unlinkSync(bannerPath);
} catch (err) {
console.error(`Error al eliminar el banner anterior: ${err.message}`);
}
}
}
// Asociar el nuevo banner
evento.banner = filename;
return this.eventoRepository.save(evento);
}
@@ -76,9 +98,9 @@ export class EventoService {
},
relations: ['cuestionarios'], // solo los cuestionarios, sin secciones
});
return eventos;
}
}
async getEventoOrFail(id_evento: number): Promise<Evento> {
const eventoFound = await this.eventoRepository.findOne({
@@ -125,10 +147,7 @@ export class EventoService {
});
}
async getCuestionarioEvento(
id_evento: number,
id_cuestionario: number,
) {
async getCuestionarioEvento(id_evento: number, id_cuestionario: number) {
const evento = await this.eventoRepository.findOne({
where: { id_evento },
relations: ['cuestionarios'],
@@ -72,7 +72,7 @@ export class ParticipanteEventoService {
id_participante,
id_cuestionario: id_evento,
},
relations: ['evento', 'participante'],
relations: ['participante'],
});
if (!participante_eventoFound) {
+1
View File
@@ -26,6 +26,7 @@ export enum TiposValidacion {
GENERO = 'genero',
CARRERA = 'carrera',
INSTITUCION = 'institucion',
RFC = 'rfc',
}
@Entity()
@@ -5,7 +5,7 @@ export class RegistroTrabajador {
@PrimaryGeneratedColumn()
id_trabajador: number;
@Column({ type: 'varchar', length: 20, unique: true })
@Column({ type: 'varchar', length: 20, unique: true, nullable: true })
numero_trabajador: string;
@Column({ type: 'varchar', length: 13, unique: true })

Before

Width:  |  Height:  |  Size: 74 KiB

After

Width:  |  Height:  |  Size: 74 KiB