Skip to content

Commit

Permalink
fix issues found while making tests
Browse files Browse the repository at this point in the history
  • Loading branch information
apricot13 committed Nov 28, 2024
1 parent 38ffe29 commit 405bab0
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
18 changes: 13 additions & 5 deletions src/controllers/v1/services/routes/get-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ module.exports = {
* @returns
*/
parseRequestParameters: async queryParams => {
const perPage = parseInt(queryParams.per_page) || 50
const perPage = queryParams.per_page
? parseInt(queryParams.per_page) || 50
: 50
if (perPage > 200) {
throw new Error("Per page limit is 200")
}
Expand Down Expand Up @@ -379,10 +381,16 @@ module.exports = {
* @returns
*/
buildContent(results, lat, lng) {
return results.map(result => ({
...result,
distance_away: calculateDistance(lat, lng, result.locations),
}))
return results.map(result => {
if (result.locations) {
return {
...result,
distance_away: calculateDistance(lat, lng, result.locations),
}
} else {
return result
}
})
},

/**
Expand Down
36 changes: 19 additions & 17 deletions src/lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,26 @@ const fetch = require("isomorphic-unfetch")
module.exports = {
calculateDistance: (lat, lng, locations) => {
let distances = []
locations.forEach(location => {
distances.push(
haversine(
{
latitude: lat,
longitude: lng,
},
{
latitude: location.geometry.coordinates[1],
longitude: location.geometry.coordinates[0],
},
{
unit: "mile",
}
if (locations) {
locations.forEach(location => {
distances.push(
haversine(
{
latitude: lat,
longitude: lng,
},
{
latitude: location.geometry.coordinates[1],
longitude: location.geometry.coordinates[0],
},
{
unit: "mile",
}
)
)
)
})
return Math.min(...distances)
})
return Math.min(...distances)
}
},

geocode: async location => {
Expand Down

0 comments on commit 405bab0

Please sign in to comment.