Skip to content

Commit

Permalink
fix: join another table to avoid value = 0
Browse files Browse the repository at this point in the history
- fix: If tansactionType = Integração and value = 0.0,
use integracao.valor_total instead.
  • Loading branch information
yxuo committed Feb 5, 2024
1 parent b17ab8e commit a78e301
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 24 deletions.
32 changes: 22 additions & 10 deletions src/ticket-revenues/ticket-revenues.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,11 +158,13 @@ export class TicketRevenuesService {
(sum, i) => sum + i.count,
0,
);
console.log('TR response', ticketRevenuesResponse.length);

return {
startDate:
ticketRevenuesResponse[ticketRevenuesResponse.length - 1].partitionDate,
endDate: ticketRevenuesResponse[0].partitionDate,
ticketRevenuesResponse[ticketRevenuesResponse.length - 1]
?.partitionDate || null,
endDate: ticketRevenuesResponse[0]?.partitionDate || null,
amountSum,
todaySum: transactionValueLastDay,
count: ticketRevenuesGroups.length,
Expand Down Expand Up @@ -266,6 +268,9 @@ export class TicketRevenuesService {
transacao: IS_PROD
? 'rj-smtr.br_rj_riodejaneiro_bilhetagem.transacao'
: 'rj-smtr-dev.br_rj_riodejaneiro_bilhetagem_cct.transacao',
integracao: IS_PROD
? 'rj-smtr.br_rj_riodejaneiro_bilhetagem.integracao'
: 'rj-smtr-dev.br_rj_riodejaneiro_bilhetagem_cct.integracao',
tTipoPgto: IS_PROD ? 'tipo_pagamento' : 'id_tipo_pagamento',
};
// Args
Expand All @@ -282,17 +287,17 @@ export class TicketRevenuesService {

if (args?.startDate !== undefined) {
const startDate = args.startDate.toISOString().slice(0, 10);
queryBuilder.pushAND(`DATE(data) >= DATE('${startDate}')`);
queryBuilder.pushAND(`DATE(t.data) >= DATE('${startDate}')`);
}
if (args?.endDate !== undefined) {
const endDate = args.endDate.toISOString().slice(0, 10);
queryBuilder.pushAND(`DATE(data) <= DATE('${endDate}')`);
queryBuilder.pushAND(`DATE(t.data) <= DATE('${endDate}')`);
}

queryBuilder.pushOR([]);
if (args?.getToday) {
const nowStr = new Date(Date.now()).toISOString().slice(0, 10);
queryBuilder.pushAND(`DATE(data) = DATE('${nowStr}')`);
queryBuilder.pushAND(`DATE(t.data) = DATE('${nowStr}')`);
}

let qWhere = queryBuilder.toSQL();
Expand All @@ -309,12 +314,16 @@ export class TicketRevenuesService {
isCpfOrCnpj(args?.cpfCnpj) === 'cpf'
? `LEFT JOIN \`${Q_CONSTS.bucket}.cadastro.operadoras\` b ON b.id_operadora = t.id_operadora `
: `LEFT JOIN \`${Q_CONSTS.bucket}.cadastro.consorcios\` b ON b.id_consorcio = t.id_consorcio `;
const joinIntegracao = `INNER JOIN ${Q_CONSTS.integracao} i ON i.id_transacao = t.id_transacao`;

const countQuery =
'SELECT COUNT(*) AS count ' +
`FROM \`${Q_CONSTS.transacao}\` t ` +
`FROM \`${Q_CONSTS.transacao}\` t\n` +
joinCpfCnpj +
(qWhere.length ? ` WHERE ${qWhere}` : '');
'\n' +
joinIntegracao +
'\n' +
(qWhere.length ? ` WHERE ${qWhere}\n` : '');
const query =
`
SELECT
Expand All @@ -338,14 +347,17 @@ export class TicketRevenuesService {
t.stop_id AS stopId,
t.stop_lat AS stopLat,
t.stop_lon AS stopLon,
t.valor_transacao AS transactionValue,
CASE WHEN t.tipo_transacao = 'Integração' THEN i.valor_transacao_total ELSE t.valor_transacao END AS transactionValue,
t.versao AS bqDataVersion,
(${countQuery}) AS count,
'ok' AS status
FROM \`${Q_CONSTS.transacao}\` t\n` +
joinCpfCnpj +
(qWhere.length ? `\nWHERE ${qWhere}` : '') +
` UNION ALL
'\n' +
joinIntegracao +
'\n' +
(qWhere.length ? `WHERE ${qWhere}\n` : '') +
`UNION ALL
SELECT ${'null, '.repeat(22)}
(${countQuery}) AS count, 'empty' AS status` +
`\nORDER BY partitionDate DESC, processingHour DESC` +
Expand Down
27 changes: 13 additions & 14 deletions test/bank-statements/bank-statements.e2e-spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { BigQuery } from '@google-cloud/bigquery';
import { isFriday, previousFriday } from 'date-fns';
import { isFriday, nextFriday, previousFriday } from 'date-fns';
import * as request from 'supertest';
import { getDateYMDString } from '../../src/utils/date-utils';
import {
Expand Down Expand Up @@ -38,11 +38,11 @@ describe('Bank statements (e2e)', () => {
await bq
.query(
`
SELECT
CAST(t.data AS STRING) AS partitionDate,
FROM \`rj-smtr.br_rj_riodejaneiro_bilhetagem.transacao\` t
LEFT JOIN \`rj-smtr.cadastro.operadoras\` o ON o.id_operadora = t.id_operadora
WHERE o.documento = '${licenseeCpfCnpj}' ORDER BY data DESC, hora DESC LIMIT 1
SELECT
CAST(t.data AS STRING) AS partitionDate,
FROM \`rj-smtr-dev.br_rj_riodejaneiro_bilhetagem_cct.transacao\` t
LEFT JOIN \`rj-smtr.cadastro.operadoras\` o ON o.id_operadora = t.id_operadora
WHERE o.documento = '${licenseeCpfCnpj}' ORDER BY data DESC, hora DESC LIMIT 1
`,
)
.then((value) => {
Expand All @@ -55,7 +55,7 @@ describe('Bank statements (e2e)', () => {
// Arrange
let friday = new Date(licenseeMaxDate);
if (!isFriday(friday)) {
friday = previousFriday(friday);
friday = nextFriday(friday);
}
const fridayStr = getDateYMDString(friday);

Expand Down Expand Up @@ -90,9 +90,9 @@ describe('Bank statements (e2e)', () => {
});

// Assert
expect(bankStatements.todaySum).toEqual(ticketRevenuesMe.todaySum);
expect(bankStatements.data.length).toBeGreaterThan(0);
expect(ticketRevenuesMe.data.length).toBeGreaterThan(0);
expect(bankStatements.todaySum).toEqual(ticketRevenuesMe.todaySum);
}, 60000);

it('should match amountSum in /bank-statements with /ticket-revenues/me in the same month', /**
Expand All @@ -101,7 +101,7 @@ describe('Bank statements (e2e)', () => {
// Arrange
let friday = new Date(licenseeMaxDate);
if (!isFriday(friday)) {
friday = previousFriday(friday);
friday = nextFriday(friday);
}

// Act
Expand Down Expand Up @@ -143,12 +143,11 @@ describe('Bank statements (e2e)', () => {
});

// Assert
friday.setDate(friday.getDate());
expect(bankStatements.amountSum).toEqual(ticketRevenuesMe.amountSum);
expect(bankStatements.data.length).toBeGreaterThan(0);
expect(ticketRevenuesMe.data.length).toBeGreaterThan(0);
expect(bankStatements.amountSum).toBeGreaterThan(0);
expect(ticketRevenuesMe.amountSum).toBeGreaterThan(0);
expect(bankStatements.amountSum).toEqual(ticketRevenuesMe.amountSum);
}, 60000);

it('should match amountSum in /bank-statements with /ticket-revenues/me in the same week', /**
Expand All @@ -157,7 +156,7 @@ describe('Bank statements (e2e)', () => {
// Arrange
let friday = new Date(licenseeMaxDate);
if (!isFriday(friday)) {
friday = previousFriday(friday);
friday = nextFriday(friday);
}
const fridayStr = getDateYMDString(friday);

Expand Down Expand Up @@ -293,7 +292,7 @@ describe('Bank statements (e2e)', () => {
// Arrange
let friday = new Date(licenseeMaxDate);
if (!isFriday(friday)) {
friday = previousFriday(friday);
friday = nextFriday(friday);
}

// Act
Expand Down Expand Up @@ -341,7 +340,7 @@ describe('Bank statements (e2e)', () => {
// Arrange
let friday = new Date(licenseeMaxDate);
if (!isFriday(friday)) {
friday = previousFriday(friday);
friday = nextFriday(friday);
}

// Act
Expand Down

0 comments on commit a78e301

Please sign in to comment.