This repository has been archived by the owner on Mar 19, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11 from nhsuk/feature/find-by-postcode
Feature/find by postcode - Postcode.io integration and error messages as well as journey
- Loading branch information
Showing
41 changed files
with
1,569 additions
and
157 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,25 @@ | ||
/* eslint-disable sort-keys */ | ||
module.exports = { | ||
ASSETS_URL: 'https://assets.nhs.uk', | ||
SITE_ROOT: '/find-a-chlamydia-test', | ||
SYMPTOMS: { | ||
yes: 'yes', | ||
no: 'no', | ||
}, | ||
AGE: { | ||
under16: '1', | ||
'16to25': '2', | ||
over25: '3', | ||
}, | ||
SERVICE_CHOICES: { | ||
symptoms: '0', | ||
under16: '1', | ||
'16to25': '2', | ||
over25: '3', | ||
}, | ||
SERVICE_TYPES: { | ||
professional: 'professional', | ||
kit: 'kit', | ||
}, | ||
}; | ||
/* eslint-enable sort-keys */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,32 @@ | ||
function invalidUrlMessage() { | ||
return 'This is not a valid url, please start again.'; | ||
} | ||
|
||
function mandatorySelectionMessage() { | ||
return 'You must choose one of the options.'; | ||
} | ||
|
||
function emptyPostcodeMessage() { | ||
return 'You must enter a postcode to find the service you are looking for.'; | ||
} | ||
|
||
function invalidPostcodeMessage(location) { | ||
return `We can't find the postcode '${location}'. Check the postcode is correct and try again.`; | ||
} | ||
|
||
function outsideOfEnglandPostcodeMessage() { | ||
return 'This is an England only service. Please enter an English postcode.'; | ||
} | ||
|
||
function technicalProblems() { | ||
return 'Sorry, we are experiencing technical problems.'; | ||
} | ||
|
||
module.exports = { | ||
emptyPostcodeMessage, | ||
invalidPostcodeMessage, | ||
invalidUrlMessage, | ||
mandatorySelectionMessage, | ||
outsideOfEnglandPostcodeMessage, | ||
technicalProblems, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,9 @@ | ||
const promClient = require('./promBundle').promClient; | ||
|
||
module.exports = { | ||
applicationStarts: new promClient.Counter({ name: 'app_starts', help: 'The number of times the application has been started' }), | ||
errorPageViews: new promClient.Counter({ name: 'error_page_views', help: 'The number of error page views' }), | ||
applicationStarts: new promClient.Counter({ help: 'The number of times the application has been started', name: 'app_starts' }), | ||
emptySearchLocationErrors: new promClient.Counter({ help: 'The number of empty search location errors', name: 'empty_search_location_errors' }), | ||
errorPageViews: new promClient.Counter({ help: 'The number of error page views', name: 'error_page_views' }), | ||
outOfEnglandLocationWarnings: new promClient.Counter({ help: 'The number of out of England location warnings', name: 'out_of_england_location_warnings' }), | ||
validationLocationErrors: new promClient.Counter({ help: 'The number of location validation errors', name: 'validation_location_errors' }), | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const constants = require('../constants'); | ||
|
||
function getLocationHeading(query) { | ||
if (query.type) { | ||
if ((query.type.localeCompare(constants.SERVICE_TYPES.professional, 'en', { sensitivity: 'base' }) === 0) | ||
&& ((query.origin === constants.SERVICE_CHOICES.symptoms) | ||
|| (query.origin === constants.SERVICE_CHOICES.under16) | ||
|| (query.origin === constants.SERVICE_CHOICES['16to25']) | ||
|| (query.origin === constants.SERVICE_CHOICES.over25))) { | ||
return 'Where would you like to see a sexual health professional?'; | ||
} | ||
if (query.type.localeCompare(constants.SERVICE_TYPES.kit, 'en', { sensitivity: 'base' }) === 0) { | ||
if (query.origin === constants.SERVICE_CHOICES['16to25']) { | ||
return 'Where would you like to collect your free test kit?'; | ||
} else if (query.origin === constants.SERVICE_CHOICES.over25) { | ||
return 'Where would you like to collect your test kit?'; | ||
} | ||
} | ||
} | ||
return undefined; | ||
} | ||
|
||
function mapServiceType(query) { | ||
if (query.type) { | ||
return query.type; | ||
} | ||
if (((query.age) && (query.age === constants.AGE.under16)) | ||
|| ((query.symptoms) && (query.symptoms === constants.SYMPTOMS.yes))) { | ||
return constants.SERVICE_TYPES.professional; | ||
} | ||
return undefined; | ||
} | ||
|
||
function mapServiceChoice(query) { | ||
if (query.origin) { | ||
return query.origin; | ||
} | ||
if ((query.age) && (query.age === constants.AGE.under16)) { | ||
return constants.SERVICE_CHOICES.under16; | ||
} | ||
if ((query.symptoms) && (query.symptoms === constants.SYMPTOMS.yes)) { | ||
return constants.SERVICE_CHOICES.symptoms; | ||
} | ||
return undefined; | ||
} | ||
|
||
module.exports = { | ||
getLocationHeading, | ||
mapServiceChoice, | ||
mapServiceType, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,18 @@ | ||
// eslint-disable-next-line no-unused-vars | ||
const queryMapper = require('../lib/utils/queryMapper'); | ||
|
||
module.exports = config => | ||
(req, res, next) => { | ||
res.locals.GOOGLE_ANALYTICS_TRACKING_ID = config.googleAnalyticsId; | ||
res.locals.WEBTRENDS_ANALYTICS_TRACKING_ID = config.webtrendsId; | ||
res.locals.HOTJAR_ANALYTICS_TRACKING_ID = config.hotjarId; | ||
res.locals.SITE_ROOT = req.app.locals.SITE_ROOT; | ||
res.locals.ASSETS_URL = req.app.locals.ASSETS_URL; | ||
|
||
res.locals.symptoms = req.query.symptoms; | ||
res.locals.age = req.query.age; | ||
|
||
res.locals.type = queryMapper.mapServiceType(req.query); | ||
res.locals.origin = queryMapper.mapServiceChoice(req.query); | ||
res.locals.location = req.query.location; | ||
res.locals.locationHeading = queryMapper.getLocationHeading(req.query); | ||
res.locals.correctLocationParams = res.locals.locationHeading; | ||
next(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
const emptySearchErrors = require('../lib/promCounters').emptySearchLocationErrors; | ||
const renderer = require('../middleware/renderer'); | ||
|
||
function validateLocation(req, res, next) { | ||
if (!(res.locals.location)) { | ||
emptySearchErrors.inc(1); | ||
renderer.emptyPostcode(req, res); | ||
} else { | ||
next(); | ||
} | ||
} | ||
|
||
module.exports = validateLocation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const renderer = require('../middleware/renderer'); | ||
const warningCounter = require('../lib/promCounters').outOfEnglandLocationWarnings; | ||
|
||
function outsideEngland(countries) { | ||
return !countries.includes('England'); | ||
} | ||
|
||
function notInEnglandHandler(req, res, next) { | ||
const location = res.locals.postcodeLocationDetails; | ||
|
||
if (location && outsideEngland(location.countries)) { | ||
warningCounter.inc(1); | ||
renderer.outsideOfEngland(req, res); | ||
} else { | ||
next(); | ||
} | ||
} | ||
|
||
module.exports = notInEnglandHandler; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
const PostcodesIOClient = require('postcodesio-client'); | ||
const errorCounter = require('../lib/promCounters').errorPageViews; | ||
const log = require('../lib/logger'); | ||
const validationCounter = require('../lib/promCounters').validationLocationErrors; | ||
|
||
// rewire (a framework for mocking) doesn't support const | ||
// eslint-disable-next-line no-var | ||
var PostcodesIO = new PostcodesIOClient(); | ||
// eslint-disable-next-line no-var | ||
var renderer = require('./renderer'); | ||
|
||
function toArray(countries) { | ||
return Array.isArray(countries) ? countries : [countries]; | ||
} | ||
|
||
function isOutcode(postcodeDetails) { | ||
return !postcodeDetails.incode; | ||
} | ||
|
||
function postcodeDetailsMapper(postcodeDetails) { | ||
return { | ||
countries: toArray(postcodeDetails.country), | ||
isOutcode: isOutcode(postcodeDetails), | ||
location: { | ||
lat: postcodeDetails.latitude, | ||
lon: postcodeDetails.longitude, | ||
}, | ||
}; | ||
} | ||
|
||
async function lookupPostcode(req, res, next) { | ||
const location = res.locals.location; | ||
|
||
log.debug({ location }, 'Postcode search text'); | ||
if (location) { | ||
try { | ||
const postcodeDetails = await PostcodesIO.lookup(location); | ||
log.debug({ postcodeIOResponse: { postcodeDetails } }, 'PostcodeIO postcode response'); | ||
if (postcodeDetails) { | ||
res.locals.postcodeLocationDetails = postcodeDetailsMapper(postcodeDetails); | ||
next(); | ||
} else { | ||
validationCounter.inc(1); | ||
renderer.invalidPostcode(req, res, location); | ||
} | ||
} catch (error) { | ||
log.debug({ location }, 'Error in postcode lookup'); | ||
errorCounter.inc(1); | ||
next(error); | ||
} | ||
} else { | ||
log.debug('No postcode'); | ||
next(); | ||
} | ||
} | ||
|
||
module.exports = lookupPostcode; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.