Skip to content

Commit

Permalink
Technical implementation for SIR-945
Browse files Browse the repository at this point in the history
  • Loading branch information
sujithvg committed Sep 24, 2024
1 parent 6edc507 commit a18e811
Show file tree
Hide file tree
Showing 6 changed files with 219 additions and 2 deletions.
97 changes: 97 additions & 0 deletions server/routes/__tests__/smell/clothing-and-hair.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import { submitGetRequest, submitPostRequest } from '../../../__test-helpers__/server.js'
import { questionSets } from '../../../utils/question-sets.js'
import constants from '../../../utils/constants.js'

const url = constants.routes.SMELL_CLOTHING_AND_HAIR
const question = questionSets.SMELL.questions.SMELL_CLOTHING_AND_HAIR
const header = question.text
const baseAnswer = {
questionId: question.questionId,
questionAsked: question.text,
questionResponse: true
}

describe(url, () => {
describe('GET', () => {
it(`Should return success response and correct view for ${url} if current smell`, async () => {
const sessionData = {
'smell/indoors': [{
questionId: questionSets.SMELL.questions.SMELL_INDOORS.questionId,
answerId: questionSets.SMELL.questions.SMELL_INDOORS.answers.yes.answerId
}]
}
await submitGetRequest({ url }, header, constants.statusCodes.OK, sessionData)
})
it(`Should return success response and correct view for ${url} if past smell`, async () => {
const sessionData = {
'smell/indoors': [{
questionId: questionSets.SMELL.questions.SMELL_INDOORS.questionId,
answerId: questionSets.SMELL.questions.SMELL_INDOORS.answers.no.answerId
}]
}
const wasHeader = header.replace('Does', 'Did')
await submitGetRequest({ url }, wasHeader, constants.statusCodes.OK, sessionData)
})
})

describe('POST', () => {
it('Happy: Yes and continues to smell/effect-on-daily-life', async () => {
const options = {
url,
payload: {
answerId: question.answers.yes.answerId
}
}
const response = await submitPostRequest(options)
expect(response.headers.location).toEqual(constants.routes.SMELL_EFFECT_ON_DAILY_LIFE)
expect(response.request.yar.get(constants.redisKeys.SMELL_CLOTHING_AND_HAIR)).toEqual([{
...baseAnswer,
answerId: question.answers.yes.answerId
}])
})
it('Happy: No and continues to smell/effect-on-daily-life', async () => {
const options = {
url,
payload: {
answerId: question.answers.no.answerId
}
}
const response = await submitPostRequest(options)
expect(response.headers.location).toEqual(constants.routes.SMELL_EFFECT_ON_DAILY_LIFE)
expect(response.request.yar.get(constants.redisKeys.SMELL_CLOTHING_AND_HAIR)).toEqual([{
...baseAnswer,
answerId: question.answers.no.answerId
}])
})
it('Sad: Rejects empty payload with current smell is noticeable indoors', async () => {
const sessionData = {
'smell/indoors': [{
questionId: questionSets.SMELL.questions.SMELL_INDOORS.questionId,
answerId: questionSets.SMELL.questions.SMELL_INDOORS.answers.yes.answerId
}]
}
const options = {
url,
payload: {}
}
const response = await submitPostRequest(options, constants.statusCodes.OK, sessionData)
expect(response.payload).toContain('There is a problem')
expect(response.payload).toContain('Select yes if the smell sticks to your clothing or hair')
})
it('Sad: Rejects empty payload with current smell is not noticeable indoors', async () => {
const sessionData = {
'smell/indors': [{
questionId: questionSets.SMELL.questions.SMELL_INDOORS.questionId,
answerId: questionSets.SMELL.questions.SMELL_INDOORS.answers.no.answerId
}]
}
const options = {
url,
payload: {}
}
const response = await submitPostRequest(options, constants.statusCodes.OK, sessionData)
expect(response.payload).toContain('There is a problem')
expect(response.payload).toContain('Select yes if the smell stuck to your clothing or hair')
})
})
})
71 changes: 69 additions & 2 deletions server/routes/smell/clothing-and-hair.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,82 @@
import constants from '../../utils/constants.js'
import { questionSets } from '../../utils/question-sets.js'
import { getErrorSummary } from '../../utils/helpers.js'

const question = questionSets.SMELL.questions.SMELL_CLOTHING_AND_HAIR

const baseAnswer = {
questionId: question.questionId,
questionAsked: question.text,
questionResponse: true
}

const handlers = {
get: (_request, _h) => {
return 'hello world'
get: async (request, h) => {
return h.view(constants.views.SMELL_CLOTHING_AND_HAIR, {
...getContext(request)
})
},
post: async (request, h) => {
// get payload
let { answerId } = request.payload
const { current } = getContext(request)

// validate payload for errors
const errorSummary = validatePayload(answerId, current)
if (errorSummary.errorList.length > 0) {
return h.view(constants.views.SMELL_CLOTHING_AND_HAIR, {
question,
current,
errorSummary
})
}

// convert answerId to number
answerId = Number(answerId)

// set answer in session
request.yar.set(constants.redisKeys.SMELL_CLOTHING_AND_HAIR, buildAnswers(answerId))

return h.redirect(constants.routes.SMELL_EFFECT_ON_DAILY_LIFE)
}
}

const getContext = request => {
const currentAnswer = request.yar.get(constants.redisKeys.SMELL_INDOORS)
const current = currentAnswer && currentAnswer[0].answerId === questionSets.SMELL.questions.SMELL_INDOORS.answers.yes.answerId
return {
question,
current
}
}

const validatePayload = (answerId, current) => {
const errorSummary = getErrorSummary()
if (!answerId) {
errorSummary.errorList.push({
text: `Select yes if the smell ${current ? 'sticks' : 'stuck'} to your clothing or hair`,
href: '#answerId'
})
}
return errorSummary
}

const buildAnswers = answerId => {
return [{
...baseAnswer,
answerId
}]
}

export default [
{
method: 'GET',
path: constants.routes.SMELL_CLOTHING_AND_HAIR,
handler: handlers.get
},
{
method: 'POST',
path: constants.routes.SMELL_CLOTHING_AND_HAIR,
handler: handlers.post
}
]
15 changes: 15 additions & 0 deletions server/routes/smell/effect-on-daily-life.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import constants from '../../utils/constants.js'

const handlers = {
get: (_request, _h) => {
return 'hello world'
}
}

export default [
{
method: 'GET',
path: constants.routes.SMELL_EFFECT_ON_DAILY_LIFE,
handler: handlers.get
}
]
2 changes: 2 additions & 0 deletions server/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const SMELL_CURRENT = 'smell/current'
const SMELL_SMELL_STRENGTH = 'smell/smell-strength'
const SMELL_INDOORS = 'smell/indoors'
const SMELL_CLOTHING_AND_HAIR = 'smell/clothing-and-hair'
const SMELL_EFFECT_ON_DAILY_LIFE = 'smell/effect-on-daily-life'
const SMELL_SOURCE = 'smell/source'
const SMELL_REPORT_LOCAL_COUNCIL = 'smell/report-local-council'
const SMELL_CONTACT_LOCAL_COUNCIL = 'smell/contact-local-council'
Expand Down Expand Up @@ -110,6 +111,7 @@ const views = {
SMELL_SMELL_STRENGTH,
SMELL_INDOORS,
SMELL_CLOTHING_AND_HAIR,
SMELL_EFFECT_ON_DAILY_LIFE,
SMELL_CONTACT,
SMELL_IMAGES_OR_VIDEO,
SMELL_OTHER_INFORMATION
Expand Down
15 changes: 15 additions & 0 deletions server/utils/question-sets.js
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,21 @@ const questionSets = {
}
}
},
SMELL_CLOTHING_AND_HAIR: {
questionId: 500,
key: constants.redisKeys.SMELL_CLOTHING_AND_HAIR,
text: 'Does the smell stick to your clothing or hair?',
answers: {
yes: {
answerId: 501,
text: 'Yes'
},
no: {
answerId: 502,
text: 'No'
}
}
},
SMELL_CONTACT: {
questionId: 1,
key: constants.redisKeys.SMELL_CONTACT,
Expand Down
21 changes: 21 additions & 0 deletions server/views/smell/clothing-and-hair.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{% extends 'form-layout.html' %}

{% set title = (question.text if current else question.text.replace('Does', 'Did')) %}
{% set pageTitle = title.replace('?', '') %}

{% set items = [
{
value: question.answers.yes.answerId,
text: question.answers.yes.text
},
{
value: question.answers.no.answerId,
text: question.answers.no.text
}
]%}

{% block formContent %}

{% include "partials/radios.html" %}

{% endblock %}

0 comments on commit a18e811

Please sign in to comment.