129 lines
4.4 KiB
TypeScript
129 lines
4.4 KiB
TypeScript
import { BadRequestException, Injectable } from '@nestjs/common';
|
|
import { CreateCuestionarioPrograma2Dto } from './dto/create-cuestionario-programa2.dto';
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
|
import { CuestionarioPrograma } from 'src/cuestionario-programa/entities/cuestionario-programa.entity';
|
|
import { IsNull, 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';
|
|
import { convertArrayToCSV } from 'convert-array-to-csv';
|
|
import { ServicioService } from 'src/servicio/servicio.service';
|
|
import { Carrera } from 'src/carrera/entities/carrera.entity';
|
|
|
|
@Injectable()
|
|
export class CuestionarioPrograma2Service {
|
|
constructor(
|
|
@InjectRepository(CuestionarioPrograma)
|
|
private cuestionarioRepo: Repository<CuestionarioPrograma>,
|
|
@InjectRepository(CuestionarioPrograma2)
|
|
private cuestionario2Repo: Repository<CuestionarioPrograma2>,
|
|
@InjectRepository(Servicio)
|
|
private servicioRepository: Repository<Servicio>,
|
|
private archivoService: ArchivoService,
|
|
private validacionService: ValidacionService,
|
|
|
|
) { }
|
|
|
|
async create(cuestionarioPrograma2: CreateCuestionarioPrograma2Dto) {
|
|
let idServicio = cuestionarioPrograma2.idServicio;
|
|
let cuestionario = this.validacionService.validarCuestionarioPrograma2(
|
|
cuestionarioPrograma2,
|
|
);
|
|
|
|
let servicio = await this.servicioRepository.findOne({
|
|
where: { idServicio },
|
|
relations: ['cuestionarioPrograma2', 'status'],
|
|
});
|
|
if (!servicio) {
|
|
throw new BadRequestException('No existe este Servicio Social.');
|
|
}
|
|
|
|
if (servicio.cuestionarioPrograma2) {
|
|
throw new BadRequestException(
|
|
'Este Servicio Social ya cuenta con cuestionario de programa',
|
|
);
|
|
}
|
|
|
|
// Esta validacion esta incorrecta la cambie por la linea de arriva
|
|
// if (servicio.cuestionarioPrograma2.idCuestionarioPrograma2) {
|
|
// throw new BadRequestException(
|
|
// 'Este Servicio Social ya cuenta con cuestionario de alumno',
|
|
// );
|
|
// }
|
|
|
|
console.log('el servicio social si existe', servicio);
|
|
|
|
console.log('antes de create ques2');
|
|
if (messageByStatus.hasOwnProperty(servicio.status.idStatus))
|
|
throw new BadRequestException(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
|
|
|
|
console.log('registrar id en servicio en cuestionario 2');
|
|
|
|
let x = await this.servicioRepository.update(
|
|
{ idServicio },
|
|
{ cuestionarioPrograma2: respuestasRegistradas },
|
|
);
|
|
|
|
console.log(x);
|
|
await this.validacionService.validarPreTermino(idServicio)
|
|
return respuestasRegistradas;
|
|
}
|
|
|
|
async getCuestionario(dto: { version: string; anio: string }) {
|
|
const { version, anio } = dto;
|
|
const filePath = `server/uploads/${anio}_cuestionario_programa.csv`;
|
|
|
|
let registros: any[];
|
|
|
|
if (version === 'v1') {
|
|
registros = await this.cuestionarioRepo.find({
|
|
where: {
|
|
idCuestionarioPrograma: Not(IsNull()),
|
|
},
|
|
relations: ['carrera', 'servicio'],
|
|
select: {
|
|
servicio: {
|
|
profesor: true,
|
|
}
|
|
}
|
|
});
|
|
} else if (version === 'v2') {
|
|
registros = await this.cuestionario2Repo.find({
|
|
where: {
|
|
idCuestionarioPrograma2: Not(IsNull()),
|
|
},
|
|
relations: ['carrera', 'servicio'],
|
|
});
|
|
} else {
|
|
throw new BadRequestException('Versión inválida');
|
|
}
|
|
|
|
const data = registros.map( item => {
|
|
const { servicio, ...rest } = item;
|
|
return {
|
|
...rest,
|
|
responsable: servicio?.profesor || 'N/A',
|
|
carrera: item.carrera ? item.carrera.carrera : 'N/A',
|
|
}
|
|
} );
|
|
|
|
console.log("Informacion del reporte programa: ", data[20])
|
|
|
|
await this.archivoService.eliminarArchivo(filePath).catch((err) => {
|
|
console.log(err);
|
|
});
|
|
|
|
await this.archivoService.crearArchivo(filePath, convertArrayToCSV(data));
|
|
|
|
return filePath;
|
|
}
|
|
}
|