Skip to content

Commit

Permalink
fix: show columns processingDate, transactionDate
Browse files Browse the repository at this point in the history
- feat: find CNPJ users too
  • Loading branch information
yxuo committed Feb 2, 2024
1 parent 76c2c32 commit 4e22117
Show file tree
Hide file tree
Showing 11 changed files with 187 additions and 57 deletions.
7 changes: 7 additions & 0 deletions src/bank-statements/bank-statements.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,10 @@ describe('BankStatementsService', () => {
].map((i) => ({
...i,
cpfCnpj: 'cc_1',

paymentOrderDate: i.date,
processingDate: i.date,
transactionDate: i.date,
permitCode: 'pc_1',
status: '',
statusCode: '',
Expand Down Expand Up @@ -340,6 +343,8 @@ describe('BankStatementsService', () => {
permitCode: 'pc_1',
date: '2023-01-27',
paymentOrderDate: '2023-01-27',
processingDate: '2023-01-27',
transactionDate: '2023-01-27',
amount: 70,
status: '',
statusCode: '',
Expand Down Expand Up @@ -500,6 +505,8 @@ describe('BankStatementsService', () => {
cpfCnpj: 'cc_1',
permitCode: 'pc_1',
paymentOrderDate: i.date,
processingDate: i.date,
transactionDate: i.date,
error: null,
errorCode: null,
bankStatus: null,
Expand Down
6 changes: 6 additions & 0 deletions src/bank-statements/bank-statements.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,8 @@ export class BankStatementsService {
amount: Number(weekAmount.toFixed(2)),
cpfCnpj: args.user.getCpfCnpj(),
date: getDateYMDString(endDate),
processingDate: getDateYMDString(endDate),
transactionDate: getDateYMDString(endDate),
paymentOrderDate: getDateYMDString(endDate),
effectivePaymentDate: null,
permitCode: args.user.getPermitCode(),
Expand Down Expand Up @@ -257,6 +259,8 @@ export class BankStatementsService {
{
id: 1,
date: getDateYMDString(new Date(intervalBSDates.endDate)),
processingDate: getDateYMDString(new Date(intervalBSDates.endDate)),
transactionDate: getDateYMDString(new Date(intervalBSDates.endDate)),
paymentOrderDate: getDateYMDString(new Date(intervalBSDates.endDate)),
effectivePaymentDate: null,
cpfCnpj: validArgs.user.getCpfCnpj(),
Expand All @@ -272,6 +276,8 @@ export class BankStatementsService {
{
id: 2,
date: getDateYMDString(new Date(intervalBSDates.endDate)),
processingDate: getDateYMDString(new Date(intervalBSDates.endDate)),
transactionDate: getDateYMDString(new Date(intervalBSDates.endDate)),
paymentOrderDate: getDateYMDString(new Date(intervalBSDates.endDate)),
effectivePaymentDate: null,
cpfCnpj: validArgs.user.getCpfCnpj(),
Expand Down
17 changes: 16 additions & 1 deletion src/bank-statements/interfaces/bank-statement.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,21 @@ export interface IBankStatement {
*/

date: string;

/**
* Date of ticket transaction.
*
* Format: `yyyy-mm-dd`
*/
transactionDate: string;

/**
* Date of reading this data. The same as `date` field.
*
* Format: `yyyy-mm-dd`
*/
processingDate: string;

/**
* Date of scheduled payment.
*
Expand All @@ -15,7 +30,7 @@ export interface IBankStatement {
paymentOrderDate: string;

/**
* Date when payment was made.
* Date when payment was made in bank.
*
* Format: `yyyy-mm-dd`
*/
Expand Down
64 changes: 45 additions & 19 deletions src/database/seeds/user/user-seed-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import { UserDataInterface } from 'src/users/interfaces/user-data.interface';
@Injectable()
export class UserSeedDataService {
nodeEnv = (): string => '';
cpfSamples: string[] = [];
cnpjSamples: string[] = [];

constructor(
private configService: ConfigService,
Expand All @@ -22,22 +24,37 @@ export class UserSeedDataService {
}

async getDataFromConfig(): Promise<UserDataInterface[]> {
let cpfCnpjSamples: string[] = [];

if (this.nodeEnv() === 'local' || this.nodeEnv() === 'test') {
cpfCnpjSamples = (
await this.bigqueryService.runQuery(
BQSInstances.smtr,
`
SELECT
DISTINCT o.documento,
FROM \`rj-smtr.cadastro.operadoras\` o
LEFT JOIN \`rj-smtr.br_rj_riodejaneiro_bilhetagem.transacao\` t ON t.id_operadora = o.id_operadora
WHERE t.modo = 'Van'
LIMIT 10
`,
)
).reduce((l: string[], i) => [...l, i['documento']], []);
if (this.cpfSamples.length === 0) {
this.cpfSamples = (
await this.bigqueryService.runQuery(
BQSInstances.smtr,
`
SELECT
DISTINCT o.documento,
FROM \`rj-smtr.cadastro.operadoras\` o
LEFT JOIN \`rj-smtr.br_rj_riodejaneiro_bilhetagem.transacao\` t ON t.id_operadora = o.id_operadora
WHERE t.modo = 'Van'
LIMIT 5
`,
)
).reduce((l: string[], i) => [...l, i['documento']], []);
}
if (this.cnpjSamples.length === 0) {
this.cnpjSamples = (
await this.bigqueryService.runQuery(
BQSInstances.smtr,
`
SELECT
DISTINCT c.cnpj,
FROM \`rj-smtr.cadastro.consorcios\` c
LEFT JOIN \`rj-smtr.br_rj_riodejaneiro_bilhetagem.transacao\` t ON t.id_consorcio = c.id_consorcio
WHERE t.modo != 'Van' AND c.cnpj IS NOT NULL
LIMIT 5
`,
)
).reduce((l: string[], i) => [...l, i['cnpj']], []);
}
}

return [
Expand Down Expand Up @@ -119,20 +136,29 @@ export class UserSeedDataService {
...(this.nodeEnv() === 'local' || this.nodeEnv() === 'test'
? ([
{
fullName: 'Henrique Santos Template',
fullName: 'Henrique Santos Template Cpf Van',
email: 'henrique@example.com',
password: 'secret',
permitCode: '213890329890312',
cpfCnpj: cpfCnpjSamples.pop(),
cpfCnpj: this.cpfSamples?.[0],
role: { id: RoleEnum.user } as Role,
status: { id: StatusEnum.active } as Status,
},
{
fullName: 'Márcia Clara Template',
fullName: 'Márcia Clara Template Cnpj Brt etc',
email: 'marcia@example.com',
password: 'secret',
permitCode: '319274392832023',
cpfCnpj: cpfCnpjSamples.pop(),
cpfCnpj: this.cnpjSamples?.[0],
role: { id: RoleEnum.user } as Role,
status: { id: StatusEnum.active } as Status,
},
{
fullName: 'Usuário Teste dos Santos Oliveira',
email: 'user@example.com',
password: 'secret',
permitCode: '213890329890749',
cpfCnpj: this.cpfSamples?.[0],
role: { id: RoleEnum.user } as Role,
status: { id: StatusEnum.active } as Status,
},
Expand Down
22 changes: 15 additions & 7 deletions src/ticket-revenues/ticket-revenues.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import {
import { TicketRevenuesGroup } from './objs/TicketRevenuesGroup';
import { TicketRevenuesGroupsType } from './types/ticket-revenues-groups.type';
import * as TicketRevenuesGroupList from './utils/ticket-revenues-groups.utils';
import { isCpfOrCnpj } from 'src/utils/cpf-cnpj';

@Injectable()
export class TicketRevenuesService {
Expand Down Expand Up @@ -286,19 +287,26 @@ export class TicketRevenuesService {
queryBuilder.pushAND(`DATE(data) = DATE('${nowStr}')`);
}

let queryBuilderStr = queryBuilder.toSQL();
let qWhere = queryBuilder.toSQL();
if (args?.cpfCnpj !== undefined) {
const cpfCnpj = args.cpfCnpj;
queryBuilderStr = `o.documento = '${cpfCnpj}' AND (${queryBuilderStr})`;
qWhere =
isCpfOrCnpj(args?.cpfCnpj) === 'cpf'
? `b.documento = '${cpfCnpj}' AND (${qWhere})`
: `b.cnpj = '${cpfCnpj}' AND (${qWhere})`;
}

// Query
const joinCpfCnpj =
isCpfOrCnpj(args?.cpfCnpj) === 'cpf'
? 'LEFT JOIN `rj-smtr.cadastro.operadoras` b ON b.id_operadora = t.id_operadora '
: 'LEFT JOIN `rj-smtr.cadastro.consorcios` b ON b.id_consorcio = t.id_consorcio ';

const countQuery =
'SELECT COUNT(*) AS count ' +
'FROM `rj-smtr.br_rj_riodejaneiro_bilhetagem.transacao` t ' +
'LEFT JOIN `rj-smtr.cadastro.operadoras` o ON o.id_operadora = t.id_operadora ' +
(queryBuilderStr.length ? ` WHERE ${queryBuilderStr}` : '');
joinCpfCnpj +
(qWhere.length ? ` WHERE ${qWhere}` : '');
const query =
`
SELECT
Expand Down Expand Up @@ -326,9 +334,9 @@ export class TicketRevenuesService {
t.versao AS bqDataVersion,
(${countQuery}) AS count,
'ok' AS status
FROM \`rj-smtr.br_rj_riodejaneiro_bilhetagem.transacao\` t
LEFT JOIN \`rj-smtr.cadastro.operadoras\` o ON o.id_operadora = t.id_operadora ` +
(queryBuilderStr.length ? `\nWHERE ${queryBuilderStr}` : '') +
FROM \`rj-smtr.br_rj_riodejaneiro_bilhetagem.transacao\` t\n` +
joinCpfCnpj +
(qWhere.length ? `\nWHERE ${qWhere}` : '') +
` UNION ALL
SELECT ${'null, '.repeat(22)}
(${countQuery}) AS count, 'empty' AS status` +
Expand Down
11 changes: 11 additions & 0 deletions src/utils/cpf-cnpj.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export function isCpfOrCnpj(cpfCnpj?: string) {
if (!cpfCnpj) {
return null;
} else if (cpfCnpj.length === 11) {
return 'cpf';
} else if (cpfCnpj.length === 14) {
return 'cnpj';
} else {
return null;
}
}
16 changes: 11 additions & 5 deletions test/admin/users.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
ADMIN_PASSWORD,
APP_URL,
LICENSEE_CASE_ACCENT,
LICENSEE_PERMIT_CODE,
LICENSEE_CPF_PERMIT_CODE,
MAILDEV_URL,
} from '../utils/constants';
import { stringUppercaseUnaccent } from 'src/utils/string-utils';
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('Admin managing users (e2e)', () => {
.auth(apiToken, {
type: 'bearer',
})
.query({ permitCode: LICENSEE_PERMIT_CODE })
.query({ permitCode: LICENSEE_CPF_PERMIT_CODE })
.expect(({ body }) => {
expect(body.data?.length).toBe(1);
})
Expand All @@ -73,21 +73,27 @@ describe('Admin managing users (e2e)', () => {
filter: { permitCode: licensee.permitCode },
expect: (body: any) =>
expect(
body.data.some((i: any) => i.permitCode === LICENSEE_PERMIT_CODE),
body.data.some(
(i: any) => i.permitCode === LICENSEE_CPF_PERMIT_CODE,
),
).toBeTruthy(),
},
{
filter: { name: licensee.fullName },
expect: (body: any) =>
expect(
body.data.some((i: any) => i.permitCode === LICENSEE_PERMIT_CODE),
body.data.some(
(i: any) => i.permitCode === LICENSEE_CPF_PERMIT_CODE,
),
).toBeTruthy(),
},
{
filter: { email: licensee.email },
expect: (body: any) =>
expect(
body.data.some((i: any) => i.permitCode === LICENSEE_PERMIT_CODE),
body.data.some(
(i: any) => i.permitCode === LICENSEE_CPF_PERMIT_CODE,
),
).toBeTruthy(),
},
{
Expand Down
9 changes: 6 additions & 3 deletions test/bank-statements/bank-statements.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { getDateYMDString } from '../../src/utils/date-utils';
import {
APP_URL,
BQ_JSON_CREDENTIALS,
LICENSEE_PASSWORD,
LICENSEE_PERMIT_CODE,
LICENSEE_CPF_PASSWORD,
LICENSEE_CPF_PERMIT_CODE,
} from '../utils/constants';

describe('Bank statements (e2e)', () => {
Expand All @@ -24,7 +24,10 @@ describe('Bank statements (e2e)', () => {
beforeAll(async () => {
await request(app)
.post('/api/v1/auth/licensee/login')
.send({ permitCode: LICENSEE_PERMIT_CODE, password: LICENSEE_PASSWORD })
.send({
permitCode: LICENSEE_CPF_PERMIT_CODE,
password: LICENSEE_CPF_PASSWORD,
})
.expect(200)
.then(({ body }) => {
apiToken = body.token;
Expand Down
Loading

0 comments on commit 4e22117

Please sign in to comment.