listo carga masiva usuarios
This commit is contained in:
parent
b0ce24dc4f
commit
69d880be84
@ -3,6 +3,7 @@ import * as csvtojson from 'csvtojson';
|
||||
import { BadRequestException, Injectable } from '@nestjs/common';
|
||||
import { AppGateway } from '../app.gateway';
|
||||
import { Equipo } from '../equipo/entity/equipo.entity';
|
||||
import { Institucion } from '../institucion/entity/institucion.entity';
|
||||
import { InstitucionUsuario } from '../institucion-usuario/entity/institucion-usuario.entity';
|
||||
import { CarritoService } from '../carrito/carrito.service';
|
||||
import { EquipoService } from '../equipo/equipo.service';
|
||||
@ -21,7 +22,6 @@ import { TipoUsuarioService } from '../tipo-usuario/tipo-usuario.service';
|
||||
import { UsuarioService } from '../usuario/usuario.service';
|
||||
import { UploadFileCargaMasivaUsuarioDto } from './dto/upload-file-carga-masiva-usuario.dto';
|
||||
import { UploadFileCargaMasivaEquipoDto } from './dto/upload-file-carga-masiva-equipo.dto';
|
||||
import { Institucion } from 'src/institucion/entity/institucion.entity';
|
||||
|
||||
@Injectable()
|
||||
export class UploadFileService {
|
||||
@ -44,6 +44,85 @@ export class UploadFileService {
|
||||
private usuarioService: UsuarioService,
|
||||
) {}
|
||||
|
||||
async createEquipos(path: string, id_institucion: number) {
|
||||
const institucion = await this.institucionService.findById(id_institucion);
|
||||
let errores: string[] = [];
|
||||
let mensajes: string[] = [];
|
||||
let equiposNuevos: Equipo[] = [];
|
||||
|
||||
csvtojson()
|
||||
.fromFile(path)
|
||||
.then(async (equipos: UploadFileCargaMasivaEquipoDto[]) => {
|
||||
for (let i = 0; i < equipos.length; i++) {
|
||||
await this.evaluarEquipo(
|
||||
institucion,
|
||||
i,
|
||||
equipos[i],
|
||||
mensajes,
|
||||
errores,
|
||||
equiposNuevos,
|
||||
);
|
||||
if ((i + 1) % 90 === 0 || i + 1 === equipos.length) {
|
||||
this.appGateway.equiposNuevos(institucion.id_institucion, {
|
||||
errores,
|
||||
mensajes,
|
||||
equiposNuevos,
|
||||
});
|
||||
errores = [];
|
||||
mensajes = [];
|
||||
equiposNuevos = [];
|
||||
}
|
||||
}
|
||||
fs.unlink(path, (err) => {});
|
||||
});
|
||||
return { message: 'Se subió y cargó correctamente su archivo csv.' };
|
||||
}
|
||||
|
||||
async createUsuarios(path: string, id_institucion: number) {
|
||||
const institucion = await this.institucionService.findById(id_institucion);
|
||||
let errores: string[] = [];
|
||||
let mensajes: string[] = [];
|
||||
let usuariosNuevos: InstitucionUsuario[] = [];
|
||||
|
||||
csvtojson()
|
||||
.fromFile(path)
|
||||
.then(async (usuarios: UploadFileCargaMasivaUsuarioDto[]) => {
|
||||
for (let i = 0; i < usuarios.length; i++) {
|
||||
await this.evaluarUsuario(
|
||||
institucion,
|
||||
i,
|
||||
usuarios[i],
|
||||
mensajes,
|
||||
errores,
|
||||
usuariosNuevos,
|
||||
);
|
||||
if ((i + 1) % 100 === 0 || i + 1 === usuarios.length) {
|
||||
this.appGateway.usuariosNuevos(institucion.id_institucion, {
|
||||
errores,
|
||||
mensajes,
|
||||
usuariosNuevos,
|
||||
});
|
||||
errores = [];
|
||||
mensajes = [];
|
||||
usuariosNuevos = [];
|
||||
}
|
||||
}
|
||||
});
|
||||
return { message: 'Se subió y cargó correctamente tu archivo csv.' };
|
||||
}
|
||||
|
||||
downloadLogo(id_institucion: number) {
|
||||
return this.institucionService
|
||||
.findById(id_institucion)
|
||||
.then((institucion) => institucion.logo);
|
||||
}
|
||||
|
||||
errorBase(index) {
|
||||
return `Se saltó la linea ${
|
||||
index + 2
|
||||
} por el siguiente/los siguientes motivo(s):`;
|
||||
}
|
||||
|
||||
async evaluarEquipo(
|
||||
institucion: Institucion,
|
||||
index: number,
|
||||
@ -251,188 +330,115 @@ export class UploadFileService {
|
||||
}
|
||||
}
|
||||
|
||||
async createEquipos(path: string, id_institucion: number) {
|
||||
const institucion = await this.institucionService.findById(id_institucion);
|
||||
let errores: string[] = [];
|
||||
let mensajes: string[] = [];
|
||||
let equiposNuevos: Equipo[] = [];
|
||||
async evaluarUsuario(
|
||||
institucion: Institucion,
|
||||
index: number,
|
||||
dataUsuario: UploadFileCargaMasivaUsuarioDto,
|
||||
mensajes: string[],
|
||||
errores: string[],
|
||||
usuariosNuevos: InstitucionUsuario[],
|
||||
) {
|
||||
let error = this.errorBase(index);
|
||||
|
||||
csvtojson()
|
||||
.fromFile(path)
|
||||
.then(async (equipos: UploadFileCargaMasivaEquipoDto[]) => {
|
||||
for (let i = 0; i < equipos.length; i++) {
|
||||
await this.evaluarEquipo(
|
||||
institucion,
|
||||
i,
|
||||
equipos[i],
|
||||
mensajes,
|
||||
errores,
|
||||
equiposNuevos,
|
||||
);
|
||||
if ((i + 1) % 100 === 0 || i + 1 === equipos.length) {
|
||||
this.appGateway.equiposNuevos(institucion.id_institucion, {
|
||||
errores,
|
||||
mensajes,
|
||||
equiposNuevos,
|
||||
});
|
||||
errores = [];
|
||||
mensajes = [];
|
||||
equiposNuevos = [];
|
||||
}
|
||||
}
|
||||
fs.unlink(path, (err) => {});
|
||||
});
|
||||
return { message: 'Se subió y cargó correctamente su archivo csv.' };
|
||||
}
|
||||
if (!dataUsuario.numero_cuenta || !dataUsuario.tipo_usuario) {
|
||||
if (!dataUsuario.numero_cuenta) error += ' falta del campo numero_cuenta';
|
||||
if (error != this.errorBase(index)) error += ',';
|
||||
if (!dataUsuario.tipo_usuario) error += ' falta del campo tipo_usuario';
|
||||
errores.push(`${error}.`);
|
||||
return;
|
||||
}
|
||||
|
||||
async createUsuarios(path: string, id_institucion: number) {
|
||||
const institucion = await this.institucionService.findById(id_institucion);
|
||||
const errores: string[] = [];
|
||||
const mensajes: string[] = [];
|
||||
const usuariosActualizados: InstitucionUsuario[] = [];
|
||||
const usuariosNuevos: InstitucionUsuario[] = [];
|
||||
|
||||
return csvtojson()
|
||||
.fromFile(path)
|
||||
.then(async (usuarios: UploadFileCargaMasivaUsuarioDto[]) => {
|
||||
for (let i = 0; i < usuarios.length; i++) {
|
||||
let error = this.errorBase(i);
|
||||
|
||||
if (!usuarios[i].numero_cuenta || !usuarios[i].tipo_usuario) {
|
||||
if (!usuarios[i].numero_cuenta)
|
||||
error += ' falta del campo numero_cuenta';
|
||||
if (error != this.errorBase(i)) error += ',';
|
||||
if (!usuarios[i].tipo_usuario)
|
||||
error += ' falta del campo tipo_usuario';
|
||||
errores.push(`${error}.`);
|
||||
continue;
|
||||
}
|
||||
|
||||
const tipoUsuario = await this.tipoUsuarioService.findByTipoUsuario(
|
||||
usuarios[i].tipo_usuario,
|
||||
const tipoUsuario = await this.tipoUsuarioService.findByTipoUsuario(
|
||||
dataUsuario.tipo_usuario,
|
||||
false,
|
||||
);
|
||||
const usuario = await this.usuarioService.findByUsuario(
|
||||
dataUsuario.numero_cuenta,
|
||||
false,
|
||||
);
|
||||
const carrera =
|
||||
tipoUsuario && (tipoUsuario.id_tipo_usuario === 5 || dataUsuario.carrera)
|
||||
? await this.institucionCarreraService.findCarreraByCarrera(
|
||||
tipoUsuario.id_tipo_usuario === 5
|
||||
? 'PROFESOR'
|
||||
: dataUsuario.carrera,
|
||||
false,
|
||||
);
|
||||
const usuario = await this.usuarioService.findByUsuario(
|
||||
usuarios[i].numero_cuenta,
|
||||
false,
|
||||
);
|
||||
const carrera =
|
||||
tipoUsuario &&
|
||||
(tipoUsuario.id_tipo_usuario === 5 || usuarios[i].carrera)
|
||||
? await this.institucionCarreraService.findCarreraByCarrera(
|
||||
tipoUsuario.id_tipo_usuario === 5
|
||||
? 'PROFESOR'
|
||||
: usuarios[i].carrera,
|
||||
false,
|
||||
)
|
||||
: null;
|
||||
const institucionCarrera = carrera
|
||||
? await this.institucionCarreraService.findByIdInstitucionIdCarrera(
|
||||
institucion,
|
||||
carrera,
|
||||
)
|
||||
: null;
|
||||
)
|
||||
: null;
|
||||
const institucionCarrera = carrera
|
||||
? await this.institucionCarreraService.findByIdInstitucionIdCarrera(
|
||||
institucion,
|
||||
carrera,
|
||||
)
|
||||
: null;
|
||||
|
||||
if (
|
||||
!tipoUsuario ||
|
||||
tipoUsuario.id_tipo_usuario < 5 ||
|
||||
!carrera ||
|
||||
(carrera && !institucionCarrera)
|
||||
) {
|
||||
if (!tipoUsuario) error += ' no existe este tipo de usuario.';
|
||||
else if (tipoUsuario.id_tipo_usuario < 5)
|
||||
' no se puede asignar este tipo de usuario a este usuario.';
|
||||
if (!carrera) error += ' falta del campo carrera para el alumno.';
|
||||
if (carrera && !institucionCarrera)
|
||||
error += ' la carrera no pertenece a la institución.';
|
||||
errores.push(error);
|
||||
continue;
|
||||
}
|
||||
if (usuario) {
|
||||
let existeInstitucionUsuario: InstitucionUsuario = null;
|
||||
if (
|
||||
!tipoUsuario ||
|
||||
tipoUsuario.id_tipo_usuario < 5 ||
|
||||
!carrera ||
|
||||
(carrera && !institucionCarrera)
|
||||
) {
|
||||
if (!tipoUsuario) error += ' no existe este tipo de usuario.';
|
||||
else if (tipoUsuario.id_tipo_usuario < 5)
|
||||
' no se puede asignar este tipo de usuario a este usuario.';
|
||||
if (!carrera) error += ' falta del campo carrera para el alumno.';
|
||||
if (carrera && !institucionCarrera)
|
||||
error += ' la carrera no pertenece a la institución.';
|
||||
errores.push(error);
|
||||
return;
|
||||
}
|
||||
if (usuario) {
|
||||
let existeInstitucionUsuario: InstitucionUsuario = null;
|
||||
|
||||
for (let i = 0; i < usuario.instituciones.length; i++)
|
||||
if (
|
||||
usuario.instituciones[i].institucionCarrera
|
||||
.id_institucion_carrera ===
|
||||
institucionCarrera.id_institucion_carrera
|
||||
) {
|
||||
existeInstitucionUsuario = usuario.instituciones[i];
|
||||
break;
|
||||
}
|
||||
if (existeInstitucionUsuario && !existeInstitucionUsuario.activo) {
|
||||
existeInstitucionUsuario.activo = true;
|
||||
await this.institucionUsuarioService.update(
|
||||
existeInstitucionUsuario,
|
||||
);
|
||||
} else if (!existeInstitucionUsuario)
|
||||
await this.institucionUsuarioService
|
||||
.create(institucionCarrera, usuario)
|
||||
.then(({ message, institucionUsuario }) => {
|
||||
usuariosActualizados.push(institucionUsuario);
|
||||
mensajes.push(message);
|
||||
});
|
||||
if (
|
||||
usuario.tipoUsuario.id_tipo_usuario != tipoUsuario.id_tipo_usuario
|
||||
) {
|
||||
usuario.tipoUsuario = tipoUsuario;
|
||||
await this.usuarioService
|
||||
.update(usuario, false)
|
||||
.then(({ message }) => mensajes.push(message));
|
||||
}
|
||||
} else {
|
||||
if (
|
||||
!usuarios[i].nombres ||
|
||||
!usuarios[i].apellido_m ||
|
||||
!usuarios[i].apellido_p
|
||||
) {
|
||||
if (error != this.errorBase(i)) error += ',';
|
||||
if (!usuarios[i].nombres) error += ' falta del campo nombres';
|
||||
if (error != this.errorBase(i)) error += ',';
|
||||
if (!usuarios[i].apellido_m)
|
||||
error += ' falta del campo apellido_m';
|
||||
if (error != this.errorBase(i)) error += ',';
|
||||
if (!usuarios[i].apellido_p)
|
||||
error += ' falta del campo apellido_p';
|
||||
errores.push(`${error}.`);
|
||||
continue;
|
||||
}
|
||||
await this.usuarioService
|
||||
.create(
|
||||
usuarios[i].numero_cuenta,
|
||||
`${usuarios[i].apellido_p.trim()} ${usuarios[
|
||||
i
|
||||
].apellido_m.trim()} ${usuarios[i].nombres.trim()}`,
|
||||
tipoUsuario,
|
||||
institucionCarrera,
|
||||
usuarios[i].correo ? usuarios[i].correo : '',
|
||||
usuarios[i].rfc ? usuarios[i].rfc : '',
|
||||
)
|
||||
.then(({ message, institucionUsuario }) => {
|
||||
usuariosNuevos.push(institucionUsuario);
|
||||
mensajes.push(message);
|
||||
});
|
||||
}
|
||||
for (let i = 0; i < usuario.instituciones.length; i++)
|
||||
if (
|
||||
usuario.instituciones[i].institucionCarrera.id_institucion_carrera ===
|
||||
institucionCarrera.id_institucion_carrera
|
||||
) {
|
||||
existeInstitucionUsuario = usuario.instituciones[i];
|
||||
break;
|
||||
}
|
||||
return {
|
||||
message: 'Se subió y cargó correctamente tu archivo csv.',
|
||||
mensajes,
|
||||
usuariosNuevos,
|
||||
errores,
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
downloadLogo(id_institucion: number) {
|
||||
return this.institucionService
|
||||
.findById(id_institucion)
|
||||
.then((institucion) => institucion.logo);
|
||||
}
|
||||
|
||||
errorBase(index) {
|
||||
return `Se saltó la linea ${
|
||||
index + 2
|
||||
} por el siguiente/los siguientes motivo(s):`;
|
||||
if (existeInstitucionUsuario && !existeInstitucionUsuario.activo) {
|
||||
existeInstitucionUsuario.activo = true;
|
||||
await this.institucionUsuarioService.update(existeInstitucionUsuario);
|
||||
} else if (!existeInstitucionUsuario)
|
||||
await this.institucionUsuarioService
|
||||
.create(institucionCarrera, usuario)
|
||||
.then(({ message }) => mensajes.push(message));
|
||||
if (usuario.tipoUsuario.id_tipo_usuario != tipoUsuario.id_tipo_usuario) {
|
||||
usuario.tipoUsuario = tipoUsuario;
|
||||
await this.usuarioService
|
||||
.update(usuario, false)
|
||||
.then(({ message }) => mensajes.push(message));
|
||||
}
|
||||
} else {
|
||||
if (
|
||||
!dataUsuario.nombres ||
|
||||
!dataUsuario.apellido_m ||
|
||||
!dataUsuario.apellido_p
|
||||
) {
|
||||
if (!dataUsuario.nombres) error += ' falta del campo nombres';
|
||||
if (error != this.errorBase(index)) error += ',';
|
||||
if (!dataUsuario.apellido_m) error += ' falta del campo apellido_m';
|
||||
if (error != this.errorBase(index)) error += ',';
|
||||
if (!dataUsuario.apellido_p) error += ' falta del campo apellido_p';
|
||||
errores.push(`${error}.`);
|
||||
return;
|
||||
}
|
||||
await this.usuarioService
|
||||
.create(
|
||||
dataUsuario.numero_cuenta,
|
||||
`${dataUsuario.apellido_p.trim()} ${dataUsuario.apellido_m.trim()} ${dataUsuario.nombres.trim()}`,
|
||||
tipoUsuario,
|
||||
institucionCarrera,
|
||||
dataUsuario.correo ? dataUsuario.correo : '',
|
||||
dataUsuario.rfc ? dataUsuario.rfc : '',
|
||||
)
|
||||
.then(({ message, institucionUsuario }) => {
|
||||
usuariosNuevos.push(institucionUsuario);
|
||||
mensajes.push(message);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
async uploadLogo(file: Express.Multer.File, id_institucion: number) {
|
||||
|
Loading…
Reference in New Issue
Block a user