From 91f5a853ca60cf8bc5ed39567ba9f5cb62ef3301 Mon Sep 17 00:00:00 2001 From: Ash Date: Wed, 17 Jul 2024 19:06:57 +0100 Subject: [PATCH] move a conditional to the handler --- server/routes/alerts-and-warnings.js | 22 ++++++++++++++++------ server/routes/river-and-sea-levels.js | 12 +++++++++++- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/server/routes/alerts-and-warnings.js b/server/routes/alerts-and-warnings.js index 6999b2635..8927d125a 100644 --- a/server/routes/alerts-and-warnings.js +++ b/server/routes/alerts-and-warnings.js @@ -49,10 +49,6 @@ async function routeHandler (request, h) { return h.view(route, { model }) } - if (isLocationEngland(location)) { - return h.redirect(`/${route}`) - } - const [place] = await locationService.find(location) if (!place) { @@ -111,7 +107,15 @@ async function locationRouteHandler (request, h) { module.exports = [{ method: 'GET', path: `/${route}`, - handler: routeHandler, + handler: (request, h) => { + if (request.query.q) { + if (isLocationEngland(util.cleanseLocation(request.query.q))) { + return h.redirect(`/${route}`) + } + } + + return routeHandler(request, h) + }, options: { validate: { query: joi.object({ @@ -151,7 +155,13 @@ module.exports = [{ { method: 'POST', path: `/${route}`, - handler: routeHandler, + handler: (request, h) => { + if (isLocationEngland(util.cleanseLocation(request.payload.location))) { + return h.redirect(`/${route}`) + } + + return routeHandler(request, h) + }, options: { validate: { payload: joi.object({ diff --git a/server/routes/river-and-sea-levels.js b/server/routes/river-and-sea-levels.js index f4da4b858..c6769893a 100644 --- a/server/routes/river-and-sea-levels.js +++ b/server/routes/river-and-sea-levels.js @@ -218,6 +218,10 @@ module.exports = [{ // note: the redirects below are to handle any bookmarks users may have as all internal links use the new format // the redirects can be removed at some point in the future when we are no longer concerned about broken bookmarks if (request.query.q) { + if (isLocationEngland(util.cleanseLocation(request.query.q))) { + return h.redirect(`/${route}`) + } + return locationQueryHandler(request, h) } if (rainfallid) { @@ -271,7 +275,13 @@ module.exports = [{ }, { method: 'POST', path: `/${route}`, - handler: locationQueryHandler, + handler: (request, h) => { + if (isLocationEngland(util.cleanseLocation(request.payload.location))) { + return h.redirect(`/${route}`) + } + + return locationQueryHandler(request, h) + }, options: { validate: { payload: joi.object({