const csv = require('csvtojson'); const { eliminarArchivo } = require('../../helper/helper'); const Profesor = require('../../db/tablas/Profesor'); const { validarNumeroEntero } = require('../../helper/validar'); const ProfesorPremiacion = require('../../db/tablas/ProfesorPremiacion'); const cargaMasiva = async (file, body) => { const idPremiacion = validarNumeroEntero(body.idPremiacion); const path = `server/uploads/${file}`; let res = []; await csv() .fromFile(path) .then(async (profesores) => { for (let i = 0; i < profesores.length; i++) { let profesor = await Profesor.findOne({ where: { numeroTrabajador: profesores[i].numeroTrabajador }, }); 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); await ProfesorPremiacion.create({ idPremiacion, idProfesor: profesor ? profesor.idProfesor : resp.idProfesor, numeroInvitados: null, numeroInvitadosDentro: null, }); profesor = null; } await eliminarArchivo(path); }); return { message: 'Se subió correctamente el archivo csv.', res, }; }; module.exports = cargaMasiva;