diff --git a/db.json b/db.json index a04d7101..09def964 100644 --- a/db.json +++ b/db.json @@ -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", diff --git a/src/config/index.js b/src/config/index.js index 92d3c764..0583a7ac 100644 --- a/src/config/index.js +++ b/src/config/index.js @@ -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', diff --git a/src/server/error/index.njk b/src/server/error/index.njk index 20ab3308..a92de23c 100644 --- a/src/server/error/index.njk +++ b/src/server/error/index.njk @@ -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' %} diff --git a/src/server/locations/helpers/get-postcode-type.js b/src/server/locations/helpers/get-postcode-type.js new file mode 100644 index 00000000..4c53e55d --- /dev/null +++ b/src/server/locations/helpers/get-postcode-type.js @@ -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 } diff --git a/src/server/locations/helpers/get-search-terms-from-url.js b/src/server/locations/helpers/get-search-terms-from-url.js index 863d9902..e59fbba4 100644 --- a/src/server/locations/helpers/get-search-terms-from-url.js +++ b/src/server/locations/helpers/get-search-terms-from-url.js @@ -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 = '' @@ -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 // '' } diff --git a/src/server/locations/middleware.js b/src/server/locations/middleware.js index 3e8cdc70..0645841a 100644 --- a/src/server/locations/middleware.js +++ b/src/server/locations/middleware.js @@ -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,