Skip to content

Commit

Permalink
fix: seed mailhsitory saving hashed pass
Browse files Browse the repository at this point in the history
- feat: seed:run __exclude to ignore modules
  • Loading branch information
yxuo committed Feb 7, 2024
1 parent 17daadd commit 3c26f56
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 14 deletions.
8 changes: 6 additions & 2 deletions src/database/seeds/mail-history/mail-history-seed.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,12 @@ export class MailHistorySeedService {
await this.mailHistoryRepository.save(
this.mailHistoryRepository.create(newItem),
);
itemUser.hash = newItem.hash;
await this.usersRepository.save(this.usersRepository.create(itemUser));
await this.usersRepository.save(
this.usersRepository.create({
id: itemUser.id,
hash: newItem.hash,
}),
);
}
}
}
Expand Down
19 changes: 13 additions & 6 deletions src/database/seeds/run-seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,19 @@ const runSeed = async () => {
];

const FORCE_PARAM = '__force';
const force = process.argv.slice(2).includes(FORCE_PARAM);
global.force = force;
const nameFilters = process.argv.slice(2).filter((i) => i !== FORCE_PARAM);
const EXCLUDE_PARAM = '__exclude';
const isForce = process.argv.slice(2).includes(FORCE_PARAM);
const isExclude = process.argv.slice(2).includes(EXCLUDE_PARAM);
global.force = isForce;
const nameFilters = process.argv
.slice(2)
.filter((i) => ![FORCE_PARAM, EXCLUDE_PARAM].includes(i));
if (nameFilters.length > 0) {
services = services.filter((s) =>
nameFilters.some((j) => s.name.toLowerCase().includes(j.toLowerCase())),
services = services.filter(
(s) =>
nameFilters.some((n) =>
s.name.toLowerCase().includes(n.toLowerCase()),
) !== isExclude,
);
}

Expand All @@ -45,7 +52,7 @@ const runSeed = async () => {
.join(', ')}`,
);
for (const module of services) {
if (!(await app.get(module).validateRun()) && !force) {
if (!(await app.get(module).validateRun()) && !isForce) {
console.log(`[${module.name}]: Database is not empty, aborting seed...`);
console.log(
`Tip: Use '${FORCE_PARAM}' parameter to ignore this message.`,
Expand Down
8 changes: 4 additions & 4 deletions src/database/seeds/user/user-seed-data.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ export class UserSeedDataService {
`
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
FROM \`rj-smtr-dev.cadastro.operadoras\` o
LEFT JOIN \`rj-smtr-dev.br_rj_riodejaneiro_bilhetagem_cct.transacao\` t ON t.id_operadora = o.id_operadora
WHERE t.modo = 'Van'
LIMIT 5
`,
Expand All @@ -47,8 +47,8 @@ export class UserSeedDataService {
`
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
FROM \`rj-smtr-dev.cadastro.consorcios\` c
LEFT JOIN \`rj-smtr-dev.br_rj_riodejaneiro_bilhetagem_cct.transacao\` t ON t.id_consorcio = c.id_consorcio
WHERE t.modo != 'Van' AND c.cnpj IS NOT NULL
LIMIT 5
`,
Expand Down
29 changes: 27 additions & 2 deletions test/ticket-revenues/ticket-revenues.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,24 @@ import * as request from 'supertest';
import { getDateYMDString } from '../../src/utils/date-utils';
import {
APP_URL,
BQ_JSON_CREDENTIALS,
LICENSEE_CNPJ_PASSWORD,
LICENSEE_CNPJ_PERMIT_CODE,
LICENSEE_CPF_PASSWORD,
LICENSEE_CPF_PERMIT_CODE,
} from '../utils/constants';
import { BigQuery } from '@google-cloud/bigquery';

describe('Ticket revenues (e2e)', () => {
const app = APP_URL;
let bq: BigQuery;
let cpfApiToken: any;
let cnpjApiToken: any;
let licenseeCnpj: string;
let licenseeCnpjMaxDate: Date;

beforeAll(async () => {
// Login CPF and CNPJ users
await request(app)
.post('/api/v1/auth/licensee/login')
.send({
Expand All @@ -25,6 +31,7 @@ describe('Ticket revenues (e2e)', () => {
.then(({ body }) => {
cpfApiToken = body.token;
});

await request(app)
.post('/api/v1/auth/licensee/login')
.send({
Expand All @@ -34,7 +41,25 @@ describe('Ticket revenues (e2e)', () => {
.expect(200)
.then(({ body }) => {
cnpjApiToken = body.token;
licenseeCnpj = body.user.cpfCnpj;
});

// get licenseeMaxDate
bq = new BigQuery({ credentials: BQ_JSON_CREDENTIALS() });
await bq
.query(
`
SELECT
CAST(t.data AS STRING) AS partitionDate,
FROM \`rj-smtr-dev.br_rj_riodejaneiro_bilhetagem_cct.transacao\` t
LEFT JOIN \`rj-smtr.cadastro.consorcios\` c ON c.id_consorcio = t.id_consorcio
WHERE c.cnpj = '${licenseeCnpj}' ORDER BY data DESC, hora DESC LIMIT 1
`,
)
.then((value) => {
licenseeCnpjMaxDate = new Date(value[0][0]?.['partitionDate']);
});
expect(Number(licenseeCnpjMaxDate)).not.toBeNaN();
});

it('should match result in /ticket-revenues/me with /ticket-revenues/me/individual', /**
Expand Down Expand Up @@ -86,7 +111,7 @@ describe('Ticket revenues (e2e)', () => {
* Requirement: 2024/01/26 {@link https://github.com/RJ-SMTR/api-cct/issues/167#issuecomment-1912764312 #167, item 4 - GitHub}
*/ async () => {
// Arrange
const startDate = subDays(new Date(), 366);
const startDate = subDays(licenseeCnpjMaxDate, 366);

// Act
let ticketRevenuesMe: any;
Expand All @@ -97,7 +122,7 @@ describe('Ticket revenues (e2e)', () => {
})
.query({
startDate: getDateYMDString(startDate),
endDate: getDateYMDString(new Date()),
endDate: getDateYMDString(licenseeCnpjMaxDate),
})
.expect(200)
.then(({ body }) => {
Expand Down

0 comments on commit 3c26f56

Please sign in to comment.