-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #701 from DEFRA/feature/FSR-1207
FSR-1207 | alerts and warnings search
- Loading branch information
Showing
8 changed files
with
392 additions
and
245 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,92 +1,175 @@ | ||
const qs = require('qs') | ||
const joi = require('@hapi/joi') | ||
const boom = require('@hapi/boom') | ||
const ViewModel = require('../models/views/alerts-and-warnings') | ||
const Floods = require('../models/floods') | ||
const locationService = require('../services/location') | ||
const util = require('../util') | ||
const { slugify } = require('./lib/utils') | ||
|
||
const page = 'alerts-and-warnings' | ||
const QUERY_STRING_LOCATION_MAX_LENGTH = 200 | ||
|
||
function renderLocationNotFound (location, h) { | ||
return h.view('location-not-found', { pageTitle: 'Error: Find location - Check for flooding', href: page, location }).takeover() | ||
} | ||
|
||
function renderNotFound (location) { | ||
return boom.notFound(`Location ${location} not found`) | ||
} | ||
|
||
function createQueryParametersString (queryObject) { | ||
const { q, location, ...otherParameters } = queryObject | ||
const queryString = qs.stringify(otherParameters, { addQueryPrefix: true, encode: false }) | ||
return queryString | ||
} | ||
|
||
async function routeHandler (request, h) { | ||
let location = request.query.q || request.payload?.location | ||
|
||
request.yar.set('q', { location }) | ||
|
||
const direction = request.query.direction === 'downstream' ? 'd' : 'u' | ||
|
||
let model, floods | ||
|
||
if (request.query.station) { | ||
const station = await request.server.methods.flood.getStationById(request.query.station, direction) | ||
|
||
const warningsAlerts = await request.server.methods.flood.getWarningsAlertsWithinStationBuffer(station.rloi_id) | ||
floods = new Floods({ floods: warningsAlerts }) | ||
model = new ViewModel({ location, floods, station }) | ||
return h.view(page, { model }) | ||
} | ||
|
||
if (!location) { | ||
const data = await request.server.methods.flood.getFloods() | ||
floods = new Floods(data) | ||
model = new ViewModel({ location, floods }) | ||
return h.view(page, { model }) | ||
} | ||
|
||
location = util.cleanseLocation(location) | ||
|
||
const [place] = await locationService.find(location) | ||
|
||
if (!place) { | ||
if (request.method === 'get') { | ||
return renderNotFound(location) | ||
} | ||
|
||
return renderLocationNotFound(location, h) | ||
} | ||
|
||
if (!place.isEngland.is_england) { | ||
request.logger.warn({ | ||
situation: 'Location search error: Valid response but location not in England.' | ||
}) | ||
|
||
if (request.method === 'post') { | ||
return renderLocationNotFound(location, h) | ||
} | ||
} | ||
|
||
const queryString = createQueryParametersString(request.query) | ||
|
||
return h.redirect(`/${page}/${slugify(place?.name)}${queryString}`).permanent() | ||
} | ||
|
||
async function locationRouteHandler (request, h) { | ||
const canonicalUrl = request.url.origin + request.url.pathname | ||
const location = util.cleanseLocation(request.params.location) | ||
|
||
const [place] = await locationService.find(location) | ||
|
||
if (location.match(/^england$/i)) { | ||
return h.redirect(`/${page}`) | ||
} | ||
|
||
if (slugify(place?.name) !== location) { | ||
return renderNotFound(location) | ||
} | ||
|
||
if (!place?.isEngland.is_england) { | ||
request.logger.warn({ | ||
situation: 'Location search error: Valid response but location not in England.' | ||
}) | ||
|
||
return renderNotFound(location) | ||
} | ||
|
||
// Data passed to floods model so the schema is the same as cached floods | ||
const data = await request.server.methods.flood.getFloodsWithin(place.bbox2k) | ||
const floods = new Floods(data) | ||
const model = new ViewModel({ location, place, floods, canonical: canonicalUrl, q: request.yar.get('q')?.location }) | ||
request.yar.set('q', null) | ||
return h.view(page, { model }) | ||
} | ||
|
||
function failActionHandler (request, h) { | ||
request.logger.warn({ | ||
situation: 'Location search error: Invalid or no string input.' | ||
}) | ||
|
||
const location = request.query.q || request.payload?.location | ||
|
||
if (!location) { | ||
return h.redirect(page).takeover() | ||
} else { | ||
return renderLocationNotFound(location, h) | ||
} | ||
} | ||
|
||
module.exports = [{ | ||
method: 'GET', | ||
path: '/alerts-and-warnings', | ||
handler: async (request, h) => { | ||
const { q: location } = request.query | ||
let model, place, floods | ||
|
||
const direction = request.query.direction === 'downstream' ? 'd' : 'u' | ||
const station = request.query.station | ||
? await request.server.methods.flood.getStationById(request.query.station, direction) | ||
: null | ||
|
||
if (station) { | ||
const warningsAlerts = await request.server.methods.flood.getWarningsAlertsWithinStationBuffer(station.rloi_id) | ||
floods = new Floods({ floods: warningsAlerts }) | ||
model = new ViewModel({ location, place, floods, station }) | ||
return h.view('alerts-and-warnings', { model }) | ||
} else if (typeof location === 'undefined' || location === '' || location.match(/^england$/i)) { | ||
floods = new Floods(await request.server.methods.flood.getFloods()) | ||
model = new ViewModel({ location, place, floods, station }) | ||
return h.view('alerts-and-warnings', { model }) | ||
} else { | ||
try { | ||
[place] = await locationService.find(util.cleanseLocation(location)) | ||
} catch (error) { | ||
request.logger.warn({ | ||
situation: `Location search error: [${error.name}] [${error.message}]`, | ||
err: error | ||
}) | ||
const floods = new Floods(await request.server.methods.flood.getFloods()) | ||
model = new ViewModel({ location, place, floods, station, error }) | ||
return h.view('alerts-and-warnings', { model }) | ||
} | ||
|
||
if (!place) { | ||
model = new ViewModel({ location, place, floods, station }) | ||
return h.view('alerts-and-warnings', { model }) | ||
} | ||
|
||
if (!place?.isEngland.is_england) { | ||
// If no place return empty floods | ||
model = new ViewModel({ location, place, floods, station }) | ||
return h.view('alerts-and-warnings', { model }) | ||
} else { | ||
// Data passed to floods model so the schema is the same as cached floods | ||
const data = await request.server.methods.flood.getFloodsWithin(place.bbox2k) | ||
floods = new Floods(data) | ||
model = new ViewModel({ location, place, floods, station }) | ||
return h.view('alerts-and-warnings', { model }) | ||
} | ||
path: `/${page}`, | ||
handler: routeHandler, | ||
options: { | ||
validate: { | ||
query: joi.object({ | ||
q: joi.string().trim().max(QUERY_STRING_LOCATION_MAX_LENGTH), | ||
station: joi.string(), | ||
btn: joi.string(), | ||
ext: joi.string(), | ||
fid: joi.string(), | ||
lyr: joi.string(), | ||
v: joi.string() | ||
}), | ||
failAction: failActionHandler | ||
} | ||
}, | ||
} | ||
}, | ||
{ | ||
method: 'GET', | ||
path: `/${page}/{location}`, | ||
handler: locationRouteHandler, | ||
options: { | ||
validate: { | ||
params: joi.object({ | ||
location: joi.string().lowercase() | ||
}), | ||
query: joi.object({ | ||
q: joi.string().allow('').trim().max(200), | ||
station: joi.string(), | ||
btn: joi.string(), | ||
ext: joi.string(), | ||
fid: joi.string(), | ||
lyr: joi.string(), | ||
v: joi.string() | ||
}) | ||
}), | ||
failAction: failActionHandler | ||
} | ||
} | ||
}, { | ||
}, | ||
{ | ||
method: 'POST', | ||
path: '/alerts-and-warnings', | ||
handler: async (request, h) => { | ||
const { location } = request.payload | ||
if (location === '') { | ||
return h.redirect(`/alerts-and-warnings?q=${location}`) | ||
} | ||
return h.redirect(`/alerts-and-warnings?q=${encodeURIComponent(util.cleanseLocation(location))}`) | ||
}, | ||
path: `/${page}`, | ||
handler: routeHandler, | ||
options: { | ||
validate: { | ||
payload: joi.object({ | ||
location: joi.string().allow('').trim().max(200).required() | ||
location: joi.string().allow('').trim().max(QUERY_STRING_LOCATION_MAX_LENGTH).required() | ||
}), | ||
failAction: (request, h, _err) => { | ||
return h.view('alerts-and-warnings').takeover() | ||
} | ||
failAction: failActionHandler | ||
} | ||
} | ||
}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.