77 lines
3.7 KiB
JavaScript
77 lines
3.7 KiB
JavaScript
const ExcelJS = require("exceljs");
|
|
const { insertProfesionales } = require("./database");
|
|
|
|
async function processExcelFile(filePath) {
|
|
const workbook = new ExcelJS.Workbook();
|
|
try {
|
|
await workbook.xlsx.readFile(filePath);
|
|
|
|
const worksheet = workbook.worksheets[0];
|
|
const profesionales = [];
|
|
|
|
const parseCondicion = (condicion) => {
|
|
const cond = condicion.toLowerCase();
|
|
return {
|
|
sancionesSimples: (cond.match(/(\d+)\s*d[ií]as de sanci[oó]n simple/g) || []).length,
|
|
sancionesSeveras: (cond.match(/(\d+)\s*d[ií]as de sanci[oó]n severa/g) || []).length,
|
|
investigacionAbierta: /investigaci[oó]n (administrativa|penal) abierta/.test(cond),
|
|
juicioAbierto: /juicio abierto/.test(cond)
|
|
};
|
|
};
|
|
|
|
worksheet.eachRow({ includeEmpty: false }, (row, rowNumber) => {
|
|
if (rowNumber > 1) {
|
|
try {
|
|
const rawData = {
|
|
grado: row.getCell(2).value?.toString().trim() || '',
|
|
nombreApellido: row.getCell(3).value?.toString().trim() || '',
|
|
cedula: row.getCell(4).value?.toString().trim() || '',
|
|
condicion: (row.getCell(5).value?.toString().trim().toLowerCase() || "no posee novedades actualmente"),
|
|
conducta: Math.min(Math.max(parseInt(row.getCell(6).value || 0, 10), 0), 100),
|
|
companeros: (row.getCell(7).value?.toString().trim().replace(/[^0-9\/]/g, '') || "0/0"),
|
|
om_a: (row.getCell(8).value?.toString().trim().replace(/[^0-9\/]/g, '') || "0/0"),
|
|
niea: (() => {
|
|
const value = row.getCell(9).value?.toString().replace(/,/g, '.');
|
|
const parsed = parseFloat(value);
|
|
if (isNaN(parsed)) throw new Error(`Valor NIEA inválido: '${value}'`);
|
|
return parsed;
|
|
})()
|
|
};
|
|
|
|
const validarFormatoPT = (valor, campo) => {
|
|
if (!valor.includes('/')) throw new Error(`${campo}: Formato debe ser P/T`);
|
|
|
|
const partes = valor.split('/');
|
|
if (partes.length !== 2) throw new Error(`${campo}: Formato inválido`);
|
|
|
|
const [p, t] = partes;
|
|
const numP = parseInt(p, 10);
|
|
const numT = parseInt(t, 10);
|
|
|
|
if (isNaN(numP)) throw new Error(`${campo}: Posición no numérica ('${p}')`);
|
|
if (isNaN(numT)) throw new Error(`${campo}: Total no numérico ('${t}')`);
|
|
if (numT <= 0) throw new Error(`${campo}: Total debe ser > 0`);
|
|
if (numP <= 0) throw new Error(`${campo}: Posición debe ser > 0`);
|
|
if (numP >= numT) throw new Error(`${campo}: Posición (${p}) debe ser < Total (${t})`);
|
|
};
|
|
|
|
validarFormatoPT(rawData.companeros, "Compañeros");
|
|
validarFormatoPT(rawData.om_a, "OM-A");
|
|
|
|
profesionales.push(rawData);
|
|
} catch (error) {
|
|
throw new Error(`Fila ${rowNumber}: ${error.message}`);
|
|
}
|
|
}
|
|
});
|
|
|
|
await insertProfesionales(profesionales);
|
|
return profesionales;
|
|
|
|
} catch (error) {
|
|
console.error("Error procesando Excel:", error);
|
|
throw new Error(`Error en archivo: ${error.message}`);
|
|
}
|
|
}
|
|
|
|
module.exports = { processExcelFile }; |