bot-functions 1.7.0 → 1.8.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (2) hide show
  1. package/index.js +83 -3
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -909,11 +909,18 @@ const sendDataClickHouse = async (params, dataJSON) => {
909
909
  return new Promise(async function (resolve, reject) {
910
910
  actualFunction = "sendDataClickHouse";
911
911
  try {
912
+ payload = {
913
+ params,
914
+ data: dataJSON
915
+ }
912
916
  const batchPromises = [];
913
917
  const stringData = JSON.stringify(dataJSON);
914
918
  const response = await axios({
915
- method: 'post', url: process.env.INSERT_CLICKHOUSE, data: {params, dataJSON: stringData },
916
- maxBodyLength: Infinity, headers: { 'Accept': 'application/json', 'Content-Type': 'application/json' }
919
+ method: 'post',
920
+ url: process.env.INSERT_CLICKHOUSE,
921
+ data: payload,
922
+ maxBodyLength: Infinity,
923
+ headers: { 'Accept': 'application/json', 'Content-Type': 'application/json'}
917
924
  });
918
925
  batchPromises.push(response.status);
919
926
  batchPromises.map(reg => log.info('Insert status', reg));
@@ -1478,6 +1485,78 @@ const uploadEvidence = async (pathDownloads, params, reintentos = 3) => {
1478
1485
  throw new Error(`[S3] Upload fallido tras ${reintentos} intentos: ${ultimoError.message}`);
1479
1486
  };
1480
1487
 
1488
+ const insertBotEvidence = async (rutaFile, params) => {
1489
+ return new Promise(async function (resolve) {
1490
+
1491
+ await axios.post(process.env.INSERT_BOT_EVIDENCE, {
1492
+ idgfc: params.robot.idgfc,
1493
+ idpc: params.robot.idPC,
1494
+ proyecto: params.robot.proyecto.toUpperCase(),
1495
+ grupo: params.robot.cadena.toUpperCase(),
1496
+ cadena: params.robot.cadena.toUpperCase(),
1497
+ fecha: params.reporte.date1,
1498
+ tipo: params.reporte.type,
1499
+ fileInsert: rutaFile
1500
+ });
1501
+ resolve(true);
1502
+ });
1503
+ }
1504
+
1505
+ const uploadBotEvidence = async (pathDownloads, params, reintentos = 3) => {
1506
+
1507
+ log.info(`[S3] Región: ${process.env.AWS_REGION_BUCKET} | Bucket: ${process.env.S3_BUCKET}`);
1508
+ const s3 = new S3Client({ region: process.env.AWS_REGION_BUCKET });
1509
+
1510
+ const zipPath = `${pathDownloads}/${params.reporte.filename}.zip`;
1511
+ const key = `${params.reporte.pathS3}/${params.reporte.filename}.zip`;
1512
+
1513
+ // Armar el zip
1514
+ const zip = new AdmZip();
1515
+ zip.addLocalFile(`${pathDownloads}/${params.reporte.filename}${process.env.EXTENSION_FILE}`);
1516
+ zip.writeZip(zipPath);
1517
+
1518
+ // Validar antes de intentar subir
1519
+ validarZip(zipPath);
1520
+
1521
+ let ultimoError;
1522
+
1523
+ for (let intento = 1; intento <= reintentos; intento++) {
1524
+ try {
1525
+ log.info(`[S3] Intento ${intento}/${reintentos} — subiendo ${key}`);
1526
+
1527
+ const upload = new Upload({
1528
+ client: s3,
1529
+ params: {
1530
+ Bucket: process.env.S3_BUCKET,
1531
+ Key: key,
1532
+ Body: fs.createReadStream(zipPath),
1533
+ ContentType: "application/zip",
1534
+ ACL: "public-read",
1535
+ },
1536
+ queueSize: 4,
1537
+ partSize: 10 * 1024 * 1024,
1538
+ });
1539
+
1540
+ await upload.done();
1541
+ const url = `https://${process.env.S3_BUCKET}.s3.${process.env.AWS_REGION_BUCKET}.amazonaws.com/${key}`;
1542
+ log.info(`[S3] Upload exitoso en intento ${intento} — ${key}`);
1543
+ await insertBotEvidence(url, params);
1544
+ await insertLog2(params, 305);
1545
+ return key;
1546
+
1547
+ } catch (error) {
1548
+ ultimoError = error;
1549
+ log.error(`[S3] Intento ${intento} fallido: ${error.message}`);
1550
+
1551
+ if (intento < reintentos) {
1552
+ const espera = 2000 * intento;
1553
+ log.info(`[S3] Reintentando en ${espera / 1000}s...`);
1554
+ await sleepFile(espera);
1555
+ }
1556
+ }
1557
+ }
1558
+ throw new Error(`[S3] Upload fallido tras ${reintentos} intentos: ${ultimoError.message}`);
1559
+ };
1481
1560
 
1482
1561
  module.exports = {
1483
1562
  waitNtype,
@@ -1535,5 +1614,6 @@ module.exports = {
1535
1614
  uploadImageToS3,
1536
1615
  insertCatspecs,
1537
1616
  uploadEvidence,
1538
- sendDataClickHouse
1617
+ sendDataClickHouse,
1618
+ uploadBotEvidence
1539
1619
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bot-functions",
3
- "version": "1.7.0",
3
+ "version": "1.8.1",
4
4
  "description": "Bot functions",
5
5
  "main": "index.js",
6
6
  "scripts": {