Skip to content

Commit

Permalink
test: validate unique cnab lote types
Browse files Browse the repository at this point in the history
  • Loading branch information
yxuo committed Feb 27, 2024
1 parent dc0e4a3 commit cdca98a
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 2 deletions.
11 changes: 11 additions & 0 deletions src/cnab/interfaces/cnab-field-map.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ export interface ICnabFieldMap {
*/
registroIdField: string;

// FOR HEADER LOTE

/**
* For Header Lote
*
* Dpen
*/
headerLotePaymentTypeField?: string;

headerLoteServiceTypeField?: string;

// FOR TRAILER LOTE

/**
Expand Down
2 changes: 1 addition & 1 deletion src/cnab/test/templates/240/104/example-240-104.rem
Original file line number Diff line number Diff line change
@@ -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
Expand Down
7 changes: 6 additions & 1 deletion src/cnab/utils/cnab-104-utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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({
Expand Down
34 changes: 34 additions & 0 deletions src/cnab/utils/cnab-104-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
*/
Expand Down

0 comments on commit cdca98a

Please sign in to comment.