Skip to content

Commit

Permalink
northern ireland and rest of uk option page done.
Browse files Browse the repository at this point in the history
  • Loading branch information
ulysses-cognizant committed Mar 8, 2024
1 parent 61a4585 commit dda70b6
Show file tree
Hide file tree
Showing 10 changed files with 362 additions and 214 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ npm-debug.log
coverage
.cache
.envrc
.env
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
"babel-plugin-module-resolver": "5.0.0",
"convict": "6.2.4",
"date-fns": "3.3.1",
"dotenv": "16.4.5",
"govuk-frontend": "5.1.0",
"hapi-pino": "12.1.0",
"https-proxy-agent": "7.0.2",
Expand Down
1 change: 1 addition & 0 deletions src/server/common/templates/layouts/page.njk
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
{% from "toggletip/macro.njk" import toggletip %}
{% from "aq-levels-table/macro.njk" import aqLevelsTable %}
{% from "input/macro.njk" import govukInput -%}
{% from "radios/macro.njk" import govukRadios -%}


{% set mainClasses = "app-main-wrapper" %}
Expand Down
3 changes: 2 additions & 1 deletion src/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { requestLogger } from '~/src/server/common/helpers/logging/request-logge
import { catchAll } from '~/src/server/common/helpers/errors'
import { secureContext } from '~/src/server/common/helpers/secure-context'
import hapiCookie from '@hapi/cookie'

import dotenv from 'dotenv'
dotenv.config()
const isProduction = config.get('isProduction')

async function createServer() {
Expand Down
2 changes: 1 addition & 1 deletion src/server/location-id/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLocationDetailsController } from '~/src/server/search-location/controller'
import { getLocationDetailsController } from '~/src/server/locations/controller'

const locationId = {
plugin: {
Expand Down
243 changes: 224 additions & 19 deletions src/server/locations/controller.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,229 @@
const searchLocationController = {
import axios from 'axios'
import {
monitoringSites,
siteTypeDescriptions,
pollutantTypes
} from '../data/monitoring-sites.js'
import * as airQualityData from '../data/air-quality.js'
import { getAirQuality } from '../data/air-quality.js'

const getLocationDataController = {
handler: async (request, h) => {
const locationType = request?.payload?.locationType
let locationNameOrPostcode = ''
if (locationType === 'uk-location') {
locationNameOrPostcode = request.payload.engScoWal
} else if (locationType === 'ni-location') {
locationNameOrPostcode = request.payload.ni
}

if (!locationNameOrPostcode && !locationType) {
request.yar.set('errors', {
errors: {
titleText: 'There is a problem',
errorList: [
{
text: 'Select a location',
href: '#'
}
]
}
})
request.yar.set('locationType', '')

return h.redirect('/aqie-front-end/search-location')
}
try {
let userLocation = locationNameOrPostcode.toUpperCase() // Use 'let' to allow reassignment
// Regex patterns to check for full and partial postcodes
const fullPostcodePattern = /^([A-Z]{1,2}\d[A-Z\d]? ?\d[A-Z]{2})$/
const partialPostcodePattern = /^([A-Z]{1,2}\d[A-Z\d]?)$/

// Insert a space for full postcodes without a space
if (
fullPostcodePattern.test(userLocation) &&
!userLocation.includes(' ')
) {
const spaceIndex = userLocation.length - 3
userLocation = `${userLocation.slice(0, spaceIndex)} ${userLocation.slice(
spaceIndex
)}`
}

if (!userLocation && locationType === 'uk-location') {
request.yar.set('errors', {
errors: {
titleText: 'There is a problem',
errorList: [
{
text: 'Enter a location or postcode',
href: '#engScoWal'
}
]
}
})
request.yar.set('errorMessage', {
errorMessage: {
text: 'Enter a location or postcode'
}
})
request.yar.set('locationType', 'uk-location')
return h.redirect('/aqie-front-end/search-location')
}
if (!userLocation && locationType === 'ni-location') {
request.yar.set('errors', {
errors: {
titleText: 'There is a problem',
errorList: [
{
text: 'Enter a postcode',
href: '#ni'
}
]
}
})
request.yar.set('errorMessage', {
errorMessage: {
text: 'Enter a postcode'
}
})
request.yar.set('locationType', 'ni-location')
return h.redirect('/aqie-front-end/search-location')
}
const airQuality = getAirQuality(request.payload.aq)

if (locationType === 'uk-location') {
const filters = [
'LOCAL_TYPE:City',
'LOCAL_TYPE:Town',
'LOCAL_TYPE:Village',
'LOCAL_TYPE:Suburban_Area',
'LOCAL_TYPE:Postcode',
'LOCAL_TYPE:Airport'
].join('+')

const apiUrl = `${process.env.OS_PLACES_API_URL}${encodeURIComponent(
userLocation
)}&fq=${encodeURIComponent(filters)}&key=${process.env.OS_PLACES_API_KEY}`

const response = await axios.get(apiUrl)

const { results } = response.data

if (!results || results.length === 0) {
return h.view('locations/location-not-found', {
userLocation: locationNameOrPostcode
})
}

let matches = results.filter((item) => {
const name = item.GAZETTEER_ENTRY.NAME1.toUpperCase()
return name.includes(userLocation) || userLocation.includes(name)
})

// If it's a partial postcode and there are matches, use the first match and adjust the title
if (
partialPostcodePattern.test(locationNameOrPostcode.toUpperCase()) &&
matches.length > 0 &&
locationNameOrPostcode.length <= 3
) {
matches[0].GAZETTEER_ENTRY.NAME1 =
locationNameOrPostcode.toUpperCase() // Set the name to the partial postcode
matches = [matches[0]]
}

request.yar.set('locationData', { data: matches })

if (matches.length === 1) {
return h.view('locations/location', {
result: matches[0],
airQuality,
airQualityData: airQualityData.commonMessages,
monitoringSites,
siteTypeDescriptions,
pollutantTypes,
locationType: 'single location',
serviceName: 'Check local air quality'
})
} else if (matches.length > 1 && locationNameOrPostcode.length > 3) {
return h.view('locations/multiple-locations', {
results: matches,
userLocation: locationNameOrPostcode,
airQuality,
airQualityData: airQualityData.commonMessages,
monitoringSites,
siteTypeDescriptions,
pollutantTypes,
serviceName: 'Check local air quality'
})
} else {
return h.view('locations/location-not-found', {
userLocation: locationNameOrPostcode
})
}
} else if (locationType === 'ni-location') {
const postcodeApiUrl = `${process.env.NORTHERN_IRELAND_POSTCODE_URL}${encodeURIComponent(userLocation)}`
const response = await axios.get(postcodeApiUrl)
const { result } = response.data

if (!result || result.length === 0) {
return h.view('locations/location-not-found', {
userLocation: locationNameOrPostcode
})
}

const locationData = {
GAZETTEER_ENTRY: {
NAME1: result[0].postcode,
DISTRICT_BOROUGH: result[0].admin_district
}
}
return h.view('locations/location', {
result: locationData,
airQuality,
airQualityData: airQualityData.commonMessages,
monitoringSites,
siteTypeDescriptions,
pollutantTypes
})
}
} catch (error) {
return h.view('error/index', {
userLocation: locationNameOrPostcode
})
}
}
}

const getLocationDetailsController = {
handler: (request, h) => {
return h.view('search-location/index', {
pageTitle: 'Check local air quality',
heading: 'Check local air quality',
page: 'location',
serviceName: 'Check local air quality',
searchParams: {
label: {
text: 'Where do you want to check?',
classes: 'govuk-label--l govuk-!-margin-bottom-6',
isPageHeading: true
},
hint: {
text: 'Enter a location or postcode'
},
id: 'location',
name: 'location'
try {
const locationId = request.path.split('/')[3]
const locationData = request.yar.get('locationData') || []
const locationDetails = locationData.data.find(
(item) => item.GAZETTEER_ENTRY.ID === locationId
)

if (locationDetails) {
const airQuality =
getAirQuality(/* Retrieved from session or another source */)
return h.view('locations/location', {
result: locationDetails,
airQuality,
airQualityData: airQualityData.commonMessages,
monitoringSites,
siteTypeDescriptions,
pollutantTypes
})
} else {
return h.view('location-not-found')
}
})
} catch (error) {
return h.status(500).render('error', {
error: 'An error occurred while retrieving location details.'
})
}
}
}

export { searchLocationController }
export { getLocationDataController, getLocationDetailsController }
2 changes: 1 addition & 1 deletion src/server/locations/index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getLocationDataController } from '~/src/server/search-location/controller'
import { getLocationDataController } from '~/src/server/locations/controller'

const locations = {
plugin: {
Expand Down
Loading

0 comments on commit dda70b6

Please sign in to comment.