Skip to content

Commit

Permalink
Merge pull request #73 from DEFRA/581
Browse files Browse the repository at this point in the history
[581] generic summary controller
  • Loading branch information
DrogoNevets authored Dec 13, 2024
2 parents 914a9eb + 9ed11f1 commit 2ee3cde
Show file tree
Hide file tree
Showing 15 changed files with 137 additions and 109 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
/** @import SummaryPage from '../../model/page/summary-page/SummaryPageModel.js' */
/** @import { SectionModel } from "~/src/server/common/model/section/section-model/index.js" */
/** @import { Server, ServerRegisterPluginObject } from '@hapi/hapi' */

export class SummaryPageController {
/** @type {string} */
indexView = 'common/controller/summary-page-controller/index.njk'

/** @type {string} */
pageTitle

/** @type {string} */
heading

/**
* @param {SummaryPage} page
*/
constructor(page) {
this.page = page
}

get urlPath() {
return `/${this.page.sectionKey}/check-answers`
}

/** @type {SectionModel} */
Model

/** @returns {ServerRegisterPluginObject<void>} */
plugin() {
return {
plugin: {
name: `${this.page.sectionKey}-check-answers`,

/** @param {Server} server */
register: (server) => {
server.route([
{
method: 'GET',
path: this.urlPath,
handler: this.getHandler.bind(this)
},
{
method: 'POST',
path: this.urlPath,
handler: this.postHandler.bind(this)
}
])
}
}
}
}

getHandler(req, res) {
const section = this.page.sectionFactory(req.yar.get('origin'))

const { isValid, firstInvalidPage } = section.validate()
if (!isValid) {
return res.redirect(
`${firstInvalidPage?.urlPath}?redirect_uri=/${this.page.sectionKey}/check-answers`
)
}

const items = section.pages.map((visitedPage) => ({
key: visitedPage.question,
value: section[visitedPage.questionKey].html,
url: `${visitedPage.urlPath}?redirect_uri=/${this.page.sectionKey}/check-answers`,
visuallyHiddenKey: visitedPage.question,
attributes: {
'data-testid': `${visitedPage.questionKey}-change-link`
}
}))

return res.view(this.indexView, {
pageTitle: this.page.pageTitle,
heading: this.page.heading,
originSummary: items
})
}

postHandler(_req, res) {
return res.redirect('/task-list')
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,14 @@
})) %}
{% endfor %}



{{ govukSummaryList({
classes: "govuk-!-margin-bottom-9",
rows: summaryRows
}) }}

{% endmacro %}

{% block questions %}

{{ createSummary(originSummary) }}

{% endblock %}
3 changes: 3 additions & 0 deletions src/server/common/model/page/page-model.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
export class Page {
/** @type {string} */
urlPath

/** @type {string} */
sectionKey
}
3 changes: 0 additions & 3 deletions src/server/common/model/page/question-page-model.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,6 @@ export class QuestionPage extends Page {
/** @type {string} */
questionKey

/** @type {string} */
sectionKey

/** @type {string} */
view

Expand Down
16 changes: 16 additions & 0 deletions src/server/common/model/page/summary-page/SummaryPageModel.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Page } from '../page-model.js'

/** @import { SectionModel } from '~/src/server/common/model/section/section-model/index.js' */

class SummaryPage extends Page {
/** @type {(_data) => SectionModel} */
sectionFactory

/** @type {string} */
heading

/** @type {string} */
pageTitle
}

export default SummaryPage
Empty file.
4 changes: 1 addition & 3 deletions src/server/common/model/section/section-model/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
import { SectionModelUpdated } from './section-model-updated.js'

export const SectionModel = SectionModelUpdated
export { SectionModel } from './section-model-updated.js'
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { QuestionPage } from '../../page/question-page-model.js'
* @typedef {{ isValid: boolean, firstInvalidPage?: QuestionPage }} SectionValidation
*/

export class SectionModelUpdated {
export class SectionModel {
/** @type {SectionPayload} */
_data

Expand Down Expand Up @@ -74,7 +74,7 @@ export class SectionModelUpdated {

/**
* @param {object} _data
* @returns {SectionModelUpdated}
* @returns {SectionModel}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
static fromState(_data) {
Expand Down
4 changes: 2 additions & 2 deletions src/server/origin/address/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Address } from '../../common/model/answer/address.js'
import { QuestionPage } from '../../common/model/page/question-page-model.js'
import { summaryPage } from '../summary/index.js'
import { originSummaryPage } from '~/src/server/origin/summary/index.js'
import { QuestionPageController } from '../../common/controller/question-page-controller.js'

/** @import { ServerRegisterPluginObject } from '@hapi/hapi' */
Expand All @@ -20,7 +20,7 @@ export class OriginAddressPage extends QuestionPage {
/** @param {Address} _answer */
// eslint-disable-next-line @typescript-eslint/no-unused-vars
nextPage(_answer) {
return summaryPage
return originSummaryPage
}
}
export const originAddressPage = new OriginAddressPage()
Expand Down
4 changes: 2 additions & 2 deletions src/server/origin/address/index.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { address, originAddressPage, OriginAddressPage } from './index.js'
import { summaryPage } from '../summary/index.js'
import { originSummaryPage } from '~/src/server/origin/summary/index.js'
import { Address } from '../../common/model/answer/address.js'

const sectionKey = 'origin'
Expand Down Expand Up @@ -42,7 +42,7 @@ describe('OriginAddressPage', () => {

it('nextPage should return summaryPage', () => {
const nextPage = page.nextPage()
expect(nextPage).toBe(summaryPage)
expect(nextPage).toBe(originSummaryPage)
})

it('should export page', () => {
Expand Down
51 changes: 0 additions & 51 deletions src/server/origin/summary/controller.js

This file was deleted.

19 changes: 9 additions & 10 deletions src/server/origin/summary/controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@ import { createServer } from '~/src/server/index.js'
import { statusCodes } from '~/src/server/common/constants/status-codes.js'
import { withCsrfProtection } from '~/src/server/common/test-helpers/csrf.js'
import { parseDocument } from '~/src/server/common/test-helpers/dom.js'

import { pageTitle } from './controller.js'
import SesessionTestHelper from '../../common/test-helpers/session-helper.js'
import { originSummaryPage } from './index.js'

describe('#originSummaryController', () => {
/** @type {Server} */
Expand Down Expand Up @@ -45,7 +44,7 @@ describe('#originSummaryController', () => {
withCsrfProtection(
{
method: 'GET',
url: '/origin/summary'
url: '/origin/check-answers'
},
{
Cookie: session.sessionID
Expand All @@ -54,7 +53,7 @@ describe('#originSummaryController', () => {
)

const document = parseDocument(payload)
expect(document.title).toBe(pageTitle)
expect(document.title).toBe(originSummaryPage.pageTitle)
expect(statusCode).toBe(statusCodes.ok)

expect(payload).toEqual(expect.stringContaining('12/123/1234'))
Expand All @@ -77,7 +76,7 @@ describe('#originSummaryController', () => {
withCsrfProtection(
{
method: 'GET',
url: '/origin/summary'
url: '/origin/check-answers'
},
{
Cookie: session.sessionID
Expand All @@ -86,7 +85,7 @@ describe('#originSummaryController', () => {
)

expect(headers.location).toBe(
'/origin/to-or-from-own-premises?redirect_uri=/origin/summary'
'/origin/to-or-from-own-premises?redirect_uri=/origin/check-answers'
)
expect(statusCode).toBe(statusCodes.redirect)
})
Expand All @@ -101,7 +100,7 @@ describe('#originSummaryController', () => {
withCsrfProtection(
{
method: 'GET',
url: '/origin/summary'
url: '/origin/check-answers'
},
{
Cookie: session.sessionID
Expand All @@ -110,7 +109,7 @@ describe('#originSummaryController', () => {
)

expect(headers.location).toBe(
'/origin/address?redirect_uri=/origin/summary'
'/origin/address?redirect_uri=/origin/check-answers'
)
expect(statusCode).toBe(statusCodes.redirect)
})
Expand All @@ -125,7 +124,7 @@ describe('#originSummaryController', () => {
withCsrfProtection(
{
method: 'GET',
url: '/origin/summary'
url: '/origin/check-answers'
},
{
Cookie: session.sessionID
Expand All @@ -134,7 +133,7 @@ describe('#originSummaryController', () => {
)

expect(headers.location).toBe(
'/origin/cph-number?redirect_uri=/origin/summary'
'/origin/cph-number?redirect_uri=/origin/check-answers'
)
expect(statusCode).toBe(statusCodes.redirect)
})
Expand Down
45 changes: 14 additions & 31 deletions src/server/origin/summary/index.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,24 @@
import { Page } from '../../common/model/page/page-model.js'
import {
originSummaryGetController,
originSummaryPostController
} from './controller.js'
import SummaryPage from '../../common/model/page/summary-page/SummaryPageModel.js'
import { SummaryPageController } from '../../common/controller/summary-page-controller/SummaryPageController.js'

/**
* Sets up the routes used in the summary page.
* These routes are registered in src/server/router.js.
*/
import { Origin } from '~/src/server/common/model/section/origin.js'

export class SummaryPage extends Page {
urlPath = '/origin/summary'
export class OriginSummaryPage extends SummaryPage {
pageTitle = 'Check your answers before you continue your application'
heading = 'Check your answers before you continue your application'
sectionKey = 'origin'
urlPath = `/${this.sectionKey}/check-answers`
sectionFactory = (data) => Origin.fromState(data)
}
export const summaryPage = new SummaryPage()

export const originSummaryPage = new OriginSummaryPage()

/**
* @satisfies {ServerRegisterPluginObject<void>}
*/
export const originSummary = {
plugin: {
name: 'origin-summary',
register(server) {
server.route([
{
method: 'GET',
path: summaryPage.urlPath,
...originSummaryGetController
},
{
method: 'POST',
path: summaryPage.urlPath,
...originSummaryPostController
}
])
}
}
}
export const originSummary = new SummaryPageController(
new OriginSummaryPage()
).plugin()

/**
* @import { ServerRegisterPluginObject } from '@hapi/hapi'
Expand Down
2 changes: 1 addition & 1 deletion src/server/task-list/controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const taskListGetController = {
title: 'Movement origin',
initialLink:
originValidity.firstInvalidPage?.urlPath ?? origin.firstPage.urlPath,
summaryLink: 'origin/summary',
summaryLink: 'origin/check-answers',
isValid: isOriginValid,
isEnabled: true
})
Expand Down
Loading

0 comments on commit 2ee3cde

Please sign in to comment.