Skip to content

Commit

Permalink
Technical implementation for SIR-1216
Browse files Browse the repository at this point in the history
  • Loading branch information
sujithvg committed Jan 17, 2025
1 parent 738dd6b commit dfb3539
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -99,18 +99,6 @@ describe(url, () => {
otherDetails: 'something else'
}])
})
it('Happy: accepts empty answerId, defaults to you do not know and redirects to WATER_POLLUTION_POLLUTION_APPEARANCE', async () => {
const options = {
url,
payload: {}
}
const response = await submitPostRequest(options)
expect(response.headers.location).toEqual(constants.routes.WATER_POLLUTION_POLLUTION_APPEARANCE)
expect(response.request.yar.get(constants.redisKeys.WATER_POLLUTION_POLLUTION_SUBSTANCE)).toEqual([{
...baseAnswer,
answerId: question.answers.unknown.answerId
}])
})
it('Happy: Redirects to referer when set', async () => {
const answerId = question.answers.sewage.answerId.toString()
const options = {
Expand All @@ -128,5 +116,14 @@ describe(url, () => {
answerId: question.answers.sewage.answerId
}])
})
it('Sad: errors on no answerId', async () => {
const options = {
url,
payload: {}
}
const response = await submitPostRequest(options, constants.statusCodes.OK)
expect(response.payload).toContain('There is a problem')
expect(response.payload).toContain('Select what you think the pollution is, or 'you do not know'')
})
})
})
51 changes: 31 additions & 20 deletions server/routes/water-pollution/pollution-substance.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import constants from '../../utils/constants.js'
import { getErrorSummary } from '../../utils/helpers.js'
import { questionSets } from '../../utils/question-sets.js'
const question = questionSets.WATER_POLLUTION.questions.WATER_POLLUTION_POLLUTION_SUBSTANCE

Expand All @@ -16,6 +17,16 @@ const handlers = {
// get payload
let { answerId, somethingElseDetail } = request.payload

// validate payload for errors
const errorSummary = validatePayload(answerId)
if (errorSummary.errorList.length > 0) {
request.yar.set(question.key, [])
return h.view(constants.views.WATER_POLLUTION_POLLUTION_SUBSTANCE, {
errorSummary,
...getContext(request)
})
}

// Convert answer to array if only a single string answer
if (!Array.isArray(answerId)) {
answerId = [answerId]
Expand All @@ -30,30 +41,19 @@ const handlers = {

const buildAnswers = (answerId, somethingElseDetail) => {
const answers = []
let somethingElse = false
// if no answer selected default to You do not know
if (answerId.length === 1 && !answerId[0]) {
answerId.forEach(item => {
answers.push({
...baseAnswer,
answerId: question.answers.unknown.answerId
answerId: Number(item)
})
} else {
answerId.forEach(item => {
if (Number(item) === question.answers.somethingElse.answerId) {
somethingElse = true
}
answers.push({
...baseAnswer,
answerId: Number(item)
})
})

if (answerId.indexOf(question.answers.somethingElse.answerId.toString()) > -1 && somethingElseDetail) {
answers.push({
...baseAnswer,
answerId: question.answers.somethingElseDetail.answerId,
otherDetails: somethingElseDetail
})
if (somethingElse && somethingElseDetail) {
answers.push({
...baseAnswer,
answerId: question.answers.somethingElseDetail.answerId,
otherDetails: somethingElseDetail
})
}
}

return answers
Expand All @@ -67,6 +67,17 @@ const getContext = request => {
}
}

const validatePayload = answerId => {
const errorSummary = getErrorSummary()
if (!answerId || answerId.length === 0) {
errorSummary.errorList.push({
text: 'Select what you think the pollution is, or \'you do not know\'',
href: '#answerId'
})
}
return errorSummary
}

export default [
{
method: 'GET',
Expand Down
3 changes: 2 additions & 1 deletion server/utils/question-sets.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const YOU_DO_NOT_KNOW = 'You do not know'
const SOMETHING_ELSE = 'Something else'
const NONE_OF_THESE = 'None of these'
const YOU_DO_NOT_KNOW_SHORT = 'Don\'t know'
const NOT_GIVEN = 'Not given'

const questionSets = {
WATER_POLLUTION: {
Expand Down Expand Up @@ -323,7 +324,7 @@ const questionSets = {
unknown: {
answerId: 2906,
text: YOU_DO_NOT_KNOW,
shortText: YOU_DO_NOT_KNOW_SHORT
shortText: NOT_GIVEN
},
somethingElseDetail: {
answerId: 2907,
Expand Down

0 comments on commit dfb3539

Please sign in to comment.