Skip to content
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
35 changes: 31 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
"license": "SEE LICENSE IN LICENSE",
"dependencies": {
"@aws-sdk/client-sns": "^3.965.0",
"@defra/forms-engine-plugin": "^4.0.34",
"@defra/forms-engine-plugin": "^4.0.35",
"@defra/forms-model": "^3.0.601",
"@defra/hapi-tracing": "^1.30.0",
"@elastic/ecs-pino-format": "^1.5.0",
Expand Down
1 change: 1 addition & 0 deletions src/server/constants.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export const PREVIEW_PATH_PREFIX = '/preview'
export const ERROR_PREVIEW_PATH_PREFIX = '/error-preview'
export const FORM_PREFIX = '/form'
export const SAVE_AND_EXIT_PAYLOAD = 'SAVE_AND_EXIT_PAYLOAD'
34 changes: 33 additions & 1 deletion src/server/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { type Server } from '@hapi/hapi'
import { StatusCodes } from 'http-status-codes'

import { FORM_PREFIX } from '~/src/server/constants.js'
import { createServer } from '~/src/server/index.js'
import { configureEnginePlugin, createServer } from '~/src/server/index.js'
import {
getFormDefinition,
getFormMetadata
Expand Down Expand Up @@ -488,4 +488,36 @@ describe('Model cache', () => {
expect(res.statusCode).toBe(StatusCodes.OK)
})
})

describe('configureEnginePlugin', () => {
const mockFlash = jest.fn()
const mockYar = {
flash: mockFlash
}
const mockRequest = /** @type {any} */ {
params: {
slug: 'test-form'
},
yar: mockYar,
url: {
pathname: '/my-path'
}
}
const mockH = {
redirect: jest.fn()
}
test('should handle save and exit', async () => {
const [pluginObject] = await configureEnginePlugin({})
expect(pluginObject).toBeDefined()
const saveAndExitFunc = pluginObject.options.saveAndExit
expect(saveAndExitFunc).toBeDefined()
saveAndExitFunc(mockRequest, mockH, undefined)
expect(mockFlash).toHaveBeenCalledWith(
'SAVE_AND_EXIT_PAYLOAD',
{ action: undefined, crumb: undefined, __currentPagePath: '/my-path' },
true
)
expect(mockH.redirect).toHaveBeenCalledWith('/save-and-exit/test-form')
})
})
})
17 changes: 14 additions & 3 deletions src/server/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { join, parse } from 'path'

import plugin from '@defra/forms-engine-plugin'
import plugin, { CURRENT_PAGE_PATH_KEY } from '@defra/forms-engine-plugin'
import { checkFormStatus } from '@defra/forms-engine-plugin/engine/helpers.js'
import { FormModel } from '@defra/forms-engine-plugin/engine/models/FormModel.js'
import { formSubmissionService } from '@defra/forms-engine-plugin/services/index.js'
Expand Down Expand Up @@ -29,7 +29,7 @@ import forwardLogs from '~/src/server/common/helpers/logging/forward-logs.js'
import { requestLogger } from '~/src/server/common/helpers/logging/request-logger.js'
import { requestTracing } from '~/src/server/common/helpers/logging/request-tracing.js'
import { buildRedisClient } from '~/src/server/common/helpers/redis-client.js'
import { FORM_PREFIX } from '~/src/server/constants.js'
import { FORM_PREFIX, SAVE_AND_EXIT_PAYLOAD } from '~/src/server/constants.js'
import { FeedbackPageController } from '~/src/server/plugins/FeedbackPageController.js'
import { SummaryPageWithConfirmationEmailController } from '~/src/server/plugins/SummaryPageWithConfirmationEmailController.js'
import { configureBlankiePlugin } from '~/src/server/plugins/blankie.js'
Expand Down Expand Up @@ -159,10 +159,21 @@ export const configureEnginePlugin = async ({
h: FormResponseToolkit,
_context: FormContext
) => {
const { params } = request
const { params, yar } = request
const { slug } = params
const { isPreview, state } = checkFormStatus(params)

// Payload from current page without crumb and action
// (in case current page is not saved to state yet i.e. only partially completed)
const pagePayload = {
...request.payload,
crumb: undefined,
action: undefined,
[CURRENT_PAGE_PATH_KEY]: request.url.pathname
}

yar.flash(SAVE_AND_EXIT_PAYLOAD, pagePayload, true)

return h.redirect(
!isPreview
? `/save-and-exit/${slug}`
Expand Down
14 changes: 14 additions & 0 deletions src/server/routes/save-and-exit-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { SAVE_AND_EXIT_PAYLOAD } from '~/src/server/constants.js'

/**
* Get Save and Exit payload - extracted here to a separate file so it can easily be mocked
* @param {Request<{ Params: SaveAndExitParams }>} request
*/
export function getPayloadFromFlash(request) {
return request.yar.flash(SAVE_AND_EXIT_PAYLOAD)
}

/**
* @import { Request } from '@hapi/hapi'
* @import { SaveAndExitParams } from '~/src/server/models/save-and-exit.js'
*/
Loading
Loading