2025-05-26 15:29:47 -06:00
|
|
|
import { BadRequestException, Injectable } from '@nestjs/common';
|
2025-05-20 15:53:57 -06:00
|
|
|
import { CreateCuestionarioPrograma2Dto } from './dto/create-cuestionario-programa2.dto';
|
2025-05-26 15:29:47 -06:00
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
|
|
|
import { CuestionarioPrograma } from 'src/cuestionario-programa/entities/cuestionario-programa.entity';
|
|
|
|
|
import { Not, Repository } from 'typeorm';
|
|
|
|
|
import { CuestionarioPrograma2 } from './entities/cuestionario-programa2.entity';
|
|
|
|
|
import { ArchivoService } from 'src/helpers.services/archivo.service';
|
|
|
|
|
import { ValidacionService } from 'src/helpers.services/validacion.service';
|
|
|
|
|
import { Servicio } from 'src/servicio/entities/servicio.entity';
|
|
|
|
|
import { messageByStatus } from 'src/messageByStatus';
|
2025-10-16 14:04:30 -06:00
|
|
|
import { convertArrayToCSV } from 'convert-array-to-csv';
|
2025-05-20 15:53:57 -06:00
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export class CuestionarioPrograma2Service {
|
2025-05-26 15:29:47 -06:00
|
|
|
constructor(
|
|
|
|
|
@InjectRepository(CuestionarioPrograma)
|
|
|
|
|
private cuestionarioRepo: Repository<CuestionarioPrograma>,
|
|
|
|
|
@InjectRepository(CuestionarioPrograma2)
|
|
|
|
|
private cuestionario2Repo: Repository<CuestionarioPrograma2>,
|
|
|
|
|
@InjectRepository(Servicio)
|
2025-10-10 11:55:16 -06:00
|
|
|
private servicioRepository: Repository<Servicio>,
|
|
|
|
|
private archivoService: ArchivoService,
|
|
|
|
|
private validacionService: ValidacionService,
|
2025-05-26 15:29:47 -06:00
|
|
|
) {}
|
2025-10-10 11:55:16 -06:00
|
|
|
|
2025-05-26 15:29:47 -06:00
|
|
|
async create(cuestionarioPrograma2: CreateCuestionarioPrograma2Dto) {
|
|
|
|
|
let idServicio = cuestionarioPrograma2.idServicio;
|
2025-10-10 11:55:16 -06:00
|
|
|
let cuestionario = this.validacionService.validarCuestionarioPrograma2(
|
|
|
|
|
cuestionarioPrograma2,
|
|
|
|
|
);
|
2025-05-26 15:29:47 -06:00
|
|
|
|
2025-10-10 11:55:16 -06:00
|
|
|
let servicio = await this.servicioRepository.findOne({
|
|
|
|
|
where: { idServicio },
|
|
|
|
|
});
|
|
|
|
|
if (!servicio) {
|
2025-05-26 15:29:47 -06:00
|
|
|
throw new Error('No existe este Servicio Social.');
|
|
|
|
|
}
|
2025-10-10 11:55:16 -06:00
|
|
|
if (servicio.cuestionarioPrograma2.idCuestionarioPrograma2) {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'Este Servicio Social ya cuenta con cuestionario de alumno',
|
|
|
|
|
);
|
2025-05-26 15:29:47 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('el servicio social si existe', servicio);
|
|
|
|
|
|
|
|
|
|
console.log('antes de create ques2');
|
|
|
|
|
if (messageByStatus.hasOwnProperty(servicio.status.idStatus))
|
|
|
|
|
throw new Error(messageByStatus[servicio.status.idStatus]);
|
|
|
|
|
|
|
|
|
|
console.log('despues de registrar cuestionario', cuestionario);
|
|
|
|
|
|
|
|
|
|
let respuestasRegistradas = await this.cuestionario2Repo.save(cuestionario);
|
|
|
|
|
|
|
|
|
|
// si el cuestionario ya existe solo hay que actualizar el id de cuestionarioprograma o alumno
|
|
|
|
|
|
2025-10-10 11:55:16 -06:00
|
|
|
console.log('registrar id en servicio en cuestionario 2');
|
2025-05-26 15:29:47 -06:00
|
|
|
|
|
|
|
|
let x = await this.servicioRepository.update(
|
|
|
|
|
{ idServicio },
|
2025-10-10 11:55:16 -06:00
|
|
|
{ cuestionarioPrograma2: respuestasRegistradas },
|
|
|
|
|
);
|
2025-05-26 15:29:47 -06:00
|
|
|
|
|
|
|
|
console.log(x);
|
|
|
|
|
|
|
|
|
|
return respuestasRegistradas;
|
2025-05-20 15:53:57 -06:00
|
|
|
}
|
|
|
|
|
|
2025-10-10 11:55:16 -06:00
|
|
|
async getCuestionario(dto: { version: string; year: string }) {
|
2025-05-26 15:29:47 -06:00
|
|
|
const { version, year } = dto;
|
|
|
|
|
const filePath = `server/uploads/${year}_cuestionario_programa.csv`;
|
|
|
|
|
|
|
|
|
|
let registros: any[];
|
|
|
|
|
|
|
|
|
|
if (version === 'v1') {
|
|
|
|
|
registros = await this.cuestionarioRepo.find();
|
|
|
|
|
} else if (version === 'v2') {
|
|
|
|
|
registros = await this.cuestionario2Repo.find();
|
|
|
|
|
} else {
|
|
|
|
|
throw new BadRequestException('Versión inválida');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const data = registros.map((item) => ({ ...item }));
|
|
|
|
|
|
|
|
|
|
await this.archivoService.eliminarArchivo(filePath).catch((err) => {
|
|
|
|
|
console.log(err);
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return this.archivoService.crearArchivo(filePath, convertArrayToCSV(data));
|
2025-05-20 15:53:57 -06:00
|
|
|
}
|
|
|
|
|
}
|