From cdca98a611a606ea93a1af01c28341d7c10a04e5 Mon Sep 17 00:00:00 2001 From: Raphael Rivas Date: Tue, 27 Feb 2024 10:34:08 -0300 Subject: [PATCH] test: validate unique cnab lote types --- .../interfaces/cnab-field-map.interface.ts | 11 ++++++ .../templates/240/104/example-240-104.rem | 2 +- src/cnab/utils/cnab-104-utils.spec.ts | 7 +++- src/cnab/utils/cnab-104-utils.ts | 34 +++++++++++++++++++ 4 files changed, 52 insertions(+), 2 deletions(-) diff --git a/src/cnab/interfaces/cnab-field-map.interface.ts b/src/cnab/interfaces/cnab-field-map.interface.ts index 79d248ee..7adfc9cd 100644 --- a/src/cnab/interfaces/cnab-field-map.interface.ts +++ b/src/cnab/interfaces/cnab-field-map.interface.ts @@ -29,6 +29,17 @@ export interface ICnabFieldMap { */ registroIdField: string; + // FOR HEADER LOTE + + /** + * For Header Lote + * + * Dpen + */ + headerLotePaymentTypeField?: string; + + headerLoteServiceTypeField?: string; + // FOR TRAILER LOTE /** diff --git a/src/cnab/test/templates/240/104/example-240-104.rem b/src/cnab/test/templates/240/104/example-240-104.rem index 45213a0f..a15e9d02 100644 --- a/src/cnab/test/templates/240/104/example-240-104.rem +++ b/src/cnab/test/templates/240/104/example-240-104.rem @@ -1,5 +1,5 @@ 10400900 20001234560011111200709P 0000 0095550000000000032 CONVE DE PAGAMENTOSSA E OEBSACAIXA 20602202710234200010100001600 000 -10400011C3001041 20001234560011111200702000201 0095550000000000032 CONVE DE PAGAMENTOSSA E PENSA RUA ALMIR PEDRAS 00000000000000000000 00000 +10400011C3041041 20001234560011111200702000201 0095550000000000032 CONVE DE PAGAMENTOSSA E PENSA RUA ALMIR PEDRAS 00000000000000000000 00000 1040001300001A0000001040095550031993933180 TEREZINHA SEVERIANA 000027 105022023BRL000000000000000000000000120012000000000 01N1060000000000000000000000000 01 0 1040001300002B 000000000000000 00104APTO 315 CENTRO RIO DE JANEIRO 22544010RJ06022023000000000120012000000000000000000000000000000000000000000000000000000000000 1040001300003A0000001040095550031993933180 TEREZINHA SEVERIANA 000027 105022023BRL000000000000000000000000120012000000000 01N1060000000000000000000000000 01 0 diff --git a/src/cnab/utils/cnab-104-utils.spec.ts b/src/cnab/utils/cnab-104-utils.spec.ts index d8a5ec89..68708cf8 100644 --- a/src/cnab/utils/cnab-104-utils.spec.ts +++ b/src/cnab/utils/cnab-104-utils.spec.ts @@ -11,6 +11,7 @@ import { cnab240_104HeaderLoteTemplateTest } from '../test/templates/240/104/cna import { cnab240_104TrailerArquivoTemplateTest } from '../test/templates/240/104/cnab-240-104-trailer-arquivo-template-test.const'; import { cnab240_104TrailerLoteTemplateTest } from '../test/templates/240/104/cnab-240-104-trailer-lote-template-test.const'; import { getCnabFileFrom104, stringifyCnab104File } from './cnab-104-utils'; +import { Cnab104FormaLancamento } from '../enums/104/cnab-104-forma-lancamento.enum'; describe('cnab-104-utils.ts', () => { const sc = structuredClone; @@ -39,10 +40,14 @@ describe('cnab-104-utils.ts', () => { ], trailerLote: sc(trailerLote), }; + const lote0 = sc(lote); + lote0.headerLote.formaLancamento.value = Cnab104FormaLancamento.DOC; + const lote1 = sc(lote); + lote0.headerLote.formaLancamento.value = Cnab104FormaLancamento.TED; const file: ICnab240_104File = { headerArquivo: sc(headerArquivo), - lotes: [sc(lote), sc(lote)], + lotes: [sc(lote0), sc(lote1)], trailerArquivo: sc(trailerArquivo), }; file.lotes[1].registros.push({ diff --git a/src/cnab/utils/cnab-104-utils.ts b/src/cnab/utils/cnab-104-utils.ts index 62cef7d2..4584453f 100644 --- a/src/cnab/utils/cnab-104-utils.ts +++ b/src/cnab/utils/cnab-104-utils.ts @@ -8,12 +8,46 @@ import { CnabRegistro } from '../types/cnab-registro.type'; import { stringifyCnabFile } from './cnab-utils'; export function stringifyCnab104File(cnab104: ICnab240_104File): string { + validateCnab104File(cnab104); const newCnab104 = structuredClone(cnab104); processCnab104File(newCnab104); const cnab = getCnabFileFrom104(newCnab104); return stringifyCnabFile(cnab); } +export function validateCnab104File(cnab: ICnab240_104File) { + validateUniqueCnab104Lotes(cnab.lotes); +} + +export function validateUniqueCnab104Lotes(lotes: ICnab240_104Lote[]) { + const loteTypesDict = lotes.reduce( + (l, i) => [ + ...l, + { + tipoCompromisso: i.headerLote.tipoCompromisso.value, + formaLancamento: i.headerLote.formaLancamento.value, + }, + ], + [], + ); + const loteTypes = lotes.reduce( + (l, i) => [ + ...l, + String(i.headerLote.tipoCompromisso.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)})`, + ); + } +} + /** * Process data in CnabFile for Caixa (bank 104) */