UNPKG

@openade/fe

Version:

Fatturazione Elettronica - Electronic Invoicing for Sistema di Interscambio (SDI)

167 lines 6.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SDIFTPClient = void 0; const ssh2_1 = require("ssh2"); class SDIFTPClient { constructor(config) { this.config = { port: 22, uploadDir: '/in', downloadDir: '/out', timeout: 30000, debug: false, ...config, }; } async sendInvoice(filename, xmlContent) { if (this.config.debug) { console.log(`[SDIFTP] Sending invoice: ${filename}`); } try { const result = await this.sendViaSdiFtp(filename, xmlContent); return { success: true, identifcativoSdI: result.identificativoSdI || 'PENDING', }; } catch (error) { return { success: false, error: error instanceof Error ? error.message : 'Unknown error', }; } } async downloadReceipts() { if (this.config.debug) { console.log(`[SDIFTP] Downloading receipts from ${this.config.downloadDir}`); } try { return new Promise((resolve, reject) => { const conn = new ssh2_1.Client(); conn.on('ready', () => { if (this.config.debug) { console.log('[SDIFTP] SFTP connection established'); } conn.sftp((err, sftp) => { if (err) { reject(err); return; } sftp.readdir(this.config.downloadDir, (err, list) => { if (err) { reject(err); return; } if (this.config.debug) { console.log(`[SDIFTP] Found ${list.length} receipt files`); } const receipts = []; let processed = 0; if (list.length === 0) { conn.end(); resolve(receipts); return; } list.forEach((file) => { const filePath = `${this.config.downloadDir}/${file.filename}`; sftp.readFile(filePath, (err, data) => { if (err) { console.error(`[SDIFTP] Error reading file ${file.filename}:`, err); } else { receipts.push({ filename: file.filename, content: data.toString('utf8'), }); if (this.config.debug) { console.log(`[SDIFTP] Receipt downloaded: ${file.filename}`); } } processed++; if (processed === list.length) { conn.end(); resolve(receipts); } }); }); }); }); }); conn.on('error', (err) => { reject(err); }); const connectOptions = { host: this.config.host, port: this.config.port, username: this.config.username, }; if (this.config.privateKey) { connectOptions.privateKey = this.config.privateKey; } else if (this.config.password) { connectOptions.password = this.config.password; } else { reject(new Error('Either privateKey or password must be provided')); return; } conn.connect(connectOptions); }); } catch (error) { throw new Error(`SFTP operation failed: ${error instanceof Error ? error.message : 'Unknown error'}`); } } async sendViaSdiFtp(filename, xmlContent) { if (this.config.debug) { console.log(`[SDIFTP] Uploading via SFTP: ${filename}`); } return new Promise((resolve, reject) => { const conn = new ssh2_1.Client(); conn.on('ready', () => { if (this.config.debug) { console.log('[SDIFTP] SFTP connection established'); } conn.sftp((err, sftp) => { if (err) { reject(err); return; } const remotePath = `${this.config.uploadDir}/${filename}`; const buffer = Buffer.from(xmlContent, 'utf8'); sftp.writeFile(remotePath, buffer, (err) => { if (err) { reject(err); return; } if (this.config.debug) { console.log(`[SDIFTP] File uploaded successfully: ${remotePath}`); } resolve({}); }); }); }); conn.on('error', (err) => { reject(err); }); const connectOptions = { host: this.config.host, port: this.config.port, username: this.config.username, }; if (this.config.privateKey) { connectOptions.privateKey = this.config.privateKey; } else if (this.config.password) { connectOptions.password = this.config.password; } else { reject(new Error('Either privateKey or password must be provided')); return; } conn.connect(connectOptions); }); } } exports.SDIFTPClient = SDIFTPClient; //# sourceMappingURL=sdiftp.client.js.map