Skip to content

Commit

Permalink
Merge pull request #45 from wearefuturegov/TOP-235-allow-adding-sched…
Browse files Browse the repository at this point in the history
…ules-to-locations-services

TOP-235 Add support for service_at_locations
  • Loading branch information
apricot13 authored Jun 24, 2024
2 parents c63c4cf + 7aaef9d commit 9d40755
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 10 deletions.
2 changes: 1 addition & 1 deletion .docker/services/mongo/setup-mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ db.indexed_services.createIndex(
}
)
db.indexed_services.createIndex({
"locations.geometry": "2dsphere",
"service_at_locations.location.geometry": "2dsphere",
})
db.indexed_services.createIndex({
"taxonomies.slug": 1,
Expand Down
6 changes: 4 additions & 2 deletions __tests__/unit/lib/filters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe("locationGeometry", () => {
const lat = "40.7128"
const lng = "-74.0060"
const expectedQuery = {
"locations.geometry": {
"service_at_locations.location.geometry": {
$nearSphere: {
$geometry: {
type: "Point",
Expand Down Expand Up @@ -203,7 +203,9 @@ describe("filterAccessibilities", () => {
"wheelchair-accessible-entrance",
]
const expectedQuery = {
"locations.accessibilities.slug": { $in: accessibilities },
"service_at_locations.location.accessibilities.slug": {
$in: accessibilities,
},
}
expect(filters.filterAccessibilities(accessibilities)).toEqual(
expectedQuery
Expand Down
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ It needs the right indices on the MongoDB collection to enable full-text and geo

```
db.indexed_services.createIndex({ name: "text", description: "text" })
db.indexed_services.createIndex({ "locations.coordinates": "2dsphere" })
db.indexed_services.createIndex({ "service_at_locations.location.geometry": "2dsphere" })
```

You can create these two, plus an index of taxonomy slugs, automatically with the `npm run prepare-indices` command.
Expand Down
6 changes: 3 additions & 3 deletions src/controllers/v1/services/routes/get-services.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,11 @@ module.exports = {
createCountQuery: query => {
// "budget deep clone" we spread $and so can modify it for countQuery only
const countQuery = { ...query, $and: [...query.$and] }
if ("locations.geometry" in countQuery) {
delete countQuery["locations.geometry"]
if ("service_at_locations.location.geometry" in countQuery) {
delete countQuery["service_at_locations.location.geometry"]

countQuery["$and"].push({
"locations.geometry": {
"service_at_locations.location.geometry": {
$exists: true,
$ne: null,
},
Expand Down
10 changes: 8 additions & 2 deletions src/lib/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ module.exports = {
locationGeometry: (lat, lng) => {
let query = {}
if (lat && lng) {
query["locations.geometry"] = {
query["service_at_locations.location.geometry"] = {
// use this option to search within a defined area
// remember if you take out nearsphere to update the executeQuery function!
// this lets us get accurate result counts
// $geoWithin: {
Expand All @@ -14,11 +15,14 @@ module.exports = {
// ],
// },
// but this is how its always been done so we will keep this for now
// added maxDistance to limit the search to 15 miles for efficiency
// nb if you add in $maxDistance you will need to update the createCountQuery function workaround
$nearSphere: {
$geometry: {
type: "Point",
coordinates: [parseFloat(lng), parseFloat(lat)],
},
// $maxDistance: 10 * 1609.34, // miles x 1609.34 = Distance in meters
},
}
}
Expand Down Expand Up @@ -202,7 +206,9 @@ module.exports = {
filterAccessibilities: accessibilities => {
if (accessibilities?.length > 0) {
return {
"locations.accessibilities.slug": { $in: accessibilities },
"service_at_locations.location.accessibilities.slug": {
$in: accessibilities,
},
}
}
return {}
Expand Down
2 changes: 1 addition & 1 deletion utils/prepare-indices.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ connect(async db => {
}
)
await db.collection("indexed_services").createIndex({
"locations.geometry": "2dsphere",
"service_at_locations.location.geometry": "2dsphere",
})
await db.collection("indexed_services").createIndex({
"taxonomies.slug": 1,
Expand Down

0 comments on commit 9d40755

Please sign in to comment.