diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..260dbe8 --- /dev/null +++ b/.gitattributes @@ -0,0 +1,105 @@ +# Common settings that generally should always be used with your language specific settings + +# Auto detect text files and perform LF normalization +* text=auto + +# +# The above will handle all files NOT found below +# + +# Documents +*.bibtex text diff=bibtex +*.doc diff=astextplain +*.DOC diff=astextplain +*.docx diff=astextplain +*.DOCX diff=astextplain +*.dot diff=astextplain +*.DOT diff=astextplain +*.pdf diff=astextplain +*.PDF diff=astextplain +*.rtf diff=astextplain +*.RTF diff=astextplain +*.md text diff=markdown +*.mdx text diff=markdown +*.tex text diff=tex +*.adoc text +*.textile text +*.mustache text +*.csv text eol=crlf +*.tab text +*.tsv text +*.txt text +*.sql text +*.epub diff=astextplain + +# Graphics +*.png binary +*.jpg binary +*.jpeg binary +*.gif binary +*.tif binary +*.tiff binary +*.ico binary +# SVG treated as text by default. +*.svg text +# If you want to treat it as binary, +# use the following line instead. +# *.svg binary +*.eps binary + +# Scripts +*.bash text eol=lf +*.fish text eol=lf +*.ksh text eol=lf +*.sh text eol=lf +*.zsh text eol=lf +# These are explicitly windows files and should use crlf +*.bat text eol=crlf +*.cmd text eol=crlf +*.ps1 text eol=crlf + +# Serialisation +*.json text +*.toml text +*.xml text +*.yaml text +*.yml text + +# Archives +*.7z binary +*.bz binary +*.bz2 binary +*.bzip2 binary +*.gz binary +*.lz binary +*.lzma binary +*.rar binary +*.tar binary +*.taz binary +*.tbz binary +*.tbz2 binary +*.tgz binary +*.tlz binary +*.txz binary +*.xz binary +*.Z binary +*.zip binary +*.zst binary + +# Fonts +*.eot binary +*.otf binary +*.ttf binary +*.woff binary +*.woff2 binary + +# Text files where line endings should be preserved +*.patch -text + +# +# Exclude files from exporting +# + +.gitattributes export-ignore +.gitignore export-ignore +.gitkeep export-ignore diff --git a/app/routes.js b/app/routes.js index 3ba1335..a0a1ded 100644 --- a/app/routes.js +++ b/app/routes.js @@ -1,2299 +1,12 @@ // External dependencies -const express = require('express'); +const express = require('express') -const router = express.Router(); +const router = express.Router() -// Add your routes here - above the module.exports line - -module.exports = router; - -router.post('/prototype_v1/relatives-with-cancer-answer', function(request, response) { -var relativesHaveCancer = request.session.data['relativesHaveCancer'] -var smokedRegularly = request.session.data['smokedRegularly'] - -if (relativesHaveCancer == "Yes"){ - // If relatives had cancer, ask about their age first - response.redirect("/prototype_v1/relatives-age-when-diagnosed") -} else if (relativesHaveCancer == "No"){ - // No relatives with cancer, route based on smoking status - if (smokedRegularly == "Yes-currently") { - response.redirect("/prototype_v1/how-old-when-started-smoking") -} else if (smokedRegularly == "Yes-usedToRegularly") { - response.redirect("/prototype_v1/how-old-when-started-smoking") - } else { - // Fallback to original logic if smoking status unclear - response.redirect("/prototype_v1/do-you-smoke-now") - } -} else { - response.redirect("/prototype_v1/relatives-with-cancer") -} -}) - - -router.post('/prototype_v1/relatives-age-answer', function(request, response) { -var smokedRegularly = request.session.data['smokedRegularly'] - -if (smokedRegularly == "Yes-currently") { - response.redirect("/prototype_v1/how-old-when-started-smoking") -} else if (smokedRegularly == "Yes-usedToRegularly") { - response.redirect("/prototype_v1/how-old-when-started-smoking") -} else { - // Fallback - response.redirect("/prototype_v1/do-you-smoke-now") -} -}) - -router.get('/prototype_v1/start-journey', function (request, response) { - delete request.session.data - response.redirect("/prototype_v1/eligibility-have-you-ever-smoked") -}) - -router.post('/prototype_v1/smokedRegularlyAnswer', function(request, response) { - var smokedRegularly = request.session.data['smokedRegularly'] - if (smokedRegularly == "Yes-currently"){ - response.redirect("/prototype_v1/eligibility-what-is-your-date-of-birth") - } else if (smokedRegularly == "Yes-usedToRegularly"){ - response.redirect("/prototype_v1/eligibility-what-is-your-date-of-birth") - } else if (smokedRegularly == "Yes-usedToFewTimes"){ - response.redirect("/prototype_v1/drop-out-never-smoked") - } else if (smokedRegularly == "No"){ - response.redirect("/prototype_v1/drop-out-never-smoked") - } - else { - response.redirect("/prototype_v1/eligibility-have-you-ever-smoked") - } -}) - -router.post('/prototype_v1/smokeNowAnswer', function(request, response) { - var smokeNow = request.session.data['smokeNow'] - if (smokeNow == "Yes"){ - response.redirect("/prototype_v1/current-smoker-how-many-years") - } else if (smokeNow == "No"){ - response.redirect("/prototype_v1/former-smoker-when-quit-smoking") - } else { - response.redirect("/prototype_v1/do-you-smoke-now") - } -}) - -router.post('/prototype_v1/who-should-not-use-answer', function(request, response) { - var smokeNow = request.session.data['canYouContinue'] - if (smokeNow == "Yes"){ - response.redirect("/prototype_v1/what-is-your-height") - } else if (smokeNow == "No"){ - response.redirect("/prototype_v1/drop-out-bmi") - } else { - response.redirect("/prototype_v1/who-should-not-use-this-online-service") - } -}) - -router.post('/prototype_v1/whatDoYouSmokeAnswer', function(request, response) { - var selectedTobacco = request.session.data['whatSmokeNow']; - - // Ensure it's an array (single selection becomes array) - if (!Array.isArray(selectedTobacco)) { - selectedTobacco = selectedTobacco ? [selectedTobacco] : []; - } - - // Define page mapping in order (matches checkbox order) - const tobaccoPages = [ - { value: 'Cigarettes', page: 'tobacco-how-many-cigarettes-per-day' }, - { value: 'Rolled cigarettes', page: 'tobacco-rolled-cigarettes-how-many-grams' }, - { value: 'Pipe', page: 'tobacco-pipe-how-many-bowls' }, - { value: 'Cigars', page: 'tobacco-what-size-cigars-do-you-smoke' }, - { value: 'Hookah', page: 'tobacco-hookah-and-shisha' } - ]; - - // Create queue of pages to visit - var pagesToVisit = []; - tobaccoPages.forEach(function(tobacco) { - if (selectedTobacco.includes(tobacco.value)) { - pagesToVisit.push(tobacco.page); - } - }); - - // Store the queue in session - request.session.data['tobaccoPageQueue'] = pagesToVisit; - request.session.data['currentTobaccoPageIndex'] = 0; - - // Redirect to first page or check answers if no pages - if (pagesToVisit.length > 0) { - response.redirect('/prototype_v1/' + pagesToVisit[0]); - } else { - response.redirect('/prototype_v1/check-your-answers'); - } -}) - -router.post('/prototype_v1/dateOfBirthAnswer', function(request, response) { - const day = request.session.data['dateOfBirth']['day'] - const month = request.session.data['dateOfBirth']['month'] - const year = request.session.data['dateOfBirth']['year'] - - // Check if all fields are provided - if (!day || !month || !year) { - return response.redirect("/prototype_v1/eligibility-what-is-your-date-of-birth") - } - - // Calculate age - const birthDate = new Date(year, month - 1, day) // month is 0-indexed - const today = new Date() - let age = today.getFullYear() - birthDate.getFullYear() - const monthDiff = today.getMonth() - birthDate.getMonth() - - if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) { - age-- - } - - // Check age eligibility (55-74 for lung health checks) - if (age < 55 || age > 74) { - return response.redirect("/prototype_v1/drop-out-age") - } - - // If eligible, show the smoking question page - response.render('prototype_v1/task-list') -}) - -router.post('/prototype_v1/whatDidYouSmokeAnswer', function(request, response) { - var selectedTobacco = request.session.data['whatDidSmoke']; - - // Ensure it's an array (single selection becomes array) - if (!Array.isArray(selectedTobacco)) { - selectedTobacco = selectedTobacco ? [selectedTobacco] : []; - } - - // Define page mapping for former smokers (same pages, different route name) - const tobaccoPages = [ - { value: 'Cigarettes', page: 'tobacco-how-many-cigarettes-per-day' }, - { value: 'Rolled cigarettes', page: 'tobacco-rolled-cigarettes-how-many-grams' }, - { value: 'Pipe', page: 'tobacco-pipe-how-many-bowls' }, - { value: 'Cigars', page: 'tobacco-what-size-cigars-do-you-smoke' }, - { value: 'Hookah', page: 'tobacco-hookah-and-shisha' } - ]; - - // Create queue of pages to visit - var pagesToVisit = []; - tobaccoPages.forEach(function(tobacco) { - if (selectedTobacco.includes(tobacco.value)) { - pagesToVisit.push(tobacco.page); - } - }); - - // Store the queue in session - request.session.data['tobaccoPageQueue'] = pagesToVisit; - request.session.data['currentTobaccoPageIndex'] = 0; - - // Redirect to first page or check answers if no pages - if (pagesToVisit.length > 0) { - response.redirect('/prototype_v1/' + pagesToVisit[0]); - } else { - response.redirect('/prototype_v1/check-your-answers'); - } -}) - -router.post('/prototype_v1/tobacco-next', function(request, response) { - var queue = request.session.data['tobaccoPageQueue'] || []; - var currentIndex = request.session.data['currentTobaccoPageIndex'] || 0; - - // Move to next page - currentIndex++; - request.session.data['currentTobaccoPageIndex'] = currentIndex; - - // Check if more pages in queue - if (currentIndex < queue.length) { - response.redirect('/prototype_v1/' + queue[currentIndex]); - } else { - // All tobacco pages completed, go to check answers - response.redirect('/prototype_v1/check-your-answers'); - } -}) -router.post('/prototype_v1/ageStartedSmokingAnswer', function(request, response) { - var smokedRegularly = request.session.data['smokedRegularly'] - - if (smokedRegularly == "Yes-currently") { - response.redirect("/prototype_v1/have-you-ever-stopped-smoking") - } else if (smokedRegularly == "Yes-usedToRegularly") { - response.redirect("/prototype_v1/former-smoker-when-quit-smoking") - } else { - // Fallback - response.redirect("/prototype_v1/how-old-when-started-smoking") - } -}) - - -router.post('/prototype_v1/tobaccoHookahSessionAnswer', function(request, response) { - var hookahSession = request.session.data['hookahSession'] - - // If hookahSession is a string (single selection), convert to array - if (typeof hookahSession === 'string') { - hookahSession = [hookahSession] - } - - // Check what was selected and route accordingly - if (hookahSession && hookahSession.includes('In a group session') && hookahSession.includes('By myself')) { - // Both selected - go to group first, then individual - response.redirect("/prototype_v1/tobacco-hookah-how-much-group") - } else if (hookahSession && hookahSession.includes('In a group session')) { - // Only group selected - response.redirect("/prototype_v1/tobacco-hookah-how-much-group") - } else if (hookahSession && hookahSession.includes('By myself')) { - // Only individual selected - response.redirect("/prototype_v1/tobacco-how-much-by-yourself") - } else { - // Nothing selected or error - continue to next tobacco type - response.redirect("/prototype_v1/tobacco-next") - } -}) - - -router.post('/prototype_v1/tobaccoHookahGroupToIndividual', function(request, response) { - var hookahSession = request.session.data['hookahSession'] - - // Convert to array if needed - if (typeof hookahSession === 'string') { - hookahSession = [hookahSession] - } - - // If both were selected and user just completed group, go to individual - if (hookahSession && hookahSession.includes('By myself')) { - response.redirect("/prototype_v1/tobacco-how-much-by-yourself") - } else { - // Otherwise continue to next tobacco type - response.redirect("/prototype_v1/tobacco-next") - } -}) - -///////////////// -// Prototype 2 // -///////////////// -///////////////// -///////////////// - - -router.post('/prototype_v2/relatives-with-cancer-answer', function(request, response) { -var relativesHaveCancer = request.session.data['relativesHaveCancer'] - -if (relativesHaveCancer == "Yes"){ - // If relatives had cancer, ask about their age first - response.redirect("/prototype_v2/relatives-age-when-diagnosed") -} else if (relativesHaveCancer == "No" || relativesHaveCancer == "I don't know"){ - // No relatives with cancer OR don't know, go straight to age started smoking - response.redirect("/prototype_v2/how-old-when-started-smoking") -} else { - // No answer provided, redirect back to the question - response.redirect("/prototype_v2/relatives-with-cancer") -} -}) - -router.post('/prototype_v2/relatives-age-answer', function(request, response) { - response.redirect("/prototype_v2/how-old-when-started-smoking") -}) - -router.get('/prototype_v2/start-journey', function (request, response) { - delete request.session.data - response.redirect("/prototype_v2/have-you-completed-by-phone") -}) - -router.post('/prototype_v2/have-you-completed-by-phone-answer', function(request, response) { - const completedByPhone = request.session.data['completedByPhone'] - - if (completedByPhone === 'Yes') { - // If they've already completed by phone, redirect to exit page - response.redirect('/prototype_v2/completed-by-phone-exit') - } else if (completedByPhone === 'No') { - // If they haven't completed by phone, continue to eligibility - response.redirect('/prototype_v2/eligibility-have-you-ever-smoked') - } else { - // If no answer provided, redirect back to the question - response.redirect('/prototype_v2/have-you-completed-by-phone') - } -}) - -router.post('/prototype_v2/smokedRegularlyAnswer', function(request, response) { - var smokedRegularly = request.session.data['smokedRegularly'] - if (smokedRegularly == "Yes-currently"){ - response.redirect("/prototype_v2/eligibility-what-is-your-date-of-birth") - } else if (smokedRegularly == "Yes-usedToRegularly"){ - response.redirect("/prototype_v2/eligibility-what-is-your-date-of-birth") - } else if (smokedRegularly == "Yes-usedToFewTimes"){ - response.redirect("/prototype_v2/drop-out-never-smoked") - } else if (smokedRegularly == "No"){ - response.redirect("/prototype_v2/drop-out-never-smoked") - } - else { - response.redirect("/prototype_v2/eligibility-have-you-ever-smoked") - } -}) - -router.post('/prototype_v2/smokeNowAnswer', function(request, response) { - var smokeNow = request.session.data['smokeNow'] - if (smokeNow == "Yes"){ - response.redirect("/prototype_v2/current-smoker-how-many-years") - } else if (smokeNow == "No"){ - response.redirect("/prototype_v2/former-smoker-when-quit-smoking") - } else { - response.redirect("/prototype_v2/do-you-smoke-now") - } -}) - -router.post('/prototype_v2/who-should-not-use-answer', function(request, response) { - var smokeNow = request.session.data['canYouContinue'] - if (smokeNow == "Yes"){ - response.redirect("/prototype_v2/drop-out-bmi") - } else if (smokeNow == "No"){ - response.redirect("/prototype_v2/what-is-your-height") - } else { - response.redirect("/prototype_v2/who-should-not-use-this-online-service") - } -}) - -router.post('/prototype_v2/whatDoYouSmokeAnswer', function(request, response) { - var selectedTobacco = request.session.data['whatSmokeNow']; - - // Ensure it's an array (single selection becomes array) - if (!Array.isArray(selectedTobacco)) { - selectedTobacco = selectedTobacco ? [selectedTobacco] : []; - } - - // Define page mapping in order (matches checkbox order) - const tobaccoPages = [ - { value: 'Cigarettes', page: 'tobacco-how-many-cigarettes-per-day' }, - { value: 'Rolled cigarettes', page: 'tobacco-rolled-cigarettes-how-many-grams' }, - { value: 'Pipe', page: 'tobacco-pipe-how-many-bowls' }, - { value: 'Cigars', page: 'tobacco-what-size-cigars-do-you-smoke' }, - { value: 'Hookah', page: 'tobacco-hookah-and-shisha' } - ]; - - // Create queue of pages to visit - var pagesToVisit = []; - tobaccoPages.forEach(function(tobacco) { - if (selectedTobacco.includes(tobacco.value)) { - pagesToVisit.push(tobacco.page); - } - }); - - // Store the queue in session - request.session.data['tobaccoPageQueue'] = pagesToVisit; - request.session.data['currentTobaccoPageIndex'] = 0; - - // Redirect to first page or check answers if no pages - if (pagesToVisit.length > 0) { - response.redirect('/prototype_v2/' + pagesToVisit[0]); - } else { - response.redirect('/prototype_v2/check-your-answers'); - } -}) - -router.post('/prototype_v2/dateOfBirthAnswer', function(request, response) { - const day = request.session.data['dateOfBirth']['day'] - const month = request.session.data['dateOfBirth']['month'] - const year = request.session.data['dateOfBirth']['year'] - - // Check if all fields are provided - if (!day || !month || !year) { - return response.redirect("/prototype_v2/eligibility-what-is-your-date-of-birth") - } - - // Calculate age - const birthDate = new Date(year, month - 1, day) // month is 0-indexed - const today = new Date() - let age = today.getFullYear() - birthDate.getFullYear() - const monthDiff = today.getMonth() - birthDate.getMonth() - - if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) { - age-- - } - - // Check age eligibility (55-74 for lung health checks) - if (age < 55 || age > 74) { - return response.redirect("/prototype_v2/drop-out-age") - } - - // If eligible, show the smoking question page - response.render('prototype_v2/check-if-you-need-face-to-face-appointment') -}) - -router.post('/prototype_v2/whatDidYouSmokeAnswer', function(request, response) { - var selectedTobacco = request.session.data['whatDidSmoke']; - - // Ensure it's an array (single selection becomes array) - if (!Array.isArray(selectedTobacco)) { - selectedTobacco = selectedTobacco ? [selectedTobacco] : []; - } - - // Define page mapping for former smokers (same pages, different route name) - const tobaccoPages = [ - { value: 'Cigarettes', page: 'tobacco-how-many-cigarettes-per-day' }, - { value: 'Rolled cigarettes', page: 'tobacco-rolled-cigarettes-how-many-grams' }, - { value: 'Pipe', page: 'tobacco-pipe-how-many-bowls' }, - { value: 'Cigars', page: 'tobacco-what-size-cigars-do-you-smoke' }, - { value: 'Hookah', page: 'tobacco-hookah-and-shisha' } - ]; - - // Create queue of pages to visit - var pagesToVisit = []; - tobaccoPages.forEach(function(tobacco) { - if (selectedTobacco.includes(tobacco.value)) { - pagesToVisit.push(tobacco.page); - } - }); - - // Store the queue in session - request.session.data['tobaccoPageQueue'] = pagesToVisit; - request.session.data['currentTobaccoPageIndex'] = 0; - - // Redirect to first page or check answers if no pages - if (pagesToVisit.length > 0) { - response.redirect('/prototype_v2/' + pagesToVisit[0]); - } else { - response.redirect('/prototype_v2/check-your-answers'); - } -}) - -router.post('/prototype_v2/tobacco-next', function(request, response) { - var queue = request.session.data['tobaccoPageQueue'] || []; - var currentIndex = request.session.data['currentTobaccoPageIndex'] || 0; - - // Move to next page - currentIndex++; - request.session.data['currentTobaccoPageIndex'] = currentIndex; - - // Check if more pages in queue - if (currentIndex < queue.length) { - response.redirect('/prototype_v2/' + queue[currentIndex]); - } else { - // All tobacco pages completed, go to check answers - response.redirect('/prototype_v2/check-your-answers'); - } -}) - -router.post('/prototype_v2/ageStartedSmokingAnswer', function(request, response) { - var smokedRegularly = request.session.data['smokedRegularly'] - - if (smokedRegularly == "Yes-currently") { - response.redirect("/prototype_v2/have-you-ever-stopped-smoking") - } else if (smokedRegularly == "Yes-usedToRegularly") { - response.redirect("/prototype_v2/former-smoker-when-quit-smoking") - } else { - // Fallback - response.redirect("/prototype_v2/how-old-when-started-smoking") - } -}) - -router.post('/prototype_v2/tobaccoHookahSessionAnswer', function(request, response) { - var hookahSession = request.session.data['hookahSession'] - - // If hookahSession is a string (single selection), convert to array - if (typeof hookahSession === 'string') { - hookahSession = [hookahSession] - } - - // Check what was selected and route accordingly - if (hookahSession && hookahSession.includes('In a group session') && hookahSession.includes('By myself')) { - // Both selected - go to group first, then individual - response.redirect("/prototype_v2/tobacco-hookah-how-much-group") - } else if (hookahSession && hookahSession.includes('In a group session')) { - // Only group selected - response.redirect("/prototype_v2/tobacco-hookah-how-much-group") - } else if (hookahSession && hookahSession.includes('By myself')) { - // Only individual selected - response.redirect("/prototype_v2/tobacco-how-much-by-yourself") - } else { - // Nothing selected or error - continue to next tobacco type - response.redirect("/prototype_v2/tobacco-next") - } -}) - -router.post('/prototype_v2/tobaccoHookahGroupToIndividual', function(request, response) { - var hookahSession = request.session.data['hookahSession'] - - // Convert to array if needed - if (typeof hookahSession === 'string') { - hookahSession = [hookahSession] - } - - // If both were selected and user just completed group, go to individual - if (hookahSession && hookahSession.includes('By myself')) { - response.redirect("/prototype_v2/tobacco-how-much-by-yourself") - } else { - // Otherwise continue to next tobacco type - response.redirect("/prototype_v2/tobacco-next") - } -}) - -///////////////// -// Prototype 3 // -///////////////// - -// ============================================ -// START AND LOGIN FLOW -// ============================================ - -router.get('/prototype_v3/start-journey', function (request, response) { - delete request.session.data - response.redirect("/prototype_v3/login") -}) - -router.post('/prototype_v3/login', function (request, response) { - request.session.data['logged-in'] = true - response.redirect('/prototype_v3/enter-the-security-code') -}) - -router.post('/prototype_v3/enter-the-security-code', function (_request, response) { - response.redirect('/prototype_v3/agree-to-share-login-info') -}) - -router.post('/prototype_v3/agree-to-share-login-info', function (_request, response) { - response.redirect('/prototype_v3/accept-terms') -}) - -// Show accept terms page (GET request clears any error state) -router.get('/prototype_v3/accept-terms', function (request, response) { - // Clear any previous error state - delete request.session.data['accept-terms-error'] - response.render('prototype_v3/accept-terms') -}) - -// Accept terms validation (POST) -router.post('/prototype_v3/accept-terms', function (request, response) { - const acceptTerms = request.session.data['accept-terms'] - - // Check if acceptTerms is an array containing 'yes' or if it's the string 'yes' - const isAccepted = (Array.isArray(acceptTerms) && acceptTerms.includes('yes')) || acceptTerms === 'yes' - - if (!isAccepted) { - // Checkbox not ticked - show error - request.session.data['accept-terms-error'] = true - response.redirect('/prototype_v3/accept-terms') - } else { - // Checkbox ticked - clear error and continue to next page - delete request.session.data['accept-terms-error'] - response.redirect('/prototype_v3/have-you-completed-by-phone') - } -}) - -// ============================================ -// INITIAL ELIGIBILITY CHECKS -// ============================================ - -router.post('/prototype_v3/have-you-completed-by-phone-answer', function(request, response) { - const completedByPhone = request.session.data['completedByPhone'] - - if (completedByPhone === 'Yes') { - response.redirect('/prototype_v3/completed-by-phone-exit') - } else if (completedByPhone === 'No') { - response.redirect('/prototype_v3/eligibility-have-you-ever-smoked') - } else { - response.redirect('/prototype_v3/have-you-completed-by-phone') - } -}) - -router.post('/prototype_v3/smokedRegularlyAnswer', function(request, response) { - var smokedRegularly = request.session.data['smokedRegularly'] - - // People who never smoked or smoked very little should be dropped out immediately - if (smokedRegularly === "Yes-usedToFewTimes"){ - return response.redirect("/prototype_v3/drop-out-never-smoked") - } - - if (smokedRegularly === "No"){ - return response.redirect("/prototype_v3/drop-out-never-smoked") - } - - // People who currently smoke or used to smoke need to check age eligibility - if (smokedRegularly === "Yes-currently"){ - return response.redirect("/prototype_v3/eligibility-what-is-your-date-of-birth") - } - - if (smokedRegularly === "Yes-usedToRegularly"){ - return response.redirect("/prototype_v3/eligibility-what-is-your-date-of-birth") - } - - // If no match, redirect back to the form - return response.redirect("/prototype_v3/eligibility-have-you-ever-smoked") -}) - -router.post('/prototype_v3/dateOfBirthAnswer', function(request, response) { - const day = request.session.data['dateOfBirth']['day'] - const month = request.session.data['dateOfBirth']['month'] - const year = request.session.data['dateOfBirth']['year'] - - if (!day || !month || !year) { - return response.redirect("/prototype_v3/eligibility-what-is-your-date-of-birth") - } - - const birthDate = new Date(year, month - 1, day) - const today = new Date() - let age = today.getFullYear() - birthDate.getFullYear() - const monthDiff = today.getMonth() - birthDate.getMonth() - - if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) { - age-- - } - - if (age < 55 || age > 74) { - return response.redirect("/prototype_v3/drop-out-age") - } - - response.render('prototype_v3/check-if-you-need-face-to-face-appointment') -}) - -router.post('/prototype_v3/who-should-not-use-answer', function(request, response) { - var canContinue = request.session.data['canYouContinue'] - - if (canContinue == "Yes"){ - response.redirect("/prototype_v3/drop-out-bmi") - } else if (canContinue == "No"){ - response.redirect("/prototype_v3/enter-your-height") - } else { - response.redirect("/prototype_v3/who-should-not-use-this-online-service") - } -}) - -// ============================================ -// HEIGHT AND WEIGHT VALIDATION -// ============================================ - -router.post('/prototype_v3/enter-your-height-answer', function(request, response) { - var heightUnit = request.session.data['heightUnit'] - var height = request.session.data['height'] - var errors = {} - - // Clear previous errors - delete request.session.data['heightErrors'] - - if (heightUnit == "imperial") { - var feet = height ? height.feet : '' - var inches = height ? height.inches : '' - - // Check if both fields are empty - if (!feet && !inches) { - errors.height = "Enter your height" - } else { - // Validate feet - if (feet) { - if (feet.includes('.') || feet.includes(',')) { - errors.feet = "Feet must be in whole numbers" - } else if (isNaN(feet) || feet < 4 || feet > 8) { - errors.feet = "Feet must be between 4 and 8" - } - } - - // Validate inches - if (inches) { - if (inches.includes('.') || inches.includes(',')) { - errors.inches = "Inches must be in whole numbers" - } else if (isNaN(inches) || inches < 0 || inches > 11) { - errors.inches = "Inches must be between 0 and 11" - } - } - - // If individual validations pass, check total range - if (!errors.feet && !errors.inches && feet && inches) { - var totalInches = (parseInt(feet) * 12) + parseInt(inches) - if (totalInches < 55 || totalInches > 96) { - errors.height = "Height must be between 4 feet 7 inches and 8 feet" - } - } - } - } else { - // Metric validation - var centimetres = height ? height.centimetres : '' - - if (!centimetres) { - errors.height = "Enter your height" - } else if (isNaN(centimetres) || centimetres < 139.7 || centimetres > 243.8) { - errors.height = "Height must be between 139.7cm and 243.8 cm" - } - } - - // If there are errors, store them and redirect back - if (Object.keys(errors).length > 0) { - request.session.data['heightErrors'] = errors - response.redirect("/prototype_v3/enter-your-height") - } else { - // No errors, continue to weight page - response.redirect("/prototype_v3/enter-your-weight") - } -}) - -router.post('/prototype_v3/enter-your-weight-answer', function(request, response) { - var weightUnit = request.session.data['weightUnit'] - var weight = request.session.data['weight'] - var errors = {} - - // Clear previous errors - delete request.session.data['weightErrors'] - - if (weightUnit == "imperial") { - var stone = weight ? weight.stone : '' - var pounds = weight ? weight.pounds : '' - - // Check if both fields are empty - if (!stone && !pounds) { - errors.weight = "Enter your weight" - } else { - // Validate stone - if (stone) { - if (stone.includes('.') || stone.includes(',')) { - errors.stone = "Stone must be in whole numbers" - } - } - - // Validate pounds - if (pounds) { - if (pounds.includes('.') || pounds.includes(',')) { - errors.pounds = "Pounds must be in whole numbers" - } else if (isNaN(pounds) || pounds < 0 || pounds > 13) { - errors.pounds = "Pounds must be between 0 and 13" - } - } - - // If individual validations pass, check total range - if (!errors.stone && !errors.pounds && stone && pounds) { - var totalPounds = (parseInt(stone) * 14) + parseInt(pounds) - if (totalPounds < 56 || totalPounds > 700) { - errors.weight = "Weight must be between 4 stone and 50 stone" - } - } - } - } else { - // Metric validation - var kilograms = weight ? weight.kilograms : '' - - if (!kilograms) { - errors.weight = "Enter your weight" - } else if (isNaN(kilograms) || kilograms < 25.4 || kilograms > 317.5) { - errors.weight = "Weight must be between 25.4kg and 317.5kg" - } - } - - // If there are errors, store them and redirect back - if (Object.keys(errors).length > 0) { - request.session.data['weightErrors'] = errors - response.redirect("/prototype_v3/enter-your-weight") - } else { - // No errors, continue to sex page - response.redirect("/prototype_v3/your-gender-identity") - } -}) - -// ============================================ -// MEDICAL HISTORY -// ============================================ -router.post('/prototype_v3/diagnosed-with-cancer-answer', function(request, response) { - response.redirect("/prototype_v3/relatives-with-cancer") -}) - -router.post('/prototype_v3/relatives-with-cancer-answer', function(request, response) { - var relativesHaveCancer = request.session.data['relativesHaveCancer'] - - if (relativesHaveCancer == "Yes"){ - response.redirect("/prototype_v3/relatives-age-when-diagnosed") - } else if (relativesHaveCancer == "No" || relativesHaveCancer == "I don't know"){ - response.redirect("/prototype_v3/how-old-when-started-smoking") - } else { - response.redirect("/prototype_v3/relatives-with-cancer") - } -}) - -router.post('/prototype_v3/relatives-age-answer', function(request, response) { - response.redirect("/prototype_v3/how-old-when-started-smoking") -}) - - - -// ============================================ -// ROUTING FOR PERIODS WHEN YOU STOPPED SMOKING -// ============================================ - -// GET route for "Periods when you stopped smoking" page (for index testing) -// Sets smokedRegularly from query parameter if provided -router.get('/prototype_v3/periods-when-you-stopped-smoking', function(request, response, next) { - if (request.query.smokedRegularly) { - request.session.data['smokedRegularly'] = request.query.smokedRegularly - } - next() -}) - -// Route handler for "How old when started smoking" page -// This sends current smokers to "periods when stopped" and former smokers to "when quit" -router.post('/prototype_v3/how-old-when-started-smoking-answer', function(request, response) { - var smokedRegularly = request.session.data['smokedRegularly'] - - if (smokedRegularly == "Yes-currently") { - response.redirect("/prototype_v3/periods-when-you-stopped-smoking") - } else if (smokedRegularly == "Yes-usedToRegularly") { - response.redirect("/prototype_v3/former-smoker-when-quit-smoking") - } else { - // Fallback - response.redirect("/prototype_v3/how-old-when-started-smoking") - } -}) - -// Route handler for "Former smoker when quit" page -// This sends former smokers to "periods when stopped" -router.post('/prototype_v3/former-smoker-when-quit-smoking-answer', function(request, response) { - response.redirect("/prototype_v3/periods-when-you-stopped-smoking") -}) - -// Route handler for "Periods when you stopped smoking" page -// This sends everyone to the tobacco selection page -router.post('/prototype_v3/periods-when-you-stopped-smoking-answer', function(request, response) { - response.redirect("/prototype_v3/what-do-or-did-smoke") -}) - - -// ============================================ -// HELPER FUNCTIONS -// ============================================ - -function moveToNextTobaccoType(request, response) { - var tobaccoQueue = request.session.data['tobaccoQueue'] || [] - var currentIndex = request.session.data['tobaccoQueueIndex'] || 0 - - currentIndex++ - request.session.data['tobaccoQueueIndex'] = currentIndex - - if (currentIndex < tobaccoQueue.length) { - response.redirect(tobaccoQueue[currentIndex]) - } else { - response.redirect('/prototype_v3/check-your-answers') - } -} - -// CIGAR SIZE SPLIT: moveToNextCigarSize is no longer used — cigar sizes now go through moveToNextTobaccoType. -// To revert: restore this function and replace moveToNextTobaccoType calls in the cigars section with it. -/* -function moveToNextCigarSize(request, response, smokerType) { - var sizeQueue = request.session.data['cigarSizeQueue'] || [] - var currentIndex = request.session.data['cigarSizeQueueIndex'] || 0 - var currentSize = request.session.data['currentCigarSize'] - - if (currentSize) { - delete request.session.data['cigar' + currentSize + 'Changes'] - } - - currentIndex++ - request.session.data['cigarSizeQueueIndex'] = currentIndex - - if (currentIndex < sizeQueue.length) { - request.session.data['currentCigarSize'] = sizeQueue[currentIndex] - response.redirect('/prototype_v3/tobacco/cigars/' + smokerType + '/quantity') - } else { - delete request.session.data['cigarSizeQueue'] - delete request.session.data['cigarSizeQueueIndex'] - delete request.session.data['currentCigarSize'] - moveToNextTobaccoType(request, response) - } -} -*/ - -// ============================================ -// TOBACCO TYPE SELECTION -// ============================================ - -// GET route for "What do or did you smoke" page (for index testing) -// Sets smokedRegularly from query parameter if provided -router.get('/prototype_v3/what-do-or-did-smoke', function(request, response, next) { - if (request.query.smokedRegularly) { - request.session.data['smokedRegularly'] = request.query.smokedRegularly - } - next() -}) - -router.post('/prototype_v3/what-do-or-did-smoke-answer', function(request, response) { - var selectedTobacco = request.session.data['tobaccoTypes'] - var smokedRegularly = request.session.data['smokedRegularly'] - - // Ensure it's an array - if (!Array.isArray(selectedTobacco)) { - selectedTobacco = selectedTobacco ? [selectedTobacco] : [] - } - - const tobaccoRoutes = { - 'Cigarettes': '/prototype_v3/tobacco/cigarettes', - 'Rolling tobacco': '/prototype_v3/tobacco/rolling-tobacco', - 'Pipe': '/prototype_v3/tobacco/pipe', - // CIGAR SIZE SPLIT - see MEMORY.md - 'Small cigars': '/prototype_v3/tobacco/cigars/Small', - 'Medium cigars': '/prototype_v3/tobacco/cigars/Medium', - 'Large cigars': '/prototype_v3/tobacco/cigars/Large', - 'Cigarillos': '/prototype_v3/tobacco/cigarillos', - 'Shisha': '/prototype_v3/tobacco/shisha' - } - - var tobaccoQueue = [] - var tobaccoOrder = ['Cigarettes', 'Rolling tobacco', 'Pipe', 'Small cigars', 'Medium cigars', 'Large cigars', 'Cigarillos', 'Shisha'] - - // Check if user selected multiple tobacco types - var multipleTypes = selectedTobacco.length > 1 - - tobaccoOrder.forEach(function(type) { - if (selectedTobacco.includes(type)) { - if (smokedRegularly === "Yes-currently") { - if (multipleTypes) { - // Multiple types - need to ask which ones they currently smoke - tobaccoQueue.push(tobaccoRoutes[type] + '/do-you-currently-smoke') - } else { - // Single type and they're a current smoker - if (type === 'Shisha') { - // Shisha doesn't have frequency and years-smoked is only for multiple types - // Go directly to group-or-alone - tobaccoQueue.push(tobaccoRoutes[type] + '/current/group-or-alone') - } else { - // Other types go straight to frequency (skip years-smoked) - tobaccoQueue.push(tobaccoRoutes[type] + '/current/frequency') - } - } - } else if (smokedRegularly === "Yes-usedToRegularly") { - if (multipleTypes) { - // Multiple types - go to years-smoked first - tobaccoQueue.push(tobaccoRoutes[type] + '/former/years-smoked') - } else { - // Single type and they're a former smoker - if (type === 'Shisha') { - // Shisha doesn't have frequency and years-smoked is only for multiple types - // Go directly to group-or-alone - tobaccoQueue.push(tobaccoRoutes[type] + '/former/group-or-alone') - } else { - // Other types go straight to frequency (skip years-smoked) - tobaccoQueue.push(tobaccoRoutes[type] + '/former/frequency') - } - } - } - } - }) - - request.session.data['tobaccoQueue'] = tobaccoQueue - request.session.data['tobaccoQueueIndex'] = 0 - - if (tobaccoQueue.length > 0) { - response.redirect(tobaccoQueue[0]) - } else { - response.redirect('/prototype_v3/check-your-answers') - } -}) - -// ============================================ -// "DO YOU CURRENTLY SMOKE" ROUTING - CIGARETTES -// ============================================ - -router.post('/prototype_v3/tobacco/cigarettes/do-you-currently-smoke-answer', function(request, response) { - var currentlySmokesCigarettes = request.session.data['currentlySmokesCigarettes'] - - if (currentlySmokesCigarettes === 'Yes') { - response.redirect('/prototype_v3/tobacco/cigarettes/current/years-smoked') - } else { - response.redirect('/prototype_v3/tobacco/cigarettes/former/years-smoked') - } -}) - -// ============================================ -// CIGARETTES ROUTING - CURRENT -// ============================================ - -router.post('/prototype_v3/tobacco/cigarettes/current/years-smoked-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarettes/current/frequency') -}) - -router.post('/prototype_v3/tobacco/cigarettes/current/frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarettes/current/quantity') -}) - -router.post('/prototype_v3/tobacco/cigarettes/current/quantity-answer', function(request, response) { - var quantity = request.session.data['cigarettesCurrentQuantity'] - - // Validate that quantity is at least 1 - if (!quantity || parseInt(quantity) < 1) { - request.session.data['cigarettesCurrentQuantity-error'] = true - return response.redirect('/prototype_v3/tobacco/cigarettes/current/quantity') - } - - // Clear any previous errors - delete request.session.data['cigarettesCurrentQuantity-error'] - - response.redirect('/prototype_v3/tobacco/cigarettes/current/has-quantity-changed') -}) - -router.post('/prototype_v3/tobacco/cigarettes/current/has-quantity-changed-answer', function(request, response) { - var changes = request.session.data['cigarettesCurrentChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('more')) { - response.redirect('/prototype_v3/tobacco/cigarettes/current/more-frequency') - } else if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/cigarettes/current/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// MORE flow -router.post('/prototype_v3/tobacco/cigarettes/current/more-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarettes/current/more-quantity') -}) - -router.post('/prototype_v3/tobacco/cigarettes/current/more-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarettes/current/more-duration') -}) - -router.post('/prototype_v3/tobacco/cigarettes/current/more-duration-answer', function(request, response) { - var changes = request.session.data['cigarettesCurrentChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - // Check if they also selected 'less' - if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/cigarettes/current/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// LESS flow -router.post('/prototype_v3/tobacco/cigarettes/current/less-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarettes/current/less-quantity') -}) - -router.post('/prototype_v3/tobacco/cigarettes/current/less-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarettes/current/less-duration') -}) - -router.post('/prototype_v3/tobacco/cigarettes/current/less-duration-answer', function(request, response) { - var changes = request.session.data['cigarettesCurrentChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - // Check if they also selected 'stopped' - if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - - -// ============================================ -// CIGARETTES ROUTING - FORMER -// ============================================ - -router.post('/prototype_v3/tobacco/cigarettes/former/years-smoked-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarettes/former/frequency') -}) - -router.post('/prototype_v3/tobacco/cigarettes/former/frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarettes/former/quantity') -}) - -router.post('/prototype_v3/tobacco/cigarettes/former/quantity-answer', function(request, response) { - var quantity = request.session.data['cigarettesFormerQuantity'] - - // Validate that quantity is at least 1 - if (!quantity || parseInt(quantity) < 1) { - request.session.data['cigarettesFormerQuantity-error'] = true - return response.redirect('/prototype_v3/tobacco/cigarettes/former/quantity') - } - - // Clear any previous errors - delete request.session.data['cigarettesFormerQuantity-error'] - - response.redirect('/prototype_v3/tobacco/cigarettes/former/has-quantity-changed') -}) - -router.post('/prototype_v3/tobacco/cigarettes/former/has-quantity-changed-answer', function(request, response) { - var changes = request.session.data['cigarettesFormerChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('more')) { - response.redirect('/prototype_v3/tobacco/cigarettes/former/more-frequency') - } else if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/cigarettes/former/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// MORE flow - FORMER -router.post('/prototype_v3/tobacco/cigarettes/former/more-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarettes/former/more-quantity') -}) - -router.post('/prototype_v3/tobacco/cigarettes/former/more-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarettes/former/more-duration') -}) - -router.post('/prototype_v3/tobacco/cigarettes/former/more-duration-answer', function(request, response) { - var changes = request.session.data['cigarettesFormerChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/cigarettes/former/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// LESS flow - FORMER -router.post('/prototype_v3/tobacco/cigarettes/former/less-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarettes/former/less-quantity') -}) - -router.post('/prototype_v3/tobacco/cigarettes/former/less-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarettes/former/less-duration') -}) - -router.post('/prototype_v3/tobacco/cigarettes/former/less-duration-answer', function(request, response) { - var changes = request.session.data['cigarettesFormerChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - - -// ============================================ -// "DO YOU CURRENTLY SMOKE" ROUTING - ROLLED CIGARETTES -// ============================================ - -router.post('/prototype_v3/tobacco/rolling-tobacco/do-you-currently-smoke-answer', function(request, response) { - var currentlySmokesRollingTobacco = request.session.data['currentlySmokesRollingTobacco'] - - if (currentlySmokesRollingTobacco === 'Yes') { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/current/years-smoked') - } else { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/former/years-smoked') - } -}) - -// ============================================ -// ROLLED CIGARETTES ROUTING - CURRENT -// ============================================ - -router.post('/prototype_v3/tobacco/rolling-tobacco/current/years-smoked-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/current/frequency') -}) - -router.post('/prototype_v3/tobacco/rolling-tobacco/current/frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/current/quantity') -}) - -router.post('/prototype_v3/tobacco/rolling-tobacco/current/quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/current/has-quantity-changed') -}) - -router.post('/prototype_v3/tobacco/rolling-tobacco/current/has-quantity-changed-answer', function(request, response) { - var changes = request.session.data['rollingTobaccoCurrentChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('more')) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/current/more-frequency') - } else if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/current/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// MORE flow -router.post('/prototype_v3/tobacco/rolling-tobacco/current/more-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/current/more-quantity') -}) - -router.post('/prototype_v3/tobacco/rolling-tobacco/current/more-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/current/more-duration') -}) - -router.post('/prototype_v3/tobacco/rolling-tobacco/current/more-duration-answer', function(request, response) { - var changes = request.session.data['rollingTobaccoCurrentChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - // Check if they also selected 'less' - if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/current/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// LESS flow -router.post('/prototype_v3/tobacco/rolling-tobacco/current/less-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/current/less-quantity') -}) - -router.post('/prototype_v3/tobacco/rolling-tobacco/current/less-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/current/less-duration') -}) - -router.post('/prototype_v3/tobacco/rolling-tobacco/current/less-duration-answer', function(request, response) { - var changes = request.session.data['rollingTobaccoCurrentChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - // Check if they also selected 'stopped' - if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - - -// ============================================ -// ROLLED CIGARETTES ROUTING - FORMER -// ============================================ - -router.post('/prototype_v3/tobacco/rolling-tobacco/former/years-smoked-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/former/frequency') -}) - -router.post('/prototype_v3/tobacco/rolling-tobacco/former/frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/former/quantity') -}) - -router.post('/prototype_v3/tobacco/rolling-tobacco/former/quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/former/has-quantity-changed') -}) - -router.post('/prototype_v3/tobacco/rolling-tobacco/former/has-quantity-changed-answer', function(request, response) { - var changes = request.session.data['rollingTobaccoFormerChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('more')) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/former/more-frequency') - } else if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/former/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// MORE flow - FORMER -router.post('/prototype_v3/tobacco/rolling-tobacco/former/more-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/former/more-quantity') -}) - -router.post('/prototype_v3/tobacco/rolling-tobacco/former/more-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/former/more-duration') -}) - -router.post('/prototype_v3/tobacco/rolling-tobacco/former/more-duration-answer', function(request, response) { - var changes = request.session.data['rollingTobaccoFormerChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/former/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// LESS flow - FORMER -router.post('/prototype_v3/tobacco/rolling-tobacco/former/less-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/former/less-quantity') -}) - -router.post('/prototype_v3/tobacco/rolling-tobacco/former/less-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/rolling-tobacco/former/less-duration') -}) - -router.post('/prototype_v3/tobacco/rolling-tobacco/former/less-duration-answer', function(request, response) { - var changes = request.session.data['rollingTobaccoFormerChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - - -// ============================================ -// "DO YOU CURRENTLY SMOKE" ROUTING - PIPE -// ============================================ - -router.post('/prototype_v3/tobacco/pipe/do-you-currently-smoke-answer', function(request, response) { - var currentlySmokesPipe = request.session.data['currentlySmokesPipe'] - - if (currentlySmokesPipe === 'Yes') { - response.redirect('/prototype_v3/tobacco/pipe/current/years-smoked') - } else { - response.redirect('/prototype_v3/tobacco/pipe/former/years-smoked') - } -}) - -// ============================================ -// PIPE ROUTING - CURRENT -// ============================================ - -router.post('/prototype_v3/tobacco/pipe/current/years-smoked-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/pipe/current/frequency') -}) - -router.post('/prototype_v3/tobacco/pipe/current/frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/pipe/current/quantity') -}) +router.use(require('./views/prototype_v1/routes')) +router.use(require('./views/prototype_v2/routes')) +router.use(require('./views/prototype_v3/routes')) -router.post('/prototype_v3/tobacco/pipe/current/quantity-answer', function(request, response) { - var quantity = request.session.data['pipeCurrentQuantity'] - - // Validate that quantity is at least 1 - if (!quantity || parseInt(quantity) < 1) { - request.session.data['pipeCurrentQuantity-error'] = true - return response.redirect('/prototype_v3/tobacco/pipe/current/quantity') - } - - // Clear any previous errors - delete request.session.data['pipeCurrentQuantity-error'] - - response.redirect('/prototype_v3/tobacco/pipe/current/has-quantity-changed') -}) - -router.post('/prototype_v3/tobacco/pipe/current/has-quantity-changed-answer', function(request, response) { - var changes = request.session.data['pipeCurrentChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('more')) { - response.redirect('/prototype_v3/tobacco/pipe/current/more-frequency') - } else if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/pipe/current/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// MORE flow -router.post('/prototype_v3/tobacco/pipe/current/more-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/pipe/current/more-quantity') -}) - -router.post('/prototype_v3/tobacco/pipe/current/more-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/pipe/current/more-duration') -}) - -router.post('/prototype_v3/tobacco/pipe/current/more-duration-answer', function(request, response) { - var changes = request.session.data['pipeCurrentChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - // Check if they also selected 'less' - if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/pipe/current/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// LESS flow -router.post('/prototype_v3/tobacco/pipe/current/less-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/pipe/current/less-quantity') -}) - -router.post('/prototype_v3/tobacco/pipe/current/less-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/pipe/current/less-duration') -}) - -router.post('/prototype_v3/tobacco/pipe/current/less-duration-answer', function(request, response) { - var changes = request.session.data['pipeCurrentChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - // Check if they also selected 'stopped' - if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - - -// ============================================ -// PIPE ROUTING - FORMER -// ============================================ - -router.post('/prototype_v3/tobacco/pipe/former/years-smoked-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/pipe/former/frequency') -}) - -router.post('/prototype_v3/tobacco/pipe/former/frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/pipe/former/quantity') -}) - -router.post('/prototype_v3/tobacco/pipe/former/quantity-answer', function(request, response) { - var quantity = request.session.data['pipeFormerQuantity'] - - // Validate that quantity is at least 1 - if (!quantity || parseInt(quantity) < 1) { - request.session.data['pipeFormerQuantity-error'] = true - return response.redirect('/prototype_v3/tobacco/pipe/former/quantity') - } - - // Clear any previous errors - delete request.session.data['pipeFormerQuantity-error'] - - response.redirect('/prototype_v3/tobacco/pipe/former/has-quantity-changed') -}) - -router.post('/prototype_v3/tobacco/pipe/former/has-quantity-changed-answer', function(request, response) { - var changes = request.session.data['pipeFormerChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('more')) { - response.redirect('/prototype_v3/tobacco/pipe/former/more-frequency') - } else if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/pipe/former/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// MORE flow - FORMER -router.post('/prototype_v3/tobacco/pipe/former/more-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/pipe/former/more-quantity') -}) - -router.post('/prototype_v3/tobacco/pipe/former/more-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/pipe/former/more-duration') -}) - -router.post('/prototype_v3/tobacco/pipe/former/more-duration-answer', function(request, response) { - var changes = request.session.data['pipeFormerChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/pipe/former/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// LESS flow - FORMER -router.post('/prototype_v3/tobacco/pipe/former/less-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/pipe/former/less-quantity') -}) - -router.post('/prototype_v3/tobacco/pipe/former/less-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/pipe/former/less-duration') -}) - -router.post('/prototype_v3/tobacco/pipe/former/less-duration-answer', function(request, response) { - var changes = request.session.data['pipeFormerChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - - -// ============================================ -// CIGAR SIZE ENTRY ROUTES - CIGAR SIZE SPLIT (see MEMORY.md) -// These set currentCigarSize in session before redirecting to the shared cigar pages. -// ============================================ - -var cigarSizes = ['Small', 'Medium', 'Large'] - -cigarSizes.forEach(function(size) { - // Multiple types, current smoker: do-you-currently-smoke - router.get('/prototype_v3/tobacco/cigars/' + size + '/do-you-currently-smoke', function(request, response) { - request.session.data['currentCigarSize'] = size - response.redirect('/prototype_v3/tobacco/cigars/do-you-currently-smoke') - }) - - // Single type, current smoker: go direct to frequency - router.get('/prototype_v3/tobacco/cigars/' + size + '/current/frequency', function(request, response) { - request.session.data['currentCigarSize'] = size - response.redirect('/prototype_v3/tobacco/cigars/current/frequency') - }) - - // Multiple types, former smoker: years-smoked - router.get('/prototype_v3/tobacco/cigars/' + size + '/former/years-smoked', function(request, response) { - request.session.data['currentCigarSize'] = size - response.redirect('/prototype_v3/tobacco/cigars/former/years-smoked') - }) - - // Single type, former smoker: go direct to frequency - router.get('/prototype_v3/tobacco/cigars/' + size + '/former/frequency', function(request, response) { - request.session.data['currentCigarSize'] = size - response.redirect('/prototype_v3/tobacco/cigars/former/frequency') - }) -}) - -// ============================================ -// "DO YOU CURRENTLY SMOKE" ROUTING - CIGARS -// ============================================ - -router.post('/prototype_v3/tobacco/cigars/do-you-currently-smoke-answer', function(request, response) { - var currentlySmokesCigars = request.session.data['currentlySmokesCigars'] - - if (currentlySmokesCigars === 'Yes') { - response.redirect('/prototype_v3/tobacco/cigars/current/years-smoked') - } else { - response.redirect('/prototype_v3/tobacco/cigars/former/years-smoked') - } -}) - -// ============================================ -// CIGARS ROUTING - CURRENT -// ============================================ - -router.post('/prototype_v3/tobacco/cigars/current/years-smoked-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigars/current/frequency') -}) - -router.post('/prototype_v3/tobacco/cigars/current/frequency-answer', function(request, response) { - // CIGAR SIZE SPLIT: copies frequency to size-specific variable, skips cigar-size page (see MEMORY.md) - var currentSize = request.session.data['currentCigarSize'] - request.session.data['cigar' + currentSize + 'Frequency'] = request.session.data['cigarsCurrentFrequency'] - response.redirect('/prototype_v3/tobacco/cigars/current/quantity') -}) - -// cigar-size-answer not used - CIGAR SIZE SPLIT (see MEMORY.md) -// router.post('/prototype_v3/tobacco/cigars/current/cigar-size-answer', ...) - -router.post('/prototype_v3/tobacco/cigars/current/quantity-answer', function(request, response) { - var currentSize = request.session.data['currentCigarSize'] - var fieldName = 'cigar' + currentSize + 'Quantity' - var quantity = request.session.data[fieldName] - - // Validate that quantity is at least 1 - if (!quantity || parseInt(quantity) < 1) { - request.session.data[fieldName + '-error'] = true - return response.redirect('/prototype_v3/tobacco/cigars/current/quantity') - } - - // Clear any previous errors - delete request.session.data[fieldName + '-error'] - - response.redirect('/prototype_v3/tobacco/cigars/current/has-quantity-changed') -}) - -router.post('/prototype_v3/tobacco/cigars/current/has-quantity-changed-answer', function(request, response) { - var currentSize = request.session.data['currentCigarSize'] - var changes = request.session.data['cigar' + currentSize + 'Changes'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('more')) { - response.redirect('/prototype_v3/tobacco/cigars/current/more-frequency') - } else if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/cigars/current/less-frequency') - } else { - moveToNextTobaccoType(request, response) - } -}) - -// MORE flow -router.post('/prototype_v3/tobacco/cigars/current/more-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigars/current/more-quantity') -}) - -router.post('/prototype_v3/tobacco/cigars/current/more-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigars/current/more-duration') -}) - -router.post('/prototype_v3/tobacco/cigars/current/more-duration-answer', function(request, response) { - var currentSize = request.session.data['currentCigarSize'] - var changes = request.session.data['cigar' + currentSize + 'Changes'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - // Check if they also selected 'less' - if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/cigars/current/less-frequency') - } else { - moveToNextTobaccoType(request, response) - } -}) - -// LESS flow -router.post('/prototype_v3/tobacco/cigars/current/less-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigars/current/less-quantity') -}) - -router.post('/prototype_v3/tobacco/cigars/current/less-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigars/current/less-duration') -}) - -router.post('/prototype_v3/tobacco/cigars/current/less-duration-answer', function(request, response) { - moveToNextTobaccoType(request, response) -}) - -// ============================================ -// CIGARS ROUTING - FORMER -// ============================================ - -router.post('/prototype_v3/tobacco/cigars/former/years-smoked-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigars/former/frequency') -}) - -router.post('/prototype_v3/tobacco/cigars/former/frequency-answer', function(request, response) { - // CIGAR SIZE SPLIT: copies frequency to size-specific variable, skips cigar-size page (see MEMORY.md) - var currentSize = request.session.data['currentCigarSize'] - request.session.data['cigar' + currentSize + 'Frequency'] = request.session.data['cigarsFormerFrequency'] - response.redirect('/prototype_v3/tobacco/cigars/former/quantity') -}) - -// cigar-size-answer not used - CIGAR SIZE SPLIT (see MEMORY.md) -// router.post('/prototype_v3/tobacco/cigars/former/cigar-size-answer', ...) - -router.post('/prototype_v3/tobacco/cigars/former/quantity-answer', function(request, response) { - var currentSize = request.session.data['currentCigarSize'] - var fieldName = 'cigar' + currentSize + 'Quantity' - var quantity = request.session.data[fieldName] - - // Validate that quantity is at least 1 - if (!quantity || parseInt(quantity) < 1) { - request.session.data[fieldName + '-error'] = true - return response.redirect('/prototype_v3/tobacco/cigars/former/quantity') - } - - // Clear any previous errors - delete request.session.data[fieldName + '-error'] - - response.redirect('/prototype_v3/tobacco/cigars/former/has-quantity-changed') -}) - -router.post('/prototype_v3/tobacco/cigars/former/has-quantity-changed-answer', function(request, response) { - var changes = request.session.data['cigarsFormerChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('more')) { - response.redirect('/prototype_v3/tobacco/cigars/former/more-frequency') - } else if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/cigars/former/less-frequency') - } else { - moveToNextTobaccoType(request, response) - } -}) - -// MORE flow - FORMER -router.post('/prototype_v3/tobacco/cigars/former/more-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigars/former/more-quantity') -}) - -router.post('/prototype_v3/tobacco/cigars/former/more-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigars/former/more-duration') -}) - -router.post('/prototype_v3/tobacco/cigars/former/more-duration-answer', function(request, response) { - var currentSize = request.session.data['currentCigarSize'] - var changes = request.session.data['cigar' + currentSize + 'Changes'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/cigars/former/less-frequency') - } else { - moveToNextTobaccoType(request, response) - } -}) - -// LESS flow - FORMER -router.post('/prototype_v3/tobacco/cigars/former/less-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigars/former/less-quantity') -}) - -router.post('/prototype_v3/tobacco/cigars/former/less-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigars/former/less-duration') -}) - -router.post('/prototype_v3/tobacco/cigars/former/less-duration-answer', function(request, response) { - moveToNextTobaccoType(request, response) -}) - -// ============================================ -// "DO YOU CURRENTLY SMOKE" ROUTING - CIGARILLOS -// ============================================ - -router.post('/prototype_v3/tobacco/cigarillos/do-you-currently-smoke-answer', function(request, response) { - var currentlySmokesCigarillos = request.session.data['currentlySmokesCigarillos'] - - if (currentlySmokesCigarillos === 'Yes') { - response.redirect('/prototype_v3/tobacco/cigarillos/current/years-smoked') - } else { - response.redirect('/prototype_v3/tobacco/cigarillos/former/years-smoked') - } -}) - -// ============================================ -// CIGARILLOS ROUTING - CURRENT -// ============================================ - -router.post('/prototype_v3/tobacco/cigarillos/current/years-smoked-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarillos/current/frequency') -}) - -router.post('/prototype_v3/tobacco/cigarillos/current/frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarillos/current/quantity') -}) - -router.post('/prototype_v3/tobacco/cigarillos/current/quantity-answer', function(request, response) { - var quantity = request.session.data['cigarillosCurrentQuantity'] - - // Validate that quantity is at least 1 - if (!quantity || parseInt(quantity) < 1) { - request.session.data['cigarillosCurrentQuantity-error'] = true - return response.redirect('/prototype_v3/tobacco/cigarillos/current/quantity') - } - - // Clear any previous errors - delete request.session.data['cigarillosCurrentQuantity-error'] - - response.redirect('/prototype_v3/tobacco/cigarillos/current/has-quantity-changed') -}) - -router.post('/prototype_v3/tobacco/cigarillos/current/has-quantity-changed-answer', function(request, response) { - var changes = request.session.data['cigarillosCurrentChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('more')) { - response.redirect('/prototype_v3/tobacco/cigarillos/current/more-frequency') - } else if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/cigarillos/current/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// MORE flow -router.post('/prototype_v3/tobacco/cigarillos/current/more-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarillos/current/more-quantity') -}) - -router.post('/prototype_v3/tobacco/cigarillos/current/more-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarillos/current/more-duration') -}) - -router.post('/prototype_v3/tobacco/cigarillos/current/more-duration-answer', function(request, response) { - var changes = request.session.data['cigarillosCurrentChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - // Check if they also selected 'less' - if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/cigarillos/current/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// LESS flow -router.post('/prototype_v3/tobacco/cigarillos/current/less-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarillos/current/less-quantity') -}) - -router.post('/prototype_v3/tobacco/cigarillos/current/less-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarillos/current/less-duration') -}) - -router.post('/prototype_v3/tobacco/cigarillos/current/less-duration-answer', function(request, response) { - var changes = request.session.data['cigarillosCurrentChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - // Check if they also selected 'stopped' - if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - - -// ============================================ -// CIGARILLOS ROUTING - FORMER -// ============================================ - -router.post('/prototype_v3/tobacco/cigarillos/former/years-smoked-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarillos/former/frequency') -}) - -router.post('/prototype_v3/tobacco/cigarillos/former/frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarillos/former/quantity') -}) - -router.post('/prototype_v3/tobacco/cigarillos/former/quantity-answer', function(request, response) { - var quantity = request.session.data['cigarillosFormerQuantity'] - - // Validate that quantity is at least 1 - if (!quantity || parseInt(quantity) < 1) { - request.session.data['cigarillosFormerQuantity-error'] = true - return response.redirect('/prototype_v3/tobacco/cigarillos/former/quantity') - } - - // Clear any previous errors - delete request.session.data['cigarillosFormerQuantity-error'] - - response.redirect('/prototype_v3/tobacco/cigarillos/former/has-quantity-changed') -}) - -router.post('/prototype_v3/tobacco/cigarillos/former/has-quantity-changed-answer', function(request, response) { - var changes = request.session.data['cigarillosFormerChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('more')) { - response.redirect('/prototype_v3/tobacco/cigarillos/former/more-frequency') - } else if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/cigarillos/former/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// MORE flow - FORMER -router.post('/prototype_v3/tobacco/cigarillos/former/more-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarillos/former/more-quantity') -}) - -router.post('/prototype_v3/tobacco/cigarillos/former/more-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarillos/former/more-duration') -}) - -router.post('/prototype_v3/tobacco/cigarillos/former/more-duration-answer', function(request, response) { - var changes = request.session.data['cigarillosFormerChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('less')) { - response.redirect('/prototype_v3/tobacco/cigarillos/former/less-frequency') - } else if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - -// LESS flow - FORMER -router.post('/prototype_v3/tobacco/cigarillos/former/less-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarillos/former/less-quantity') -}) - -router.post('/prototype_v3/tobacco/cigarillos/former/less-quantity-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/cigarillos/former/less-duration') -}) - -router.post('/prototype_v3/tobacco/cigarillos/former/less-duration-answer', function(request, response) { - var changes = request.session.data['cigarillosFormerChanges'] - - if (!Array.isArray(changes)) { - changes = changes ? [changes] : [] - } - - if (changes.includes('stopped')) { - moveToNextTobaccoType(request, response) - } else { - moveToNextTobaccoType(request, response) - } -}) - - -// ============================================ -// "DO YOU CURRENTLY SMOKE" ROUTING - SHISHA -// ============================================ - -router.post('/prototype_v3/tobacco/shisha/do-you-currently-smoke-answer', function(request, response) { - var currentlySmokesShisha = request.session.data['currentlySmokesShisha'] - - if (currentlySmokesShisha === 'Yes') { - response.redirect('/prototype_v3/tobacco/shisha/current/years-smoked') - } else { - response.redirect('/prototype_v3/tobacco/shisha/former/years-smoked') - } -}) - -// ============================================ -// SHISHA ROUTING - CURRENT -// ============================================ - -router.post('/prototype_v3/tobacco/shisha/current/years-smoked-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/shisha/current/group-or-alone') -}) - -router.post('/prototype_v3/tobacco/shisha/current/group-or-alone-answer', function(request, response) { - var groupOrAlone = request.session.data['shishaCurrentGroupOrAlone'] - - // Convert to array if it's not already (handles single selection) - if (!Array.isArray(groupOrAlone)) { - groupOrAlone = [groupOrAlone] - } - - // Check if both are selected - var hasGroup = groupOrAlone.includes('Group') - var hasAlone = groupOrAlone.includes('Alone') - - if (hasGroup && hasAlone) { - // Both selected - go to group questions first, then alone - response.redirect('/prototype_v3/tobacco/shisha/current/group-frequency') - } else if (hasGroup) { - // Only group selected - response.redirect('/prototype_v3/tobacco/shisha/current/group-frequency') - } else if (hasAlone) { - // Only alone selected - response.redirect('/prototype_v3/tobacco/shisha/current/alone-frequency') - } -}) - -// GROUP flow -router.post('/prototype_v3/tobacco/shisha/current/group-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/shisha/current/group-quantity') -}) - -router.post('/prototype_v3/tobacco/shisha/current/group-quantity-answer', function(request, response) { - var groupOrAlone = request.session.data['shishaCurrentGroupOrAlone'] - - // Convert to array if it's not already - if (!Array.isArray(groupOrAlone)) { - groupOrAlone = [groupOrAlone] - } - - // If they smoke both group and alone, now ask about alone - if (groupOrAlone.includes('Group') && groupOrAlone.includes('Alone')) { - response.redirect('/prototype_v3/tobacco/shisha/current/alone-frequency') - } else { - moveToNextTobaccoType(request, response) - } -}) - -// ALONE flow -router.post('/prototype_v3/tobacco/shisha/current/alone-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/shisha/current/alone-quantity') -}) - -router.post('/prototype_v3/tobacco/shisha/current/alone-quantity-answer', function(request, response) { - moveToNextTobaccoType(request, response) -}) - -// ============================================ -// SHISHA ROUTING - FORMER -// ============================================ - -router.post('/prototype_v3/tobacco/shisha/former/years-smoked-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/shisha/former/group-or-alone') -}) - -router.post('/prototype_v3/tobacco/shisha/former/group-or-alone-answer', function(request, response) { - var groupOrAlone = request.session.data['shishaFormerGroupOrAlone'] - - // Convert to array if it's not already (handles single selection) - if (!Array.isArray(groupOrAlone)) { - groupOrAlone = [groupOrAlone] - } - - // Check if both are selected - var hasGroup = groupOrAlone.includes('Group') - var hasAlone = groupOrAlone.includes('Alone') - - if (hasGroup && hasAlone) { - // Both selected - go to group questions first, then alone - response.redirect('/prototype_v3/tobacco/shisha/former/group-frequency') - } else if (hasGroup) { - // Only group selected - response.redirect('/prototype_v3/tobacco/shisha/former/group-frequency') - } else if (hasAlone) { - // Only alone selected - response.redirect('/prototype_v3/tobacco/shisha/former/alone-frequency') - } -}) - -// GROUP flow - FORMER -router.post('/prototype_v3/tobacco/shisha/former/group-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/shisha/former/group-quantity') -}) - -router.post('/prototype_v3/tobacco/shisha/former/group-quantity-answer', function(request, response) { - var groupOrAlone = request.session.data['shishaFormerGroupOrAlone'] - - // Convert to array if it's not already - if (!Array.isArray(groupOrAlone)) { - groupOrAlone = [groupOrAlone] - } - - // If they smoked both group and alone, now ask about alone - if (groupOrAlone.includes('Group') && groupOrAlone.includes('Alone')) { - response.redirect('/prototype_v3/tobacco/shisha/former/alone-frequency') - } else { - moveToNextTobaccoType(request, response) - } -}) - -// ALONE flow - FORMER -router.post('/prototype_v3/tobacco/shisha/former/alone-frequency-answer', function(request, response) { - response.redirect('/prototype_v3/tobacco/shisha/former/alone-quantity') -}) - -router.post('/prototype_v3/tobacco/shisha/former/alone-quantity-answer', function(request, response) { - moveToNextTobaccoType(request, response) -}) - -// ============================================ -// INDEX-ALLPAGES - Clear all session data -// ============================================ - -router.get('/prototype_v3/index-allpages', function(request, response) { - // Clear ALL session data - request.session.data = {} - - // Render the page - response.render('prototype_v3/index-allpages') -}) - -// ============================================ -// CHECK YOUR ANSWERS - Calculate years smoked -// ============================================ - -router.get('/prototype_v3/check-your-answers', function(request, response) { - // Calculate years smoked if not explicitly entered - var currentYear = new Date().getFullYear() - var birthYear = request.session.data['dateOfBirth'] && request.session.data['dateOfBirth']['year'] - ? parseInt(request.session.data['dateOfBirth']['year']) - : 0 - var currentAge = birthYear > 0 ? currentYear - birthYear : 0 - var ageStarted = request.session.data['ageStartedSmoking'] - ? parseInt(request.session.data['ageStartedSmoking']) - : 0 - var periodsStopped = request.session.data['totalYearsStoppedSmoking'] - ? parseInt(request.session.data['totalYearsStoppedSmoking']) - : 0 - var ageQuit = request.session.data['formerSmokingQuitDate'] && request.session.data['formerSmokingQuitDate']['age'] - ? parseInt(request.session.data['formerSmokingQuitDate']['age']) - : 0 - - // Calculate based on smoker type - var calculatedYears = 0 - if (request.session.data['smokedRegularly'] === 'Yes-currently') { - calculatedYears = currentAge > 0 && ageStarted > 0 ? currentAge - ageStarted - periodsStopped : 0 - } else if (request.session.data['smokedRegularly'] === 'Yes-usedToRegularly') { - calculatedYears = ageQuit > 0 && ageStarted > 0 ? ageQuit - ageStarted - periodsStopped : 0 - } - - // Ensure non-negative - calculatedYears = Math.max(0, calculatedYears) - - // Only set calculated value for tobacco types that were actually selected - var selectedTobaccoTypes = request.session.data['tobaccoTypes'] || [] - - // Ensure it's an array - if (!Array.isArray(selectedTobaccoTypes)) { - selectedTobaccoTypes = selectedTobaccoTypes ? [selectedTobaccoTypes] : [] - } - - var isCurrent = request.session.data['smokedRegularly'] === 'Yes-currently' - - // Map tobacco type names to their data field names - var tobaccoTypeMapping = { - 'Cigarettes': isCurrent ? 'cigarettesCurrentYearsSmoked' : 'cigarettesFormerYearsSmoked', - 'Rolling tobacco': isCurrent ? 'rollingTobaccoCurrentYearsSmoked' : 'rollingTobaccoFormerYearsSmoked', - 'Pipe': isCurrent ? 'pipeCurrentYearsSmoked' : 'pipeFormerYearsSmoked', - 'Small cigars': isCurrent ? 'cigarsCurrentYearsSmoked' : 'cigarsFormerYearsSmoked', - 'Medium cigars': isCurrent ? 'cigarsCurrentYearsSmoked' : 'cigarsFormerYearsSmoked', - 'Large cigars': isCurrent ? 'cigarsCurrentYearsSmoked' : 'cigarsFormerYearsSmoked', - 'Cigarillos': isCurrent ? 'cigarillosCurrentYearsSmoked' : 'cigarillosFormerYearsSmoked', - 'Shisha': isCurrent ? 'shishaCurrentYearsSmoked' : 'shishaFormerYearsSmoked' - } - - // Only set years for selected tobacco types - selectedTobaccoTypes.forEach(function(tobaccoType) { - var fieldName = tobaccoTypeMapping[tobaccoType] - if (fieldName && !request.session.data[fieldName] && calculatedYears > 0) { - request.session.data[fieldName] = calculatedYears - } - }) - - // Render the page - response.render('prototype_v3/check-your-answers') -}) - -// ============================================ -// SKIP TO TOBACCO SECTION - CURRENT (FOR TESTING) -// ============================================ - -router.get('/prototype_v3/skip-to-tobacco', function(request, response) { - // CLEAR ALL SESSION DATA - request.session.data = {} - - // NOW SET THE PRE-FILLED DATA FOR TESTING - request.session.data['smokedRegularly'] = "Yes-currently" - - // Date of birth - request.session.data['dateOfBirth'] = { - day: "19", - month: "06", - year: "1965" - } - - // About you section - request.session.data['height'] = { - feet: "5", - inches: "10" - } - request.session.data['weight'] = { - kilograms: "80" - } - request.session.data['whatIsYourSex'] = "Male" - request.session.data['bestDescribe'] = "Male" - request.session.data['ethnicBackground'] = "White" - request.session.data['educationCompleted'] = "Bachelors degree" - - // Your health section - request.session.data['EverDiagnosedWith'] = ["Pneumonia"] - request.session.data['exposedAsbestos'] = "No" - request.session.data['livedWithAsbestosWorker'] = "No" - request.session.data['diagnosedCancer'] = "No" - - // Family history - request.session.data['relativesHaveCancer'] = "Yes" - request.session.data['relativeAge'] = "Yes" - - // Redirect to how-old-when-started-smoking with clean slate - response.redirect("/prototype_v3/how-old-when-started-smoking") -}) - -// ============================================ -// SKIP TO TOBACCO SECTION (FORMER SMOKER) -// ============================================ - -router.get('/prototype_v3/skip-to-tobacco-former', function(request, response) { - // CLEAR ALL SESSION DATA - request.session.data = {} - - // NOW SET THE PRE-FILLED DATA FOR TESTING - request.session.data['smokedRegularly'] = "Yes-usedToRegularly" - - // Date of birth - request.session.data['dateOfBirth'] = { - day: "19", - month: "06", - year: "1965" - } - - // About you section - request.session.data['height'] = { - feet: "5", - inches: "10" - } - request.session.data['weight'] = { - kilograms: "80" - } - request.session.data['whatIsYourSex'] = "Male" - request.session.data['bestDescribe'] = "Male" - request.session.data['ethnicBackground'] = "White" - request.session.data['educationCompleted'] = "Bachelors degree" - - // Your health section - request.session.data['EverDiagnosedWith'] = ["Pneumonia"] - request.session.data['exposedAsbestos'] = "No" - request.session.data['livedWithAsbestosWorker'] = "No" - request.session.data['diagnosedCancer'] = "No" - - // Family history - request.session.data['relativesHaveCancer'] = "Yes" - request.session.data['relativeAge'] = "Yes" +// Add your routes here - above the module.exports line - // Redirect to how-old-when-started-smoking with clean slate - response.redirect("/prototype_v3/how-old-when-started-smoking") -}) \ No newline at end of file +module.exports = router diff --git a/app/views/prototype_v1/routes.js b/app/views/prototype_v1/routes.js new file mode 100644 index 0000000..3583870 --- /dev/null +++ b/app/views/prototype_v1/routes.js @@ -0,0 +1,262 @@ +// External dependencies +const express = require('express') +const router = express.Router() + +///////////////// +// Prototype 1 // +///////////////// + +router.post('/prototype_v1/relatives-with-cancer-answer', (request, response) => { +var relativesHaveCancer = request.session.data['relativesHaveCancer'] +var smokedRegularly = request.session.data['smokedRegularly'] + +if (relativesHaveCancer == "Yes"){ + // If relatives had cancer, ask about their age first + response.redirect("/prototype_v1/relatives-age-when-diagnosed") +} else if (relativesHaveCancer == "No"){ + // No relatives with cancer, route based on smoking status + if (smokedRegularly == "Yes-currently") { + response.redirect("/prototype_v1/how-old-when-started-smoking") +} else if (smokedRegularly == "Yes-usedToRegularly") { + response.redirect("/prototype_v1/how-old-when-started-smoking") + } else { + // Fallback to original logic if smoking status unclear + response.redirect("/prototype_v1/do-you-smoke-now") + } +} else { + response.redirect("/prototype_v1/relatives-with-cancer") +} +}) + + +router.post('/prototype_v1/relatives-age-answer', (request, response) => { + var smokedRegularly = request.session.data['smokedRegularly'] + + if (smokedRegularly == "Yes-currently") { + response.redirect("/prototype_v1/how-old-when-started-smoking") + } else if (smokedRegularly == "Yes-usedToRegularly") { + response.redirect("/prototype_v1/how-old-when-started-smoking") + } else { + // Fallback + response.redirect("/prototype_v1/do-you-smoke-now") + } +}) + +router.get('/prototype_v1/start-journey', (request, response) => { + delete request.session.data + response.redirect("/prototype_v1/eligibility-have-you-ever-smoked") +}) + +router.post('/prototype_v1/smokedRegularlyAnswer', (request, response) => { + var smokedRegularly = request.session.data['smokedRegularly'] + if (smokedRegularly == "Yes-currently"){ + response.redirect("/prototype_v1/eligibility-what-is-your-date-of-birth") + } else if (smokedRegularly == "Yes-usedToRegularly"){ + response.redirect("/prototype_v1/eligibility-what-is-your-date-of-birth") + } else if (smokedRegularly == "Yes-usedToFewTimes"){ + response.redirect("/prototype_v1/drop-out-never-smoked") + } else if (smokedRegularly == "No"){ + response.redirect("/prototype_v1/drop-out-never-smoked") + } + else { + response.redirect("/prototype_v1/eligibility-have-you-ever-smoked") + } +}) + +router.post('/prototype_v1/smokeNowAnswer', (request, response) => { + var smokeNow = request.session.data['smokeNow'] + if (smokeNow == "Yes"){ + response.redirect("/prototype_v1/current-smoker-how-many-years") + } else if (smokeNow == "No"){ + response.redirect("/prototype_v1/former-smoker-when-quit-smoking") + } else { + response.redirect("/prototype_v1/do-you-smoke-now") + } +}) + +router.post('/prototype_v1/who-should-not-use-answer', (request, response) => { + var smokeNow = request.session.data['canYouContinue'] + if (smokeNow == "Yes"){ + response.redirect("/prototype_v1/what-is-your-height") + } else if (smokeNow == "No"){ + response.redirect("/prototype_v1/drop-out-bmi") + } else { + response.redirect("/prototype_v1/who-should-not-use-this-online-service") + } +}) + +router.post('/prototype_v1/whatDoYouSmokeAnswer', (request, response) => { + var selectedTobacco = request.session.data['whatSmokeNow']; + + // Ensure it's an array (single selection becomes array) + if (!Array.isArray(selectedTobacco)) { + selectedTobacco = selectedTobacco ? [selectedTobacco] : []; + } + + // Define page mapping in order (matches checkbox order) + const tobaccoPages = [ + { value: 'Cigarettes', page: 'tobacco-how-many-cigarettes-per-day' }, + { value: 'Rolled cigarettes', page: 'tobacco-rolled-cigarettes-how-many-grams' }, + { value: 'Pipe', page: 'tobacco-pipe-how-many-bowls' }, + { value: 'Cigars', page: 'tobacco-what-size-cigars-do-you-smoke' }, + { value: 'Hookah', page: 'tobacco-hookah-and-shisha' } + ]; + + // Create queue of pages to visit + var pagesToVisit = []; + tobaccoPages.forEach(function(tobacco) { + if (selectedTobacco.includes(tobacco.value)) { + pagesToVisit.push(tobacco.page); + } + }); + + // Store the queue in session + request.session.data['tobaccoPageQueue'] = pagesToVisit; + request.session.data['currentTobaccoPageIndex'] = 0; + + // Redirect to first page or check answers if no pages + if (pagesToVisit.length > 0) { + response.redirect('/prototype_v1/' + pagesToVisit[0]); + } else { + response.redirect('/prototype_v1/check-your-answers'); + } +}) + +router.post('/prototype_v1/dateOfBirthAnswer', (request, response) => { + const day = request.session.data['dateOfBirth']['day'] + const month = request.session.data['dateOfBirth']['month'] + const year = request.session.data['dateOfBirth']['year'] + + // Check if all fields are provided + if (!day || !month || !year) { + return response.redirect("/prototype_v1/eligibility-what-is-your-date-of-birth") + } + + // Calculate age + const birthDate = new Date(year, month - 1, day) // month is 0-indexed + const today = new Date() + let age = today.getFullYear() - birthDate.getFullYear() + const monthDiff = today.getMonth() - birthDate.getMonth() + + if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) { + age-- + } + + // Check age eligibility (55-74 for lung health checks) + if (age < 55 || age > 74) { + return response.redirect("/prototype_v1/drop-out-age") + } + + // If eligible, show the smoking question page + response.render('prototype_v1/task-list') +}) + +router.post('/prototype_v1/whatDidYouSmokeAnswer', (request, response) => { + var selectedTobacco = request.session.data['whatDidSmoke']; + + // Ensure it's an array (single selection becomes array) + if (!Array.isArray(selectedTobacco)) { + selectedTobacco = selectedTobacco ? [selectedTobacco] : []; + } + + // Define page mapping for former smokers (same pages, different route name) + const tobaccoPages = [ + { value: 'Cigarettes', page: 'tobacco-how-many-cigarettes-per-day' }, + { value: 'Rolled cigarettes', page: 'tobacco-rolled-cigarettes-how-many-grams' }, + { value: 'Pipe', page: 'tobacco-pipe-how-many-bowls' }, + { value: 'Cigars', page: 'tobacco-what-size-cigars-do-you-smoke' }, + { value: 'Hookah', page: 'tobacco-hookah-and-shisha' } + ]; + + // Create queue of pages to visit + var pagesToVisit = []; + tobaccoPages.forEach(function(tobacco) { + if (selectedTobacco.includes(tobacco.value)) { + pagesToVisit.push(tobacco.page); + } + }); + + // Store the queue in session + request.session.data['tobaccoPageQueue'] = pagesToVisit; + request.session.data['currentTobaccoPageIndex'] = 0; + + // Redirect to first page or check answers if no pages + if (pagesToVisit.length > 0) { + response.redirect('/prototype_v1/' + pagesToVisit[0]); + } else { + response.redirect('/prototype_v1/check-your-answers'); + } +}) + +router.post('/prototype_v1/tobacco-next', (request, response) => { + var queue = request.session.data['tobaccoPageQueue'] || []; + var currentIndex = request.session.data['currentTobaccoPageIndex'] || 0; + + // Move to next page + currentIndex++; + request.session.data['currentTobaccoPageIndex'] = currentIndex; + + // Check if more pages in queue + if (currentIndex < queue.length) { + response.redirect('/prototype_v1/' + queue[currentIndex]); + } else { + // All tobacco pages completed, go to check answers + response.redirect('/prototype_v1/check-your-answers'); + } +}) +router.post('/prototype_v1/ageStartedSmokingAnswer', (request, response) => { + var smokedRegularly = request.session.data['smokedRegularly'] + + if (smokedRegularly == "Yes-currently") { + response.redirect("/prototype_v1/have-you-ever-stopped-smoking") + } else if (smokedRegularly == "Yes-usedToRegularly") { + response.redirect("/prototype_v1/former-smoker-when-quit-smoking") + } else { + // Fallback + response.redirect("/prototype_v1/how-old-when-started-smoking") + } +}) + + +router.post('/prototype_v1/tobaccoHookahSessionAnswer', (request, response) => { + var hookahSession = request.session.data['hookahSession'] + + // If hookahSession is a string (single selection), convert to array + if (typeof hookahSession === 'string') { + hookahSession = [hookahSession] + } + + // Check what was selected and route accordingly + if (hookahSession && hookahSession.includes('In a group session') && hookahSession.includes('By myself')) { + // Both selected - go to group first, then individual + response.redirect("/prototype_v1/tobacco-hookah-how-much-group") + } else if (hookahSession && hookahSession.includes('In a group session')) { + // Only group selected + response.redirect("/prototype_v1/tobacco-hookah-how-much-group") + } else if (hookahSession && hookahSession.includes('By myself')) { + // Only individual selected + response.redirect("/prototype_v1/tobacco-how-much-by-yourself") + } else { + // Nothing selected or error - continue to next tobacco type + response.redirect("/prototype_v1/tobacco-next") + } +}) + + +router.post('/prototype_v1/tobaccoHookahGroupToIndividual', (request, response) => { + var hookahSession = request.session.data['hookahSession'] + + // Convert to array if needed + if (typeof hookahSession === 'string') { + hookahSession = [hookahSession] + } + + // If both were selected and user just completed group, go to individual + if (hookahSession && hookahSession.includes('By myself')) { + response.redirect("/prototype_v1/tobacco-how-much-by-yourself") + } else { + // Otherwise continue to next tobacco type + response.redirect("/prototype_v1/tobacco-next") + } +}) +module.exports = router diff --git a/app/views/prototype_v2/routes.js b/app/views/prototype_v2/routes.js new file mode 100644 index 0000000..665bda6 --- /dev/null +++ b/app/views/prototype_v2/routes.js @@ -0,0 +1,259 @@ +// External dependencies +const express = require('express') +const router = express.Router() + +///////////////// +// Prototype 2 // +///////////////// + +router.post('/prototype_v2/relatives-with-cancer-answer', (request, response) => { +var relativesHaveCancer = request.session.data['relativesHaveCancer'] + +if (relativesHaveCancer == "Yes"){ + // If relatives had cancer, ask about their age first + response.redirect("/prototype_v2/relatives-age-when-diagnosed") +} else if (relativesHaveCancer == "No" || relativesHaveCancer == "I don't know"){ + // No relatives with cancer OR don't know, go straight to age started smoking + response.redirect("/prototype_v2/how-old-when-started-smoking") +} else { + // No answer provided, redirect back to the question + response.redirect("/prototype_v2/relatives-with-cancer") +} +}) + +router.post('/prototype_v2/relatives-age-answer', (request, response) => { + response.redirect("/prototype_v2/how-old-when-started-smoking") +}) + +router.get('/prototype_v2/start-journey', (request, response) => { + delete request.session.data + response.redirect("/prototype_v2/have-you-completed-by-phone") +}) + +router.post('/prototype_v2/have-you-completed-by-phone-answer', (request, response) => { + const completedByPhone = request.session.data['completedByPhone'] + + if (completedByPhone === 'Yes') { + // If they've already completed by phone, redirect to exit page + response.redirect('/prototype_v2/completed-by-phone-exit') + } else if (completedByPhone === 'No') { + // If they haven't completed by phone, continue to eligibility + response.redirect('/prototype_v2/eligibility-have-you-ever-smoked') + } else { + // If no answer provided, redirect back to the question + response.redirect('/prototype_v2/have-you-completed-by-phone') + } +}) + +router.post('/prototype_v2/smokedRegularlyAnswer', (request, response) => { + var smokedRegularly = request.session.data['smokedRegularly'] + if (smokedRegularly == "Yes-currently"){ + response.redirect("/prototype_v2/eligibility-what-is-your-date-of-birth") + } else if (smokedRegularly == "Yes-usedToRegularly"){ + response.redirect("/prototype_v2/eligibility-what-is-your-date-of-birth") + } else if (smokedRegularly == "Yes-usedToFewTimes"){ + response.redirect("/prototype_v2/drop-out-never-smoked") + } else if (smokedRegularly == "No"){ + response.redirect("/prototype_v2/drop-out-never-smoked") + } + else { + response.redirect("/prototype_v2/eligibility-have-you-ever-smoked") + } +}) + +router.post('/prototype_v2/smokeNowAnswer', (request, response) => { + var smokeNow = request.session.data['smokeNow'] + if (smokeNow == "Yes"){ + response.redirect("/prototype_v2/current-smoker-how-many-years") + } else if (smokeNow == "No"){ + response.redirect("/prototype_v2/former-smoker-when-quit-smoking") + } else { + response.redirect("/prototype_v2/do-you-smoke-now") + } +}) + +router.post('/prototype_v2/who-should-not-use-answer', (request, response) => { + var smokeNow = request.session.data['canYouContinue'] + if (smokeNow == "Yes"){ + response.redirect("/prototype_v2/drop-out-bmi") + } else if (smokeNow == "No"){ + response.redirect("/prototype_v2/what-is-your-height") + } else { + response.redirect("/prototype_v2/who-should-not-use-this-online-service") + } +}) + +router.post('/prototype_v2/whatDoYouSmokeAnswer', (request, response) => { + var selectedTobacco = request.session.data['whatSmokeNow']; + + // Ensure it's an array (single selection becomes array) + if (!Array.isArray(selectedTobacco)) { + selectedTobacco = selectedTobacco ? [selectedTobacco] : []; + } + + // Define page mapping in order (matches checkbox order) + const tobaccoPages = [ + { value: 'Cigarettes', page: 'tobacco-how-many-cigarettes-per-day' }, + { value: 'Rolled cigarettes', page: 'tobacco-rolled-cigarettes-how-many-grams' }, + { value: 'Pipe', page: 'tobacco-pipe-how-many-bowls' }, + { value: 'Cigars', page: 'tobacco-what-size-cigars-do-you-smoke' }, + { value: 'Hookah', page: 'tobacco-hookah-and-shisha' } + ]; + + // Create queue of pages to visit + var pagesToVisit = []; + tobaccoPages.forEach(function(tobacco) { + if (selectedTobacco.includes(tobacco.value)) { + pagesToVisit.push(tobacco.page); + } + }); + + // Store the queue in session + request.session.data['tobaccoPageQueue'] = pagesToVisit; + request.session.data['currentTobaccoPageIndex'] = 0; + + // Redirect to first page or check answers if no pages + if (pagesToVisit.length > 0) { + response.redirect('/prototype_v2/' + pagesToVisit[0]); + } else { + response.redirect('/prototype_v2/check-your-answers'); + } +}) + +router.post('/prototype_v2/dateOfBirthAnswer', (request, response) => { + const day = request.session.data['dateOfBirth']['day'] + const month = request.session.data['dateOfBirth']['month'] + const year = request.session.data['dateOfBirth']['year'] + + // Check if all fields are provided + if (!day || !month || !year) { + return response.redirect("/prototype_v2/eligibility-what-is-your-date-of-birth") + } + + // Calculate age + const birthDate = new Date(year, month - 1, day) // month is 0-indexed + const today = new Date() + let age = today.getFullYear() - birthDate.getFullYear() + const monthDiff = today.getMonth() - birthDate.getMonth() + + if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) { + age-- + } + + // Check age eligibility (55-74 for lung health checks) + if (age < 55 || age > 74) { + return response.redirect("/prototype_v2/drop-out-age") + } + + // If eligible, show the smoking question page + response.render('prototype_v2/check-if-you-need-face-to-face-appointment') +}) + +router.post('/prototype_v2/whatDidYouSmokeAnswer', (request, response) => { + var selectedTobacco = request.session.data['whatDidSmoke']; + + // Ensure it's an array (single selection becomes array) + if (!Array.isArray(selectedTobacco)) { + selectedTobacco = selectedTobacco ? [selectedTobacco] : []; + } + + // Define page mapping for former smokers (same pages, different route name) + const tobaccoPages = [ + { value: 'Cigarettes', page: 'tobacco-how-many-cigarettes-per-day' }, + { value: 'Rolled cigarettes', page: 'tobacco-rolled-cigarettes-how-many-grams' }, + { value: 'Pipe', page: 'tobacco-pipe-how-many-bowls' }, + { value: 'Cigars', page: 'tobacco-what-size-cigars-do-you-smoke' }, + { value: 'Hookah', page: 'tobacco-hookah-and-shisha' } + ]; + + // Create queue of pages to visit + var pagesToVisit = []; + tobaccoPages.forEach(function(tobacco) { + if (selectedTobacco.includes(tobacco.value)) { + pagesToVisit.push(tobacco.page); + } + }); + + // Store the queue in session + request.session.data['tobaccoPageQueue'] = pagesToVisit; + request.session.data['currentTobaccoPageIndex'] = 0; + + // Redirect to first page or check answers if no pages + if (pagesToVisit.length > 0) { + response.redirect('/prototype_v2/' + pagesToVisit[0]); + } else { + response.redirect('/prototype_v2/check-your-answers'); + } +}) + +router.post('/prototype_v2/tobacco-next', (request, response) => { + var queue = request.session.data['tobaccoPageQueue'] || []; + var currentIndex = request.session.data['currentTobaccoPageIndex'] || 0; + + // Move to next page + currentIndex++; + request.session.data['currentTobaccoPageIndex'] = currentIndex; + + // Check if more pages in queue + if (currentIndex < queue.length) { + response.redirect('/prototype_v2/' + queue[currentIndex]); + } else { + // All tobacco pages completed, go to check answers + response.redirect('/prototype_v2/check-your-answers'); + } +}) + +router.post('/prototype_v2/ageStartedSmokingAnswer', (request, response) => { + var smokedRegularly = request.session.data['smokedRegularly'] + + if (smokedRegularly == "Yes-currently") { + response.redirect("/prototype_v2/have-you-ever-stopped-smoking") + } else if (smokedRegularly == "Yes-usedToRegularly") { + response.redirect("/prototype_v2/former-smoker-when-quit-smoking") + } else { + // Fallback + response.redirect("/prototype_v2/how-old-when-started-smoking") + } +}) + +router.post('/prototype_v2/tobaccoHookahSessionAnswer', (request, response) => { + var hookahSession = request.session.data['hookahSession'] + + // If hookahSession is a string (single selection), convert to array + if (typeof hookahSession === 'string') { + hookahSession = [hookahSession] + } + + // Check what was selected and route accordingly + if (hookahSession && hookahSession.includes('In a group session') && hookahSession.includes('By myself')) { + // Both selected - go to group first, then individual + response.redirect("/prototype_v2/tobacco-hookah-how-much-group") + } else if (hookahSession && hookahSession.includes('In a group session')) { + // Only group selected + response.redirect("/prototype_v2/tobacco-hookah-how-much-group") + } else if (hookahSession && hookahSession.includes('By myself')) { + // Only individual selected + response.redirect("/prototype_v2/tobacco-how-much-by-yourself") + } else { + // Nothing selected or error - continue to next tobacco type + response.redirect("/prototype_v2/tobacco-next") + } +}) + +router.post('/prototype_v2/tobaccoHookahGroupToIndividual', (request, response) => { + var hookahSession = request.session.data['hookahSession'] + + // Convert to array if needed + if (typeof hookahSession === 'string') { + hookahSession = [hookahSession] + } + + // If both were selected and user just completed group, go to individual + if (hookahSession && hookahSession.includes('By myself')) { + response.redirect("/prototype_v2/tobacco-how-much-by-yourself") + } else { + // Otherwise continue to next tobacco type + response.redirect("/prototype_v2/tobacco-next") + } +}) +module.exports = router diff --git a/app/views/prototype_v3/accept-terms.html b/app/views/prototype_v3/accept-terms.html index b759d4e..25e6514 100644 --- a/app/views/prototype_v3/accept-terms.html +++ b/app/views/prototype_v3/accept-terms.html @@ -16,7 +16,7 @@ titleText: "There is a problem", errorList: [ { - text: "Aggree to the terms of use to continue", + text: "Agree to the terms of use to continue", href: "#accept-terms" } ] diff --git a/app/views/prototype_v3/routes.js b/app/views/prototype_v3/routes.js new file mode 100644 index 0000000..b643d12 --- /dev/null +++ b/app/views/prototype_v3/routes.js @@ -0,0 +1,1784 @@ +// External dependencies +const express = require('express') +const router = express.Router() + +///////////////// +// Prototype 3 // +///////////////// + +// ============================================ +// START AND LOGIN FLOW +// ============================================ + +router.get('/prototype_v3/start-journey', (req, res) => { + delete req.session.data + res.redirect("/prototype_v3/login") +}) + +router.post('/prototype_v3/login', (req, res) => { + req.session.data['logged-in'] = true + res.redirect('/prototype_v3/enter-the-security-code') +}) + +router.post('/prototype_v3/enter-the-security-code', (_req, res) => { + res.redirect('/prototype_v3/agree-to-share-login-info') +}) + +router.post('/prototype_v3/agree-to-share-login-info', (_req, res) => { + res.redirect('/prototype_v3/accept-terms') +}) + +// Show accept terms page (GET req clears any error state) +router.get('/prototype_v3/accept-terms', (req, res) => { + // Clear any previous error state + delete req.session.data['accept-terms-error'] + res.render('prototype_v3/accept-terms') +}) + +// Accept terms validation (POST) +router.post('/prototype_v3/accept-terms', (req, res) => { + const acceptTerms = req.session.data['accept-terms'] + + // Check if acceptTerms is an array containing 'yes' or if it's the string 'yes' + const isAccepted = (Array.isArray(acceptTerms) && acceptTerms.includes('yes')) || acceptTerms === 'yes' + + if (!isAccepted) { + // Checkbox not ticked - show error + req.session.data['accept-terms-error'] = true + res.redirect('/prototype_v3/accept-terms') + } else { + // Checkbox ticked - clear error and continue to next page + delete req.session.data['accept-terms-error'] + res.redirect('/prototype_v3/have-you-completed-by-phone') + } +}) + +// ============================================ +// INITIAL ELIGIBILITY CHECKS +// ============================================ + +router.post('/prototype_v3/have-you-completed-by-phone-answer', (req, res) => { + const completedByPhone = req.session.data['completedByPhone'] + + if (completedByPhone === 'Yes') { + res.redirect('/prototype_v3/completed-by-phone-exit') + } else if (completedByPhone === 'No') { + res.redirect('/prototype_v3/eligibility-have-you-ever-smoked') + } else { + res.redirect('/prototype_v3/have-you-completed-by-phone') + } +}) + +router.post('/prototype_v3/smokedRegularlyAnswer', (req, res) => { + var smokedRegularly = req.session.data['smokedRegularly'] + + // People who never smoked or smoked very little should be dropped out immediately + if (smokedRegularly === "Yes-usedToFewTimes"){ + return res.redirect("/prototype_v3/drop-out-never-smoked") + } + + if (smokedRegularly === "No"){ + return res.redirect("/prototype_v3/drop-out-never-smoked") + } + + // People who currently smoke or used to smoke need to check age eligibility + if (smokedRegularly === "Yes-currently"){ + return res.redirect("/prototype_v3/eligibility-what-is-your-date-of-birth") + } + + if (smokedRegularly === "Yes-usedToRegularly"){ + return res.redirect("/prototype_v3/eligibility-what-is-your-date-of-birth") + } + + // If no match, redirect back to the form + return res.redirect("/prototype_v3/eligibility-have-you-ever-smoked") +}) + +router.post('/prototype_v3/dateOfBirthAnswer', (req, res) => { + const day = req.session.data['dateOfBirth']['day'] + const month = req.session.data['dateOfBirth']['month'] + const year = req.session.data['dateOfBirth']['year'] + + if (!day || !month || !year) { + return res.redirect("/prototype_v3/eligibility-what-is-your-date-of-birth") + } + + const birthDate = new Date(year, month - 1, day) + const today = new Date() + let age = today.getFullYear() - birthDate.getFullYear() + const monthDiff = today.getMonth() - birthDate.getMonth() + + if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) { + age-- + } + + if (age < 55 || age > 74) { + return res.redirect("/prototype_v3/drop-out-age") + } + + res.render('prototype_v3/check-if-you-need-face-to-face-appointment') +}) + +router.post('/prototype_v3/who-should-not-use-answer', (req, res) => { + var canContinue = req.session.data['canYouContinue'] + + if (canContinue == "Yes"){ + res.redirect("/prototype_v3/drop-out-bmi") + } else if (canContinue == "No"){ + res.redirect("/prototype_v3/enter-your-height") + } else { + res.redirect("/prototype_v3/who-should-not-use-this-online-service") + } +}) + +// ============================================ +// HEIGHT AND WEIGHT VALIDATION +// ============================================ + +router.post('/prototype_v3/enter-your-height-answer', (req, res) => { + var heightUnit = req.session.data['heightUnit'] + var height = req.session.data['height'] + var errors = {} + + // Clear previous errors + delete req.session.data['heightErrors'] + + if (heightUnit == "imperial") { + var feet = height ? height.feet : '' + var inches = height ? height.inches : '' + + // Check if both fields are empty + if (!feet && !inches) { + errors.height = "Enter your height" + } else { + // Validate feet + if (feet) { + if (feet.includes('.') || feet.includes(',')) { + errors.feet = "Feet must be in whole numbers" + } else if (isNaN(feet) || feet < 4 || feet > 8) { + errors.feet = "Feet must be between 4 and 8" + } + } + + // Validate inches + if (inches) { + if (inches.includes('.') || inches.includes(',')) { + errors.inches = "Inches must be in whole numbers" + } else if (isNaN(inches) || inches < 0 || inches > 11) { + errors.inches = "Inches must be between 0 and 11" + } + } + + // If individual validations pass, check total range + if (!errors.feet && !errors.inches && feet && inches) { + var totalInches = (parseInt(feet) * 12) + parseInt(inches) + if (totalInches < 55 || totalInches > 96) { + errors.height = "Height must be between 4 feet 7 inches and 8 feet" + } + } + } + } else { + // Metric validation + var centimetres = height ? height.centimetres : '' + + if (!centimetres) { + errors.height = "Enter your height" + } else if (isNaN(centimetres) || centimetres < 139.7 || centimetres > 243.8) { + errors.height = "Height must be between 139.7cm and 243.8 cm" + } + } + + // If there are errors, store them and redirect back + if (Object.keys(errors).length > 0) { + req.session.data['heightErrors'] = errors + res.redirect("/prototype_v3/enter-your-height") + } else { + // No errors, continue to weight page + res.redirect("/prototype_v3/enter-your-weight") + } +}) + +router.post('/prototype_v3/enter-your-weight-answer', (req, res) => { + var weightUnit = req.session.data['weightUnit'] + var weight = req.session.data['weight'] + var errors = {} + + // Clear previous errors + delete req.session.data['weightErrors'] + + if (weightUnit == "imperial") { + var stone = weight ? weight.stone : '' + var pounds = weight ? weight.pounds : '' + + // Check if both fields are empty + if (!stone && !pounds) { + errors.weight = "Enter your weight" + } else { + // Validate stone + if (stone) { + if (stone.includes('.') || stone.includes(',')) { + errors.stone = "Stone must be in whole numbers" + } + } + + // Validate pounds + if (pounds) { + if (pounds.includes('.') || pounds.includes(',')) { + errors.pounds = "Pounds must be in whole numbers" + } else if (isNaN(pounds) || pounds < 0 || pounds > 13) { + errors.pounds = "Pounds must be between 0 and 13" + } + } + + // If individual validations pass, check total range + if (!errors.stone && !errors.pounds && stone && pounds) { + var totalPounds = (parseInt(stone) * 14) + parseInt(pounds) + if (totalPounds < 56 || totalPounds > 700) { + errors.weight = "Weight must be between 4 stone and 50 stone" + } + } + } + } else { + // Metric validation + var kilograms = weight ? weight.kilograms : '' + + if (!kilograms) { + errors.weight = "Enter your weight" + } else if (isNaN(kilograms) || kilograms < 25.4 || kilograms > 317.5) { + errors.weight = "Weight must be between 25.4kg and 317.5kg" + } + } + + // If there are errors, store them and redirect back + if (Object.keys(errors).length > 0) { + req.session.data['weightErrors'] = errors + res.redirect("/prototype_v3/enter-your-weight") + } else { + // No errors, continue to sex page + res.redirect("/prototype_v3/your-gender-identity") + } +}) + +// ============================================ +// MEDICAL HISTORY +// ============================================ +router.post('/prototype_v3/diagnosed-with-cancer-answer', (req, res) => { + res.redirect("/prototype_v3/relatives-with-cancer") +}) + +router.post('/prototype_v3/relatives-with-cancer-answer', (req, res) => { + var relativesHaveCancer = req.session.data['relativesHaveCancer'] + + if (relativesHaveCancer == "Yes"){ + res.redirect("/prototype_v3/relatives-age-when-diagnosed") + } else if (relativesHaveCancer == "No" || relativesHaveCancer == "I don't know"){ + res.redirect("/prototype_v3/how-old-when-started-smoking") + } else { + res.redirect("/prototype_v3/relatives-with-cancer") + } +}) + +router.post('/prototype_v3/relatives-age-answer', (req, res) => { + res.redirect("/prototype_v3/how-old-when-started-smoking") +}) + + + +// ============================================ +// ROUTING FOR PERIODS WHEN YOU STOPPED SMOKING +// ============================================ + +// GET route for "Periods when you stopped smoking" page (for index testing) +// Sets smokedRegularly from query parameter if provided +router.get('/prototype_v3/periods-when-you-stopped-smoking', function(req, res, next) { + if (req.query.smokedRegularly) { + req.session.data['smokedRegularly'] = req.query.smokedRegularly + } + next() +}) + +// Route handler for "How old when started smoking" page +// This sends current smokers to "periods when stopped" and former smokers to "when quit" +router.post('/prototype_v3/how-old-when-started-smoking-answer', (req, res) => { + var smokedRegularly = req.session.data['smokedRegularly'] + + if (smokedRegularly == "Yes-currently") { + res.redirect("/prototype_v3/periods-when-you-stopped-smoking") + } else if (smokedRegularly == "Yes-usedToRegularly") { + res.redirect("/prototype_v3/former-smoker-when-quit-smoking") + } else { + // Fallback + res.redirect("/prototype_v3/how-old-when-started-smoking") + } +}) + +// Route handler for "Former smoker when quit" page +// This sends former smokers to "periods when stopped" +router.post('/prototype_v3/former-smoker-when-quit-smoking-answer', (req, res) => { + res.redirect("/prototype_v3/periods-when-you-stopped-smoking") +}) + +// Route handler for "Periods when you stopped smoking" page +// This sends everyone to the tobacco selection page +router.post('/prototype_v3/periods-when-you-stopped-smoking-answer', (req, res) => { + res.redirect("/prototype_v3/what-do-or-did-smoke") +}) + + +// ============================================ +// HELPER FUNCTIONS +// ============================================ + +function moveToNextTobaccoType(req, res) { + var tobaccoQueue = req.session.data['tobaccoQueue'] || [] + var currentIndex = req.session.data['tobaccoQueueIndex'] || 0 + + currentIndex++ + req.session.data['tobaccoQueueIndex'] = currentIndex + + if (currentIndex < tobaccoQueue.length) { + res.redirect(tobaccoQueue[currentIndex]) + } else { + res.redirect('/prototype_v3/check-your-answers') + } +} + +// CIGAR SIZE SPLIT: moveToNextCigarSize is no longer used — cigar sizes now go through moveToNextTobaccoType. +// To revert: restore this function and replace moveToNextTobaccoType calls in the cigars section with it. +/* +function moveToNextCigarSize(req, res, smokerType) { + var sizeQueue = req.session.data['cigarSizeQueue'] || [] + var currentIndex = req.session.data['cigarSizeQueueIndex'] || 0 + var currentSize = req.session.data['currentCigarSize'] + + if (currentSize) { + delete req.session.data['cigar' + currentSize + 'Changes'] + } + + currentIndex++ + req.session.data['cigarSizeQueueIndex'] = currentIndex + + if (currentIndex < sizeQueue.length) { + req.session.data['currentCigarSize'] = sizeQueue[currentIndex] + res.redirect('/prototype_v3/tobacco/cigars/' + smokerType + '/quantity') + } else { + delete req.session.data['cigarSizeQueue'] + delete req.session.data['cigarSizeQueueIndex'] + delete req.session.data['currentCigarSize'] + moveToNextTobaccoType(req, res) + } +} +*/ + +// ============================================ +// TOBACCO TYPE SELECTION +// ============================================ + +// GET route for "What do or did you smoke" page (for index testing) +// Sets smokedRegularly from query parameter if provided +router.get('/prototype_v3/what-do-or-did-smoke', function(req, res, next) { + if (req.query.smokedRegularly) { + req.session.data['smokedRegularly'] = req.query.smokedRegularly + } + next() +}) + +router.post('/prototype_v3/what-do-or-did-smoke-answer', (req, res) => { + var selectedTobacco = req.session.data['tobaccoTypes'] + var smokedRegularly = req.session.data['smokedRegularly'] + + // Ensure it's an array + if (!Array.isArray(selectedTobacco)) { + selectedTobacco = selectedTobacco ? [selectedTobacco] : [] + } + + const tobaccoRoutes = { + 'Cigarettes': '/prototype_v3/tobacco/cigarettes', + 'Rolling tobacco': '/prototype_v3/tobacco/rolling-tobacco', + 'Pipe': '/prototype_v3/tobacco/pipe', + // CIGAR SIZE SPLIT - see MEMORY.md + 'Small cigars': '/prototype_v3/tobacco/cigars/Small', + 'Medium cigars': '/prototype_v3/tobacco/cigars/Medium', + 'Large cigars': '/prototype_v3/tobacco/cigars/Large', + 'Cigarillos': '/prototype_v3/tobacco/cigarillos', + 'Shisha': '/prototype_v3/tobacco/shisha' + } + + var tobaccoQueue = [] + var tobaccoOrder = ['Cigarettes', 'Rolling tobacco', 'Pipe', 'Small cigars', 'Medium cigars', 'Large cigars', 'Cigarillos', 'Shisha'] + + // Check if user selected multiple tobacco types + var multipleTypes = selectedTobacco.length > 1 + + tobaccoOrder.forEach(function(type) { + if (selectedTobacco.includes(type)) { + if (smokedRegularly === "Yes-currently") { + if (multipleTypes) { + // Multiple types - need to ask which ones they currently smoke + tobaccoQueue.push(tobaccoRoutes[type] + '/do-you-currently-smoke') + } else { + // Single type and they're a current smoker + if (type === 'Shisha') { + // Shisha doesn't have frequency and years-smoked is only for multiple types + // Go directly to group-or-alone + tobaccoQueue.push(tobaccoRoutes[type] + '/current/group-or-alone') + } else { + // Other types go straight to frequency (skip years-smoked) + tobaccoQueue.push(tobaccoRoutes[type] + '/current/frequency') + } + } + } else if (smokedRegularly === "Yes-usedToRegularly") { + if (multipleTypes) { + // Multiple types - go to years-smoked first + tobaccoQueue.push(tobaccoRoutes[type] + '/former/years-smoked') + } else { + // Single type and they're a former smoker + if (type === 'Shisha') { + // Shisha doesn't have frequency and years-smoked is only for multiple types + // Go directly to group-or-alone + tobaccoQueue.push(tobaccoRoutes[type] + '/former/group-or-alone') + } else { + // Other types go straight to frequency (skip years-smoked) + tobaccoQueue.push(tobaccoRoutes[type] + '/former/frequency') + } + } + } + } + }) + + req.session.data['tobaccoQueue'] = tobaccoQueue + req.session.data['tobaccoQueueIndex'] = 0 + + if (tobaccoQueue.length > 0) { + res.redirect(tobaccoQueue[0]) + } else { + res.redirect('/prototype_v3/check-your-answers') + } +}) + +// ============================================ +// "DO YOU CURRENTLY SMOKE" ROUTING - CIGARETTES +// ============================================ + +router.post('/prototype_v3/tobacco/cigarettes/do-you-currently-smoke-answer', (req, res) => { + var currentlySmokesCigarettes = req.session.data['currentlySmokesCigarettes'] + + if (currentlySmokesCigarettes === 'Yes') { + res.redirect('/prototype_v3/tobacco/cigarettes/current/years-smoked') + } else { + res.redirect('/prototype_v3/tobacco/cigarettes/former/years-smoked') + } +}) + +// ============================================ +// CIGARETTES ROUTING - CURRENT +// ============================================ + +router.post('/prototype_v3/tobacco/cigarettes/current/years-smoked-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarettes/current/frequency') +}) + +router.post('/prototype_v3/tobacco/cigarettes/current/frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarettes/current/quantity') +}) + +router.post('/prototype_v3/tobacco/cigarettes/current/quantity-answer', (req, res) => { + var quantity = req.session.data['cigarettesCurrentQuantity'] + + // Validate that quantity is at least 1 + if (!quantity || parseInt(quantity) < 1) { + req.session.data['cigarettesCurrentQuantity-error'] = true + return res.redirect('/prototype_v3/tobacco/cigarettes/current/quantity') + } + + // Clear any previous errors + delete req.session.data['cigarettesCurrentQuantity-error'] + + res.redirect('/prototype_v3/tobacco/cigarettes/current/has-quantity-changed') +}) + +router.post('/prototype_v3/tobacco/cigarettes/current/has-quantity-changed-answer', (req, res) => { + var changes = req.session.data['cigarettesCurrentChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('more')) { + res.redirect('/prototype_v3/tobacco/cigarettes/current/more-frequency') + } else if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/cigarettes/current/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// MORE flow +router.post('/prototype_v3/tobacco/cigarettes/current/more-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarettes/current/more-quantity') +}) + +router.post('/prototype_v3/tobacco/cigarettes/current/more-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarettes/current/more-duration') +}) + +router.post('/prototype_v3/tobacco/cigarettes/current/more-duration-answer', (req, res) => { + var changes = req.session.data['cigarettesCurrentChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + // Check if they also selected 'less' + if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/cigarettes/current/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// LESS flow +router.post('/prototype_v3/tobacco/cigarettes/current/less-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarettes/current/less-quantity') +}) + +router.post('/prototype_v3/tobacco/cigarettes/current/less-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarettes/current/less-duration') +}) + +router.post('/prototype_v3/tobacco/cigarettes/current/less-duration-answer', (req, res) => { + var changes = req.session.data['cigarettesCurrentChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + // Check if they also selected 'stopped' + if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + + +// ============================================ +// CIGARETTES ROUTING - FORMER +// ============================================ + +router.post('/prototype_v3/tobacco/cigarettes/former/years-smoked-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarettes/former/frequency') +}) + +router.post('/prototype_v3/tobacco/cigarettes/former/frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarettes/former/quantity') +}) + +router.post('/prototype_v3/tobacco/cigarettes/former/quantity-answer', (req, res) => { + var quantity = req.session.data['cigarettesFormerQuantity'] + + // Validate that quantity is at least 1 + if (!quantity || parseInt(quantity) < 1) { + req.session.data['cigarettesFormerQuantity-error'] = true + return res.redirect('/prototype_v3/tobacco/cigarettes/former/quantity') + } + + // Clear any previous errors + delete req.session.data['cigarettesFormerQuantity-error'] + + res.redirect('/prototype_v3/tobacco/cigarettes/former/has-quantity-changed') +}) + +router.post('/prototype_v3/tobacco/cigarettes/former/has-quantity-changed-answer', (req, res) => { + var changes = req.session.data['cigarettesFormerChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('more')) { + res.redirect('/prototype_v3/tobacco/cigarettes/former/more-frequency') + } else if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/cigarettes/former/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// MORE flow - FORMER +router.post('/prototype_v3/tobacco/cigarettes/former/more-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarettes/former/more-quantity') +}) + +router.post('/prototype_v3/tobacco/cigarettes/former/more-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarettes/former/more-duration') +}) + +router.post('/prototype_v3/tobacco/cigarettes/former/more-duration-answer', (req, res) => { + var changes = req.session.data['cigarettesFormerChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/cigarettes/former/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// LESS flow - FORMER +router.post('/prototype_v3/tobacco/cigarettes/former/less-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarettes/former/less-quantity') +}) + +router.post('/prototype_v3/tobacco/cigarettes/former/less-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarettes/former/less-duration') +}) + +router.post('/prototype_v3/tobacco/cigarettes/former/less-duration-answer', (req, res) => { + var changes = req.session.data['cigarettesFormerChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + + +// ============================================ +// "DO YOU CURRENTLY SMOKE" ROUTING - ROLLED CIGARETTES +// ============================================ + +router.post('/prototype_v3/tobacco/rolling-tobacco/do-you-currently-smoke-answer', (req, res) => { + var currentlySmokesRollingTobacco = req.session.data['currentlySmokesRollingTobacco'] + + if (currentlySmokesRollingTobacco === 'Yes') { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/current/years-smoked') + } else { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/former/years-smoked') + } +}) + +// ============================================ +// ROLLED CIGARETTES ROUTING - CURRENT +// ============================================ + +router.post('/prototype_v3/tobacco/rolling-tobacco/current/years-smoked-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/current/frequency') +}) + +router.post('/prototype_v3/tobacco/rolling-tobacco/current/frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/current/quantity') +}) + +router.post('/prototype_v3/tobacco/rolling-tobacco/current/quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/current/has-quantity-changed') +}) + +router.post('/prototype_v3/tobacco/rolling-tobacco/current/has-quantity-changed-answer', (req, res) => { + var changes = req.session.data['rollingTobaccoCurrentChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('more')) { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/current/more-frequency') + } else if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/current/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// MORE flow +router.post('/prototype_v3/tobacco/rolling-tobacco/current/more-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/current/more-quantity') +}) + +router.post('/prototype_v3/tobacco/rolling-tobacco/current/more-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/current/more-duration') +}) + +router.post('/prototype_v3/tobacco/rolling-tobacco/current/more-duration-answer', (req, res) => { + var changes = req.session.data['rollingTobaccoCurrentChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + // Check if they also selected 'less' + if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/current/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// LESS flow +router.post('/prototype_v3/tobacco/rolling-tobacco/current/less-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/current/less-quantity') +}) + +router.post('/prototype_v3/tobacco/rolling-tobacco/current/less-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/current/less-duration') +}) + +router.post('/prototype_v3/tobacco/rolling-tobacco/current/less-duration-answer', (req, res) => { + var changes = req.session.data['rollingTobaccoCurrentChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + // Check if they also selected 'stopped' + if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + + +// ============================================ +// ROLLED CIGARETTES ROUTING - FORMER +// ============================================ + +router.post('/prototype_v3/tobacco/rolling-tobacco/former/years-smoked-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/former/frequency') +}) + +router.post('/prototype_v3/tobacco/rolling-tobacco/former/frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/former/quantity') +}) + +router.post('/prototype_v3/tobacco/rolling-tobacco/former/quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/former/has-quantity-changed') +}) + +router.post('/prototype_v3/tobacco/rolling-tobacco/former/has-quantity-changed-answer', (req, res) => { + var changes = req.session.data['rollingTobaccoFormerChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('more')) { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/former/more-frequency') + } else if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/former/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// MORE flow - FORMER +router.post('/prototype_v3/tobacco/rolling-tobacco/former/more-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/former/more-quantity') +}) + +router.post('/prototype_v3/tobacco/rolling-tobacco/former/more-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/former/more-duration') +}) + +router.post('/prototype_v3/tobacco/rolling-tobacco/former/more-duration-answer', (req, res) => { + var changes = req.session.data['rollingTobaccoFormerChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/former/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// LESS flow - FORMER +router.post('/prototype_v3/tobacco/rolling-tobacco/former/less-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/former/less-quantity') +}) + +router.post('/prototype_v3/tobacco/rolling-tobacco/former/less-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/rolling-tobacco/former/less-duration') +}) + +router.post('/prototype_v3/tobacco/rolling-tobacco/former/less-duration-answer', (req, res) => { + var changes = req.session.data['rollingTobaccoFormerChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + + +// ============================================ +// "DO YOU CURRENTLY SMOKE" ROUTING - PIPE +// ============================================ + +router.post('/prototype_v3/tobacco/pipe/do-you-currently-smoke-answer', (req, res) => { + var currentlySmokesPipe = req.session.data['currentlySmokesPipe'] + + if (currentlySmokesPipe === 'Yes') { + res.redirect('/prototype_v3/tobacco/pipe/current/years-smoked') + } else { + res.redirect('/prototype_v3/tobacco/pipe/former/years-smoked') + } +}) + +// ============================================ +// PIPE ROUTING - CURRENT +// ============================================ + +router.post('/prototype_v3/tobacco/pipe/current/years-smoked-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/pipe/current/frequency') +}) + +router.post('/prototype_v3/tobacco/pipe/current/frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/pipe/current/quantity') +}) + +router.post('/prototype_v3/tobacco/pipe/current/quantity-answer', (req, res) => { + var quantity = req.session.data['pipeCurrentQuantity'] + + // Validate that quantity is at least 1 + if (!quantity || parseInt(quantity) < 1) { + req.session.data['pipeCurrentQuantity-error'] = true + return res.redirect('/prototype_v3/tobacco/pipe/current/quantity') + } + + // Clear any previous errors + delete req.session.data['pipeCurrentQuantity-error'] + + res.redirect('/prototype_v3/tobacco/pipe/current/has-quantity-changed') +}) + +router.post('/prototype_v3/tobacco/pipe/current/has-quantity-changed-answer', (req, res) => { + var changes = req.session.data['pipeCurrentChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('more')) { + res.redirect('/prototype_v3/tobacco/pipe/current/more-frequency') + } else if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/pipe/current/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// MORE flow +router.post('/prototype_v3/tobacco/pipe/current/more-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/pipe/current/more-quantity') +}) + +router.post('/prototype_v3/tobacco/pipe/current/more-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/pipe/current/more-duration') +}) + +router.post('/prototype_v3/tobacco/pipe/current/more-duration-answer', (req, res) => { + var changes = req.session.data['pipeCurrentChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + // Check if they also selected 'less' + if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/pipe/current/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// LESS flow +router.post('/prototype_v3/tobacco/pipe/current/less-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/pipe/current/less-quantity') +}) + +router.post('/prototype_v3/tobacco/pipe/current/less-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/pipe/current/less-duration') +}) + +router.post('/prototype_v3/tobacco/pipe/current/less-duration-answer', (req, res) => { + var changes = req.session.data['pipeCurrentChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + // Check if they also selected 'stopped' + if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + + +// ============================================ +// PIPE ROUTING - FORMER +// ============================================ + +router.post('/prototype_v3/tobacco/pipe/former/years-smoked-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/pipe/former/frequency') +}) + +router.post('/prototype_v3/tobacco/pipe/former/frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/pipe/former/quantity') +}) + +router.post('/prototype_v3/tobacco/pipe/former/quantity-answer', (req, res) => { + var quantity = req.session.data['pipeFormerQuantity'] + + // Validate that quantity is at least 1 + if (!quantity || parseInt(quantity) < 1) { + req.session.data['pipeFormerQuantity-error'] = true + return res.redirect('/prototype_v3/tobacco/pipe/former/quantity') + } + + // Clear any previous errors + delete req.session.data['pipeFormerQuantity-error'] + + res.redirect('/prototype_v3/tobacco/pipe/former/has-quantity-changed') +}) + +router.post('/prototype_v3/tobacco/pipe/former/has-quantity-changed-answer', (req, res) => { + var changes = req.session.data['pipeFormerChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('more')) { + res.redirect('/prototype_v3/tobacco/pipe/former/more-frequency') + } else if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/pipe/former/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// MORE flow - FORMER +router.post('/prototype_v3/tobacco/pipe/former/more-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/pipe/former/more-quantity') +}) + +router.post('/prototype_v3/tobacco/pipe/former/more-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/pipe/former/more-duration') +}) + +router.post('/prototype_v3/tobacco/pipe/former/more-duration-answer', (req, res) => { + var changes = req.session.data['pipeFormerChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/pipe/former/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// LESS flow - FORMER +router.post('/prototype_v3/tobacco/pipe/former/less-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/pipe/former/less-quantity') +}) + +router.post('/prototype_v3/tobacco/pipe/former/less-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/pipe/former/less-duration') +}) + +router.post('/prototype_v3/tobacco/pipe/former/less-duration-answer', (req, res) => { + var changes = req.session.data['pipeFormerChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + + +// ============================================ +// CIGAR SIZE ENTRY ROUTES - CIGAR SIZE SPLIT (see MEMORY.md) +// These set currentCigarSize in session before redirecting to the shared cigar pages. +// ============================================ + +var cigarSizes = ['Small', 'Medium', 'Large'] + +cigarSizes.forEach(function(size) { + // Multiple types, current smoker: do-you-currently-smoke + router.get('/prototype_v3/tobacco/cigars/' + size + '/do-you-currently-smoke', (req, res) => { + req.session.data['currentCigarSize'] = size + res.redirect('/prototype_v3/tobacco/cigars/do-you-currently-smoke') + }) + + // Single type, current smoker: go direct to frequency + router.get('/prototype_v3/tobacco/cigars/' + size + '/current/frequency', (req, res) => { + req.session.data['currentCigarSize'] = size + res.redirect('/prototype_v3/tobacco/cigars/current/frequency') + }) + + // Multiple types, former smoker: years-smoked + router.get('/prototype_v3/tobacco/cigars/' + size + '/former/years-smoked', (req, res) => { + req.session.data['currentCigarSize'] = size + res.redirect('/prototype_v3/tobacco/cigars/former/years-smoked') + }) + + // Single type, former smoker: go direct to frequency + router.get('/prototype_v3/tobacco/cigars/' + size + '/former/frequency', (req, res) => { + req.session.data['currentCigarSize'] = size + res.redirect('/prototype_v3/tobacco/cigars/former/frequency') + }) +}) + +// ============================================ +// "DO YOU CURRENTLY SMOKE" ROUTING - CIGARS +// ============================================ + +router.post('/prototype_v3/tobacco/cigars/do-you-currently-smoke-answer', (req, res) => { + var currentlySmokesCigars = req.session.data['currentlySmokesCigars'] + + if (currentlySmokesCigars === 'Yes') { + res.redirect('/prototype_v3/tobacco/cigars/current/years-smoked') + } else { + res.redirect('/prototype_v3/tobacco/cigars/former/years-smoked') + } +}) + +// ============================================ +// CIGARS ROUTING - CURRENT +// ============================================ + +router.post('/prototype_v3/tobacco/cigars/current/years-smoked-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigars/current/frequency') +}) + +router.post('/prototype_v3/tobacco/cigars/current/frequency-answer', (req, res) => { + // CIGAR SIZE SPLIT: copies frequency to size-specific variable, skips cigar-size page (see MEMORY.md) + var currentSize = req.session.data['currentCigarSize'] + req.session.data['cigar' + currentSize + 'Frequency'] = req.session.data['cigarsCurrentFrequency'] + res.redirect('/prototype_v3/tobacco/cigars/current/quantity') +}) + +// cigar-size-answer not used - CIGAR SIZE SPLIT (see MEMORY.md) +// router.post('/prototype_v3/tobacco/cigars/current/cigar-size-answer', ...) + +router.post('/prototype_v3/tobacco/cigars/current/quantity-answer', (req, res) => { + var currentSize = req.session.data['currentCigarSize'] + var fieldName = 'cigar' + currentSize + 'Quantity' + var quantity = req.session.data[fieldName] + + // Validate that quantity is at least 1 + if (!quantity || parseInt(quantity) < 1) { + req.session.data[fieldName + '-error'] = true + return res.redirect('/prototype_v3/tobacco/cigars/current/quantity') + } + + // Clear any previous errors + delete req.session.data[fieldName + '-error'] + + res.redirect('/prototype_v3/tobacco/cigars/current/has-quantity-changed') +}) + +router.post('/prototype_v3/tobacco/cigars/current/has-quantity-changed-answer', (req, res) => { + var currentSize = req.session.data['currentCigarSize'] + var changes = req.session.data['cigar' + currentSize + 'Changes'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('more')) { + res.redirect('/prototype_v3/tobacco/cigars/current/more-frequency') + } else if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/cigars/current/less-frequency') + } else { + moveToNextTobaccoType(req, res) + } +}) + +// MORE flow +router.post('/prototype_v3/tobacco/cigars/current/more-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigars/current/more-quantity') +}) + +router.post('/prototype_v3/tobacco/cigars/current/more-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigars/current/more-duration') +}) + +router.post('/prototype_v3/tobacco/cigars/current/more-duration-answer', (req, res) => { + var currentSize = req.session.data['currentCigarSize'] + var changes = req.session.data['cigar' + currentSize + 'Changes'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + // Check if they also selected 'less' + if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/cigars/current/less-frequency') + } else { + moveToNextTobaccoType(req, res) + } +}) + +// LESS flow +router.post('/prototype_v3/tobacco/cigars/current/less-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigars/current/less-quantity') +}) + +router.post('/prototype_v3/tobacco/cigars/current/less-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigars/current/less-duration') +}) + +router.post('/prototype_v3/tobacco/cigars/current/less-duration-answer', (req, res) => { + moveToNextTobaccoType(req, res) +}) + +// ============================================ +// CIGARS ROUTING - FORMER +// ============================================ + +router.post('/prototype_v3/tobacco/cigars/former/years-smoked-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigars/former/frequency') +}) + +router.post('/prototype_v3/tobacco/cigars/former/frequency-answer', (req, res) => { + // CIGAR SIZE SPLIT: copies frequency to size-specific variable, skips cigar-size page (see MEMORY.md) + var currentSize = req.session.data['currentCigarSize'] + req.session.data['cigar' + currentSize + 'Frequency'] = req.session.data['cigarsFormerFrequency'] + res.redirect('/prototype_v3/tobacco/cigars/former/quantity') +}) + +// cigar-size-answer not used - CIGAR SIZE SPLIT (see MEMORY.md) +// router.post('/prototype_v3/tobacco/cigars/former/cigar-size-answer', ...) + +router.post('/prototype_v3/tobacco/cigars/former/quantity-answer', (req, res) => { + var currentSize = req.session.data['currentCigarSize'] + var fieldName = 'cigar' + currentSize + 'Quantity' + var quantity = req.session.data[fieldName] + + // Validate that quantity is at least 1 + if (!quantity || parseInt(quantity) < 1) { + req.session.data[fieldName + '-error'] = true + return res.redirect('/prototype_v3/tobacco/cigars/former/quantity') + } + + // Clear any previous errors + delete req.session.data[fieldName + '-error'] + + res.redirect('/prototype_v3/tobacco/cigars/former/has-quantity-changed') +}) + +router.post('/prototype_v3/tobacco/cigars/former/has-quantity-changed-answer', (req, res) => { + var changes = req.session.data['cigarsFormerChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('more')) { + res.redirect('/prototype_v3/tobacco/cigars/former/more-frequency') + } else if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/cigars/former/less-frequency') + } else { + moveToNextTobaccoType(req, res) + } +}) + +// MORE flow - FORMER +router.post('/prototype_v3/tobacco/cigars/former/more-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigars/former/more-quantity') +}) + +router.post('/prototype_v3/tobacco/cigars/former/more-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigars/former/more-duration') +}) + +router.post('/prototype_v3/tobacco/cigars/former/more-duration-answer', (req, res) => { + var currentSize = req.session.data['currentCigarSize'] + var changes = req.session.data['cigar' + currentSize + 'Changes'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/cigars/former/less-frequency') + } else { + moveToNextTobaccoType(req, res) + } +}) + +// LESS flow - FORMER +router.post('/prototype_v3/tobacco/cigars/former/less-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigars/former/less-quantity') +}) + +router.post('/prototype_v3/tobacco/cigars/former/less-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigars/former/less-duration') +}) + +router.post('/prototype_v3/tobacco/cigars/former/less-duration-answer', (req, res) => { + moveToNextTobaccoType(req, res) +}) + +// ============================================ +// "DO YOU CURRENTLY SMOKE" ROUTING - CIGARILLOS +// ============================================ + +router.post('/prototype_v3/tobacco/cigarillos/do-you-currently-smoke-answer', (req, res) => { + var currentlySmokesCigarillos = req.session.data['currentlySmokesCigarillos'] + + if (currentlySmokesCigarillos === 'Yes') { + res.redirect('/prototype_v3/tobacco/cigarillos/current/years-smoked') + } else { + res.redirect('/prototype_v3/tobacco/cigarillos/former/years-smoked') + } +}) + +// ============================================ +// CIGARILLOS ROUTING - CURRENT +// ============================================ + +router.post('/prototype_v3/tobacco/cigarillos/current/years-smoked-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarillos/current/frequency') +}) + +router.post('/prototype_v3/tobacco/cigarillos/current/frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarillos/current/quantity') +}) + +router.post('/prototype_v3/tobacco/cigarillos/current/quantity-answer', (req, res) => { + var quantity = req.session.data['cigarillosCurrentQuantity'] + + // Validate that quantity is at least 1 + if (!quantity || parseInt(quantity) < 1) { + req.session.data['cigarillosCurrentQuantity-error'] = true + return res.redirect('/prototype_v3/tobacco/cigarillos/current/quantity') + } + + // Clear any previous errors + delete req.session.data['cigarillosCurrentQuantity-error'] + + res.redirect('/prototype_v3/tobacco/cigarillos/current/has-quantity-changed') +}) + +router.post('/prototype_v3/tobacco/cigarillos/current/has-quantity-changed-answer', (req, res) => { + var changes = req.session.data['cigarillosCurrentChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('more')) { + res.redirect('/prototype_v3/tobacco/cigarillos/current/more-frequency') + } else if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/cigarillos/current/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// MORE flow +router.post('/prototype_v3/tobacco/cigarillos/current/more-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarillos/current/more-quantity') +}) + +router.post('/prototype_v3/tobacco/cigarillos/current/more-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarillos/current/more-duration') +}) + +router.post('/prototype_v3/tobacco/cigarillos/current/more-duration-answer', (req, res) => { + var changes = req.session.data['cigarillosCurrentChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + // Check if they also selected 'less' + if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/cigarillos/current/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// LESS flow +router.post('/prototype_v3/tobacco/cigarillos/current/less-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarillos/current/less-quantity') +}) + +router.post('/prototype_v3/tobacco/cigarillos/current/less-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarillos/current/less-duration') +}) + +router.post('/prototype_v3/tobacco/cigarillos/current/less-duration-answer', (req, res) => { + var changes = req.session.data['cigarillosCurrentChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + // Check if they also selected 'stopped' + if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + + +// ============================================ +// CIGARILLOS ROUTING - FORMER +// ============================================ + +router.post('/prototype_v3/tobacco/cigarillos/former/years-smoked-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarillos/former/frequency') +}) + +router.post('/prototype_v3/tobacco/cigarillos/former/frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarillos/former/quantity') +}) + +router.post('/prototype_v3/tobacco/cigarillos/former/quantity-answer', (req, res) => { + var quantity = req.session.data['cigarillosFormerQuantity'] + + // Validate that quantity is at least 1 + if (!quantity || parseInt(quantity) < 1) { + req.session.data['cigarillosFormerQuantity-error'] = true + return res.redirect('/prototype_v3/tobacco/cigarillos/former/quantity') + } + + // Clear any previous errors + delete req.session.data['cigarillosFormerQuantity-error'] + + res.redirect('/prototype_v3/tobacco/cigarillos/former/has-quantity-changed') +}) + +router.post('/prototype_v3/tobacco/cigarillos/former/has-quantity-changed-answer', (req, res) => { + var changes = req.session.data['cigarillosFormerChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('more')) { + res.redirect('/prototype_v3/tobacco/cigarillos/former/more-frequency') + } else if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/cigarillos/former/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// MORE flow - FORMER +router.post('/prototype_v3/tobacco/cigarillos/former/more-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarillos/former/more-quantity') +}) + +router.post('/prototype_v3/tobacco/cigarillos/former/more-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarillos/former/more-duration') +}) + +router.post('/prototype_v3/tobacco/cigarillos/former/more-duration-answer', (req, res) => { + var changes = req.session.data['cigarillosFormerChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('less')) { + res.redirect('/prototype_v3/tobacco/cigarillos/former/less-frequency') + } else if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + +// LESS flow - FORMER +router.post('/prototype_v3/tobacco/cigarillos/former/less-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarillos/former/less-quantity') +}) + +router.post('/prototype_v3/tobacco/cigarillos/former/less-quantity-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/cigarillos/former/less-duration') +}) + +router.post('/prototype_v3/tobacco/cigarillos/former/less-duration-answer', (req, res) => { + var changes = req.session.data['cigarillosFormerChanges'] + + if (!Array.isArray(changes)) { + changes = changes ? [changes] : [] + } + + if (changes.includes('stopped')) { + moveToNextTobaccoType(req, res) + } else { + moveToNextTobaccoType(req, res) + } +}) + + +// ============================================ +// "DO YOU CURRENTLY SMOKE" ROUTING - SHISHA +// ============================================ + +router.post('/prototype_v3/tobacco/shisha/do-you-currently-smoke-answer', (req, res) => { + var currentlySmokesShisha = req.session.data['currentlySmokesShisha'] + + if (currentlySmokesShisha === 'Yes') { + res.redirect('/prototype_v3/tobacco/shisha/current/years-smoked') + } else { + res.redirect('/prototype_v3/tobacco/shisha/former/years-smoked') + } +}) + +// ============================================ +// SHISHA ROUTING - CURRENT +// ============================================ + +router.post('/prototype_v3/tobacco/shisha/current/years-smoked-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/shisha/current/group-or-alone') +}) + +router.post('/prototype_v3/tobacco/shisha/current/group-or-alone-answer', (req, res) => { + var groupOrAlone = req.session.data['shishaCurrentGroupOrAlone'] + + // Convert to array if it's not already (handles single selection) + if (!Array.isArray(groupOrAlone)) { + groupOrAlone = [groupOrAlone] + } + + // Check if both are selected + var hasGroup = groupOrAlone.includes('Group') + var hasAlone = groupOrAlone.includes('Alone') + + if (hasGroup && hasAlone) { + // Both selected - go to group questions first, then alone + res.redirect('/prototype_v3/tobacco/shisha/current/group-frequency') + } else if (hasGroup) { + // Only group selected + res.redirect('/prototype_v3/tobacco/shisha/current/group-frequency') + } else if (hasAlone) { + // Only alone selected + res.redirect('/prototype_v3/tobacco/shisha/current/alone-frequency') + } +}) + +// GROUP flow +router.post('/prototype_v3/tobacco/shisha/current/group-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/shisha/current/group-quantity') +}) + +router.post('/prototype_v3/tobacco/shisha/current/group-quantity-answer', (req, res) => { + var groupOrAlone = req.session.data['shishaCurrentGroupOrAlone'] + + // Convert to array if it's not already + if (!Array.isArray(groupOrAlone)) { + groupOrAlone = [groupOrAlone] + } + + // If they smoke both group and alone, now ask about alone + if (groupOrAlone.includes('Group') && groupOrAlone.includes('Alone')) { + res.redirect('/prototype_v3/tobacco/shisha/current/alone-frequency') + } else { + moveToNextTobaccoType(req, res) + } +}) + +// ALONE flow +router.post('/prototype_v3/tobacco/shisha/current/alone-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/shisha/current/alone-quantity') +}) + +router.post('/prototype_v3/tobacco/shisha/current/alone-quantity-answer', (req, res) => { + moveToNextTobaccoType(req, res) +}) + +// ============================================ +// SHISHA ROUTING - FORMER +// ============================================ + +router.post('/prototype_v3/tobacco/shisha/former/years-smoked-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/shisha/former/group-or-alone') +}) + +router.post('/prototype_v3/tobacco/shisha/former/group-or-alone-answer', (req, res) => { + var groupOrAlone = req.session.data['shishaFormerGroupOrAlone'] + + // Convert to array if it's not already (handles single selection) + if (!Array.isArray(groupOrAlone)) { + groupOrAlone = [groupOrAlone] + } + + // Check if both are selected + var hasGroup = groupOrAlone.includes('Group') + var hasAlone = groupOrAlone.includes('Alone') + + if (hasGroup && hasAlone) { + // Both selected - go to group questions first, then alone + res.redirect('/prototype_v3/tobacco/shisha/former/group-frequency') + } else if (hasGroup) { + // Only group selected + res.redirect('/prototype_v3/tobacco/shisha/former/group-frequency') + } else if (hasAlone) { + // Only alone selected + res.redirect('/prototype_v3/tobacco/shisha/former/alone-frequency') + } +}) + +// GROUP flow - FORMER +router.post('/prototype_v3/tobacco/shisha/former/group-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/shisha/former/group-quantity') +}) + +router.post('/prototype_v3/tobacco/shisha/former/group-quantity-answer', (req, res) => { + var groupOrAlone = req.session.data['shishaFormerGroupOrAlone'] + + // Convert to array if it's not already + if (!Array.isArray(groupOrAlone)) { + groupOrAlone = [groupOrAlone] + } + + // If they smoked both group and alone, now ask about alone + if (groupOrAlone.includes('Group') && groupOrAlone.includes('Alone')) { + res.redirect('/prototype_v3/tobacco/shisha/former/alone-frequency') + } else { + moveToNextTobaccoType(req, res) + } +}) + +// ALONE flow - FORMER +router.post('/prototype_v3/tobacco/shisha/former/alone-frequency-answer', (req, res) => { + res.redirect('/prototype_v3/tobacco/shisha/former/alone-quantity') +}) + +router.post('/prototype_v3/tobacco/shisha/former/alone-quantity-answer', (req, res) => { + moveToNextTobaccoType(req, res) +}) + +// ============================================ +// INDEX-ALLPAGES - Clear all session data +// ============================================ + +router.get('/prototype_v3/index-allpages', (req, res) => { + // Clear ALL session data + req.session.data = {} + + // Render the page + res.render('prototype_v3/index-allpages') +}) + +// ============================================ +// CHECK YOUR ANSWERS - Calculate years smoked +// ============================================ + +router.get('/prototype_v3/check-your-answers', (req, res) => { + // Calculate years smoked if not explicitly entered + var currentYear = new Date().getFullYear() + var birthYear = req.session.data['dateOfBirth'] && req.session.data['dateOfBirth']['year'] + ? parseInt(req.session.data['dateOfBirth']['year']) + : 0 + var currentAge = birthYear > 0 ? currentYear - birthYear : 0 + var ageStarted = req.session.data['ageStartedSmoking'] + ? parseInt(req.session.data['ageStartedSmoking']) + : 0 + var periodsStopped = req.session.data['totalYearsStoppedSmoking'] + ? parseInt(req.session.data['totalYearsStoppedSmoking']) + : 0 + var ageQuit = req.session.data['formerSmokingQuitDate'] && req.session.data['formerSmokingQuitDate']['age'] + ? parseInt(req.session.data['formerSmokingQuitDate']['age']) + : 0 + + // Calculate based on smoker type + var calculatedYears = 0 + if (req.session.data['smokedRegularly'] === 'Yes-currently') { + calculatedYears = currentAge > 0 && ageStarted > 0 ? currentAge - ageStarted - periodsStopped : 0 + } else if (req.session.data['smokedRegularly'] === 'Yes-usedToRegularly') { + calculatedYears = ageQuit > 0 && ageStarted > 0 ? ageQuit - ageStarted - periodsStopped : 0 + } + + // Ensure non-negative + calculatedYears = Math.max(0, calculatedYears) + + // Only set calculated value for tobacco types that were actually selected + var selectedTobaccoTypes = req.session.data['tobaccoTypes'] || [] + + // Ensure it's an array + if (!Array.isArray(selectedTobaccoTypes)) { + selectedTobaccoTypes = selectedTobaccoTypes ? [selectedTobaccoTypes] : [] + } + + var isCurrent = req.session.data['smokedRegularly'] === 'Yes-currently' + + // Map tobacco type names to their data field names + var tobaccoTypeMapping = { + 'Cigarettes': isCurrent ? 'cigarettesCurrentYearsSmoked' : 'cigarettesFormerYearsSmoked', + 'Rolling tobacco': isCurrent ? 'rollingTobaccoCurrentYearsSmoked' : 'rollingTobaccoFormerYearsSmoked', + 'Pipe': isCurrent ? 'pipeCurrentYearsSmoked' : 'pipeFormerYearsSmoked', + 'Small cigars': isCurrent ? 'cigarsCurrentYearsSmoked' : 'cigarsFormerYearsSmoked', + 'Medium cigars': isCurrent ? 'cigarsCurrentYearsSmoked' : 'cigarsFormerYearsSmoked', + 'Large cigars': isCurrent ? 'cigarsCurrentYearsSmoked' : 'cigarsFormerYearsSmoked', + 'Cigarillos': isCurrent ? 'cigarillosCurrentYearsSmoked' : 'cigarillosFormerYearsSmoked', + 'Shisha': isCurrent ? 'shishaCurrentYearsSmoked' : 'shishaFormerYearsSmoked' + } + + // Only set years for selected tobacco types + selectedTobaccoTypes.forEach(function(tobaccoType) { + var fieldName = tobaccoTypeMapping[tobaccoType] + if (fieldName && !req.session.data[fieldName] && calculatedYears > 0) { + req.session.data[fieldName] = calculatedYears + } + }) + + // Render the page + res.render('prototype_v3/check-your-answers') +}) + +// ============================================ +// SKIP TO TOBACCO SECTION - CURRENT (FOR TESTING) +// ============================================ + +router.get('/prototype_v3/skip-to-tobacco', (req, res) => { + // CLEAR ALL SESSION DATA + req.session.data = {} + + // NOW SET THE PRE-FILLED DATA FOR TESTING + req.session.data['smokedRegularly'] = "Yes-currently" + + // Date of birth + req.session.data['dateOfBirth'] = { + day: "19", + month: "06", + year: "1965" + } + + // About you section + req.session.data['height'] = { + feet: "5", + inches: "10" + } + req.session.data['weight'] = { + kilograms: "80" + } + req.session.data['whatIsYourSex'] = "Male" + req.session.data['bestDescribe'] = "Male" + req.session.data['ethnicBackground'] = "White" + req.session.data['educationCompleted'] = "Bachelors degree" + + // Your health section + req.session.data['EverDiagnosedWith'] = ["Pneumonia"] + req.session.data['exposedAsbestos'] = "No" + req.session.data['livedWithAsbestosWorker'] = "No" + req.session.data['diagnosedCancer'] = "No" + + // Family history + req.session.data['relativesHaveCancer'] = "Yes" + req.session.data['relativeAge'] = "Yes" + + // Redirect to how-old-when-started-smoking with clean slate + res.redirect("/prototype_v3/how-old-when-started-smoking") +}) + +// ============================================ +// SKIP TO TOBACCO SECTION (FORMER SMOKER) +// ============================================ + +router.get('/prototype_v3/skip-to-tobacco-former', (req, res) => { + // CLEAR ALL SESSION DATA + req.session.data = {} + + // NOW SET THE PRE-FILLED DATA FOR TESTING + req.session.data['smokedRegularly'] = "Yes-usedToRegularly" + + // Date of birth + req.session.data['dateOfBirth'] = { + day: "19", + month: "06", + year: "1965" + } + + // About you section + req.session.data['height'] = { + feet: "5", + inches: "10" + } + req.session.data['weight'] = { + kilograms: "80" + } + req.session.data['whatIsYourSex'] = "Male" + req.session.data['bestDescribe'] = "Male" + req.session.data['ethnicBackground'] = "White" + req.session.data['educationCompleted'] = "Bachelors degree" + + // Your health section + req.session.data['EverDiagnosedWith'] = ["Pneumonia"] + req.session.data['exposedAsbestos'] = "No" + req.session.data['livedWithAsbestosWorker'] = "No" + req.session.data['diagnosedCancer'] = "No" + + // Family history + req.session.data['relativesHaveCancer'] = "Yes" + req.session.data['relativeAge'] = "Yes" + + // Redirect to how-old-when-started-smoking with clean slate + res.redirect("/prototype_v3/how-old-when-started-smoking") +}) + +module.exports = router