the module of caso especial is created
This commit is contained in:
@@ -8,9 +8,6 @@ import * as moment from 'moment';
|
||||
|
||||
import { Programa } from './entities/programa.entity';
|
||||
|
||||
|
||||
|
||||
|
||||
import { Usuario } from 'src/usuario/entities/usuario.entity';
|
||||
import { ValidacionService } from 'src/helpers.services/validacion.service';
|
||||
import { AuthService } from 'src/auth/auth.service';
|
||||
@@ -24,12 +21,9 @@ export class ProgramaService {
|
||||
private programaRepo: Repository<Programa>,
|
||||
@InjectRepository(Usuario)
|
||||
private usuarioRepo: Repository<Usuario>,
|
||||
private validacionService:ValidacionService,
|
||||
private authService:AuthService,
|
||||
private gmail:gmail,
|
||||
|
||||
|
||||
|
||||
private validacionService: ValidacionService,
|
||||
private authService: AuthService,
|
||||
private gmail: gmail,
|
||||
) {}
|
||||
|
||||
async cargaMasiva(nombreArchivo: string): Promise<{ message: string }> {
|
||||
@@ -42,7 +36,14 @@ export class ProgramaService {
|
||||
const prog = programas[i];
|
||||
mensaje += `Línea ${i + 2}\n`;
|
||||
|
||||
if (!prog.correo || !prog.nombre || !prog.institucion || !prog.dependencia || !prog.programa || !prog.clave) {
|
||||
if (
|
||||
!prog.correo ||
|
||||
!prog.nombre ||
|
||||
!prog.institucion ||
|
||||
!prog.dependencia ||
|
||||
!prog.programa ||
|
||||
!prog.clave
|
||||
) {
|
||||
mensaje += 'Faltan campos requeridos. Línea omitida.\n\n';
|
||||
continue;
|
||||
}
|
||||
@@ -58,35 +59,35 @@ export class ProgramaService {
|
||||
const activo = Number(prog.clave.substr(0, 4)) === today.year();
|
||||
|
||||
let responsable = await this.usuarioRepo.findOne({
|
||||
where: {
|
||||
usuario: prog.correo,
|
||||
where: {
|
||||
usuario: prog.correo,
|
||||
tipoUsuario: {
|
||||
idTipoUsuario:2
|
||||
}
|
||||
idTipoUsuario: 2,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
if (!responsable) {
|
||||
const password = await this.authService.encriptar(this.authService.generarPassword());
|
||||
const password = await this.authService.encriptar(
|
||||
this.authService.generarPassword(),
|
||||
);
|
||||
const correoInfo = enviarSec(password, prog.correo, prog.nombre);
|
||||
|
||||
|
||||
responsable = await this.usuarioRepo.create({
|
||||
usuario: prog.correo,
|
||||
password: password,
|
||||
nombre: prog.nombre,
|
||||
activo: true,
|
||||
tipoUsuario:{
|
||||
idTipoUsuario: 2
|
||||
tipoUsuario: {
|
||||
idTipoUsuario: 2,
|
||||
},
|
||||
});
|
||||
|
||||
const sendCorreoDto: SendCorreoDto = {
|
||||
to: prog.correo,
|
||||
subject: correoInfo.subject,
|
||||
fecha_recibido: new Date(),
|
||||
text: correoInfo.msj,
|
||||
html: '',
|
||||
adjuntos: undefined
|
||||
};
|
||||
|
||||
if (!process.env.TOKEN_GMAIL) {
|
||||
@@ -96,7 +97,7 @@ export class ProgramaService {
|
||||
try {
|
||||
responsable = await this.usuarioRepo.save(responsable);
|
||||
|
||||
await this.gmail.send(sendCorreoDto, process.env.TOKEN_GMAIL);
|
||||
await this.gmail.sendMail(sendCorreoDto);
|
||||
mensaje += `Responsable creado con ID ${responsable.idUsuario} y correo ${responsable.usuario}\n`;
|
||||
} catch (err) {
|
||||
mensaje += `Error creando responsable: ${err.message}\n`;
|
||||
@@ -118,9 +119,9 @@ export class ProgramaService {
|
||||
programa: prog.programa,
|
||||
clavePrograma: prog.clave,
|
||||
activo,
|
||||
usuario:{
|
||||
idUsuario:responsable.idUsuario
|
||||
}
|
||||
usuario: {
|
||||
idUsuario: responsable.idUsuario,
|
||||
},
|
||||
});
|
||||
const creado = await this.programaRepo.save(nuevoPrograma);
|
||||
mensaje += `Programa ${creado.clavePrograma} asignado al usuario ${responsable.usuario}\n`;
|
||||
@@ -139,8 +140,8 @@ export class ProgramaService {
|
||||
const existente = await this.programaRepo.findOne({
|
||||
where: {
|
||||
clavePrograma: prog.clave,
|
||||
usuario:{
|
||||
idUsuario: responsable.idUsuario
|
||||
usuario: {
|
||||
idUsuario: responsable.idUsuario,
|
||||
},
|
||||
},
|
||||
});
|
||||
@@ -154,8 +155,8 @@ export class ProgramaService {
|
||||
clavePrograma: prog.clave,
|
||||
activo,
|
||||
acatlan,
|
||||
usuario:{
|
||||
idUsuario: responsable.idUsuario
|
||||
usuario: {
|
||||
idUsuario: responsable.idUsuario,
|
||||
},
|
||||
});
|
||||
const creado = await this.programaRepo.save(nuevoPrograma);
|
||||
@@ -173,18 +174,16 @@ export class ProgramaService {
|
||||
|
||||
fs.unlinkSync(filePath);
|
||||
|
||||
|
||||
return {
|
||||
message: 'Se subió correctamente el archivo CSV para la carga masiva.',
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
async programasAdmin(body: { idUsuario: number }) {
|
||||
const idUsuario = this.validacionService.validarNumeroEntero(body.idUsuario, 'id usuario');
|
||||
const idUsuario = this.validacionService.validarNumeroEntero(
|
||||
body.idUsuario,
|
||||
'id usuario',
|
||||
);
|
||||
|
||||
const usuario = await this.usuarioRepo.findOne({
|
||||
where: { idUsuario },
|
||||
@@ -196,17 +195,17 @@ export class ProgramaService {
|
||||
|
||||
if (usuario.tipoUsuario.idTipoUsuario !== 2) {
|
||||
throw new Error('No es un usuario de tipo responsable');
|
||||
|
||||
}
|
||||
return this.programaRepo.find({
|
||||
where: { usuario:{idUsuario} },
|
||||
where: { usuario: { idUsuario } },
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
async programasResponsable(body: { idUsuario: number }) {
|
||||
const idUsuario = this.validacionService.validarNumeroEntero(body.idUsuario, 'id usuario');
|
||||
const idUsuario = this.validacionService.validarNumeroEntero(
|
||||
body.idUsuario,
|
||||
'id usuario',
|
||||
);
|
||||
|
||||
const usuario = await this.usuarioRepo.findOne({
|
||||
where: { idUsuario },
|
||||
@@ -222,40 +221,47 @@ export class ProgramaService {
|
||||
|
||||
return this.programaRepo.find({
|
||||
where: {
|
||||
usuario:{idUsuario},
|
||||
usuario: { idUsuario },
|
||||
activo: true,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
async reasignarProgramas(idUsuario:number,correoOtroResponsable:string){
|
||||
const idusuario= this.validacionService.validarNumeroEntero(idUsuario,'id usuario')
|
||||
async reasignarProgramas(idUsuario: number, correoOtroResponsable: string) {
|
||||
const idusuario = this.validacionService.validarNumeroEntero(
|
||||
idUsuario,
|
||||
'id usuario',
|
||||
);
|
||||
const otroResponsable = this.validacionService.validarCorreo(
|
||||
correoOtroResponsable
|
||||
correoOtroResponsable,
|
||||
);
|
||||
|
||||
let usuarioNuevo= await this.usuarioRepo.findOne({where:{idUsuario:idusuario}})
|
||||
let usuarioNuevo = await this.usuarioRepo.findOne({
|
||||
where: { idUsuario: idusuario },
|
||||
});
|
||||
|
||||
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.');
|
||||
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.');
|
||||
|
||||
let viejoResponsable = await this.usuarioRepo.findOne({
|
||||
where: { usuario: otroResponsable },
|
||||
});
|
||||
|
||||
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 }
|
||||
{ usuario: viejoResponsable },
|
||||
{ usuario: usuarioNuevo },
|
||||
);
|
||||
|
||||
// Eliminar usuario antiguo
|
||||
await this.usuarioRepo.delete({idUsuario:viejoResponsable.idUsuario});
|
||||
|
||||
|
||||
await this.usuarioRepo.delete({ idUsuario: viejoResponsable.idUsuario });
|
||||
|
||||
return {
|
||||
message: `Se eliminó correctamente al usuario ${correoOtroResponsable} y se reasignaron sus programas.`,
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user