2025-06-13 17:54:38 -06:00
|
|
|
|
// src/excel/excel.service.ts
|
|
|
|
|
|
import { Injectable, BadRequestException, Logger } from '@nestjs/common';
|
|
|
|
|
|
import { Workbook } from 'exceljs';
|
2025-06-18 07:33:21 -06:00
|
|
|
|
import { In, Repository } from 'typeorm';
|
2025-06-13 17:54:38 -06:00
|
|
|
|
import { InjectRepository } from '@nestjs/typeorm';
|
2025-06-30 12:53:08 -06:00
|
|
|
|
import { CarreraUsuario, TipoUsuario, Usuario, UsuariosDelSistema, UsuarioTipoUsuario } from '../entities/entities';
|
2025-06-13 17:54:38 -06:00
|
|
|
|
import { Genero } from '../entities/entities';
|
|
|
|
|
|
import { Carrera } from '../entities/entities';
|
2025-06-20 11:59:13 -06:00
|
|
|
|
import { MovimientoService } from 'src/movimiento/movimiento.service';
|
|
|
|
|
|
import { ServActivos } from 'src/usuarios/entities/servActivos.entitie';
|
2025-06-30 12:53:08 -06:00
|
|
|
|
import { MailService } from 'src/mail/mail.service';
|
2025-06-13 17:54:38 -06:00
|
|
|
|
|
|
|
|
|
|
export interface UsuarioRow {
|
|
|
|
|
|
cuenta: string;
|
|
|
|
|
|
nombreCompleto: string;
|
|
|
|
|
|
clave: string;
|
|
|
|
|
|
nomCarr: string;
|
|
|
|
|
|
gen: string; // generación
|
|
|
|
|
|
fechnac: string; // YYYYMMDD
|
|
|
|
|
|
apellidopa: string;
|
|
|
|
|
|
apellidoma: string;
|
|
|
|
|
|
nombres: string;
|
|
|
|
|
|
sexo: string; // m/f o texto
|
|
|
|
|
|
tipo: string;
|
|
|
|
|
|
correo: string;
|
|
|
|
|
|
rfc: string;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
|
export class ExcelService {
|
|
|
|
|
|
private readonly logger = new Logger(ExcelService.name);
|
|
|
|
|
|
|
|
|
|
|
|
constructor(
|
|
|
|
|
|
@InjectRepository(Usuario)
|
|
|
|
|
|
private readonly usuarioRepo: Repository<Usuario>,
|
|
|
|
|
|
@InjectRepository(Genero)
|
|
|
|
|
|
private readonly generoRepo: Repository<Genero>,
|
|
|
|
|
|
@InjectRepository(Carrera)
|
|
|
|
|
|
private readonly carreraRepo: Repository<Carrera>,
|
2025-06-18 12:54:26 -06:00
|
|
|
|
@InjectRepository(CarreraUsuario)
|
|
|
|
|
|
private readonly carreraUsuarioRepo: Repository<CarreraUsuario>,
|
|
|
|
|
|
@InjectRepository(UsuarioTipoUsuario)
|
|
|
|
|
|
private readonly usuarioTipoUsuarioRepo: Repository<UsuarioTipoUsuario>,
|
|
|
|
|
|
@InjectRepository(TipoUsuario)
|
|
|
|
|
|
private readonly tipoUsuarioRepo: Repository<TipoUsuario>,
|
2025-06-20 11:59:13 -06:00
|
|
|
|
@InjectRepository(ServActivos)
|
|
|
|
|
|
private readonly servActivosRepo: Repository<ServActivos>,
|
2025-06-30 12:53:08 -06:00
|
|
|
|
@InjectRepository(UsuariosDelSistema)
|
|
|
|
|
|
private readonly usuariosDelSistemaRepo: Repository<UsuariosDelSistema>,
|
2025-06-20 11:59:13 -06:00
|
|
|
|
private readonly movimientoService: MovimientoService,
|
2025-06-30 12:53:08 -06:00
|
|
|
|
private readonly mailService: MailService
|
2025-06-20 11:59:13 -06:00
|
|
|
|
|
2025-06-13 17:54:38 -06:00
|
|
|
|
// … inyecta otros repositorios si los usarás
|
2025-06-18 07:33:21 -06:00
|
|
|
|
) { }
|
2025-06-13 17:54:38 -06:00
|
|
|
|
|
|
|
|
|
|
/** Lee el buffer del Excel y devuelve un arreglo de filas tipadas */
|
|
|
|
|
|
private async parseFile(buffer: Buffer): Promise<UsuarioRow[]> {
|
|
|
|
|
|
const wb = new Workbook();
|
|
|
|
|
|
await wb.xlsx.load(buffer);
|
|
|
|
|
|
const sheet = wb.worksheets[0];
|
|
|
|
|
|
const rows: UsuarioRow[] = [];
|
|
|
|
|
|
|
|
|
|
|
|
// Asume que la primera fila es encabezados
|
|
|
|
|
|
sheet.eachRow((row, idx) => {
|
|
|
|
|
|
if (idx === 1) return; // salto encabezados
|
|
|
|
|
|
|
|
|
|
|
|
// 1) Asegurarnos de que row.values no sea null/undefined
|
|
|
|
|
|
if (!row.values) {
|
|
|
|
|
|
this.logger.warn(`Fila ${idx + 1}: row.values vacío, se omite.`);
|
|
|
|
|
|
return;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2) row.values[0] es null, así que slice(1) sí existe
|
|
|
|
|
|
const [
|
|
|
|
|
|
cuenta,
|
|
|
|
|
|
nombreCompleto,
|
|
|
|
|
|
clave,
|
|
|
|
|
|
nomCarr,
|
|
|
|
|
|
gen,
|
|
|
|
|
|
fechnac,
|
|
|
|
|
|
apellidopa,
|
|
|
|
|
|
apellidoma,
|
|
|
|
|
|
nombres,
|
|
|
|
|
|
sexo,
|
|
|
|
|
|
tipo,
|
|
|
|
|
|
correo,
|
|
|
|
|
|
rfc,
|
|
|
|
|
|
] = (row.values as any[]).slice(1);
|
|
|
|
|
|
|
|
|
|
|
|
rows.push({
|
|
|
|
|
|
cuenta,
|
|
|
|
|
|
nombreCompleto,
|
|
|
|
|
|
clave,
|
|
|
|
|
|
nomCarr,
|
|
|
|
|
|
gen,
|
|
|
|
|
|
fechnac,
|
|
|
|
|
|
apellidopa,
|
|
|
|
|
|
apellidoma,
|
|
|
|
|
|
nombres,
|
|
|
|
|
|
sexo,
|
|
|
|
|
|
tipo,
|
|
|
|
|
|
correo,
|
|
|
|
|
|
rfc,
|
|
|
|
|
|
});
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
return rows;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** Valida duplicados, celdas vacías y patrones básicos */
|
2025-06-18 07:33:21 -06:00
|
|
|
|
validateRows(rows: UsuarioRow[]) {
|
|
|
|
|
|
const errors: string[] = [];
|
|
|
|
|
|
const seen = new Set<string>();
|
2025-06-20 11:59:13 -06:00
|
|
|
|
const rowsGood: UsuarioRow[] = [];
|
|
|
|
|
|
let flagError: boolean = false;
|
2025-06-18 07:33:21 -06:00
|
|
|
|
|
|
|
|
|
|
rows.forEach((r, i) => {
|
|
|
|
|
|
const rowNum = i + 2; // por el encabezado
|
|
|
|
|
|
|
|
|
|
|
|
// Campos que no pueden quedar vacíos
|
2025-06-20 11:59:13 -06:00
|
|
|
|
['cuenta', 'nombreCompleto', 'tipo'].forEach(field => {
|
2025-06-18 07:33:21 -06:00
|
|
|
|
const raw = (r as any)[field];
|
|
|
|
|
|
const str = raw != null ? raw.toString().trim() : '';
|
|
|
|
|
|
if (!str) {
|
|
|
|
|
|
errors.push(`Fila ${rowNum}: campo "${field}" está vacío.`);
|
2025-06-20 11:59:13 -06:00
|
|
|
|
flagError = true;
|
2025-06-18 07:33:21 -06:00
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-20 11:59:13 -06:00
|
|
|
|
// El número de cuenta debe ser de nueve dígitos para los alumnos
|
|
|
|
|
|
if (r.cuenta && !/^\d{9}$/.test(r.cuenta.toString().trim()) && r.tipo.trim() === 'Licenciatura') {
|
|
|
|
|
|
errors.push(`Fila ${rowNum}: cuenta "${r.cuenta}" debe tener 9 dígitos.`);
|
|
|
|
|
|
flagError = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
// Duplicados de "cuenta"
|
|
|
|
|
|
const cuentaStr = r.cuenta != null ? r.cuenta.toString().trim() : '';
|
|
|
|
|
|
if (seen.has(cuentaStr)) {
|
|
|
|
|
|
errors.push(`Fila ${rowNum}: cuenta duplicada "${cuentaStr}".`);
|
2025-06-20 11:59:13 -06:00
|
|
|
|
flagError = true;
|
2025-06-18 07:33:21 -06:00
|
|
|
|
} else {
|
|
|
|
|
|
seen.add(cuentaStr);
|
2025-06-13 17:54:38 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
// Numérico en cuenta y generación
|
2025-06-13 17:54:38 -06:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
const genStr = r.gen != null ? r.gen.toString().trim() : '';
|
|
|
|
|
|
if (genStr && !/^[0-9]{4}$/.test(genStr)) {
|
|
|
|
|
|
errors.push(`Fila ${rowNum}: generación inválida "${genStr}".`);
|
2025-06-20 11:59:13 -06:00
|
|
|
|
flagError = true;
|
2025-06-18 07:33:21 -06:00
|
|
|
|
}
|
2025-06-13 17:54:38 -06:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
// Fecha en formato YYYYMMDD
|
|
|
|
|
|
const fechaStr = r.fechnac != null ? r.fechnac.toString().trim() : '';
|
|
|
|
|
|
if (!/^[0-9]{8}$/.test(fechaStr)) {
|
|
|
|
|
|
errors.push(`Fila ${rowNum}: fecha de nacimiento inválida "${fechaStr}".`);
|
2025-06-20 11:59:13 -06:00
|
|
|
|
flagError = true;
|
2025-06-18 07:33:21 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// RFC alfanumérico
|
|
|
|
|
|
const rfcStr = r.rfc != null ? r.rfc.toString().trim() : '';
|
|
|
|
|
|
if (rfcStr && !/^[A-Z0-9]+$/.test(rfcStr)) {
|
|
|
|
|
|
errors.push(`Fila ${rowNum}: RFC contiene caracteres no válidos.`);
|
2025-06-20 11:59:13 -06:00
|
|
|
|
flagError = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (!flagError) {
|
|
|
|
|
|
rowsGood.push(r)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
flagError = false; // reset para la siguiente fila
|
2025-06-18 07:33:21 -06:00
|
|
|
|
}
|
2025-06-20 11:59:13 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
});
|
|
|
|
|
|
|
2025-06-20 11:59:13 -06:00
|
|
|
|
console.log(rowsGood);
|
|
|
|
|
|
|
|
|
|
|
|
return { errors, rowsGood };
|
2025-06-18 07:33:21 -06:00
|
|
|
|
}
|
2025-06-13 17:54:38 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Solo valida: retorna lista de errores (vacío = ok)
|
|
|
|
|
|
*/
|
|
|
|
|
|
async validateFile(buffer: Buffer) {
|
|
|
|
|
|
const rows = await this.parseFile(buffer);
|
2025-06-20 11:59:13 -06:00
|
|
|
|
const status = this.validateRows(rows);
|
|
|
|
|
|
return status;
|
2025-06-13 17:54:38 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* Valida y luego persiste usuarios y relaciones básicas
|
|
|
|
|
|
*/
|
|
|
|
|
|
async loadFile(buffer: Buffer) {
|
2025-06-20 11:59:13 -06:00
|
|
|
|
const file = await this.parseFile(buffer);
|
|
|
|
|
|
const status = this.validateRows(file);
|
|
|
|
|
|
const errs = status.errors;
|
|
|
|
|
|
const rows = status.rowsGood;
|
2025-06-30 12:53:08 -06:00
|
|
|
|
let carga
|
|
|
|
|
|
if (rows.length == 0) {
|
|
|
|
|
|
throw new BadRequestException('No se encontraron filas válidas para cargar. ' + errs);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-20 11:59:13 -06:00
|
|
|
|
|
|
|
|
|
|
const movimiento = await this.movimientoService.log(
|
|
|
|
|
|
|
|
|
|
|
|
'LOAD',
|
|
|
|
|
|
'LOADING',
|
2025-06-30 12:53:08 -06:00
|
|
|
|
undefined,
|
|
|
|
|
|
`CARGA MASIVA DE USUARIOS`,
|
|
|
|
|
|
true, // flag para indicar que es una carga masiva
|
2025-06-20 11:59:13 -06:00
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-30 12:53:08 -06:00
|
|
|
|
let contador: number = 0;
|
2025-06-13 17:54:38 -06:00
|
|
|
|
|
|
|
|
|
|
for (const r of rows) {
|
2025-06-20 11:59:13 -06:00
|
|
|
|
//0.1
|
|
|
|
|
|
const userExists = await this.usuarioRepo.findOne({
|
|
|
|
|
|
where: { num_cuenta: r.cuenta },
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (userExists) {
|
|
|
|
|
|
errs.push(`Cuenta ${r.cuenta} ya existe, se omite.`);
|
2025-06-30 12:53:08 -06:00
|
|
|
|
contador++;
|
2025-06-20 11:59:13 -06:00
|
|
|
|
continue; // si ya existe, no lo insertamos
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-13 17:54:38 -06:00
|
|
|
|
// 1) Género (crea o reutiliza)
|
|
|
|
|
|
let genero = await this.generoRepo.findOne({ where: { genero: r.sexo } });
|
|
|
|
|
|
if (!genero) {
|
|
|
|
|
|
genero = this.generoRepo.create({ genero: r.sexo });
|
|
|
|
|
|
await this.generoRepo.save(genero);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// 2) Carrera (por clave)
|
|
|
|
|
|
let carrera = await this.carreraRepo.findOne({
|
|
|
|
|
|
where: { clave: r.clave },
|
|
|
|
|
|
});
|
|
|
|
|
|
if (!carrera) {
|
|
|
|
|
|
carrera = this.carreraRepo.create({
|
|
|
|
|
|
clave: r.clave,
|
|
|
|
|
|
carrera: r.nomCarr.slice(0, 100),
|
|
|
|
|
|
});
|
|
|
|
|
|
await this.carreraRepo.save(carrera);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-18 12:54:26 -06:00
|
|
|
|
let tipo = await this.tipoUsuarioRepo.findOne({
|
|
|
|
|
|
where: { tipo_usuario: r.tipo.trim() },
|
|
|
|
|
|
});
|
|
|
|
|
|
if (!tipo) {
|
|
|
|
|
|
tipo = this.tipoUsuarioRepo.create({
|
|
|
|
|
|
tipo_usuario: r.tipo.trim(),
|
|
|
|
|
|
});
|
|
|
|
|
|
await this.tipoUsuarioRepo.save(tipo);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
const generacion = /^[0-9]{4}$/.test(r.gen?.toString().trim())
|
|
|
|
|
|
? parseInt(r.gen.toString().trim(), 10)
|
|
|
|
|
|
: null;
|
2025-06-20 11:59:13 -06:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-13 17:54:38 -06:00
|
|
|
|
// 3) Usuario
|
|
|
|
|
|
const usuario = this.usuarioRepo.create({
|
|
|
|
|
|
num_cuenta: r.cuenta,
|
|
|
|
|
|
nombre: r.nombres.trim().split(' ')[0], // ajusta si quieres
|
|
|
|
|
|
a_paterno: r.apellidopa,
|
|
|
|
|
|
a_materno: r.apellidoma,
|
|
|
|
|
|
rfc: r.rfc,
|
|
|
|
|
|
fecha_nacimiento: r.fechnac,
|
2025-06-18 07:33:21 -06:00
|
|
|
|
generacion: generacion,
|
2025-06-13 17:54:38 -06:00
|
|
|
|
genero,
|
2025-06-20 11:59:13 -06:00
|
|
|
|
movimiento: { id_mov: movimiento.id_mov }, // Relación con movimiento
|
2025-06-18 07:33:21 -06:00
|
|
|
|
|
2025-06-13 17:54:38 -06:00
|
|
|
|
});
|
2025-06-20 11:59:13 -06:00
|
|
|
|
const user = await this.usuarioRepo.save(usuario);
|
2025-06-13 17:54:38 -06:00
|
|
|
|
|
2025-06-18 12:54:26 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// 4) Relacion usuario–carrxera (tabla intermedia)
|
|
|
|
|
|
const carreraUsuario = this.carreraUsuarioRepo.create({
|
|
|
|
|
|
usuario,
|
|
|
|
|
|
carrera,
|
|
|
|
|
|
});
|
|
|
|
|
|
await this.carreraUsuarioRepo.save(carreraUsuario);
|
|
|
|
|
|
|
|
|
|
|
|
// 5) Tipo de usuario
|
|
|
|
|
|
|
|
|
|
|
|
const usuarioTipo = this.usuarioTipoUsuarioRepo.create({
|
|
|
|
|
|
usuario,
|
|
|
|
|
|
tipoUsuario: tipo,
|
|
|
|
|
|
});
|
|
|
|
|
|
await this.usuarioTipoUsuarioRepo.save(usuarioTipo);
|
|
|
|
|
|
|
2025-06-20 11:59:13 -06:00
|
|
|
|
// 6) ServActivos (si es necesario)
|
|
|
|
|
|
switch (r.tipo.trim()) {
|
2025-06-18 12:54:26 -06:00
|
|
|
|
|
2025-06-30 12:53:08 -06:00
|
|
|
|
case 'Diplomado':
|
|
|
|
|
|
const servActivosDiplomado = this.servActivosRepo.create({
|
|
|
|
|
|
Correo: true,
|
|
|
|
|
|
usuario: user,
|
|
|
|
|
|
RedStatus: 'Inactivo',
|
|
|
|
|
|
ATStatus: 'Inactivo',
|
|
|
|
|
|
CorreoStatus: 'Inactivo',
|
|
|
|
|
|
PrestamosStatus: 'Inactivo',
|
|
|
|
|
|
});
|
|
|
|
|
|
await this.servActivosRepo.save(servActivosDiplomado);
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'Extra Largo':
|
|
|
|
|
|
const servActivosExtraLargo = this.servActivosRepo.create({
|
|
|
|
|
|
Prestamos: true,
|
|
|
|
|
|
Correo: true,
|
|
|
|
|
|
usuario: user,
|
|
|
|
|
|
RedStatus: 'Inactivo',
|
|
|
|
|
|
ATStatus: 'Inactivo',
|
|
|
|
|
|
CorreoStatus: 'Inactivo',
|
|
|
|
|
|
PrestamosStatus: 'Inactivo',
|
|
|
|
|
|
});
|
|
|
|
|
|
await this.servActivosRepo.save(servActivosExtraLargo);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
case 'Servicio Social':
|
|
|
|
|
|
const servActivosServicio = this.servActivosRepo.create({
|
|
|
|
|
|
Red: true,
|
|
|
|
|
|
AT: true,
|
|
|
|
|
|
Correo: true,
|
|
|
|
|
|
usuario: user,
|
|
|
|
|
|
RedStatus: 'Inactivo',
|
|
|
|
|
|
ATStatus: 'Inactivo',
|
|
|
|
|
|
CorreoStatus: 'Inactivo',
|
|
|
|
|
|
PrestamosStatus: 'Inactivo',
|
|
|
|
|
|
});
|
|
|
|
|
|
await this.servActivosRepo.save(servActivosServicio);
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-13 17:54:38 -06:00
|
|
|
|
|
2025-06-30 12:53:08 -06:00
|
|
|
|
case 'Idiomas R (UNAM)':
|
|
|
|
|
|
case 'Idiomas Sabatino':
|
|
|
|
|
|
const servActivosIdiomas = this.servActivosRepo.create({
|
|
|
|
|
|
Red: true,
|
|
|
|
|
|
Correo: true,
|
|
|
|
|
|
usuario: user,
|
|
|
|
|
|
RedStatus: 'Inactivo',
|
|
|
|
|
|
ATStatus: 'Inactivo',
|
|
|
|
|
|
CorreoStatus: 'Inactivo',
|
|
|
|
|
|
PrestamosStatus: 'Inactivo',
|
|
|
|
|
|
});
|
|
|
|
|
|
await this.servActivosRepo.save(servActivosIdiomas);
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 'Reinscrito':
|
|
|
|
|
|
case 'Posgrado':
|
|
|
|
|
|
case 'Intercambio UNAM':
|
|
|
|
|
|
case 'Movilidad':
|
|
|
|
|
|
case 'Ampliación de Conocimiento':
|
2025-06-20 11:59:13 -06:00
|
|
|
|
case 'Licenciatura':
|
|
|
|
|
|
case 'Profesor':
|
|
|
|
|
|
const servActivos = this.servActivosRepo.create({
|
|
|
|
|
|
|
|
|
|
|
|
Red: true,
|
|
|
|
|
|
AT: true,
|
|
|
|
|
|
Correo: true,
|
|
|
|
|
|
Prestamos: true,
|
|
|
|
|
|
usuario: user,
|
|
|
|
|
|
RedStatus: 'Inactivo',
|
|
|
|
|
|
ATStatus: 'Inactivo',
|
|
|
|
|
|
CorreoStatus: 'Inactivo',
|
|
|
|
|
|
PrestamosStatus: 'Inactivo',
|
|
|
|
|
|
});
|
|
|
|
|
|
await this.servActivosRepo.save(servActivos);
|
|
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case 'Trabajadores':
|
|
|
|
|
|
const servActivosTrabajadores = this.servActivosRepo.create({
|
|
|
|
|
|
Red: true,
|
|
|
|
|
|
usuario: user,
|
|
|
|
|
|
RedStatus: 'Inactivo',
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
await this.servActivosRepo.save(servActivosTrabajadores);
|
|
|
|
|
|
break;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|
2025-06-30 12:53:08 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return { inserted: rows.length - contador, id_movimiento: movimiento.id_mov, errors: errs };
|
2025-06-13 17:54:38 -06:00
|
|
|
|
}
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-20 11:59:13 -06:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
// async generateCsv(): Promise<string> {
|
|
|
|
|
|
// const users = await this.usuarioRepo.find({
|
|
|
|
|
|
// relations: ['genero', 'carreraUsuarios'], // si quieres más info
|
|
|
|
|
|
// select: ['num_cuenta', 'nombre', 'a_paterno', 'a_materno', 'rfc', 'fecha_nacimiento', 'generacion'],
|
|
|
|
|
|
// });
|
|
|
|
|
|
|
|
|
|
|
|
// const header = [
|
|
|
|
|
|
// 'num_cuenta', 'nombre', 'a_paterno', 'a_materno',
|
|
|
|
|
|
// 'carrera', 'rfc', 'fecha_nacimiento', 'generacion'
|
|
|
|
|
|
// ].join('\t') + '\n';
|
|
|
|
|
|
|
|
|
|
|
|
// const rows = users.map(u => [
|
|
|
|
|
|
// u.num_cuenta,
|
|
|
|
|
|
// u.nombre,
|
|
|
|
|
|
// u.a_paterno,
|
|
|
|
|
|
// u.a_materno,
|
|
|
|
|
|
// u.rfc ?? '',
|
|
|
|
|
|
// u.fecha_nacimiento,
|
|
|
|
|
|
// (u.generacion != null ? u.generacion.toString() : '')
|
|
|
|
|
|
|
|
|
|
|
|
// ].join('\t')).join('\n');
|
|
|
|
|
|
|
|
|
|
|
|
// return header + rows;
|
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
async generateCsvCorreo(): Promise<string> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const tipos = [
|
|
|
|
|
|
'Diplomado',
|
|
|
|
|
|
'Extra Largo',
|
|
|
|
|
|
'Ampliación de Conocimiento',
|
|
|
|
|
|
'Servicio Social',
|
|
|
|
|
|
'Movilidad',
|
|
|
|
|
|
'Intercambio UNAM',
|
|
|
|
|
|
'Idiomas Sabatino',
|
|
|
|
|
|
'Idiomas R (UNAM)',
|
|
|
|
|
|
'Trabajadores',
|
|
|
|
|
|
'Profesor',
|
|
|
|
|
|
'Oyente',
|
|
|
|
|
|
'Posgrado',
|
|
|
|
|
|
'Reinscrito',
|
|
|
|
|
|
'Licenciatura',
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
const usuarios = await this.usuarioRepo.find({
|
|
|
|
|
|
where: {
|
|
|
|
|
|
usuarioTipos: {
|
|
|
|
|
|
tipoUsuario: {
|
|
|
|
|
|
tipo_usuario: In(tipos),
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2025-06-20 11:59:13 -06:00
|
|
|
|
servActivo: {
|
|
|
|
|
|
CorreoStatus: 'Inactivo', // solo los que no tienen servicio activo
|
|
|
|
|
|
}
|
2025-06-18 07:33:21 -06:00
|
|
|
|
},
|
|
|
|
|
|
relations: [
|
|
|
|
|
|
'genero',
|
|
|
|
|
|
'carreraUsuarios', // relación intermedia
|
|
|
|
|
|
'carreraUsuarios.carrera', // carrera asociada a través de la intermedia
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
|
|
});
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
const header = [
|
|
|
|
|
|
'num_cuenta', 'nombre', 'a_paterno', 'a_materno',
|
|
|
|
|
|
'carreras', 'rfc', 'fecha_nacimiento', 'generacion'
|
|
|
|
|
|
].join('\t') + '\n';
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
const rows = usuarios.map(u => {
|
|
|
|
|
|
const carreras = (u.carreraUsuarios ?? [])
|
|
|
|
|
|
.map(cu => cu.carrera?.carrera ?? '') // obtenemos solo el nombre
|
|
|
|
|
|
.join(', '); // une todas las carreras en un solo string
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
u.num_cuenta,
|
|
|
|
|
|
u.nombre,
|
|
|
|
|
|
u.a_paterno,
|
|
|
|
|
|
u.a_materno,
|
|
|
|
|
|
carreras,
|
|
|
|
|
|
u.rfc ?? '',
|
|
|
|
|
|
u.fecha_nacimiento,
|
|
|
|
|
|
u.generacion?.toString() ?? ''
|
|
|
|
|
|
].join('\t');
|
|
|
|
|
|
}).join('\n');
|
|
|
|
|
|
|
|
|
|
|
|
const tablaCompleta = header + rows;
|
|
|
|
|
|
|
|
|
|
|
|
return tablaCompleta;
|
|
|
|
|
|
}
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
async generateCsvSolicita(): Promise<string> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const tipos = [
|
|
|
|
|
|
|
|
|
|
|
|
'Extra Largo',
|
|
|
|
|
|
'Ampliación de Conocimiento',
|
|
|
|
|
|
'Movilidad',
|
|
|
|
|
|
'Intercambio UNAM',
|
|
|
|
|
|
'Profesor',
|
|
|
|
|
|
'Posgrado',
|
|
|
|
|
|
'Reinscrito',
|
|
|
|
|
|
'Licenciatura',
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
const usuarios = await this.usuarioRepo.find({
|
|
|
|
|
|
where: {
|
|
|
|
|
|
usuarioTipos: {
|
|
|
|
|
|
tipoUsuario: {
|
|
|
|
|
|
tipo_usuario: In(tipos),
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2025-06-20 11:59:13 -06:00
|
|
|
|
servActivo: {
|
|
|
|
|
|
PrestamosStatus: 'Inactivo', // solo los que no tienen servicio activo
|
|
|
|
|
|
}
|
2025-06-18 07:33:21 -06:00
|
|
|
|
},
|
|
|
|
|
|
relations: [
|
|
|
|
|
|
'genero',
|
|
|
|
|
|
'carreraUsuarios', // relación intermedia
|
|
|
|
|
|
'carreraUsuarios.carrera', // carrera asociada a través de la intermedia
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
|
|
});
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-18 12:54:26 -06:00
|
|
|
|
const usuariosTipo = await this.usuarioRepo.find({
|
|
|
|
|
|
|
|
|
|
|
|
relations: [
|
|
|
|
|
|
'genero',
|
|
|
|
|
|
'carreraUsuarios', // relación intermedia
|
|
|
|
|
|
'carreraUsuarios.carrera',
|
|
|
|
|
|
'usuarioTipos', // relación con tipo de usuario
|
|
|
|
|
|
'usuarioTipos.tipoUsuario', // tipo de usuario asociado
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
console.log('Usuarios encontrados:', usuariosTipo);
|
|
|
|
|
|
|
|
|
|
|
|
console.log('Usuarios encontrados:', usuarios);
|
|
|
|
|
|
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
const header = [
|
|
|
|
|
|
'num_cuenta', 'nombre', 'a_paterno', 'a_materno',
|
|
|
|
|
|
'carreras', 'rfc'
|
|
|
|
|
|
].join('\t') + '\n';
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
const rows = usuarios.map(u => {
|
|
|
|
|
|
const carreras = (u.carreraUsuarios ?? [])
|
|
|
|
|
|
.map(cu => cu.carrera?.carrera ?? '') // obtenemos solo el nombre
|
|
|
|
|
|
.join(', '); // une todas las carreras en un solo string
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
return [
|
|
|
|
|
|
u.num_cuenta,
|
|
|
|
|
|
u.nombre,
|
|
|
|
|
|
u.a_paterno,
|
|
|
|
|
|
u.a_materno,
|
|
|
|
|
|
carreras,
|
|
|
|
|
|
u.rfc ?? '',
|
|
|
|
|
|
].join('\t');
|
|
|
|
|
|
}).join('\n');
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
const tablaCompleta = header + rows;
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
return tablaCompleta;
|
|
|
|
|
|
}
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
async generateCsvRed(): Promise<string> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const tipos = [
|
|
|
|
|
|
'Ampliación de Conocimiento',
|
|
|
|
|
|
'Servicio Social',
|
|
|
|
|
|
'Movilidad',
|
|
|
|
|
|
'Intercambio UNAM',
|
|
|
|
|
|
'Idiomas Sabatino',
|
|
|
|
|
|
'Idiomas R (UNAM)',
|
|
|
|
|
|
'Trabajadores',
|
|
|
|
|
|
'Profesor',
|
|
|
|
|
|
'Posgrado',
|
|
|
|
|
|
'Reinscrito',
|
|
|
|
|
|
'Licenciatura',
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
const usuarios = await this.usuarioRepo.find({
|
|
|
|
|
|
where: {
|
|
|
|
|
|
usuarioTipos: {
|
|
|
|
|
|
tipoUsuario: {
|
|
|
|
|
|
tipo_usuario: In(tipos),
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2025-06-20 11:59:13 -06:00
|
|
|
|
servActivo: {
|
|
|
|
|
|
RedStatus: 'Inactivo',
|
|
|
|
|
|
}
|
2025-06-18 07:33:21 -06:00
|
|
|
|
},
|
|
|
|
|
|
relations: [
|
|
|
|
|
|
'genero',
|
|
|
|
|
|
'carreraUsuarios', // relación intermedia
|
|
|
|
|
|
'carreraUsuarios.carrera', // carrera asociada a través de la intermedia
|
|
|
|
|
|
],
|
2025-06-18 12:54:26 -06:00
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
});
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const header = [
|
2025-06-18 07:33:21 -06:00
|
|
|
|
'num_cuenta', 'fecha_nacimiento'
|
2025-06-16 11:26:17 -06:00
|
|
|
|
].join('\t') + '\n';
|
|
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
const rows = usuarios.map(u => {
|
|
|
|
|
|
return [
|
|
|
|
|
|
u.num_cuenta,
|
|
|
|
|
|
u.fecha_nacimiento,
|
|
|
|
|
|
].join('\t');
|
|
|
|
|
|
}).join('\n');
|
|
|
|
|
|
|
|
|
|
|
|
const tablaCompleta = header + rows;
|
2025-06-30 12:53:08 -06:00
|
|
|
|
console.log(tablaCompleta);
|
|
|
|
|
|
console.log('Usuarios encontrados:', usuarios);
|
2025-06-18 07:33:21 -06:00
|
|
|
|
return tablaCompleta;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
async generateCsvAT(): Promise<string> {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const tipos = [
|
|
|
|
|
|
'Ampliación de Conocimiento',
|
|
|
|
|
|
'Servicio Social',
|
|
|
|
|
|
'Movilidad',
|
|
|
|
|
|
'Intercambio UNAM',
|
|
|
|
|
|
'Profesor',
|
|
|
|
|
|
'Posgrado',
|
|
|
|
|
|
'Reinscrito',
|
|
|
|
|
|
'Licenciatura',
|
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
|
|
const usuarios = await this.usuarioRepo.find({
|
|
|
|
|
|
where: {
|
|
|
|
|
|
usuarioTipos: {
|
|
|
|
|
|
tipoUsuario: {
|
|
|
|
|
|
tipo_usuario: In(tipos),
|
|
|
|
|
|
},
|
|
|
|
|
|
},
|
2025-06-20 11:59:13 -06:00
|
|
|
|
servActivo: {
|
|
|
|
|
|
ATStatus: 'Inactivo',
|
2025-06-30 12:53:08 -06:00
|
|
|
|
AT: true
|
2025-06-20 11:59:13 -06:00
|
|
|
|
},
|
2025-06-18 07:33:21 -06:00
|
|
|
|
},
|
|
|
|
|
|
relations: [
|
|
|
|
|
|
'genero',
|
|
|
|
|
|
'carreraUsuarios', // relación intermedia
|
|
|
|
|
|
'carreraUsuarios.carrera', // carrera asociada a través de la intermedia
|
|
|
|
|
|
],
|
|
|
|
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const header = [
|
|
|
|
|
|
'num_cuenta', 'nombre', 'a_paterno', 'a_materno',
|
|
|
|
|
|
'carreras', 'rfc', 'fecha_nacimiento', 'generacion'
|
|
|
|
|
|
].join('\t') + '\n';
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-18 07:33:21 -06:00
|
|
|
|
const rows = usuarios.map(u => {
|
|
|
|
|
|
const carreras = (u.carreraUsuarios ?? [])
|
|
|
|
|
|
.map(cu => cu.carrera?.carrera ?? '') // obtenemos solo el nombre
|
|
|
|
|
|
.join(', '); // une todas las carreras en un solo string
|
|
|
|
|
|
|
|
|
|
|
|
return [
|
|
|
|
|
|
u.num_cuenta,
|
|
|
|
|
|
u.nombre,
|
|
|
|
|
|
u.a_paterno,
|
|
|
|
|
|
u.a_materno,
|
|
|
|
|
|
carreras,
|
|
|
|
|
|
u.fecha_nacimiento,
|
|
|
|
|
|
u.generacion?.toString() ?? ''
|
|
|
|
|
|
].join('\t');
|
|
|
|
|
|
}).join('\n');
|
|
|
|
|
|
|
|
|
|
|
|
const tablaCompleta = header + rows;
|
|
|
|
|
|
|
|
|
|
|
|
return tablaCompleta;
|
2025-06-16 11:26:17 -06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-20 11:59:13 -06:00
|
|
|
|
//Funcion que se le debe hacer su propio módulo
|
|
|
|
|
|
async activarServicios(id_movimiento: number, origen: string): Promise<void> {
|
|
|
|
|
|
if (!['AT', 'SOLICITA', 'CORREO', 'RED'].includes(origen)) {
|
|
|
|
|
|
throw new BadRequestException(`Origen ${origen} no válido`);
|
|
|
|
|
|
}
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-20 11:59:13 -06:00
|
|
|
|
const usuarios = await this.usuarioRepo.find({
|
|
|
|
|
|
where: { movimiento: { id_mov: id_movimiento } },
|
|
|
|
|
|
});
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-20 11:59:13 -06:00
|
|
|
|
if (!usuarios || usuarios.length === 0) {
|
|
|
|
|
|
throw new BadRequestException('No se encontró el usuario asociado al movimiento');
|
|
|
|
|
|
}
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-20 11:59:13 -06:00
|
|
|
|
for (const usuario of usuarios) {
|
|
|
|
|
|
const servActivos = await this.servActivosRepo.findOne({
|
|
|
|
|
|
where: { usuario: { id_usuario: usuario.id_usuario } },
|
|
|
|
|
|
});
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-20 11:59:13 -06:00
|
|
|
|
if (!servActivos) {
|
|
|
|
|
|
throw new BadRequestException(
|
|
|
|
|
|
`No se encontraron servicios activos para el usuario con ID ${usuario.id_usuario}`,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-20 11:59:13 -06:00
|
|
|
|
const timestamp = 'Activo ' + new Date().toISOString();
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-20 11:59:13 -06:00
|
|
|
|
if (origen === 'AT' && servActivos.AT) {
|
|
|
|
|
|
servActivos.ATStatus = timestamp;
|
|
|
|
|
|
} else if (origen === 'RED' && servActivos.Red) {
|
|
|
|
|
|
servActivos.RedStatus = timestamp;
|
|
|
|
|
|
} else if (origen === 'CORREO' && servActivos.Correo) {
|
|
|
|
|
|
servActivos.CorreoStatus = timestamp;
|
|
|
|
|
|
} else if (origen === 'SOLICITA' && servActivos.Prestamos) {
|
|
|
|
|
|
servActivos.PrestamosStatus = timestamp;
|
|
|
|
|
|
} else {
|
|
|
|
|
|
throw new BadRequestException(
|
|
|
|
|
|
`Origen ${origen} no válido o servicio no activo para el usuario con ID ${usuario.id_usuario}`,
|
|
|
|
|
|
);
|
|
|
|
|
|
}
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-20 11:59:13 -06:00
|
|
|
|
await this.servActivosRepo.save(servActivos);
|
|
|
|
|
|
console.log(`Servicio ${servActivos}`);
|
|
|
|
|
|
}
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-20 11:59:13 -06:00
|
|
|
|
}
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
2025-06-30 12:53:08 -06:00
|
|
|
|
async enviarCargaMasiva(): Promise<void> {
|
|
|
|
|
|
const usuarios_red = await this.usuariosDelSistemaRepo.findOne({ where: { origen: { origen: 'RED' } } });
|
|
|
|
|
|
const usuarios_at = await this.usuariosDelSistemaRepo.findOne({ where: { origen: { origen: 'AT' } } });
|
|
|
|
|
|
const usuarios_correo = await this.usuariosDelSistemaRepo.findOne({ where: { origen: { origen: 'CORREO' } } });
|
|
|
|
|
|
const usuarios_solicita = await this.usuariosDelSistemaRepo.findOne({ where: { origen: { origen: 'SOLICITA' } } });
|
|
|
|
|
|
if (!usuarios_red || !usuarios_at || !usuarios_correo || !usuarios_solicita) {
|
|
|
|
|
|
throw new BadRequestException('No se encontraron usuarios para enviar correo');
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Aquí puedes enviar el correo usando MailService
|
|
|
|
|
|
await this.mailService.sendMail({
|
|
|
|
|
|
to: usuarios_red.correo ?? '',
|
|
|
|
|
|
subject: 'Carga de Usuarios',
|
|
|
|
|
|
text: 'Se ha realizado una carga de usuarios en el sistema.',
|
|
|
|
|
|
html: '',
|
|
|
|
|
|
});
|
|
|
|
|
|
await this.mailService.sendMail({
|
|
|
|
|
|
to: usuarios_at.correo ?? '',
|
|
|
|
|
|
subject: 'Carga de Usuarios',
|
|
|
|
|
|
text: 'Se ha realizado una carga de usuarios en el sistema.',
|
|
|
|
|
|
html: '',
|
|
|
|
|
|
});
|
|
|
|
|
|
await this.mailService.sendMail({
|
|
|
|
|
|
to: usuarios_correo.correo ?? '',
|
|
|
|
|
|
subject: 'Carga de Usuarios',
|
|
|
|
|
|
text: 'Se ha realizado una carga de usuarios en el sistema.',
|
|
|
|
|
|
html: '',
|
|
|
|
|
|
});
|
|
|
|
|
|
await this.mailService.sendMail({
|
|
|
|
|
|
to: usuarios_solicita.correo ?? '',
|
|
|
|
|
|
subject: 'Carga de Usuarios',
|
|
|
|
|
|
text: 'Se ha realizado una carga de usuarios en el sistema.',
|
|
|
|
|
|
html: '',
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
console.log(`Correo enviados`);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2025-06-16 11:26:17 -06:00
|
|
|
|
|
|
|
|
|
|
|
2025-06-13 17:54:38 -06:00
|
|
|
|
}
|