module.exports = function(comprobante, nameFile) { var nameFileDest = nameFile.split('\\')[nameFile.split('\\').length - 1]; knex('planex_transacciones') .insert({ archivoRecibido: nameFileDest, estado: 0, // Ingresada GLN: comprobante.cabecera.GLN }) .returning('id') .then(saveDebo); function saveDebo(id) { comprobante.cabecera.idTransaccion = id; var promesas = [knex('PMAEFACT_TEMP').insert(comprobante.cabecera)]; comprobante.cuerpo.forEach(cuerpo => { promesas.push(knex('PMOVFACT_TEMP').insert(cuerpo)); }); Promise.all(promesas).then(function() { var pathDest = config.dirDestino + '/' + nameFileDest; fs.rename(nameFile, pathDest, function(err) { if(err) console.log(err); console.log('COMPROBANTE PROCESADO Y GUARDADO EN PMAEFACT_TEMP CON ÉXITO'); }); }).catch(function(e) { console.log(e); console.log('ERROR AL GUARDAR EL COMPROBANTE'); }); } }