herramientaenvioestupendo 1.0.26 → 1.0.27
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.
- package/package.json +1 -1
- package/src/index.js +1 -2
- package/src/services.js +79 -33
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -3,12 +3,11 @@ import { program } from "commander"
|
|
|
3
3
|
import { sendTxt, sendXml, sendAttached } from "./services.js"
|
|
4
4
|
|
|
5
5
|
|
|
6
|
-
program.name("estupendo").version("1.0.
|
|
6
|
+
program.name("estupendo").version("1.0.27")
|
|
7
7
|
|
|
8
8
|
program.command("send_xml")
|
|
9
9
|
.requiredOption("-d, --directory <char>", "Directorio donde se encuentran los documentos XML")
|
|
10
10
|
.option("-p, --prod", "Producción, si no es enviado recibe el api pruebas")
|
|
11
|
-
.requiredOption("-n, --nit <char>", "Nit de la empresa")
|
|
12
11
|
.description("Envio masivos XML")
|
|
13
12
|
.action(sendXml)
|
|
14
13
|
|
package/src/services.js
CHANGED
|
@@ -8,40 +8,86 @@ const __filename = fileURLToPath(import.meta.url)
|
|
|
8
8
|
const __dirname = path.dirname(__filename)
|
|
9
9
|
|
|
10
10
|
//envio aumaticos XML
|
|
11
|
-
|
|
12
11
|
const sendXml = async (options) => {
|
|
13
12
|
if (!fs.existsSync(options.directory)) {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
13
|
+
console.error("La carpeta no existe")
|
|
14
|
+
return
|
|
15
|
+
}
|
|
16
|
+
const archivos = fs.readdirSync(options.directory)
|
|
17
|
+
const pattern = /.xml$/
|
|
18
|
+
const txts = archivos.filter(archivo => pattern.test(archivo))
|
|
19
|
+
let urlBase = "https://pruebas.estupendo.com.co/api"
|
|
20
|
+
if (options.prod) {
|
|
21
|
+
urlBase = "https://app.estupendo.com.co/api"
|
|
22
|
+
}
|
|
23
|
+
const urlApiConsulta = `${urlBase}/CYP/consultarEstadoDocumento`;
|
|
24
|
+
const urlApi = `${urlBase}/CYP`;
|
|
25
|
+
const output = [];
|
|
26
|
+
const pdfDir = path.join(process.cwd(), "pdfs");
|
|
27
|
+
const xmlDir = path.join(process.cwd(), "xmls");
|
|
28
|
+
const procesadosDir = path.join(process.cwd(), "procesados");
|
|
29
|
+
|
|
30
|
+
// Crear carpetas si no existen
|
|
31
|
+
if (!fs.existsSync(pdfDir)) fs.mkdirSync(pdfDir);
|
|
32
|
+
if (!fs.existsSync(xmlDir)) fs.mkdirSync(xmlDir);
|
|
33
|
+
if (!fs.existsSync(procesadosDir)) fs.mkdirSync(procesadosDir);
|
|
34
|
+
console.log("Procesando...")
|
|
35
|
+
for (let archivo of txts) {
|
|
36
|
+
const name = path.join(options.directory, archivo)
|
|
37
|
+
const content = fs.readFileSync(name, { encoding: "utf-8" })
|
|
38
|
+
console.log(`Procesando Archivo: ${archivo}`);
|
|
39
|
+
const encoded = Buffer.from(content).toString("base64");
|
|
40
|
+
try {
|
|
41
|
+
const data =await request(urlApi, {xml: encoded})
|
|
42
|
+
console.log(data)
|
|
43
|
+
// Validar que el documento fue autorizado (estado === 2)
|
|
44
|
+
if (data.estado === 2 ) {
|
|
45
|
+
console.log(urlApiConsulta)
|
|
46
|
+
const dataconsult =await request(urlApiConsulta, {nit: "891400754", numeral: data.documento_id})
|
|
47
|
+
console.log(dataconsult)
|
|
48
|
+
|
|
49
|
+
if (dataconsult.result === true) {
|
|
50
|
+
const pdfUrl = dataconsult.pdf;
|
|
51
|
+
const pdfPath = path.join(pdfDir, `${data.documento_id}.pdf`);
|
|
52
|
+
try {
|
|
53
|
+
const response = await axios.get(pdfUrl, { responseType: 'arraybuffer' });
|
|
54
|
+
fs.writeFileSync(pdfPath, response.data);
|
|
55
|
+
console.log(`PDF guardado: ${pdfPath}`);
|
|
56
|
+
} catch (error) {
|
|
57
|
+
console.error(`Error descargando PDF de ${data.documento_id}:`, error.message);
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
const xmlUrl = dataconsult.xml;
|
|
61
|
+
const xmlPath = path.join(xmlDir, `${data.documento_id}.xml`);
|
|
62
|
+
try {
|
|
63
|
+
const response = await axios.get(xmlUrl, { responseType: 'arraybuffer' });
|
|
64
|
+
fs.writeFileSync(xmlPath, response.data);
|
|
65
|
+
console.log(`XML guardado: ${xmlPath}`);
|
|
66
|
+
} catch (error) {
|
|
67
|
+
console.error(`Error descargando XML de ${data.documento_id}:`, error.message);
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
// Mover archivo .txt a carpeta "procesados"
|
|
71
|
+
const destino = path.join(procesadosDir, archivo);
|
|
72
|
+
fs.renameSync(name, destino);
|
|
73
|
+
console.log(`Archivo movido a: ${destino}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
} else {
|
|
77
|
+
console.log(`Documento no autorizado o con estado diferente de 2. Archivo: ${archivo}`);
|
|
78
|
+
}
|
|
79
|
+
output.push(data)
|
|
80
|
+
} catch (error) {
|
|
81
|
+
console.error(`Error al procesar el archivo ${archivo}:`, error);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
}
|
|
86
|
+
const now = new Date();
|
|
87
|
+
const fecha = now.toISOString().slice(0, 19).replace("T", "_").replace(/:/g, "-");
|
|
88
|
+
const filenameDate = `Output_txt_${fecha}.json`;
|
|
89
|
+
const ruta = path.join(process.cwd(), filenameDate)
|
|
90
|
+
fs.appendFile(ruta, JSON.stringify(output, null, 4), () => { })
|
|
45
91
|
}
|
|
46
92
|
|
|
47
93
|
// envios automaticos TXT
|
|
@@ -51,7 +97,7 @@ const sendTxt = async (options) => {
|
|
|
51
97
|
return
|
|
52
98
|
}
|
|
53
99
|
const archivos = fs.readdirSync(options.directory)
|
|
54
|
-
const pattern = /.
|
|
100
|
+
const pattern = /.txt$/
|
|
55
101
|
const txts = archivos.filter(archivo => pattern.test(archivo))
|
|
56
102
|
let urlBase = "https://pruebas.estupendo.com.co/api"
|
|
57
103
|
if (options.prod) {
|