Skip to content

Commit

Permalink
Merge pull request #289 from RJ-SMTR/hotfix/remessa
Browse files Browse the repository at this point in the history
[prod] fix: faster query CNAB value sum
  • Loading branch information
williamfl2007 committed May 31, 2024
2 parents 54cfd74 + c569888 commit a30a1d9
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/cnab/cnab.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ export class CnabService {
const ter = endOfDay(subDays(friday, 3));
const transacoesView = await this.transacaoViewService.find({
datetimeProcessamento: Between(qua, ter),
});
}, false);
return transacoesView;
}

Expand Down
11 changes: 6 additions & 5 deletions src/cnab/utils/cnab/cnab-104-utils.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Ocorrencia } from 'src/cnab/entity/pagamento/ocorrencia.entity';
import { CnabCodigoSegmento } from 'src/cnab/enums/all/cnab-codigo-segmento.enum';
import { CnabFile104 } from '../../interfaces/cnab-240/104/cnab-file-104.interface';
import { CnabHeaderArquivo104 } from '../../interfaces/cnab-240/104/cnab-header-arquivo-104.interface';
Expand All @@ -22,7 +23,6 @@ import {
removeCnabDetalheZ,
stringifyCnabFile,
} from './cnab-utils';
import { Ocorrencia } from 'src/cnab/entity/pagamento/ocorrencia.entity';

const sc = structuredClone;

Expand Down Expand Up @@ -129,11 +129,12 @@ function processCnab104TrailerLote(lote: CnabLote104) {
}

function getSomarioValoresCnabLote(lote: CnabLote104): number {
return lote.registros.reduce(
(s2, regGroup) =>
s2 + Number(regGroup.detalheA?.valorLancamento?.convertedValue || 0),
0,
const original = lote.registros.map((i) =>
Number(i.detalheA?.valorLancamento?.convertedValue || 0),
);
const rounded = original.map((i) => Number(i.toFixed(2)));
const sum = rounded.reduce((num, sum) => sum + num, 0);
return sum;
}

// #region getCnab104FromFile
Expand Down
4 changes: 3 additions & 1 deletion src/transacao-bq/transacao-view.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ export class TransacaoViewService {

save = this.transacaoViewRepository.save;

async find(fields: EntityCondition<TransacaoView>) {
async find(fields: EntityCondition<TransacaoView>, eager = true) {
return await this.transacaoViewRepository.find({
where: fields,
loadEagerRelations: eager,
});
}

Expand Down Expand Up @@ -75,6 +76,7 @@ export class TransacaoViewService {
where: {
idTransacao: In(transacaoIds),
},
loadEagerRelations: false
});
existing.push(...existingSlice);
}
Expand Down
4 changes: 2 additions & 2 deletions src/utils/number-utils.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/**
* Remove decimals with no round.
*/
export function cropDecimals(x: number, decimals = 0): number {
export function cropDecimals(x: number, maxDecimals = 0): number {
const s = String(x);
const i = s.indexOf('.');
if (i < 0) {
return x;
}
const d = decimals ? decimals + 1 : 0;
const d = maxDecimals ? maxDecimals + 1 : 0;
const result = Number(s.slice(0, i + d));
return result;
}

0 comments on commit a30a1d9

Please sign in to comment.