diff --git a/server/models/views/target-area.js b/server/models/views/target-area.js index 402a8526f..2ee719983 100644 --- a/server/models/views/target-area.js +++ b/server/models/views/target-area.js @@ -6,32 +6,20 @@ const getThresholdsForTargetArea = require('./lib/latest-levels') class ViewModel { constructor (options) { const { area, flood, parentFlood, thresholds } = options - const severityLevel = flood && severity.filter(item => { - return item.id === flood.severity_value - })[0] - const parentSeverityLevel = parentFlood && severity.filter(item => { - return item.id === parentFlood.severity_value - })[0] - const type = area.code.charAt(4).toLowerCase() === 'w' - ? 'warning' - : 'alert' + const severityLevel = flood && severity.filter(item => item.id === flood.severity_value)[0] + const parentSeverityLevel = parentFlood && severity.filter(item => item.id === parentFlood.severity_value)[0] - const mapTitle = `View map of the flood ${type} area` + const type = area.code.charAt(4).toLowerCase() === 'w' ? 'warning' : 'alert' - let fallbackText - if (type === 'alert') { - fallbackText = '

We\'ll update this page when there\'s a flood alert in the area, which means flooding to low lying land is possible.

' - } else { - fallbackText = '

We\'ll update this page when there\'s a flood warning in the area.

A flood warning means flooding to some property is expected. A severe flood warning means there\'s a danger to life.

' - } + const mapTitle = `View map of the flood ${type} area` + const fallbackText = getFallbackText(type) let situation = fallbackText + if (flood?.situation) { flood.situation = flood.situation.trim() - const message = flood.situation - - situation = messageValidator(message) + situation = messageValidator(flood.situation) } const dateSituationChanged = flood @@ -44,19 +32,18 @@ class ViewModel { area.description = area.description.trim() const description = area.description.endsWith('.') ? area.description.slice(0, -1) : area.description + const areaDescription = `Flood ${type} area: ${description}.` const metaDescription = `Flooding information and advice for the area: ${description}.` const parentAreaAlert = (!!(((flood && severityLevel.id === 4) && (type === 'warning')) || !flood) && (parentSeverityLevel?.isActive)) - let situationChanged = flood - ? `Updated ${timeSituationChanged} on ${dateSituationChanged}` - : `Up to date as of ${timeSituationChanged} on ${dateSituationChanged}` - if (severityLevel?.id === 4) { - situationChanged = `Removed ${timeSituationChanged} on ${dateSituationChanged}` - } + const situationChanged = getSituationChangedText(flood, severityLevel, timeSituationChanged, dateSituationChanged) + + const pageTitle = severityLevel?.isActive + ? `${severityLevel.title} for ${area.name}` + : `${area.name} flood ${type} area` - const pageTitle = (severityLevel?.isActive ? `${severityLevel.title} for ${area.name}` : `${area.name} flood ${type} area`) const metaCanonical = `/target-area/${area.code}` const latestLevels = thresholds ? getThresholdsForTargetArea(thresholds) : null @@ -85,8 +72,26 @@ class ViewModel { } } +function getFallbackText (type) { + if (type === 'alert') { + return '

We\'ll update this page when there\'s a flood alert in the area, which means flooding to low lying land is possible.

' + } else { + return '

We\'ll update this page when there\'s a flood warning in the area.

A flood warning means flooding to some property is expected. A severe flood warning means there\'s a danger to life.

' + } +} + +function getSituationChangedText (flood, severityLevel, timeSituationChanged, dateSituationChanged) { + if (severityLevel?.id === 4) { + return `Removed ${timeSituationChanged} on ${dateSituationChanged}` + } + return flood + ? `Updated ${timeSituationChanged} on ${dateSituationChanged}` + : `Up to date as of ${timeSituationChanged} on ${dateSituationChanged}` +} + function messageValidator (message) { const strippedMessage = message.replace(/(\r?\n)+/g, '\n') return strippedMessage.split('\n').map(p => `

${p}

`).join(' ') } + module.exports = ViewModel diff --git a/server/util.js b/server/util.js index 1e138a0f4..04fd07bc0 100644 --- a/server/util.js +++ b/server/util.js @@ -6,6 +6,7 @@ const wreck = require('@hapi/wreck').defaults({ }) const LocationSearchError = require('./location-search-error') const ALLOWED_SEARCH_CHARS = 'a-zA-Z0-9\',-.& ()!' +const timezone = 'Europe/London' async function request (method, url, options) { let res, payload @@ -50,12 +51,12 @@ function getJson (url) { } function formatDate (value, format = 'D/M/YY h:mma') { - return moment(value).tz('Europe/London').format(format) + return moment(value).tz(timezone).format(format) } function formatElapsedTime (datetime) { - const now = moment.tz('Europe/London') - const diffMinutes = now.diff(moment.tz(datetime, 'Europe/London'), 'minutes') + const now = moment.tz(timezone) + const diffMinutes = now.diff(moment.tz(datetime, timezone), 'minutes') if (diffMinutes < 60) { return `${diffMinutes} minutes ago`