Skip to content

Commit

Permalink
NI partial postcode fix and page not found fix for url.
Browse files Browse the repository at this point in the history
  • Loading branch information
ulysses-cognizant committed Mar 6, 2025
1 parent 49201c8 commit 6cc33c2
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
18 changes: 18 additions & 0 deletions db.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,23 @@
{
"results": [
{
"addressLine": "ROYAL MAIL, 20, DONEGALL QUAY, BELFAST, BT1",
"subBuildingName": "ROYAL MAIL",
"buildingNumber": "20",
"street": "DONEGALL QUAY",
"town": "BELFAST",
"administrativeArea": "BELFAST",
"historicCounty": "COUNTY ANTRIM",
"ceremonialCounty": "COUNTY ANTRIM",
"postcode": "BT1",
"country": "NORTHERN IRELAND",
"xCoordinate": 146778,
"yCoordinate": 530104,
"uprn": "185870402",
"match": "1",
"matchDescription": "EXACT",
"language": "EN"
},
{
"addressLine": "ROYAL MAIL, 20, DONEGALL QUAY, BELFAST, BT1 1FB",
"subBuildingName": "ROYAL MAIL",
Expand Down
2 changes: 1 addition & 1 deletion src/config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ const config = convict({
enabledMock: {
doc: 'Enabled Mock Data for Northern Ireland Names API',
format: Boolean,
default: false
default: true
},
logLevel: {
doc: 'Logging level',
Expand Down
2 changes: 1 addition & 1 deletion src/server/error/index.njk
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{% block content %}
{% if statusCode == '500' %}
{% include 'partials/error-500.njk' %}
{% elif statusCode == '404' %}
{% elif statusCode == '404' or statusCode == '400' %}
{% if lang == 'en' %}
{% include 'partials/error-404-en.njk' %}
{% elif lang == 'cy' %}
Expand Down
22 changes: 22 additions & 0 deletions src/server/locations/helpers/get-postcode-type.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const fullNorthernIrelandPostcodeRegex = /^BT\d{1,2}\s?\d[A-Z]{2}$/i // Regular expression to match full Northern Ireland postcode format ''
const partialNorthernIrelandPostcodeRegex = /^BT\d{1,2}$/i // Regular expression to match partial Northern Ireland postcode format ''
const fullUKPostcodeRegex = /^[A-Z]{1,2}\d[A-Z\d]?\s?\d[A-Z]{2}$/i // Regular expression to match full UK postcode format ''
const partialUKPostcodeRegex = /^[A-Z]{1,2}\d[A-Z\d]?$/i // Regular expression to match partial UK postcode format ''

function getPostcode(postcode) {
let postcodeType = ''
if (fullNorthernIrelandPostcodeRegex.test(postcode)) {
postcodeType = 'Full Northern Ireland Postcode' // Return postcodeType for full Northern Ireland postcode ''
} else if (partialNorthernIrelandPostcodeRegex.test(postcode)) {
postcodeType = 'Partial Northern Ireland Postcode' // Return postcodeType for partial Northern Ireland postcode ''
} else if (fullUKPostcodeRegex.test(postcode)) {
postcodeType = 'Full UK Postcode' // Return postcodeType for full UK postcode ''
} else if (partialUKPostcodeRegex.test(postcode)) {
postcodeType = 'Partial UK Postcode' // Return postcodeType for partial UK postcode ''
} else {
postcodeType = 'Invalid Postcode' // Return postcodeType for invalid postcode ''
}
return { postcodeType }
}

export { getPostcode }
23 changes: 12 additions & 11 deletions src/server/locations/helpers/get-search-terms-from-url.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { LOCATION_TYPE_NI, LOCATION_TYPE_UK } from '~/src/server/data/constants'
import { getPostcode } from '~/src/server/locations/helpers/get-postcode-type'

const getSearchTermsFromUrl = (url) => {
let searchTermsLang = ''
Expand All @@ -22,23 +23,23 @@ const getSearchTermsFromUrl = (url) => {
) // Extract the string between the last forward slash and the interrogation sign
const parts = extractedString?.split(/[_-]/) // Split by hyphen and underscore
let searchTerms = parts.join(' ') // Join the parts with spaces
// Extract the postcode from the searchTerms variable
const postcodeRegex = /\b[A-Z]{1,2}\d{1,2}[A-Z]?\s?\d[A-Z]{2}\b/i // Regular expression to match postcode format
const postcodeMatch = searchTerms.match(postcodeRegex) // Match the postcode in the searchTerms

const isFullNorthernIrelandPostcode =
postcodeMatch && /^BT\d{1,2}\s?\d[A-Z]{2}$/i.test(postcodeMatch[0]) // Regular expression to match full Northern Ireland postcode format ''
const isPartialNorthernIrelandPostcode =
postcodeMatch && /^BT\d{1,2}$/i.test(postcodeMatch[0]) // Regular expression to match partial Northern Ireland postcode format ''
if (isFullNorthernIrelandPostcode || isPartialNorthernIrelandPostcode) {
const { postcodeType } = getPostcode(searchTerms) // Get the postcode type
if (
postcodeType === 'Full Northern Ireland Postcode' ||
postcodeType === 'Partial Northern Ireland Postcode'
) {
searchTermsLocationType = LOCATION_TYPE_NI // Set the location type to Northern Ireland ''
} else {
}
if (
postcodeType === 'Full UK Postcode' ||
postcodeType === 'Partial UK Postcode'
) {
searchTermsLocationType = LOCATION_TYPE_UK // Set the location type to UK ''
}
// Separate the string after the underscore
const underscoreParts = extractedString?.split('_') // Split the string by underscore
let secondSearchTerm = '' // Initialize the second search term
if (!postcodeMatch) {
if (postcodeType === 'Invalid Postcode') {
searchTerms = underscoreParts[0]?.split('-').join(' ') // Get the part before the underscore // ''
secondSearchTerm = underscoreParts[1]?.split('-').join(' ') // Get the part after the underscore // ''
}
Expand Down
4 changes: 4 additions & 0 deletions src/server/locations/middleware.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ const searchMiddleware = async (request, h) => {
const searchTerms = query?.searchTerms?.toUpperCase()
const secondSearchTerm = query?.secondSearchTerm?.toUpperCase()
const searchTermsLocationType = query?.searchTermsLocationType
if (searchTerms !== undefined && searchTermsLocationType === '') {
request.yar.clear('searchTermsSaved')
return h.redirect('error/index').takeover()
}
const redirectError = handleErrorInputAndRedirect(
request,
h,
Expand Down

0 comments on commit 6cc33c2

Please sign in to comment.