diff --git a/src/programa/programa.service.ts b/src/programa/programa.service.ts index e7fd150..5519085 100644 --- a/src/programa/programa.service.ts +++ b/src/programa/programa.service.ts @@ -234,56 +234,28 @@ export class ProgramaService { correoOtroResponsable ); - const user= await this.usuarioRepo.findOne({where:{idUsuario:idusuario}}) + let usuarioNuevo= await this.usuarioRepo.findOne({where:{idUsuario:idusuario}}) - if (!user) throw new Error('No existe este usuario.'); - if (user.tipoUsuario.idTipoUsuario != 2) throw new Error('No es un usuario de tipo responsable'); - if (user.usuario === correoOtroResponsable) throw new Error('Son el mismo usuario.'); + if (!usuarioNuevo) throw new Error('No existe este usuario.'); + if (usuarioNuevo.tipoUsuario.idTipoUsuario != 2) throw new Error('No es un usuario de tipo responsable'); + if (usuarioNuevo.usuario === correoOtroResponsable) throw new Error('Son el mismo usuario.'); - const nuevoResponsable=await this.usuarioRepo.findOne({where:{usuario:correoOtroResponsable}}) + let viejoResponsable=await this.usuarioRepo.findOne({where:{usuario:otroResponsable}}) + + if (!viejoResponsable) throw new Error('No existe este usuario.'); + + await this.programaRepo.update( + { usuario:viejoResponsable }, + { usuario:usuarioNuevo } + ); + + // Eliminar usuario antiguo + await this.usuarioRepo.delete({idUsuario:viejoResponsable.idUsuario}); - - + + return { + message: `Se eliminó correctamente al usuario ${correoOtroResponsable} y se reasignaron sus programas.`, + } } - - const reasignarProgramas = async (body) => { - const idUsuario = validar.validarNumeroEntero(body.idUsuario, 'id usuario'); - const correoOtroResponsable = validar.validarCorreo( - body.correoOtroResponsable - ); - let otorResponsable = {}; - - return Usuario.findOne({ - where: { idUsuario }, - }) - .then((res) => { - if (!res) throw new Error('No existe este usuario.'); - if (res.idTipoUsuario != 2) - throw new Error('No es un usuario de tipo responsable'); - if (res.usuario === correoOtroResponsable) - throw new Error('Son el mismo usuario.'); - return Usuario.findOne({ where: { usuario: correoOtroResponsable } }); - }) - .then((res) => { - if (!res) throw new Error('No existe este usuario.'); - otorResponsable = res; - return Programa.update( - { idUsuario }, - { - where: { idUsuario: otorResponsable.idUsuario }, - } - ); - }) - .then((res) => - Usuario.destroy({ where: { idUsuario: otorResponsable.idUsuario } }) - ) - .then((res) => ({ - message: `Se elimino correctamente al usuario ${correoOtroResponsable} y se reasigno correctamente sus programas.`, - })); - }; - - - - } diff --git a/src/servicio/servicio.service.ts b/src/servicio/servicio.service.ts index 7d7bd27..0325936 100644 --- a/src/servicio/servicio.service.ts +++ b/src/servicio/servicio.service.ts @@ -1,26 +1,233 @@ -import { Injectable } from '@nestjs/common'; +import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common'; import { CreateServicioDto } from './dto/create-servicio.dto'; import { UpdateServicioDto } from './dto/update-servicio.dto'; +import { InjectRepository } from '@nestjs/typeorm'; +import { Repository } from 'typeorm'; +import { Servicio } from './entities/servicio.entity'; +import { Usuario } from 'src/usuario/entities/usuario.entity'; +import { ValidacionService } from 'src/helpers.services/validacion.service'; +import { gmail } from 'src/helpers.services/gmail.service'; +import { SendCorreoDto } from 'src/helpers.services/dto/send-email.dto'; @Injectable() export class ServicioService { - create(createServicioDto: CreateServicioDto) { - return 'This action adds a new servicio'; + constructor( + @InjectRepository(Servicio) + private servicioRepo:Repository, + + @InjectRepository(Usuario) + private usuarioRepo:Repository, + + private validacionService:ValidacionService, + + private gmailService:gmail + ){} + + async obtenerServicioAlumno(idUsuario: number) { + const usuario = await this.usuarioRepo.findOne({ + where: { idUsuario }, + }); + + if (!usuario) { + throw new NotFoundException('No existe este usuario.'); + } + + if (usuario.tipoUsuario.idTipoUsuario !== 3) { + throw new NotFoundException('No es un usuario de tipo alumno.'); + } + + const servicio = await this.servicioRepo + .createQueryBuilder('servicio') + .leftJoinAndSelect('servicio.carrera', 'carrera') + .leftJoinAndSelect('servicio.status', 'status') + .leftJoinAndSelect('servicio.programas', 'programa') + .where('servicio.idUsuario = :idUsuario', { idUsuario }) + .andWhere('servicio.idStatus != :status', { status: 10 }) + .select([ + 'servicio.idServicio', + 'servicio.creditos', + 'servicio.correo', + 'servicio.telefono', + 'servicio.direccion', + 'servicio.fechaInicio', + 'servicio.fechaFin', + 'servicio.fechaLiberacion', + 'servicio.fechaNacimiento', + 'servicio.informeGlobal', + 'servicio.programaInterno', + 'servicio.profesor', + 'servicio.createdAt', + 'servicio.idCuestionarioAlumno', + 'servicio.idCuestionarioAlumno2', + + 'programa.idPrograma', + 'programa.institucion', + 'programa.dependencia', + 'programa.programa', + 'programa.clavePrograma', + + 'carrera.idCarrera', + 'carrera.nombre', + + 'status.idStatus', + 'status.descripcion', + ]) + .getOne(); + + if (!servicio) { + throw new NotFoundException('No existe este servicio social.'); + } + + return servicio; } - findAll() { - return `This action returns all servicio`; + + + + + + async obtenerDetalleServicio(idServicio:number) { + const servicio = await this.servicioRepo + .createQueryBuilder('servicio') + .leftJoinAndSelect('servicio.usuario', 'usuario') + .leftJoinAndSelect('usuario.tipoUsuario', 'tipoUsuario') + .leftJoinAndSelect('servicio.carrera', 'carrera') + .leftJoinAndSelect('servicio.status', 'status') + .leftJoinAndSelect('servicio.programas', 'programa') + .leftJoinAndSelect('programa.usuario', 'usuarioPrograma') + .where('servicio.idServicio = :idServicio', { idServicio }) + .select([ + 'servicio.idServicio', + 'servicio.creditos', + 'servicio.correo', + 'servicio.telefono', + 'servicio.direccion', + 'servicio.fechaInicio', + 'servicio.fechaFin', + 'servicio.fechaLiberacion', + 'servicio.fechaNacimiento', + 'servicio.cartaAceptacion', + 'servicio.cartaTermino', + 'servicio.informeGlobal', + 'servicio.programaInterno', + 'servicio.profesor', + 'servicio.vistoBuenoAcatlan', + 'servicio.createdAt', + 'servicio.idCuestionarioAlumno', + 'servicio.idCuestionarioAlumno2', + 'servicio.idCuestionarioPrograma', + 'servicio.idCuestionarioPrograma2', + + 'usuario.idUsuario', + 'usuario.usuario', + 'usuario.nombre', + + 'tipoUsuario.idTipoUsuario', + 'carrera.idCarrera', + 'carrera.nombre', + + 'status.idStatus', + 'status.descripcion', + + 'programa.idPrograma', + 'programa.institucion', + 'programa.dependencia', + 'programa.programa', + 'programa.clavePrograma', + 'programa.acatlan', + + 'usuarioPrograma.idUsuario', + 'usuarioPrograma.usuario', + 'usuarioPrograma.nombre', + ]) + .getOne(); + + if (!servicio) { + throw new NotFoundException('No existe este servicio social.'); + } + + return servicio; } - findOne(id: number) { - return `This action returns a #${id} servicio`; + + async cancelarServicio(id: number,) { + const idServicio = this.validacionService.validarNumeroEntero(body.idServicio, 'id servicio'); + const mensaje = this.validacionService.validarAlfanumerico(body.mensaje, 'mensaje', true, 800); + + const servicio = await this.servicioRepo.findOne({ + where:{ idServicio }, + relations: [ + 'usuario', // alumno + 'programa', + 'programa.usuario', // responsable + ], + }); + + if (!servicio) { + throw new NotFoundException('No existe este Servicio Social.'); + } + + if (servicio.status.idStatus === 10) { + throw new BadRequestException('Este Servicio Social ya fue cancelado.'); + } + + if (servicio.status.idStatus === 6) { + throw new BadRequestException('Este Servicio Social ya fue finalizado, no se puede cancelar.'); + } + + const alumno = servicio.usuario; + const responsable = servicio.programa.usuario; + + const correoAlumno = canceladoAlumno(mensaje, alumno.nombre); + const correoResponsable = canceladoResponsable(mensaje, alumno.nombre); + + const sendCorreoAlumnoDto: SendCorreoDto = { + to: servicio.correo, + subject: correoAlumno.subject, + fecha_recibido: new Date(), + text: correoAlumno.msj, + html: '', + adjuntos: undefined + }; + + const sendCorreoResponsableDto: SendCorreoDto = { + to: responsable.usuario, + subject: correoResponsable.subject, + fecha_recibido: new Date(), + text: correoResponsable.msj, + html: '', + adjuntos: undefined + }; + + if (!process.env.TOKEN_GMAIL) { + throw new Error('No existe el token'); + } + + + if (servicio.correo) { + await this.gmailService.send(sendCorreoAlumnoDto, process.env.TOKEN_GMAIL); + } + + if (responsable.usuario) { + await this.gmailService.send(sendCorreoResponsableDto,process.env.TOKEN_GMAIL); + } + + // Desactivar al usuario (alumno) + await this.usuarioRepo.update(alumno.idUsuario, { + activo: false, + password: null, + }); + + // Cambiar estado del servicio + await this.servicioRepo.update(idServicio, { status:{idStatus: 10} }); + + return { + message: 'Se canceló correctamente este Servicio Social.', + }; } - update(id: number, updateServicioDto: UpdateServicioDto) { - return `This action updates a #${id} servicio`; - } - remove(id: number) { - return `This action removes a #${id} servicio`; - } + } + +