Skip to content

Commit

Permalink
FSR-1237: Fix SonarCloud issues and refactor code for improved quality
Browse files Browse the repository at this point in the history
  • Loading branch information
Keyurx11 committed Aug 13, 2024
1 parent 3316ed4 commit b337f9a
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 29 deletions.
57 changes: 31 additions & 26 deletions server/models/views/target-area.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<p>We\'ll update this page when there\'s a flood alert in the area, which means flooding to low lying land is possible.</p>'
} else {
fallbackText = '<p>We\'ll update this page when there\'s a flood warning in the area.</p><p>A flood warning means flooding to some property is expected. A severe flood warning means there\'s a danger to life.</p>'
}
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
Expand All @@ -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
Expand Down Expand Up @@ -85,8 +72,26 @@ class ViewModel {
}
}

function getFallbackText (type) {
if (type === 'alert') {
return '<p>We\'ll update this page when there\'s a flood alert in the area, which means flooding to low lying land is possible.</p>'
} else {
return '<p>We\'ll update this page when there\'s a flood warning in the area.</p><p>A flood warning means flooding to some property is expected. A severe flood warning means there\'s a danger to life.</p>'
}
}

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>${p}</p>`).join(' ')
}

module.exports = ViewModel
7 changes: 4 additions & 3 deletions server/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down

0 comments on commit b337f9a

Please sign in to comment.