bot-functions 1.5.1 → 1.6.0

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 +3 -1
package/index.js CHANGED
@@ -14,8 +14,11 @@ const _ = require('lodash');
14
14
  const { simpleParser } = require('mailparser');
15
15
  const xpath = require('xpath');
16
16
  const { JSDOM } = require('jsdom');
17
- const { param } = require('../retail-analyzer-models/src/routes');
18
17
 
18
+ const { S3Client } = require("@aws-sdk/client-s3");
19
+ const { Upload } = require("@aws-sdk/lib-storage");
20
+
21
+ const separador = (os.type() === "Windows_NT") ? `\\` : "/";
19
22
 
20
23
  async function obtenerCorreos(email, password, subject) {
21
24
  const config = {
@@ -376,8 +379,6 @@ async function manageEmails(config, searchOptions) {
376
379
  }
377
380
  }
378
381
 
379
- let separador = (os.type() === "Windows_NT") ? `\\` : "/";
380
-
381
382
  async function* asyncGenerator(final) {
382
383
  var i = 0;
383
384
  while (i < final) {
@@ -1375,6 +1376,84 @@ const uploadImageToS3 = async (filePath, key) => {
1375
1376
  return uploadFileResponse.data.data;
1376
1377
  }
1377
1378
 
1379
+ const sleepFile = (ms) => new Promise((r) => setTimeout(r, ms));
1380
+
1381
+ const validarZip = (zipPath) => {
1382
+ try {
1383
+ const zip = new AdmZip(zipPath);
1384
+ const entries = zip.getEntries();
1385
+
1386
+ if (entries.length === 0)
1387
+ throw new Error("El zip está vacío, no contiene archivos.");
1388
+
1389
+ // Verifica que cada entrada tenga contenido
1390
+ for (const entry of entries) {
1391
+ if (!entry.header || entry.header.size === 0)
1392
+ throw new Error(`La entrada "${entry.entryName}" está corrupta o vacía.`);
1393
+ }
1394
+ log.info(`[ZIP] Válido — ${entries.length} archivo(s) dentro.`);
1395
+ return true;
1396
+ } catch (error) {
1397
+ throw new Error(`[ZIP] Validación fallida: ${error.message}`);
1398
+ }
1399
+ };
1400
+
1401
+ const uploadEvidence = async (pathDownloads, params, reintentos = 3) => {
1402
+
1403
+ log.info(`[S3] Región: ${process.env.AWS_REGION_BUCKET} | Bucket: ${process.env.S3_BUCKET}`);
1404
+ const s3 = new S3Client({ region: process.env.AWS_REGION_BUCKET });
1405
+
1406
+ const zipPath = `${pathDownloads}/${params.reporte.filename}.zip`;
1407
+ const key = `${params.reporte.pathS3}/${params.reporte.filename}.zip`;
1408
+
1409
+ // Armar el zip
1410
+ const zip = new AdmZip();
1411
+ zip.addLocalFile(`${pathDownloads}/${params.reporte.filename}${process.env.EXTENSION_FILE}`);
1412
+ zip.writeZip(zipPath);
1413
+
1414
+ // Validar antes de intentar subir
1415
+ validarZip(zipPath);
1416
+
1417
+ let ultimoError;
1418
+
1419
+ for (let intento = 1; intento <= reintentos; intento++) {
1420
+ try {
1421
+ log.info(`[S3] Intento ${intento}/${reintentos} — subiendo ${key}`);
1422
+
1423
+ const upload = new Upload({
1424
+ client: s3,
1425
+ params: {
1426
+ Bucket: process.env.S3_BUCKET,
1427
+ Key: key,
1428
+ Body: fs.createReadStream(zipPath),
1429
+ ContentType: "application/zip",
1430
+ ACL: "public-read",
1431
+ },
1432
+ queueSize: 4,
1433
+ partSize: 10 * 1024 * 1024,
1434
+ });
1435
+
1436
+ await upload.done();
1437
+ const url = `https://${process.env.S3_BUCKET}.s3.${process.env.AWS_REGION_BUCKET}.amazonaws.com/${key}`;
1438
+ log.info(`[S3] Upload exitoso en intento ${intento} — ${key}`);
1439
+ await insertEvidenceCore(url, '', params.robot.baseHyperCube, params.robot.idgfc, process.env.PERIODO, params.reporte.date1, params.reporte.type);
1440
+ await insertLog2(params, 305);
1441
+ return key;
1442
+
1443
+ } catch (error) {
1444
+ ultimoError = error;
1445
+ log.error(`[S3] Intento ${intento} fallido: ${error.message}`);
1446
+
1447
+ if (intento < reintentos) {
1448
+ const espera = 2000 * intento;
1449
+ log.info(`[S3] Reintentando en ${espera / 1000}s...`);
1450
+ await sleepFile(espera);
1451
+ }
1452
+ }
1453
+ }
1454
+ throw new Error(`[S3] Upload fallido tras ${reintentos} intentos: ${ultimoError.message}`);
1455
+ };
1456
+
1378
1457
 
1379
1458
  module.exports = {
1380
1459
  waitNtype,
@@ -1431,4 +1510,5 @@ module.exports = {
1431
1510
  processDataDaily,
1432
1511
  uploadImageToS3,
1433
1512
  insertCatspecs,
1513
+ uploadEvidence
1434
1514
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "bot-functions",
3
- "version": "1.5.1",
3
+ "version": "1.6.0",
4
4
  "description": "Bot functions",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -9,6 +9,8 @@
9
9
  "author": "Juan Monrroy Reyes",
10
10
  "license": "ISC",
11
11
  "dependencies": {
12
+ "@aws-sdk/client-s3": "^3.1022.0",
13
+ "@aws-sdk/lib-storage": "^3.1022.0",
12
14
  "adm-zip": "0.5.10",
13
15
  "axios": "0.21.1",
14
16
  "concat-stream": "2.0.0",