From 796d3f0eba9ac53e35cd40d8bc38d21664469787 Mon Sep 17 00:00:00 2001 From: neil mclaughlin Date: Tue, 24 Dec 2024 10:22:03 +0000 Subject: [PATCH] Add slug property to search result This is to remove code duplication and also to allow different slug creation behaviour based on entityType --- server/routes/alerts-and-warnings.js | 3 +-- server/routes/lib/utils.js | 7 +------ server/routes/location.js | 5 ++--- server/routes/national.js | 3 +-- server/routes/river-and-sea-levels.js | 3 +-- server/services/lib/bing-results-parser.js | 13 ++++++++++--- test/routes/national.js | 1 + test/services/lib/bing-results-parser.js | 11 +++++++++-- test/services/location.js | 1 + 9 files changed, 27 insertions(+), 20 deletions(-) diff --git a/server/routes/alerts-and-warnings.js b/server/routes/alerts-and-warnings.js index eedf34064..8540d8564 100644 --- a/server/routes/alerts-and-warnings.js +++ b/server/routes/alerts-and-warnings.js @@ -4,7 +4,6 @@ const Floods = require('../models/floods') const locationService = require('../services/location') const util = require('../util') const { - slugify, isLocationEngland, isValidLocationSlug, isPlaceEngland, @@ -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) { diff --git a/server/routes/lib/utils.js b/server/routes/lib/utils.js index 64dc727f5..475727879 100644 --- a/server/routes/lib/utils.js +++ b/server/routes/lib/utils.js @@ -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 } @@ -30,7 +26,7 @@ function isPlaceEngland (place) { } function isValidLocationSlug (location, place) { - return slugify(place?.name) === location + return place?.slug === location } function createQueryParametersString (queryObject) { @@ -62,7 +58,6 @@ function failActionHandler (request, h, page) { } module.exports = { - slugify, failActionHandler, isLocationEngland, isPlaceEngland, diff --git a/server/routes/location.js b/server/routes/location.js index 12605c530..3be396000 100644 --- a/server/routes/location.js +++ b/server/routes/location.js @@ -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) { @@ -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`) } @@ -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`) } diff --git a/server/routes/national.js b/server/routes/national.js index c0117ab79..dd1b510cc 100644 --- a/server/routes/national.js +++ b/server/routes/national.js @@ -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()) @@ -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: { diff --git a/server/routes/river-and-sea-levels.js b/server/routes/river-and-sea-levels.js index cd54aeab6..65d1470ab 100644 --- a/server/routes/river-and-sea-levels.js +++ b/server/routes/river-and-sea-levels.js @@ -13,7 +13,6 @@ const { const locationService = require('../services/location') const util = require('../util') const { - slugify, failActionHandler, renderNotFound, renderLocationNotFound, @@ -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) { diff --git a/server/services/lib/bing-results-parser.js b/server/services/lib/bing-results-parser.js index 90365904c..d0d6544a3 100644 --- a/server/services/lib/bing-results-parser.js +++ b/server/services/lib/bing-results-parser.js @@ -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) { @@ -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() @@ -125,6 +131,7 @@ async function bingResultsParser (bingData) { return [{ name, + slug, center, bbox2k, bbox10k, diff --git a/test/routes/national.js b/test/routes/national.js index e313c8948..265b91935 100644 --- a/test/routes/national.js +++ b/test/routes/national.js @@ -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, diff --git a/test/services/lib/bing-results-parser.js b/test/services/lib/bing-results-parser.js index 45baf7fe9..8b3b90d84 100644 --- a/test/services/lib/bing-results-parser.js +++ b/test/services/lib/bing-results-parser.js @@ -78,6 +78,7 @@ experiment('bingResultsParser', () => { const expectedResult = [ { name: 'Knaresborough, North Yorkshire', + slug: 'knaresborough-north-yorkshire', center: [-1.46303844, 54.00714111], bbox2k: [ -1.534142855800849, @@ -230,6 +231,7 @@ experiment('bingResultsParser', () => { // response { name: 'Northumberland', + slug: 'northumberland', center: [-2.06545234, 55.24245834], bbox2k: [ -2.721803715494745, @@ -298,6 +300,7 @@ experiment('bingResultsParser', () => { const expectedResult = [ { name: 'Cumbria', + slug: 'cumbria', center: [-2.91157079, 54.57675934], bbox2k: [ -3.672137848226576, @@ -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, @@ -442,7 +446,8 @@ experiment('bingResultsParser', () => { const expectedResult = [ { - name: 'HG5', + name: 'Knaresborough, HG5', + slug: 'hg5', center: [-1.45626473, 54.01323318], bbox2k: [ -1.554794384621328, @@ -578,6 +583,7 @@ experiment('bingResultsParser', () => { const expectedResult = [ { name: 'Knaresborough, North Yorkshire', + slug: 'knaresborough-north-yorkshire', center: [-1.46303844, 54.00714111], bbox2k: [ -1.534142855800849, @@ -832,6 +838,7 @@ experiment('bingResultsParser', () => { const expectedResult = [ { name: 'Ashford, Kent', + slug: 'ashford-kent', center: [0.87279475, 51.14772797], bbox2k: [ 0.80935719234919, diff --git a/test/services/location.js b/test/services/location.js index 43639843c..52c3b4f32 100644 --- a/test/services/location.js +++ b/test/services/location.js @@ -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,