Files

49 lines
1.5 KiB
JavaScript
Raw Permalink Normal View History

2022-04-17 22:23:39 -05:00
const csv = require('csvtojson');
const { eliminarArchivo } = require('../../helper/helper');
const Profesor = require('../../db/tablas/Profesor');
2022-05-13 22:10:54 -05:00
const { validarNumeroEntero } = require('../../helper/validar');
2022-05-18 20:26:06 -05:00
const ProfesorPremiacion = require('../../db/tablas/ProfesorPremiacion');
2022-04-17 22:23:39 -05:00
2022-05-13 22:10:54 -05:00
const cargaMasiva = async (file, body) => {
const idPremiacion = validarNumeroEntero(body.idPremiacion);
2022-04-17 22:23:39 -05:00
const path = `server/uploads/${file}`;
let res = [];
await csv()
.fromFile(path)
.then(async (profesores) => {
for (let i = 0; i < profesores.length; i++) {
2022-05-14 23:41:03 -05:00
let profesor = await Profesor.findOne({
where: { numeroTrabajador: profesores[i].numeroTrabajador },
2022-05-13 22:10:54 -05:00
});
2022-05-14 23:41:03 -05:00
let resp;
if (!profesor) {
resp = await Profesor.create({
numeroTrabajador: profesores[i].numeroTrabajador,
nombre: profesores[i].nombre,
correo: `${profesores[i].numeroTrabajador}@pcpuma.acatlan.unam.mx`,
adscripcion: profesores[i].adscripcion,
});
}
profesor ? res.push(profesor) : res.push(resp);
2022-05-13 22:10:54 -05:00
await ProfesorPremiacion.create({
idPremiacion,
2022-05-14 23:41:03 -05:00
idProfesor: profesor ? profesor.idProfesor : resp.idProfesor,
2022-05-13 22:10:54 -05:00
numeroInvitados: null,
numeroInvitadosDentro: null,
});
2022-05-14 23:41:03 -05:00
profesor = null;
2022-04-17 22:23:39 -05:00
}
await eliminarArchivo(path);
});
return {
message: 'Se subió correctamente el archivo csv.',
2022-05-13 22:10:54 -05:00
res,
2022-04-17 22:23:39 -05:00
};
};
2022-05-13 22:10:54 -05:00
module.exports = cargaMasiva;