Skip to content

Commit

Permalink
feat: read retorno
Browse files Browse the repository at this point in the history
  • Loading branch information
yxuo committed Mar 6, 2024
1 parent 8784ebd commit 434e7e3
Show file tree
Hide file tree
Showing 13 changed files with 481 additions and 173 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@ import { ICnabFieldMap } from './cnab-field-map.interface';

export interface ICnabFieldMapDetalhe extends ICnabFieldMap {
detalheLoteRegistroSequenceField: string;
detalheSegmentoNameField: string;
detalheSegmentoCodeField: string;
}
2 changes: 1 addition & 1 deletion src/cnab/interfaces/cnab-all/cnab-field-map.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ export interface ICnabFieldMap {
*
* @example "A", "B", "J"
*/
detalheSegmentoNameField?: string;
detalheSegmentoCodeField?: string;

// TRAILER ARQUIVO FIELDS

Expand Down
2 changes: 1 addition & 1 deletion src/cnab/service/header-arquivo.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +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';
import { SftpService } from 'src/sftp/sftp.service';

@Injectable()
export class HeaderArquivoService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const trailerLote: ICnabFieldMapTrailerLote = {
const detalheLote: ICnabFieldMapDetalhe = {
...registro,
detalheLoteRegistroSequenceField: 'nsr',
detalheSegmentoNameField: 'codigoSegmento',
detalheSegmentoCodeField: 'codigoSegmento',
};

export const cnabAll104FieldMapTemplate = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const base: ICnabFieldMap = {
registroIdField: 'codigoRegistro',
registroLoteSequenceField: 'loteServico',
trailerLoteRegistroCountField: 'quantidadeRegistrosLote',
detalheSegmentoNameField: 'codigoSegmento',
detalheSegmentoCodeField: 'codigoSegmento',
detalheLoteRegistroSequenceField: 'nsr',
trailerArquivoLoteCountField: 'quantidadeLotesArquivo',
trailerArquivoRegistroCountField: 'quantidadeRegistrosArquivo',
Expand All @@ -14,7 +14,7 @@ const detalheB: ICnabFieldMap = {
registroIdField: 'codigoRegistro',
registroLoteSequenceField: 'loteServico2',
trailerLoteRegistroCountField: 'quantidadeRegistrosLote',
detalheSegmentoNameField: 'codigoSegmento',
detalheSegmentoCodeField: 'codigoSegmento',
detalheLoteRegistroSequenceField: 'nsr',
trailerArquivoLoteCountField: 'quantidadeLotesArquivo',
trailerArquivoRegistroCountField: 'quantidadeRegistrosArquivo',
Expand Down
40 changes: 35 additions & 5 deletions src/cnab/utils/cnab-104-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,43 @@ import { ICnab240_104Lote } from '../interfaces/cnab-240/104/cnab-240-104-lote.i
import { ICnab240_104Registro } from '../interfaces/cnab-240/104/cnab-240-104-registro.interface';
import { ICnab240_104TrailerArquivo } from '../interfaces/cnab-240/104/cnab-240-104-trailer-arquivo.interface';
import { ICnab240_104TrailerLote } from '../interfaces/cnab-240/104/cnab-240-104-trailer-lote.interface';
import { cnab240_104DetalheATemplate } from '../templates/cnab-240/104/cnab-240-104-detalhe-a-template.const';
import { cnab240_104DetalheBTemplate } from '../templates/cnab-240/104/cnab-240-104-detalhe-b-template.const';
import { cnab240_104HeaderArquivoTemplate } from '../templates/cnab-240/104/cnab-240-104-header-arquivo-template.const';
import { cnab240_104HeaderLoteTemplate } from '../templates/cnab-240/104/cnab-240-104-header-lote-template.const';
import { cnab240_104TrailerArquivoTemplate } from '../templates/cnab-240/104/cnab-240-104-trailer-arquivo-template.const';
import { cnab240_104TrailerLoteTemplate } from '../templates/cnab-240/104/cnab-240-104-trailer-lote-template.const';
import { cnabAll104FieldMapTemplate as fieldMapTemplate } from '../templates/cnab-all/cnab-all-104-registro-field-map-template';
import { CnabFile } from '../types/cnab-file.type';
import { CnabLote } from '../types/cnab-lote.type';
import { CnabRegistro } from '../types/cnab-registro.type';
import {
getCnabMappedValue,
parseCnabFile,
processCnabFile,
stringifyCnabFile,
} from './cnab-utils';

export function parseCnab104(cnabString: string): ICnab240_104File {
const fileDTO = getCnabFileFrom104({
headerArquivo: structuredClone(cnab240_104HeaderArquivoTemplate),
lotes: [{
headerLote: structuredClone(cnab240_104HeaderLoteTemplate),
registros: [{
detalheA: structuredClone(cnab240_104DetalheATemplate),
detalheB: structuredClone(cnab240_104DetalheBTemplate),
}],
trailerLote: structuredClone(cnab240_104TrailerLoteTemplate),
}],
trailerArquivo: structuredClone(cnab240_104TrailerArquivoTemplate),
})
const file = parseCnabFile(cnabString, fileDTO);
return getCnab104FromFile(file);
}

/**
* Validate and add Cnab 104 information
*/
export function getProcessedCnab104(
cnab104: ICnab240_104File,
): ICnab240_104File {
Expand All @@ -27,6 +54,9 @@ export function getProcessedCnab104(
return getCnab104FromFile(cnab);
}

/**
* Validate, process and transform into string
*/
export function stringifyCnab104File(cnab104: ICnab240_104File): string {
validateCnab104File(cnab104);
const newCnab104 = structuredClone(cnab104);
Expand Down Expand Up @@ -54,16 +84,16 @@ export function validateUniqueCnab104Lotes(lotes: ICnab240_104Lote[]) {
(l, i) => [
...l,
String(i.headerLote.tipoCompromisso.value) +
String(i.headerLote.formaLancamento.value),
String(i.headerLote.formaLancamento.value),
],
[],
);
const uniqueLoteTypes = [...new Set(loteTypes)];
if (loteTypes.length !== uniqueLoteTypes.length) {
throw new Error(
'Each headerLote must have unique combination of ' +
"`tipoCompromisso` and 'formaLancamento' but there are repeated ones " +
`(${JSON.stringify(loteTypesDict)})`,
"`tipoCompromisso` and 'formaLancamento' but there are repeated ones " +
`(${JSON.stringify(loteTypesDict)})`,
);
}
}
Expand Down Expand Up @@ -136,7 +166,7 @@ function getCnab104Registros(lote: CnabLote): ICnab240_104Registro[] {
// add detalhe to registro set
const codSegmento = getCnabMappedValue(
registro,
'detalheSegmentoNameField',
'detalheSegmentoCodeField',
);
newRegistro[`detalhe${codSegmento}`] = registro.fields;
}
Expand All @@ -159,7 +189,7 @@ function getCnab104Registros(lote: CnabLote): ICnab240_104Registro[] {
*/
function isNewCnab104RegistroSet(registro: CnabRegistro): boolean {
return [Cnab104CodigoSegmento.A].includes(
getCnabMappedValue(registro, 'detalheSegmentoNameField'),
getCnabMappedValue(registro, 'detalheSegmentoCodeField'),
);
}

Expand Down
Loading

0 comments on commit 434e7e3

Please sign in to comment.