diff --git a/app/prototype_v4_1/controllers/authentication.js b/app/prototype_v4_1/controllers/authentication.js index 7aa43b8..1ec3f2c 100644 --- a/app/prototype_v4_1/controllers/authentication.js +++ b/app/prototype_v4_1/controllers/authentication.js @@ -1,15 +1,12 @@ -const version = 'v4_1' -const view = (template) => { - return `prototype_${version}/views/${template}` -} +const { path: prototypePath, view } = require('../lib/settings') exports.signIn_get = (req, res) => { delete req.session.data res.render(view('authentication/sign-in'), { actions: { - back: `/prototype_${version}`, - next: `/prototype_${version}/sign-in` + back: prototypePath, + next: `${prototypePath}/sign-in` } }) } @@ -21,20 +18,20 @@ exports.signIn_post = (req, res) => { res.render(view('authentication/sign-in'), { errors, actions: { - back: `/prototype_${version}`, - next: `/prototype_${version}/sign-in` + back: prototypePath, + next: `${prototypePath}/sign-in` } }) } else { - res.redirect(`/prototype_${version}/security-code`) + res.redirect(`${prototypePath}/security-code`) } } exports.securityCode_get = (req, res) => { res.render(view('authentication/security-code'), { actions: { - back: `/prototype_${version}/sign-in`, - next: `/prototype_${version}/security-code` + back: `${prototypePath}/sign-in`, + next: `${prototypePath}/security-code` } }) } @@ -46,21 +43,21 @@ exports.securityCode_post = (req, res) => { res.render(view('authentication/security-code'), { errors, actions: { - back: `/prototype_${version}/sign-in`, - next: `/prototype_${version}/security-code` + back: `${prototypePath}/sign-in`, + next: `${prototypePath}/security-code` } }) } else { - res.redirect(`/prototype_${version}/sign-in-agreement`) + res.redirect(`${prototypePath}/sign-in-agreement`) } } exports.signInAgreement_get = (req, res) => { res.render(view('authentication/sign-in-agreement'), { actions: { - back: `/prototype_${version}/security-code`, - accept: `/prototype_${version}/sign-in-agreement`, - decline: `/prototype_${version}/sign-in-agreement-declined` + back: `${prototypePath}/security-code`, + accept: `${prototypePath}/sign-in-agreement`, + decline: `${prototypePath}/sign-in-agreement-declined` } }) } @@ -72,20 +69,20 @@ exports.signInAgreement_post = (req, res) => { res.render(view('authentication/sign-in-agreement'), { errors, actions: { - back: `/prototype_${version}/security-code`, - accept: `/prototype_${version}/sign-in-agreement`, - decline: `/prototype_${version}/sign-in-agreement-declined` + back: `${prototypePath}/security-code`, + accept: `${prototypePath}/sign-in-agreement`, + decline: `${prototypePath}/sign-in-agreement-declined` } }) } else { - res.redirect(`/prototype_${version}/accept-terms`) + res.redirect(`${prototypePath}/accept-terms`) } } exports.signInAgreementDeclined_get = (req, res) => { res.render(view('authentication/sign-in-agreement-declined'), { actions: { - back: `/prototype_${version}` + back: prototypePath } }) } diff --git a/app/prototype_v4_1/controllers/content.js b/app/prototype_v4_1/controllers/content.js index a2f2067..2e0212d 100644 --- a/app/prototype_v4_1/controllers/content.js +++ b/app/prototype_v4_1/controllers/content.js @@ -1,8 +1,7 @@ const fs = require('fs') const path = require('path') const matter = require('gray-matter') - -const version = 'v4_1' +const { view } = require('../lib/settings') const contentDirectory = path.join(__dirname, '..', 'content') @@ -60,7 +59,7 @@ const renderContent = (fileName) => (req, res, next) => { return next(error) } - res.render(`prototype_${version}/views/content/show`, { + res.render(view('content/show'), { content: renderedMarkdown, contentData }) diff --git a/app/prototype_v4_1/controllers/error.js b/app/prototype_v4_1/controllers/error.js index 81f4bda..f8dd496 100644 --- a/app/prototype_v4_1/controllers/error.js +++ b/app/prototype_v4_1/controllers/error.js @@ -1,13 +1,13 @@ -const version = 'v4_1' +const { view } = require('../lib/settings') exports.pageNotFound = (req, res) => { - res.status(404).render(`prototype_${version}/views/errors/404`) + res.status(404).render(view('errors/404')) } exports.unexpectedError = (req, res) => { - res.status(500).render(`prototype_${version}/views/errors/500`) + res.status(500).render(view('errors/500')) } exports.serviceUnavailable = (req, res) => { - res.status(503).render(`prototype_${version}/views/errors/503`) + res.status(503).render(view('errors/503')) } diff --git a/app/prototype_v4_1/data/questions.yaml b/app/prototype_v4_1/data/questions.yaml index 97e33ee..40fcc0c 100644 --- a/app/prototype_v4_1/data/questions.yaml +++ b/app/prototype_v4_1/data/questions.yaml @@ -6,7 +6,7 @@ questions: heading: title: Terms of use description: | - To continue, confirm that you have read and agree to the [NHS Check if you need a lung scan terms of use](/prototype_v4_1/terms-of-use). + To continue, confirm that you have read and agree to the [NHS Check if you need a lung scan terms of use](terms-of-use). input: label: Confirm you agree to the terms of use options: diff --git a/app/prototype_v4_1/lib/page-index.js b/app/prototype_v4_1/lib/page-index.js new file mode 100644 index 0000000..c37a28c --- /dev/null +++ b/app/prototype_v4_1/lib/page-index.js @@ -0,0 +1,94 @@ +const defaultAnswers = { + acceptTerms: ['yes'], + phoneQuestionnaire: 'no', + smoker: 'yes_current', + dateOfBirth: { + day: '15', + month: '3', + year: '1964' + }, + faceToFaceAppointment: 'no', + height: { + metric: '170' + }, + weight: { + metric: '70' + }, + gender: 'female', + sex: 'female', + ethnicity: 'white', + education: 'further_education', + respiratoryConditions: ['no'], + asbestosAtWork: 'no', + asbestosAtHome: 'no', + cancerDiagnosis: 'no', + cancerDiagnosisRelatives: 'yes', + cancerDiagnosisRelativesAge: 'no', + ageStartedSmoking: '18', + periodsStoppedSmoking: 'no', + smokingType: ['cigarettes'], + cigarettes: { + smokingStatus: 'yes', + smokingFrequency: 'daily', + smokingQuantity: '10', + smokingChange: ['greater'], + smokingChangeIncrease: { + frequency: 'weekly', + quantity: '5', + years: '10' + } + } +} + +const cloneAnswers = () => JSON.parse(JSON.stringify(defaultAnswers)) + +const getDefaultAnswerProfile = (profile) => { + const answers = cloneAnswers() + + if (profile === 'former') { + answers.smoker = 'yes_previous' + answers.ageStoppedSmoking = '55' + answers.cigarettes = { + smokingFrequency: 'daily', + smokingQuantity: '10', + smokingChange: ['greater'], + smokingChangeIncrease: { + frequency: 'weekly', + quantity: '5', + years: '10' + } + } + } + + if (profile === 'shisha') { + answers.smokingType = ['shisha'] + delete answers.cigarettes + answers.shisha = { + smokingStatus: 'yes', + smokingSetting: ['group'], + group: { + smokingFrequency: 'weekly', + smokingQuantity: '30_minutes_to_1_hour' + } + } + } + + return answers +} + +const getIndexRedirect = (returnUrl, prototypePath) => { + if (!returnUrl || typeof returnUrl !== 'string') { + return `${prototypePath}/check-your-answers` + } + + if (!returnUrl.startsWith(`${prototypePath}/`) || returnUrl.includes('//')) { + return `${prototypePath}/page-index` + } + + return returnUrl +} + +module.exports = { + getDefaultAnswerProfile, + getIndexRedirect +} diff --git a/app/prototype_v4_1/lib/question-renderer.js b/app/prototype_v4_1/lib/question-renderer.js index 7098012..6f079a7 100644 --- a/app/prototype_v4_1/lib/question-renderer.js +++ b/app/prototype_v4_1/lib/question-renderer.js @@ -1,16 +1,7 @@ const { getQuestion } = require('./questions') +const settings = require('./settings') -const version = 'v4_1' - -/** - * Build a prototype v4_1 view path. - * - * @param {string} template - Template path below the prototype views folder. - * @returns {string} Nunjucks template path. - */ -const view = (template) => { - return `prototype_${version}/views/${template}` -} +const { version, view } = settings /** * Render a YAML-backed question using optional runtime overrides. diff --git a/app/prototype_v4_1/lib/settings.js b/app/prototype_v4_1/lib/settings.js new file mode 100644 index 0000000..6648aa7 --- /dev/null +++ b/app/prototype_v4_1/lib/settings.js @@ -0,0 +1,24 @@ +const version = 'v4_1' +const name = `prototype_${version}` +const path = `/${name}` +const viewPath = `${name}/views` + +const view = (template) => { + return `${viewPath}/${template}` +} + +const locals = { + version, + name, + path, + viewPath +} + +module.exports = { + locals, + name, + path, + version, + view, + viewPath +} diff --git a/app/prototype_v4_1/routes.js b/app/prototype_v4_1/routes.js index fc35e74..7a74075 100644 --- a/app/prototype_v4_1/routes.js +++ b/app/prototype_v4_1/routes.js @@ -2,14 +2,11 @@ const express = require('express') const fs = require('fs') const path = require('path') const router = express.Router() +const settings = require('./lib/settings') -const version = 'v4_1' +const { path: prototypePath, version, view } = settings const viewsDirectory = path.join(__dirname, 'views') -const view = (template) => { - return `prototype_${version}/views/${template}` -} - const hasView = (template) => { if (!template || template.includes('..')) { return false @@ -26,20 +23,26 @@ const hasView = (template) => { const authenticationController = require('./controllers/authentication') const contentController = require('./controllers/content') const errorController = require('./controllers/error') +const { getDefaultAnswerProfile, getIndexRedirect } = require('./lib/page-index') const questionController = require('./controllers/question') +router.use((req, res, next) => { + res.locals.prototype = settings.locals + next() +}) + /// ------------------------------------------------------------------------ /// /// Start page /// ------------------------------------------------------------------------ /// -router.get(`/prototype_${version}`, (req, res) => { - res.redirect(`/prototype_${version}/start-page`) +router.get(prototypePath, (req, res) => { + res.redirect(`${prototypePath}/start-page`) }) -router.get(`/prototype_${version}/start-page`, (req, res) => { - res.render(view('index'), { +router.get(`${prototypePath}/start-page`, (req, res) => { + res.render(view('start'), { actions: { - start: `/prototype_${version}/sign-in` + start: `${prototypePath}/sign-in` } }) }) @@ -218,6 +221,33 @@ router.get(`/prototype_${version}/server-error`, errorController.unexpectedError router.get(`/prototype_${version}/503`, errorController.serviceUnavailable) router.get(`/prototype_${version}/service-unavailable`, errorController.serviceUnavailable) +/// ------------------------------------------------------------------------ /// +/// Page index +/// ------------------------------------------------------------------------ /// + +router.get(`/prototype_${version}/set-default-answers`, (req, res) => { + req.session.data = req.session.data || {} + req.session.data.answers = getDefaultAnswerProfile(req.query.profile) + + res.redirect(getIndexRedirect(req.query.returnUrl, prototypePath)) +}) + +router.get(`/prototype_${version}/page-index`, (req, res) => { + res.render(view('index'), { + actions: { + setDefaultAnswers: `/prototype_${version}/set-default-answers` + } + }) +}) + +router.get(`/prototype_${version}/index`, (req, res) => { + res.redirect(`/prototype_${version}/page-index`) +}) + +router.get(`/prototype_${version}/index-allpages`, (req, res) => { + res.redirect(`/prototype_${version}/page-index`) +}) + /// ------------------------------------------------------------------------ /// /// Add your routes above /// ------------------------------------------------------------------------ /// diff --git a/app/prototype_v4_1/views/authentication/security-code.html b/app/prototype_v4_1/views/authentication/security-code.html index 5f77bc5..bc6062e 100644 --- a/app/prototype_v4_1/views/authentication/security-code.html +++ b/app/prototype_v4_1/views/authentication/security-code.html @@ -1,14 +1,14 @@ -{% extends "prototype_v4/views/layouts/main.html" %} +{% extends prototype.viewPath + "/layouts/main.html" %} {% set title = "Enter the security code" %} {% block content %} - {% include "prototype_v4/views/includes/error-summary.html" %} + {% include prototype.viewPath + "/includes/error-summary.html" %}
We have sent a 6-digit security code to your phone number ending in 0887. diff --git a/app/prototype_v4_1/views/authentication/sign-in-agreement-declined.html b/app/prototype_v4_1/views/authentication/sign-in-agreement-declined.html index aacf829..2ab24b7 100644 --- a/app/prototype_v4_1/views/authentication/sign-in-agreement-declined.html +++ b/app/prototype_v4_1/views/authentication/sign-in-agreement-declined.html @@ -1,4 +1,4 @@ -{% extends "prototype_v4/views/layouts/main.html" %} +{% extends prototype.viewPath + "/layouts/main.html" %} {% set title = "Sorry, you cannot test the online service" %} @@ -18,7 +18,7 @@