Skip to content

Commit

Permalink
FSR-1194 | Add support for location query parameter (#687)
Browse files Browse the repository at this point in the history
* Add support for location parameter
* Remove misleading comment
  • Loading branch information
neilbmclaughlin authored May 1, 2024
1 parent 1258a24 commit 833b397
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 4 deletions.
9 changes: 5 additions & 4 deletions server/routes/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,13 @@ const { slugify } = require('./lib/utils')
const qs = require('qs')

function createQueryParametersString (queryObject) {
const { q, ...otherParameters } = queryObject
// otherParameters has all parameters except q
const { q, location, ...otherParameters } = queryObject
const queryString = qs.stringify(otherParameters, { addQueryPrefix: true, encode: false })
return queryString
}

async function legacyRouteHandler (request, h) {
const location = request.query.q
const location = request.query.q || request.query.location
const [place] = await locationService.find(location)
const queryString = createQueryParametersString(request.query)
if (place) {
Expand Down Expand Up @@ -88,7 +87,9 @@ module.exports = [{
handler: legacyRouteHandler,
options: {
validate: {
query: joi.object({ q: joi.string().required(), ...queryValidation }),
query: joi
.object({ location: joi.string(), q: joi.string(), ...queryValidation })
.or('q', 'location'),
failAction: failActionHandler
}
}
Expand Down
78 changes: 78 additions & 0 deletions test/routes/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,84 @@ lab.experiment('Routes test - location - 2', () => {
Code.expect(response.statusCode).to.equal(301)
Code.expect(response.headers.location).to.equal('/location/coxwold-north-yorkshire?lyr=mv,ts,tw,ta')
})
lab.test('GET /location with location query parameter', async () => {
const fakeGetJson = () => {
return {
authenticationResultCode: 'ValidCredentials',
brandLogoUri: 'http://dev.virtualearth.net/Branding/logo_powered_by.png',
copyright: 'Copyright',
resourceSets: [
{
estimatedTotal: 1,
resources: [
{
__type: 'Location:http://schemas.microsoft.com/search/local/ws/rest/v1',
bbox: [54.18498992919922, -1.1886399984359741, 54.1898307800293, -1.1779199838638306],
name: 'Coxwold, North Yorkshire',
point: {
type: 'Point',
coordinates: [54.187835693359375, -1.182824969291687]
},
address: {
adminDistrict: 'England',
adminDistrict2: 'North Yorkshire',
countryRegion: 'United Kingdom',
formattedAddress: 'Coxwold, North Yorkshire',
locality: 'Coxwold',
countryRegionIso2: 'GB'
},
confidence: 'High',
entityType: 'PopulatedPlace',
geocodePoints: [
{
type: 'Point',
coordinates: [54.187835693359375, -1.182824969291687],
calculationMethod: 'Rooftop',
usageTypes: ['Display']
}
],
matchCodes: ['Good']
}
]
}
],
statusCode: 200,
tatusDescription: 'OK',
traceId: 'trace-id'
}
}

const util = require('../../server/util')
sandbox.stub(util, 'getJson').callsFake(fakeGetJson)

const locationPlugin = {
plugin: {
name: 'location',
register: (server, options) => {
server.route(require('../../server/routes/location'))
}
}
}

await server.register(require('../../server/plugins/logging'))
await server.register(require('../../server/plugins/views'))
await server.register(require('../../server/plugins/session'))
await server.register(require('../../server/plugins/error-pages'))
await server.register(locationPlugin)
// Add Cache methods to server
const registerServerMethods = require('../../server/services/server-methods')
registerServerMethods(server)

await server.initialize()

const options = {
method: 'GET',
url: '/location?location=coxwold'
}
const response = await server.inject(options)
Code.expect(response.statusCode).to.equal(301)
Code.expect(response.headers.location).to.equal('/location/coxwold-north-yorkshire')
})
lab.test('GET /location with no query parameters', async () => {
const locationPlugin = {
plugin: {
Expand Down

0 comments on commit 833b397

Please sign in to comment.