Skip to content

Commit

Permalink
Add slug property to search result
Browse files Browse the repository at this point in the history
This is to remove code duplication and also to allow different slug creation
behaviour based on entityType
  • Loading branch information
neilbmclaughlin committed Dec 24, 2024
1 parent f00c96c commit 796d3f0
Show file tree
Hide file tree
Showing 9 changed files with 27 additions and 20 deletions.
3 changes: 1 addition & 2 deletions server/routes/alerts-and-warnings.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const Floods = require('../models/floods')
const locationService = require('../services/location')
const util = require('../util')
const {
slugify,
isLocationEngland,
isValidLocationSlug,
isPlaceEngland,
Expand Down Expand Up @@ -71,7 +70,7 @@ async function routeHandler (request, h) {

const queryString = createQueryParametersString(request.query)

return h.redirect(`/${route}/${slugify(place?.name)}${queryString}`).permanent()
return h.redirect(`/${route}/${place?.slug}${queryString}`).permanent()
}

async function locationRouteHandler (request, h) {
Expand Down
7 changes: 1 addition & 6 deletions server/routes/lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
const qs = require('qs')
const boom = require('@hapi/boom')

function slugify (text = '') {
return text.replace(/,/g, '').replace(/ /g, '-').toLowerCase()
}

function hasInvalidCharacters (location, q) {
return !location && q
}
Expand All @@ -30,7 +26,7 @@ function isPlaceEngland (place) {
}

function isValidLocationSlug (location, place) {
return slugify(place?.name) === location
return place?.slug === location
}

function createQueryParametersString (queryObject) {
Expand Down Expand Up @@ -62,7 +58,6 @@ function failActionHandler (request, h, page) {
}

module.exports = {
slugify,
failActionHandler,
isLocationEngland,
isPlaceEngland,
Expand Down
5 changes: 2 additions & 3 deletions server/routes/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ const locationService = require('../services/location')
const formatDate = require('../util').formatDate
const moment = require('moment-timezone')
const tz = 'Europe/London'
const { slugify } = require('./lib/utils')
const qs = require('qs')

function createQueryParametersString (queryObject) {
Expand All @@ -20,7 +19,7 @@ async function legacyRouteHandler (request, h) {
const [place] = await locationService.find(location)
const queryString = createQueryParametersString(request.query)
if (place) {
return h.redirect(`/location/${slugify(place?.name)}${queryString}`).permanent()
return h.redirect(`/location/${place?.slug}${queryString}`).permanent()
}
return boom.notFound(`Location ${location} not found`)
}
Expand All @@ -33,7 +32,7 @@ async function routeHandler (request, h) {

const [place] = await locationService.find(location)

if (slugify(place?.name) !== location) {
if (place?.slug !== location) {
return boom.notFound(`Location ${location} not found`)
}

Expand Down
3 changes: 1 addition & 2 deletions server/routes/national.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ const OutlookModel = require('../models/outlook')
const FloodsModel = require('../models/floods')
const ViewModel = require('../models/views/national')
const locationService = require('../services/location')
const { slugify } = require('./lib/utils')

async function getModel (request, location) {
const floods = new FloodsModel(await request.server.methods.flood.getFloods())
Expand Down Expand Up @@ -45,7 +44,7 @@ module.exports = [
if (!place?.name || !place.isEngland.is_england) {
return h.view('location-not-found', { pageTitle: 'Error: Find location - Check for flooding', location })
}
return h.redirect(`/location/${encodeURIComponent(slugify(place?.name))}`)
return h.redirect(`/location/${encodeURIComponent(place?.slug)}`)
},
options: {
validate: {
Expand Down
3 changes: 1 addition & 2 deletions server/routes/river-and-sea-levels.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ const {
const locationService = require('../services/location')
const util = require('../util')
const {
slugify,
failActionHandler,
renderNotFound,
renderLocationNotFound,
Expand Down Expand Up @@ -112,7 +111,7 @@ async function locationQueryHandler (request, h) {

const queryString = createQueryParametersString(request.query)

return h.redirect(`/${route}/${slugify(place?.name)}${queryString}`).permanent()
return h.redirect(`/${route}/${place?.slug}${queryString}`).permanent()
}

async function findPlaces (location) {
Expand Down
13 changes: 10 additions & 3 deletions server/services/lib/bing-results-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ const englishCeremonialCounties =
'worcestershire'
]

function slugify (text = '') {
return text.replace(/,/g, '').replace(/ /g, '-').toLowerCase()
}

async function bingResultsParser (bingData) {
const set = bingData.resourceSets[0]
if (set.estimatedTotal === 0) {
Expand Down Expand Up @@ -102,9 +106,11 @@ async function bingResultsParser (bingData) {
point: { coordinates: center }
} = data

const name = ['postcode1', 'postcode3'].includes(data.entityType.toLowerCase())
? data.address.postalCode
: formatName(data.name)
const name = formatName(data.name)

const slug = ['postcode1', 'postcode3'].includes(data.entityType.toLowerCase())
? slugify(data.address.postalCode)
: slugify(name)

// Reverse as Bing returns as [y (lat), x (long)]
bbox.reverse()
Expand All @@ -125,6 +131,7 @@ async function bingResultsParser (bingData) {

return [{
name,
slug,
center,
bbox2k,
bbox10k,
Expand Down
1 change: 1 addition & 0 deletions test/routes/national.js
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,7 @@ lab.experiment('Routes test - national view', () => {
return [
{
name: 'Ashford, Kent',
slug: 'ashford-kent',
center: [0.87279475, 51.14772797],
bbox2k: [
0.80935719234919,
Expand Down
11 changes: 9 additions & 2 deletions test/services/lib/bing-results-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ experiment('bingResultsParser', () => {
const expectedResult = [
{
name: 'Knaresborough, North Yorkshire',
slug: 'knaresborough-north-yorkshire',
center: [-1.46303844, 54.00714111],
bbox2k: [
-1.534142855800849,
Expand Down Expand Up @@ -230,6 +231,7 @@ experiment('bingResultsParser', () => {
// response
{
name: 'Northumberland',
slug: 'northumberland',
center: [-2.06545234, 55.24245834],
bbox2k: [
-2.721803715494745,
Expand Down Expand Up @@ -298,6 +300,7 @@ experiment('bingResultsParser', () => {
const expectedResult = [
{
name: 'Cumbria',
slug: 'cumbria',
center: [-2.91157079, 54.57675934],
bbox2k: [
-3.672137848226576,
Expand Down Expand Up @@ -369,7 +372,8 @@ experiment('bingResultsParser', () => {

const expectedResult = [
{
name: 'HG5 0JL',
name: 'Knaresborough, HG5 0JL',
slug: 'hg5-0jl',
center: [-1.46519089, 54.00955582],
bbox2k: [
-1.5045644526149113,
Expand Down Expand Up @@ -442,7 +446,8 @@ experiment('bingResultsParser', () => {

const expectedResult = [
{
name: 'HG5',
name: 'Knaresborough, HG5',
slug: 'hg5',
center: [-1.45626473, 54.01323318],
bbox2k: [
-1.554794384621328,
Expand Down Expand Up @@ -578,6 +583,7 @@ experiment('bingResultsParser', () => {
const expectedResult = [
{
name: 'Knaresborough, North Yorkshire',
slug: 'knaresborough-north-yorkshire',
center: [-1.46303844, 54.00714111],
bbox2k: [
-1.534142855800849,
Expand Down Expand Up @@ -832,6 +838,7 @@ experiment('bingResultsParser', () => {
const expectedResult = [
{
name: 'Ashford, Kent',
slug: 'ashford-kent',
center: [0.87279475, 51.14772797],
bbox2k: [
0.80935719234919,
Expand Down
1 change: 1 addition & 0 deletions test/services/location.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ describe('location service', () => {
expect(result.length).to.equal(1)
expect(result[0]).to.equal({
name: 'Ashford, Kent',
slug: 'ashford-kent',
center: [0.87279475, 51.14772797],
bbox2k: [
0.80935719234919,
Expand Down

0 comments on commit 796d3f0

Please sign in to comment.