From 1c94dffa9f7eee5c27ae8b633f0c5df7c0af84aa Mon Sep 17 00:00:00 2001 From: Tedd Mason Date: Wed, 18 Sep 2024 07:57:53 +0100 Subject: [PATCH] SIR-949 - contact --- server/routes/__tests__/smell/contact.spec.js | 60 +++++++++++++++ server/routes/smell/contact.js | 75 +++++++++++++++++++ server/routes/smell/images-or-video.js | 15 ++++ server/utils/constants.js | 6 +- server/utils/question-sets.js | 15 ++++ 5 files changed, 170 insertions(+), 1 deletion(-) create mode 100644 server/routes/__tests__/smell/contact.spec.js create mode 100644 server/routes/smell/contact.js create mode 100644 server/routes/smell/images-or-video.js diff --git a/server/routes/__tests__/smell/contact.spec.js b/server/routes/__tests__/smell/contact.spec.js new file mode 100644 index 0000000..e66c0ce --- /dev/null +++ b/server/routes/__tests__/smell/contact.spec.js @@ -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') + }) + }) +}) diff --git a/server/routes/smell/contact.js b/server/routes/smell/contact.js new file mode 100644 index 0000000..ddfdc6b --- /dev/null +++ b/server/routes/smell/contact.js @@ -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 + } +] diff --git a/server/routes/smell/images-or-video.js b/server/routes/smell/images-or-video.js new file mode 100644 index 0000000..6e08a64 --- /dev/null +++ b/server/routes/smell/images-or-video.js @@ -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 + } +] diff --git a/server/utils/constants.js b/server/utils/constants.js index f23b5de..d752be9 100644 --- a/server/utils/constants.js +++ b/server/utils/constants.js @@ -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' @@ -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 = { diff --git a/server/utils/question-sets.js b/server/utils/question-sets.js index b78e9f3..30d9e7e 100644 --- a/server/utils/question-sets.js +++ b/server/utils/question-sets.js @@ -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' + } + } } } },