Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
LorhanSohaky committed Feb 24, 2024
2 parents 0794d6a + efb827d commit 3c88f13
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 18 deletions.
10 changes: 7 additions & 3 deletions lib/fetchGeocoordinateFromBrazilLocation.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,14 @@ async function fetchGeocoordinateFromBrazilLocation({
const agent = getAgent();
const encodedState = encodeURI(state);
const encodedCity = encodeURI(city);
const encodedStreet = encodeURI(extractAltFromStreet(street));

const country = 'Brasil';
const queryString = `format=json&addressdetails=1&country=${country}&state=${encodedState}&city=${encodedCity}&street=${encodedStreet}`;

let queryString = `format=json&addressdetails=1&country=${country}&state=${encodedState}&city=${encodedCity}`;

if (street) {
const encodedStreet = encodeURI(extractAltFromStreet(street));
queryString += `&street=${encodedStreet}`;
}

const headers = new Headers({
'User-Agent': 'brasil-api-cep-v2',
Expand Down
14 changes: 7 additions & 7 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
"apollo-server-micro": "2.24.0",
"axios": "0.21.1",
"bluebird": "3.7.2",
"cep-promise": "4.4.0",
"cep-promise": "4.4.1",
"core-js": "3.33.1",
"cors": "2.8.5",
"dayjs": "1.11.7",
Expand Down
8 changes: 8 additions & 0 deletions pages/api/cep/v2/[cep].js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ async function Cep(request, response) {
cepFromCepPromise
);

if (!cepFromCepPromise.street) {
cepFromCepPromise.street = null;
}

if (!cepFromCepPromise.neighborhood) {
cepFromCepPromise.neighborhood = null;
}

response.status(200);
response.json({ ...cepFromCepPromise, location });
} catch (error) {
Expand Down
6 changes: 4 additions & 2 deletions pages/docs/doc/cep.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,10 +158,12 @@
"type": "string"
},
"neighborhood": {
"type": "string"
"type": "string",
"nullable": true
},
"street": {
"type": "string"
"type": "string",
"nullable": true
},
"service": {
"type": "string"
Expand Down
4 changes: 2 additions & 2 deletions pages/docs/doc/cnpj.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@
"cnae_fiscal_descricao": {
"type": "string"
},
"descricao_tipo_logradouro": {
"descricao_tipo_de_logradouro": {
"type": "string"
},
"logradouro": {
Expand Down Expand Up @@ -261,7 +261,7 @@
"data_inicio_atividade": "2013-10-03",
"cnae_fiscal": 9430800,
"cnae_fiscal_descricao": "Atividades de associações de defesa de direitos sociais",
"descricao_tipo_logradouro": "ALAMEDA",
"descricao_tipo_de_logradouro": "ALAMEDA",
"logradouro": "FRANCA",
"numero": "144",
"complemento": "APT 34",
Expand Down
23 changes: 20 additions & 3 deletions services/sefaz/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,28 @@ function parseObject(obj) {
return newDate.toISOString().slice(0, 10);
};

const newObj = Object.fromEntries(
const convertKeysToLowerCase = Object.fromEntries(
Object.entries(obj).map(([k, v]) => [k.toLowerCase(), v])
);
newObj.data_fim = formatDate(newObj.data_fim);
newObj.data_inicio = formatDate(newObj.data_inicio);

const {
tipo_ato_ini: tipoAtoIni,
numero_ato_ini: numeroAtoIni,
ano_ato_ini: anoAtoIni,
data_inicio: dataInicio,
data_fim: dataFim,
...rest
} = convertKeysToLowerCase;

const newObj = {
...rest,
data_inicio: formatDate(dataInicio),
data_fim: formatDate(dataFim),
tipo_ato: tipoAtoIni,
numero_ato: numeroAtoIni,
ano_ato: anoAtoIni,
};

return newObj;
}

Expand Down
21 changes: 21 additions & 0 deletions tests/cep-v2.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,25 @@ describe('/cep/v2 (E2E)', () => {
},
});
});

test('Uma cidade com CEP único, exemplo: 87360000, deve retornar as coordenadas -24.1851885 e -53.0250623', async () => {
const requestUrl = `${global.SERVER_URL}/api/cep/v2/87360000`;
const response = await axios.get(requestUrl);

expect(response.data).toEqual({
cep: '87360000',
state: 'PR',
city: 'Goioerê',
neighborhood: null,
street: null,
service: expect.any(String),
location: {
type: 'Point',
coordinates: {
longitude: '-53.0250623',
latitude: '-24.1851885',
},
},
});
});
});

0 comments on commit 3c88f13

Please sign in to comment.