Skip to content

Commit

Permalink
feat: upload sftp WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
yxuo committed Mar 5, 2024
1 parent 906c604 commit 117c36a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 22 deletions.
10 changes: 8 additions & 2 deletions src/cnab/service/header-arquivo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import { DetalheBService } from './detalhe-b.service';
import { HeaderLoteService } from './header-lote.service';
import { PagadorService } from './pagador.service';
import { TransacaoService } from './transacao.service';
import { SftpService } from '../sftp/sftp.service';

@Injectable()
export class HeaderArquivoService {
Expand All @@ -51,19 +52,24 @@ export class HeaderArquivoService {
private detalheBService: DetalheBService,
private cnab104Service: Cnab104Service,
private banksService: BanksService,
private sftpService: SftpService,
) { }

public async saveRemessa(): Promise<void> {
const listAllTransacao = await this.transacaoService.getAll();
for (const transacao of listAllTransacao) {
if (!this.headerArquivoExists(transacao.id_transacao)) {
const { cnabTables } = await this.generateCnab(transacao);
const { cnabString, cnabTables } = await this.generateCnab(transacao);
await this.performSaveRemessa(cnabTables);
// await sendCnabSFTP(cnabString);
await this.sendCnabSFTP(cnabString);
}
}
}

private async sendCnabSFTP(cnabString: string) {
await this.sftpService.submitFromString(cnabString, 'arquivo/123-wip-rem.txt');
}

private async performSaveRemessa(cnabTables: ICnabTables) {
const headerLote = cnabTables.lotes[0].headerLote;
const detalhes = cnabTables.lotes[0].detalhes;
Expand Down
57 changes: 37 additions & 20 deletions src/cnab/sftp/sftp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,42 @@ import { SftpClientService } from 'nest-sftp';
import { ConnectConfig } from 'ssh2';

export class SftpService {
private readonly logger: Logger;
constructor(private readonly sftpClient: SftpClientService) {
this.logger = new Logger();
}
private readonly logger: Logger;
constructor(private readonly sftpClient: SftpClientService) {
this.logger = new Logger();
}

async download(
remotePath: string,
localPath: string,
): Promise<string | NodeJS.ReadableStream | Buffer> {
return await this.sftpClient.download(remotePath, localPath) as unknown as Promise<string | NodeJS.ReadableStream | Buffer>;
}
// change connection to a different user/password prior to upload
async submit(
remotePath: string,
localPath: string,
submitConfig: ConnectConfig,
): Promise<string | NodeJS.ReadableStream | Buffer> {
await this.sftpClient.resetConnection(submitConfig);
return await this.sftpClient.upload(remotePath, localPath);
public async download(
remotePath: string,
localPath: string,
): Promise<string | NodeJS.ReadableStream | Buffer> {
return await this.sftpClient.download(remotePath, localPath) as unknown as Promise<string | NodeJS.ReadableStream | Buffer>;
}

public async downloadToString(remotePath: string): Promise<string> {
try {
const buffer = await this.sftpClient.download(remotePath);
const content = buffer.toString('utf-8');
return content;
} catch (error) {
this.logger.error(`Error downloading file from SFTP: ${error.message}`);
throw error;
}
}
}

/**
* Change connection to a different user/password prior to upload
*/
public async submit(
remotePath: string,
localPath: string,
submitConfig: ConnectConfig,
): Promise<string | NodeJS.ReadableStream | Buffer> {
await this.sftpClient.resetConnection(submitConfig);
return await this.sftpClient.upload(remotePath, localPath);
}

async submitFromString(content: string, remotePath: string) {
await this.sftpClient.upload(Buffer.from(content, 'utf-8'), remotePath);
}
}

0 comments on commit 117c36a

Please sign in to comment.