Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SIR-949 - contact #177

Merged
merged 1 commit into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions server/routes/__tests__/smell/contact.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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_CONTACT
const question = questionSets.SMELL.questions.SMELL_CONTACT
const baseAnswer = {
questionId: question.questionId,
questionAsked: question.text,
questionResponse: true
}

describe(url, () => {
describe('GET', () => {
it(`Should return success response and correct view for ${url}`, async () => {
await submitGetRequest({ url }, question.text)
})
})
describe('POST', () => {
it('Should accept yes option and redirect to smell/images-or-video', async () => {
const answerId = question.answers.yes.answerId
const options = {
url,
payload: {
answerId
}
}
const response = await submitPostRequest(options)
expect(response.headers.location).toEqual(constants.routes.SMELL_IMAGES_OR_VIDEO)
expect(response.request.yar.get(constants.redisKeys.SMELL_CONTACT)).toEqual([{
...baseAnswer,
answerId
}])
})
it('Should accept no and redirect to smell/images-or-video', async () => {
const answerId = question.answers.no.answerId
const options = {
url,
payload: {
answerId
}
}
const response = await submitPostRequest(options)
expect(response.headers.location).toEqual(constants.routes.SMELL_IMAGES_OR_VIDEO)
expect(response.request.yar.get(constants.redisKeys.SMELL_CONTACT)).toEqual([{
...baseAnswer,
answerId
}])
})
it('Sad: no radio selected, returns error state', 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 yes if we can contact you')
})
})
})
75 changes: 75 additions & 0 deletions server/routes/smell/contact.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import constants from '../../utils/constants.js'
import { getErrorSummary } from '../../utils/helpers.js'
import { questionSets } from '../../utils/question-sets.js'

const sharedYesNo = 'shared/yes-no'
const question = questionSets.SMELL.questions.SMELL_CONTACT

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

const handlers = {
get: async (_request, h) => {
return h.view(sharedYesNo, {
...getContext()
})
},
post: async (request, h) => {
let { answerId } = request.payload

// validate payload
const errorSummary = validatePayload(answerId)
if (errorSummary.errorList.length > 0) {
return h.view(sharedYesNo, {
...getContext(),
errorSummary
})
}
// convert answerId to number
answerId = Number(answerId)

request.yar.set(constants.redisKeys.SMELL_CONTACT, buildAnswers(answerId))

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

const getContext = () => {
return {
question
}
}

const validatePayload = answerId => {
const errorSummary = getErrorSummary()
if (!answerId) {
errorSummary.errorList.push({
text: 'Select yes if we can contact you',
href: '#answerId'
})
}
return errorSummary
}

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

export default [
{
method: 'GET',
path: constants.routes.SMELL_CONTACT,
handler: handlers.get
},
{
method: 'POST',
path: constants.routes.SMELL_CONTACT,
handler: handlers.post
}
]
15 changes: 15 additions & 0 deletions server/routes/smell/images-or-video.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_IMAGES_OR_VIDEO,
handler: handlers.get
}
]
6 changes: 5 additions & 1 deletion server/utils/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ const SMELL_SOURCE = 'smell/source'
const SMELL_REPORT_LOCAL_COUNCIL = 'smell/report-local-council'
const SMELL_CONTACT_LOCAL_COUNCIL = 'smell/contact-local-council'
const SMELL_SOURCE_DETAILS = 'smell/source-details'
const SMELL_CONTACT = 'smell/contact'
const SMELL_IMAGES_OR_VIDEO = 'smell/images-or-video'

// Meta data
const SUBMISSION_TIMESTAMP = 'submission-timestamp'
Expand Down Expand Up @@ -106,7 +108,9 @@ const views = {
SMELL_CURRENT,
SMELL_SMELL_STRENGTH,
SMELL_INDOORS,
SMELL_CLOTHING_AND_HAIR
SMELL_CLOTHING_AND_HAIR,
SMELL_CONTACT,
SMELL_IMAGES_OR_VIDEO
}

const routes = {
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 @@ -516,6 +516,21 @@ const questionSets = {
text: 'No, you can only smell it outside'
}
}
},
SMELL_CONTACT: {
questionId: 1,
key: constants.redisKeys.SMELL_CONTACT,
text: 'Can we contact you for more information if needed?',
answers: {
yes: {
answerId: 2,
text: 'Yes'
},
no: {
answerId: 3,
text: 'No'
}
}
}
}
},
Expand Down
Loading