Skip to content

Commit 1ab468b

Browse files
authored
Merge branch 'main' into main
2 parents eb92d8f + ac09c6f commit 1ab468b

File tree

2 files changed

+46
-9
lines changed

2 files changed

+46
-9
lines changed

lib/fetchGeocoordinateFromBrazilLocation.js

+25-9
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,19 @@ function getAgent() {
1010
return cacheAgent;
1111
}
1212

13-
async function fetchGeocoordinateFromBrazilLocation({ state, city, street }) {
13+
async function fetchGeocoordinateFromBrazilLocation({
14+
state,
15+
city,
16+
street,
17+
cep,
18+
}) {
1419
const agent = getAgent();
1520
const encodedState = encodeURI(state);
1621
const encodedCity = encodeURI(city);
1722
const encodedStreet = encodeURI(street);
1823

1924
const country = 'Brasil';
20-
const queryString = `format=json&addressdetails=1&country=${country}&state=${encodedState}&city=${encodedCity}&street=${encodedStreet}&limit=1`;
25+
const queryString = `format=json&addressdetails=1&country=${country}&state=${encodedState}&city=${encodedCity}&street=${encodedStreet}`;
2126

2227
const response = await fetch(
2328
`https://nominatim.openstreetmap.org/search/?${queryString}`,
@@ -26,15 +31,26 @@ async function fetchGeocoordinateFromBrazilLocation({ state, city, street }) {
2631

2732
const jsonData = await response.json();
2833

29-
if (jsonData.length > 0) {
30-
const { lat: latitude, lon: longitude } = jsonData[0];
31-
return { type: 'Point', coordinates: { longitude, latitude } };
34+
const cleanedCep = cep.replace(/\D/g, '');
35+
const exactLocation = jsonData.find(
36+
(item) => item.address.postcode.replace(/\D/g, '') === cleanedCep
37+
);
38+
const approximateLocation = jsonData.find(
39+
(item) =>
40+
item.address.postcode.replace(/\D/g, '').slice(0, 5) ===
41+
cleanedCep.slice(0, 5)
42+
);
43+
const location = exactLocation || approximateLocation;
44+
45+
if (!location) {
46+
return {
47+
type: 'Point',
48+
coordinates: { longitude: undefined, latitude: undefined },
49+
};
3250
}
3351

34-
return {
35-
type: 'Point',
36-
coordinates: { longitude: undefined, latitude: undefined },
37-
};
52+
const { lat: latitude, lon: longitude } = location;
53+
return { type: 'Point', coordinates: { longitude, latitude } };
3854
}
3955

4056
export default fetchGeocoordinateFromBrazilLocation;

tests/cep-v2.test.js

+21
Original file line numberDiff line numberDiff line change
@@ -63,4 +63,25 @@ describe('/cep/v2 (E2E)', () => {
6363
});
6464
}
6565
});
66+
67+
test('Deve retornar as coordenadas -22.883892 e -43.3061123', async () => {
68+
const requestUrl = `${global.SERVER_URL}/api/cep/v2/20751120`;
69+
const response = await axios.get(requestUrl);
70+
71+
expect(response.data).toEqual({
72+
cep: '20751120',
73+
state: 'RJ',
74+
city: 'Rio de Janeiro',
75+
neighborhood: 'Piedade',
76+
street: 'Rua Marcolino',
77+
service: expect.any(String),
78+
location: {
79+
type: 'Point',
80+
coordinates: {
81+
longitude: '-43.3061123',
82+
latitude: '-22.883892',
83+
},
84+
},
85+
});
86+
});
6687
});

0 commit comments

Comments
 (0)