@defra/forms-engine-plugin 1.3.1 → 1.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/.server/server/plugins/engine/index.js +1 -1
  2. package/.server/server/plugins/engine/index.js.map +1 -1
  3. package/.server/server/plugins/engine/models/SummaryViewModel.d.ts +1 -0
  4. package/.server/server/plugins/engine/models/SummaryViewModel.js +1 -0
  5. package/.server/server/plugins/engine/models/SummaryViewModel.js.map +1 -1
  6. package/.server/server/plugins/engine/options.js +4 -1
  7. package/.server/server/plugins/engine/options.js.map +1 -1
  8. package/.server/server/plugins/engine/options.test.js +20 -0
  9. package/.server/server/plugins/engine/options.test.js.map +1 -1
  10. package/.server/server/plugins/engine/pageControllers/QuestionPageController.d.ts +5 -0
  11. package/.server/server/plugins/engine/pageControllers/QuestionPageController.js +27 -1
  12. package/.server/server/plugins/engine/pageControllers/QuestionPageController.js.map +1 -1
  13. package/.server/server/plugins/engine/pageControllers/StartPageController.d.ts +2 -0
  14. package/.server/server/plugins/engine/pageControllers/StartPageController.js +3 -0
  15. package/.server/server/plugins/engine/pageControllers/StartPageController.js.map +1 -1
  16. package/.server/server/plugins/engine/pageControllers/SummaryPageController.d.ts +1 -0
  17. package/.server/server/plugins/engine/pageControllers/SummaryPageController.js +4 -0
  18. package/.server/server/plugins/engine/pageControllers/SummaryPageController.js.map +1 -1
  19. package/.server/server/plugins/engine/plugin.js +5 -2
  20. package/.server/server/plugins/engine/plugin.js.map +1 -1
  21. package/.server/server/plugins/engine/routes/exit.d.ts +46 -0
  22. package/.server/server/plugins/engine/routes/exit.js +36 -0
  23. package/.server/server/plugins/engine/routes/exit.js.map +1 -0
  24. package/.server/server/plugins/engine/types.d.ts +6 -2
  25. package/.server/server/plugins/engine/types.js.map +1 -1
  26. package/.server/server/plugins/engine/views/exit.html +31 -0
  27. package/.server/server/plugins/engine/views/partials/form.html +17 -6
  28. package/.server/server/routes/types.d.ts +2 -1
  29. package/.server/server/routes/types.js +1 -0
  30. package/.server/server/routes/types.js.map +1 -1
  31. package/.server/server/schemas/index.js +1 -1
  32. package/.server/server/schemas/index.js.map +1 -1
  33. package/.server/server/services/cacheService.d.ts +2 -0
  34. package/.server/server/services/cacheService.js +9 -5
  35. package/.server/server/services/cacheService.js.map +1 -1
  36. package/package.json +1 -1
  37. package/src/server/index.test.ts +39 -0
  38. package/src/server/plugins/engine/components/helpers.test.ts +31 -0
  39. package/src/server/plugins/engine/index.ts +1 -3
  40. package/src/server/plugins/engine/models/SummaryViewModel.test.ts +59 -0
  41. package/src/server/plugins/engine/models/SummaryViewModel.ts +1 -0
  42. package/src/server/plugins/engine/options.js +4 -1
  43. package/src/server/plugins/engine/options.test.js +20 -0
  44. package/src/server/plugins/engine/pageControllers/PageController.test.ts +25 -0
  45. package/src/server/plugins/engine/pageControllers/QuestionPageController.test.ts +178 -1
  46. package/src/server/plugins/engine/pageControllers/QuestionPageController.ts +28 -1
  47. package/src/server/plugins/engine/pageControllers/StartPageController.ts +4 -0
  48. package/src/server/plugins/engine/pageControllers/SummaryPageController.ts +5 -0
  49. package/src/server/plugins/engine/plugin.ts +5 -1
  50. package/src/server/plugins/engine/routes/exit.ts +47 -0
  51. package/src/server/plugins/engine/types.ts +10 -4
  52. package/src/server/plugins/engine/views/exit.html +31 -0
  53. package/src/server/plugins/engine/views/partials/form.html +17 -6
  54. package/src/server/routes/types.ts +2 -1
  55. package/src/server/schemas/index.ts +2 -1
  56. package/src/server/services/cacheService.test.ts +45 -0
  57. package/src/server/services/cacheService.ts +20 -9
@@ -1 +1 @@
1
- {"version":3,"file":"plugin.js","names":["validatePluginOptions","getRoutes","getFileUploadStatusRoutes","makeLoadFormPreHandler","getQuestionRoutes","getRepeaterItemDeleteRoutes","getRepeaterSummaryRoutes","registerVision","CacheService","plugin","name","dependencies","multiple","register","server","options","model","cacheName","keyGenerator","sessionHydrator","nunjucks","nunjucksOptions","viewContext","preparePageEventRequestOptions","cacheService","expose","baseLayoutPath","app","itemCache","Map","models","loadFormPreHandler","getRouteOptions","pre","method","postRouteOptions","payload","parse","routes","route"],"sources":["../../../../src/server/plugins/engine/plugin.ts"],"sourcesContent":["import {\n type Lifecycle,\n type Plugin,\n type RouteOptions,\n type Server,\n type ServerRoute\n} from '@hapi/hapi'\n\nimport { type FormModel } from '~/src/server/plugins/engine/models/index.js'\nimport { validatePluginOptions } from '~/src/server/plugins/engine/options.js'\nimport { getRoutes as getFileUploadStatusRoutes } from '~/src/server/plugins/engine/routes/file-upload.js'\nimport { makeLoadFormPreHandler } from '~/src/server/plugins/engine/routes/index.js'\nimport { getRoutes as getQuestionRoutes } from '~/src/server/plugins/engine/routes/questions.js'\nimport { getRoutes as getRepeaterItemDeleteRoutes } from '~/src/server/plugins/engine/routes/repeaters/item-delete.js'\nimport { getRoutes as getRepeaterSummaryRoutes } from '~/src/server/plugins/engine/routes/repeaters/summary.js'\nimport { type PluginOptions } from '~/src/server/plugins/engine/types.js'\nimport { registerVision } from '~/src/server/plugins/engine/vision.js'\nimport {\n type FormRequestPayloadRefs,\n type FormRequestRefs\n} from '~/src/server/routes/types.js'\nimport { CacheService } from '~/src/server/services/index.js'\n\nexport const plugin = {\n name: '@defra/forms-engine-plugin',\n dependencies: ['@hapi/crumb', '@hapi/yar', 'hapi-pino'],\n multiple: true,\n async register(server: Server, options: PluginOptions) {\n options = validatePluginOptions(options)\n\n const {\n model,\n cacheName,\n keyGenerator,\n sessionHydrator,\n nunjucks: nunjucksOptions,\n viewContext,\n preparePageEventRequestOptions\n } = options\n const cacheService = new CacheService({\n server,\n cacheName,\n options: {\n keyGenerator,\n sessionHydrator\n }\n })\n\n await registerVision(server, options)\n\n server.expose('baseLayoutPath', nunjucksOptions.baseLayoutPath)\n server.expose('viewContext', viewContext)\n server.expose('cacheService', cacheService)\n\n server.app.model = model\n\n // In-memory cache of FormModel items, exposed\n // (for testing purposes) through `server.app.models`\n const itemCache = new Map<string, { model: FormModel; updatedAt: Date }>()\n server.app.models = itemCache\n\n const loadFormPreHandler = makeLoadFormPreHandler(server, options)\n\n const getRouteOptions: RouteOptions<FormRequestRefs> = {\n pre: [\n {\n method:\n loadFormPreHandler as unknown as Lifecycle.Method<FormRequestRefs>\n }\n ]\n }\n\n const postRouteOptions: RouteOptions<FormRequestPayloadRefs> = {\n payload: {\n parse: true\n },\n pre: [\n {\n method:\n loadFormPreHandler as unknown as Lifecycle.Method<FormRequestPayloadRefs>\n }\n ]\n }\n\n const routes = [\n ...getQuestionRoutes(\n getRouteOptions,\n postRouteOptions,\n preparePageEventRequestOptions\n ),\n ...getRepeaterSummaryRoutes(getRouteOptions, postRouteOptions),\n ...getRepeaterItemDeleteRoutes(getRouteOptions, postRouteOptions),\n ...getFileUploadStatusRoutes()\n ]\n\n server.route(routes as unknown as ServerRoute[]) // TODO\n }\n} satisfies Plugin<PluginOptions>\n"],"mappings":"AASA,SAASA,qBAAqB;AAC9B,SAASC,SAAS,IAAIC,yBAAyB;AAC/C,SAASC,sBAAsB;AAC/B,SAASF,SAAS,IAAIG,iBAAiB;AACvC,SAASH,SAAS,IAAII,2BAA2B;AACjD,SAASJ,SAAS,IAAIK,wBAAwB;AAE9C,SAASC,cAAc;AAKvB,SAASC,YAAY;AAErB,OAAO,MAAMC,MAAM,GAAG;EACpBC,IAAI,EAAE,4BAA4B;EAClCC,YAAY,EAAE,CAAC,aAAa,EAAE,WAAW,EAAE,WAAW,CAAC;EACvDC,QAAQ,EAAE,IAAI;EACd,MAAMC,QAAQA,CAACC,MAAc,EAAEC,OAAsB,EAAE;IACrDA,OAAO,GAAGf,qBAAqB,CAACe,OAAO,CAAC;IAExC,MAAM;MACJC,KAAK;MACLC,SAAS;MACTC,YAAY;MACZC,eAAe;MACfC,QAAQ,EAAEC,eAAe;MACzBC,WAAW;MACXC;IACF,CAAC,GAAGR,OAAO;IACX,MAAMS,YAAY,GAAG,IAAIhB,YAAY,CAAC;MACpCM,MAAM;MACNG,SAAS;MACTF,OAAO,EAAE;QACPG,YAAY;QACZC;MACF;IACF,CAAC,CAAC;IAEF,MAAMZ,cAAc,CAACO,MAAM,EAAEC,OAAO,CAAC;IAErCD,MAAM,CAACW,MAAM,CAAC,gBAAgB,EAAEJ,eAAe,CAACK,cAAc,CAAC;IAC/DZ,MAAM,CAACW,MAAM,CAAC,aAAa,EAAEH,WAAW,CAAC;IACzCR,MAAM,CAACW,MAAM,CAAC,cAAc,EAAED,YAAY,CAAC;IAE3CV,MAAM,CAACa,GAAG,CAACX,KAAK,GAAGA,KAAK;;IAExB;IACA;IACA,MAAMY,SAAS,GAAG,IAAIC,GAAG,CAAgD,CAAC;IAC1Ef,MAAM,CAACa,GAAG,CAACG,MAAM,GAAGF,SAAS;IAE7B,MAAMG,kBAAkB,GAAG5B,sBAAsB,CAACW,MAAM,EAAEC,OAAO,CAAC;IAElE,MAAMiB,eAA8C,GAAG;MACrDC,GAAG,EAAE,CACH;QACEC,MAAM,EACJH;MACJ,CAAC;IAEL,CAAC;IAED,MAAMI,gBAAsD,GAAG;MAC7DC,OAAO,EAAE;QACPC,KAAK,EAAE;MACT,CAAC;MACDJ,GAAG,EAAE,CACH;QACEC,MAAM,EACJH;MACJ,CAAC;IAEL,CAAC;IAED,MAAMO,MAAM,GAAG,CACb,GAAGlC,iBAAiB,CAClB4B,eAAe,EACfG,gBAAgB,EAChBZ,8BACF,CAAC,EACD,GAAGjB,wBAAwB,CAAC0B,eAAe,EAAEG,gBAAgB,CAAC,EAC9D,GAAG9B,2BAA2B,CAAC2B,eAAe,EAAEG,gBAAgB,CAAC,EACjE,GAAGjC,yBAAyB,CAAC,CAAC,CAC/B;IAEDY,MAAM,CAACyB,KAAK,CAACD,MAAkC,CAAC,EAAC;EACnD;AACF,CAAiC","ignoreList":[]}
1
+ {"version":3,"file":"plugin.js","names":["validatePluginOptions","getRoutes","getSaveAndReturnExitRoutes","getFileUploadStatusRoutes","makeLoadFormPreHandler","getQuestionRoutes","getRepeaterItemDeleteRoutes","getRepeaterSummaryRoutes","registerVision","CacheService","plugin","name","dependencies","multiple","register","server","options","model","cacheName","keyGenerator","sessionHydrator","sessionPersister","nunjucks","nunjucksOptions","viewContext","preparePageEventRequestOptions","cacheService","expose","baseLayoutPath","app","itemCache","Map","models","loadFormPreHandler","getRouteOptions","pre","method","postRouteOptions","payload","parse","routes","route"],"sources":["../../../../src/server/plugins/engine/plugin.ts"],"sourcesContent":["import {\n type Lifecycle,\n type Plugin,\n type RouteOptions,\n type Server,\n type ServerRoute\n} from '@hapi/hapi'\n\nimport { type FormModel } from '~/src/server/plugins/engine/models/index.js'\nimport { validatePluginOptions } from '~/src/server/plugins/engine/options.js'\nimport { getRoutes as getSaveAndReturnExitRoutes } from '~/src/server/plugins/engine/routes/exit.js'\nimport { getRoutes as getFileUploadStatusRoutes } from '~/src/server/plugins/engine/routes/file-upload.js'\nimport { makeLoadFormPreHandler } from '~/src/server/plugins/engine/routes/index.js'\nimport { getRoutes as getQuestionRoutes } from '~/src/server/plugins/engine/routes/questions.js'\nimport { getRoutes as getRepeaterItemDeleteRoutes } from '~/src/server/plugins/engine/routes/repeaters/item-delete.js'\nimport { getRoutes as getRepeaterSummaryRoutes } from '~/src/server/plugins/engine/routes/repeaters/summary.js'\nimport { type PluginOptions } from '~/src/server/plugins/engine/types.js'\nimport { registerVision } from '~/src/server/plugins/engine/vision.js'\nimport {\n type FormRequestPayloadRefs,\n type FormRequestRefs\n} from '~/src/server/routes/types.js'\nimport { CacheService } from '~/src/server/services/index.js'\n\nexport const plugin = {\n name: '@defra/forms-engine-plugin',\n dependencies: ['@hapi/crumb', '@hapi/yar', 'hapi-pino'],\n multiple: true,\n async register(server: Server, options: PluginOptions) {\n options = validatePluginOptions(options)\n\n const {\n model,\n cacheName,\n keyGenerator,\n sessionHydrator,\n sessionPersister,\n nunjucks: nunjucksOptions,\n viewContext,\n preparePageEventRequestOptions\n } = options\n const cacheService = new CacheService({\n server,\n cacheName,\n options: {\n keyGenerator,\n sessionHydrator,\n sessionPersister\n }\n })\n\n await registerVision(server, options)\n\n server.expose('baseLayoutPath', nunjucksOptions.baseLayoutPath)\n server.expose('viewContext', viewContext)\n server.expose('cacheService', cacheService)\n\n server.app.model = model\n\n // In-memory cache of FormModel items, exposed\n // (for testing purposes) through `server.app.models`\n const itemCache = new Map<string, { model: FormModel; updatedAt: Date }>()\n server.app.models = itemCache\n\n const loadFormPreHandler = makeLoadFormPreHandler(server, options)\n\n const getRouteOptions: RouteOptions<FormRequestRefs> = {\n pre: [\n {\n method:\n loadFormPreHandler as unknown as Lifecycle.Method<FormRequestRefs>\n }\n ]\n }\n\n const postRouteOptions: RouteOptions<FormRequestPayloadRefs> = {\n payload: {\n parse: true\n },\n pre: [\n {\n method:\n loadFormPreHandler as unknown as Lifecycle.Method<FormRequestPayloadRefs>\n }\n ]\n }\n\n const routes = [\n ...getQuestionRoutes(\n getRouteOptions,\n postRouteOptions,\n preparePageEventRequestOptions\n ),\n ...getRepeaterSummaryRoutes(getRouteOptions, postRouteOptions),\n ...getRepeaterItemDeleteRoutes(getRouteOptions, postRouteOptions),\n ...getSaveAndReturnExitRoutes(getRouteOptions),\n ...getFileUploadStatusRoutes()\n ]\n\n server.route(routes as unknown as ServerRoute[]) // TODO\n }\n} satisfies Plugin<PluginOptions>\n"],"mappings":"AASA,SAASA,qBAAqB;AAC9B,SAASC,SAAS,IAAIC,0BAA0B;AAChD,SAASD,SAAS,IAAIE,yBAAyB;AAC/C,SAASC,sBAAsB;AAC/B,SAASH,SAAS,IAAII,iBAAiB;AACvC,SAASJ,SAAS,IAAIK,2BAA2B;AACjD,SAASL,SAAS,IAAIM,wBAAwB;AAE9C,SAASC,cAAc;AAKvB,SAASC,YAAY;AAErB,OAAO,MAAMC,MAAM,GAAG;EACpBC,IAAI,EAAE,4BAA4B;EAClCC,YAAY,EAAE,CAAC,aAAa,EAAE,WAAW,EAAE,WAAW,CAAC;EACvDC,QAAQ,EAAE,IAAI;EACd,MAAMC,QAAQA,CAACC,MAAc,EAAEC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A2B,CAAC4B,eAAe,EAAEG,gBAAgB,CAAC,EACjE,GAAGnC,0BAA0B,CAACgC,eAAe,CAAC,EAC9C,GAAG/B,yBAAyB,CAAC,CAAC,CAC/B;IAEDY,MAAM,CAAC0B,KAAK,CAACD,MAAkC,CAAC,EAAC;EACnD;AACF,CAAiC","ignoreList":[]}
@@ -0,0 +1,46 @@
1
+ import { type ResponseToolkit, type RouteOptions } from '@hapi/hapi';
2
+ import Joi from 'joi';
3
+ import { type FormRequest, type FormRequestRefs } from '~/src/server/routes/types.js';
4
+ export declare function getRoutes(getRouteOptions: RouteOptions<FormRequestRefs>): {
5
+ method: string;
6
+ path: string;
7
+ handler: (request: FormRequest, h: Pick<ResponseToolkit, "redirect" | "view">) => import("@hapi/hapi").ResponseObject;
8
+ options: {
9
+ validate: {
10
+ params: Joi.ObjectSchema<any>;
11
+ };
12
+ auth?: false | string | import("@hapi/hapi").RouteOptionsAccess | undefined;
13
+ app?: import("@hapi/hapi").RouteOptionsApp | undefined;
14
+ bind?: object | null | undefined;
15
+ cache?: false | import("@hapi/hapi").RouteOptionsCache | undefined;
16
+ compression?: { [P in keyof import("@hapi/hapi").ContentEncoders]?: Parameters<import("@hapi/hapi").ContentEncoders[P]>[0]; } | undefined;
17
+ cors?: boolean | import("@hapi/hapi").RouteOptionsCors | undefined;
18
+ description?: string | undefined;
19
+ ext?: { [key in import("@hapi/hapi").RouteRequestExtType]?: import("@hapi/hapi").RouteExtObject | import("@hapi/hapi").RouteExtObject[] | undefined; } | undefined;
20
+ files?: {
21
+ relativeTo: string;
22
+ } | undefined;
23
+ handler?: object | import("@hapi/hapi").Lifecycle.Method<FormRequestRefs, import("@hapi/hapi").Lifecycle.ReturnValue<FormRequestRefs>> | undefined;
24
+ id?: string | undefined;
25
+ isInternal?: boolean | undefined;
26
+ json?: import("@hapi/hapi").Json.StringifyArguments | undefined;
27
+ log?: {
28
+ collect: boolean;
29
+ } | undefined;
30
+ notes?: string | string[] | undefined;
31
+ payload?: import("@hapi/hapi").RouteOptionsPayload | undefined;
32
+ plugins?: import("@hapi/hapi").PluginSpecificConfiguration | undefined;
33
+ pre?: import("@hapi/hapi").RouteOptionsPreArray<FormRequestRefs> | undefined;
34
+ response?: import("@hapi/hapi").RouteOptionsResponse | undefined;
35
+ security?: import("@hapi/hapi").RouteOptionsSecure | undefined;
36
+ state?: {
37
+ parse?: boolean | undefined;
38
+ failAction?: import("@hapi/hapi").Lifecycle.FailAction | undefined;
39
+ } | undefined;
40
+ tags?: string[] | undefined;
41
+ timeout?: {
42
+ server?: boolean | number | undefined;
43
+ socket?: boolean | number | undefined;
44
+ } | undefined;
45
+ };
46
+ }[];
@@ -0,0 +1,36 @@
1
+ import { slugSchema } from '@defra/forms-model';
2
+ import Boom from '@hapi/boom';
3
+ import Joi from 'joi';
4
+ export function getRoutes(getRouteOptions) {
5
+ return [{
6
+ method: 'get',
7
+ path: '/{slug}/exit',
8
+ handler: (request, h) => {
9
+ const {
10
+ app
11
+ } = request;
12
+ const {
13
+ model
14
+ } = app;
15
+ if (!model) {
16
+ throw Boom.notFound('No model found for exit page');
17
+ }
18
+ const returnUrl = request.query.returnUrl;
19
+ const exitViewModel = {
20
+ pageTitle: 'Your progress has been saved',
21
+ phaseTag: model.def.phaseBanner?.phase,
22
+ returnUrl
23
+ };
24
+ return h.view('exit', exitViewModel);
25
+ },
26
+ options: {
27
+ ...getRouteOptions,
28
+ validate: {
29
+ params: Joi.object().keys({
30
+ slug: slugSchema
31
+ })
32
+ }
33
+ }
34
+ }];
35
+ }
36
+ //# sourceMappingURL=exit.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"exit.js","names":["slugSchema","Boom","Joi","getRoutes","getRouteOptions","method","path","handler","request","h","app","model","notFound","returnUrl","query","exitViewModel","pageTitle","phaseTag","def","phaseBanner","phase","view","options","validate","params","object","keys","slug"],"sources":["../../../../../src/server/plugins/engine/routes/exit.ts"],"sourcesContent":["import { slugSchema } from '@defra/forms-model'\nimport Boom from '@hapi/boom'\nimport { type ResponseToolkit, type RouteOptions } from '@hapi/hapi'\nimport Joi from 'joi'\n\nimport {\n type FormRequest,\n type FormRequestRefs\n} from '~/src/server/routes/types.js'\n\nexport function getRoutes(getRouteOptions: RouteOptions<FormRequestRefs>) {\n return [\n {\n method: 'get',\n path: '/{slug}/exit',\n handler: (\n request: FormRequest,\n h: Pick<ResponseToolkit, 'redirect' | 'view'>\n ) => {\n const { app } = request\n const { model } = app\n\n if (!model) {\n throw Boom.notFound('No model found for exit page')\n }\n\n const returnUrl = request.query.returnUrl\n\n const exitViewModel = {\n pageTitle: 'Your progress has been saved',\n phaseTag: model.def.phaseBanner?.phase,\n returnUrl\n }\n\n return h.view('exit', exitViewModel)\n },\n options: {\n ...getRouteOptions,\n validate: {\n params: Joi.object().keys({\n slug: slugSchema\n })\n }\n }\n }\n ]\n}\n"],"mappings":"AAAA,SAASA,UAAU,QAAQ,oBAAoB;AAC/C,OAAOC,IAAI,MAAM,YAAY;AAE7B,OAAOC,GAAG,MAAM,KAAK;AAOrB,OAAO,SAASC,SAASA,CAACC,eAA8C,EAAE;EACxE,OAAO,CACL;IACEC,MAAM,EAAE,KAAK;IACbC,IAAI,EAAE,cAAc;IACpBC,OAAO,EAAEA,CACPC,OAAoB,EACpBC,CAA6C,KAC1C;MACH,MAAM;QAAEC;MAAI,CAAC,GAAGF,OAAO;MACvB,MAAM;QAAEG;MAAM,CAAC,GAAGD,GAAG;MAErB,IAAI,CAACC,KAAK,EAAE;QACV,MAAMV,IAAI,CAACW,QAAQ,CAAC,8BAA8B,CAAC;MACrD;MAEA,MAAMC,SAAS,GAAGL,OAAO,CAACM,KAAK,CAACD,SAAS;MAEzC,MAAME,aAAa,GAAG;QACpBC,SAAS,EAAE,8BAA8B;QACzCC,QAAQ,EAAEN,KAAK,CAACO,GAAG,CAACC,WAAW,EAAEC,KAAK;QACtCP;MACF,CAAC;MAED,OAAOJ,CAAC,CAACY,IAAI,CAAC,MAAM,EAAEN,aAAa,CAAC;IACtC,CAAC;IACDO,OAAO,EAAE;MACP,GAAGlB,eAAe;MAClBmB,QAAQ,EAAE;QACRC,MAAM,EAAEtB,GAAG,CAACuB,MAAM,CAAC,CAAC,CAACC,IAAI,CAAC;UACxBC,IAAI,EAAE3B;QACR,CAAC;MACH;IACF;EACF,CAAC,CACF;AACH","ignoreList":[]}
@@ -10,6 +10,7 @@ import { type ViewContext } from '~/src/server/plugins/nunjucks/types.js';
10
10
  import { type FormAction, type FormParams, type FormRequest, type FormRequestPayload } from '~/src/server/routes/types.js';
11
11
  import { type RequestOptions } from '~/src/server/services/httpService.js';
12
12
  import { type Services } from '~/src/server/types.js';
13
+ type RequestType = Request | FormRequest | FormRequestPayload;
13
14
  /**
14
15
  * Form submission state stores the following in Redis:
15
16
  * Props containing user's submitted values as `{ [inputId]: value }` or as `{ [sectionName]: { [inputName]: value } }`
@@ -234,6 +235,7 @@ export interface FormPageViewModel extends PageViewModelBase {
234
235
  context: FormContext;
235
236
  errors?: FormSubmissionError[];
236
237
  hasMissingNotificationEmail?: boolean;
238
+ allowSaveAndReturn?: boolean;
237
239
  }
238
240
  export interface RepeaterSummaryPageViewModel extends PageViewModelBase {
239
241
  context: FormContext;
@@ -268,8 +270,9 @@ export interface PluginOptions {
268
270
  cacheName?: string;
269
271
  globals?: Record<string, GlobalFunction>;
270
272
  filters?: Record<string, FilterFunction>;
271
- keyGenerator?: (request: Request | FormRequest | FormRequestPayload) => string;
272
- sessionHydrator?: (request: Request | FormRequest | FormRequestPayload) => Promise<FormSubmissionState>;
273
+ keyGenerator?: (request: RequestType) => string;
274
+ sessionHydrator?: (request: RequestType) => Promise<FormSubmissionState>;
275
+ sessionPersister?: (key: string, state: FormSubmissionState, request: RequestType) => Promise<void>;
273
276
  pluginPath?: string;
274
277
  nunjucks: {
275
278
  baseLayoutPath: string;
@@ -280,3 +283,4 @@ export interface PluginOptions {
280
283
  onRequest?: OnRequestCallback;
281
284
  baseUrl: string;
282
285
  }
286
+ export {};
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","names":["UploadStatus","FileStatus"],"sources":["../../../../src/server/plugins/engine/types.ts"],"sourcesContent":["import {\n type ComponentDef,\n type Event,\n type FormDefinition,\n type FormMetadata,\n type Item,\n type List,\n type Page\n} from '@defra/forms-model'\nimport { type PluginProperties, type Request } from '@hapi/hapi'\nimport { type JoiExpression, type ValidationErrorItem } from 'joi'\n\nimport { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { type Component } from '~/src/server/plugins/engine/components/helpers.js'\nimport {\n type BackLink,\n type ComponentText,\n type ComponentViewModel\n} from '~/src/server/plugins/engine/components/types.js'\nimport { type FormModel } from '~/src/server/plugins/engine/models/index.js'\nimport { type PageController } from '~/src/server/plugins/engine/pageControllers/PageController.js'\nimport { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers.js'\nimport { type ViewContext } from '~/src/server/plugins/nunjucks/types.js'\nimport {\n type FormAction,\n type FormParams,\n type FormRequest,\n type FormRequestPayload\n} from '~/src/server/routes/types.js'\nimport { type RequestOptions } from '~/src/server/services/httpService.js'\nimport { type Services } from '~/src/server/types.js'\n\n/**\n * Form submission state stores the following in Redis:\n * Props containing user's submitted values as `{ [inputId]: value }` or as `{ [sectionName]: { [inputName]: value } }`\n * a) . e.g:\n * ```ts\n * {\n * _C9PRHmsgt: 'Ben',\n * WfLk9McjzX: 'Music',\n * IK7jkUFCBL: 'Royal Academy of Music'\n * }\n * ```\n *\n * b)\n * ```ts\n * {\n * checkBeforeYouStart: { ukPassport: true },\n * applicantDetails: {\n * numberOfApplicants: 1,\n * phoneNumber: '77777777',\n * emailAddress: 'aaa@aaa.com'\n * },\n * applicantOneDetails: {\n * firstName: 'a',\n * middleName: 'a',\n * lastName: 'a',\n * address: { addressLine1: 'a', addressLine2: 'a', town: 'a', postcode: 'a' }\n * }\n * }\n * ```\n */\n\n/**\n * Form submission state\n */\nexport type FormSubmissionState = {\n upload?: Record<string, TempFileState>\n} & FormState\n\nexport interface FormSubmissionError\n extends Pick<ValidationErrorItem, 'context' | 'path'> {\n href: string // e.g: '#dateField__day'\n name: string // e.g: 'dateField__day'\n text: string // e.g: 'Date field must be a real date'\n}\n\nexport interface FormPayloadParams {\n action?: FormAction\n confirm?: true\n crumb?: string\n itemId?: string\n}\n\n/**\n * Form POST for question pages\n * (after Joi has converted value types)\n */\nexport type FormPayload = FormPayloadParams & Partial<Record<string, FormValue>>\n\nexport type FormValue =\n | Item['value']\n | Item['value'][]\n | UploadState\n | RepeatListState\n | undefined\n\nexport type FormState = Partial<Record<string, FormStateValue>>\nexport type FormStateValue = Exclude<FormValue, undefined> | null\n\nexport interface FormValidationResult<\n ValueType extends FormPayload | FormSubmissionState\n> {\n value: ValueType\n errors: FormSubmissionError[] | undefined\n}\n\nexport interface FormContext {\n /**\n * Evaluation form state only (filtered by visited paths),\n * with values formatted for condition evaluation using\n * {@link FormComponent.getContextValueFromState}\n */\n evaluationState: FormState\n\n /**\n * Relevant form state only (filtered by visited paths)\n */\n relevantState: FormState\n\n /**\n * Relevant pages only (filtered by visited paths)\n */\n relevantPages: PageControllerClass[]\n\n /**\n * Form submission payload (single page)\n */\n payload: FormPayload\n\n /**\n * Form submission state (entire form)\n */\n state: FormSubmissionState\n\n /**\n * Validation errors (entire form)\n */\n errors?: FormSubmissionError[]\n\n /**\n * Visited paths evaluated from form state\n */\n paths: string[]\n\n /**\n * Preview URL direct access is allowed\n */\n isForceAccess: boolean\n\n /**\n * Miscellaneous extra data from event responses\n */\n data: object\n\n pageDefMap: Map<string, Page>\n listDefMap: Map<string, List>\n componentDefMap: Map<string, ComponentDef>\n pageMap: Map<string, PageControllerClass>\n componentMap: Map<string, Component>\n referenceNumber: string\n}\n\nexport type FormContextRequest = (\n | {\n method: 'get'\n payload?: undefined\n }\n | {\n method: 'post'\n payload: FormPayload\n }\n | {\n method: FormRequest['method']\n payload?: object | undefined\n }\n) &\n Pick<FormRequest, 'app' | 'method' | 'params' | 'path' | 'query' | 'url'>\n\nexport interface UploadInitiateResponse {\n uploadId: string\n uploadUrl: string\n statusUrl: string\n}\n\nexport enum UploadStatus {\n initiated = 'initiated',\n pending = 'pending',\n ready = 'ready'\n}\n\nexport enum FileStatus {\n complete = 'complete',\n rejected = 'rejected',\n pending = 'pending'\n}\n\nexport type UploadState = FileState[]\n\nexport type FileUpload = {\n fileId: string\n filename: string\n contentLength: number\n} & (\n | {\n fileStatus: FileStatus.complete | FileStatus.rejected | FileStatus.pending\n errorMessage?: string\n }\n | {\n fileStatus: FileStatus.complete\n errorMessage?: undefined\n }\n)\n\nexport interface FileUploadMetadata {\n retrievalKey: string\n}\n\nexport type UploadStatusResponse =\n | {\n uploadStatus: UploadStatus.initiated\n metadata: FileUploadMetadata\n form: { file?: undefined }\n }\n | {\n uploadStatus: UploadStatus.pending | UploadStatus.ready\n metadata: FileUploadMetadata\n form: { file: FileUpload }\n numberOfRejectedFiles?: number\n }\n | {\n uploadStatus: UploadStatus.ready\n metadata: FileUploadMetadata\n form: { file: FileUpload }\n numberOfRejectedFiles: 0\n }\n\nexport type UploadStatusFileResponse = Exclude<\n UploadStatusResponse,\n { uploadStatus: UploadStatus.initiated }\n>\n\nexport interface FileState {\n uploadId: string\n status: UploadStatusFileResponse\n}\n\nexport interface TempFileState {\n upload?: UploadInitiateResponse\n files: UploadState\n}\n\nexport interface RepeatItemState extends FormPayload {\n itemId: string\n}\n\nexport type RepeatListState = RepeatItemState[]\n\nexport interface CheckAnswers {\n title?: ComponentText\n summaryList: SummaryList\n}\n\nexport interface SummaryList {\n classes?: string\n rows: SummaryListRow[]\n}\n\nexport interface SummaryListRow {\n key: ComponentText\n value: ComponentText\n actions?: { items: SummaryListAction[] }\n}\n\nexport type SummaryListAction = ComponentText & {\n href: string\n visuallyHiddenText: string\n}\n\nexport interface PageViewModelBase extends Partial<ViewContext> {\n page: PageController\n name?: string\n pageTitle: string\n sectionTitle?: string\n showTitle: boolean\n isStartPage: boolean\n backLink?: BackLink\n feedbackLink?: string\n serviceUrl: string\n phaseTag?: string\n}\n\nexport interface ItemDeletePageViewModel extends PageViewModelBase {\n context: FormContext\n itemTitle: string\n confirmation?: ComponentText\n buttonConfirm: ComponentText\n buttonCancel: ComponentText\n}\n\nexport interface FormPageViewModel extends PageViewModelBase {\n components: ComponentViewModel[]\n context: FormContext\n errors?: FormSubmissionError[]\n hasMissingNotificationEmail?: boolean\n}\n\nexport interface RepeaterSummaryPageViewModel extends PageViewModelBase {\n context: FormContext\n errors?: FormSubmissionError[]\n checkAnswers: CheckAnswers[]\n repeatTitle: string\n}\n\nexport interface FeaturedFormPageViewModel extends FormPageViewModel {\n formAction?: string\n formComponent: ComponentViewModel\n componentsBefore: ComponentViewModel[]\n uploadId: string | undefined\n proxyUrl: string | null\n}\n\nexport type PageViewModel =\n | PageViewModelBase\n | ItemDeletePageViewModel\n | FormPageViewModel\n | RepeaterSummaryPageViewModel\n | FeaturedFormPageViewModel\n\nexport type GlobalFunction = (value: unknown) => unknown\nexport type FilterFunction = (value: unknown) => unknown\nexport interface ErrorMessageTemplate {\n type: string\n template: JoiExpression\n}\n\nexport interface ErrorMessageTemplateList {\n baseErrors: ErrorMessageTemplate[]\n advancedSettingsErrors: ErrorMessageTemplate[]\n}\n\nexport type PreparePageEventRequestOptions = (\n options: RequestOptions,\n event: Event,\n page: PageControllerClass,\n context: FormContext\n) => void\n\nexport type OnRequestCallback = (\n request: FormRequest | FormRequestPayload,\n params: FormParams,\n definition: FormDefinition,\n metadata: FormMetadata\n) => void\n\nexport interface PluginOptions {\n model?: FormModel\n services?: Services\n controllers?: Record<string, typeof PageController>\n cacheName?: string\n globals?: Record<string, GlobalFunction>\n filters?: Record<string, FilterFunction>\n keyGenerator?: (request: Request | FormRequest | FormRequestPayload) => string\n sessionHydrator?: (\n request: Request | FormRequest | FormRequestPayload\n ) => Promise<FormSubmissionState>\n pluginPath?: string\n nunjucks: {\n baseLayoutPath: string\n paths: string[]\n }\n viewContext: PluginProperties['forms-engine-plugin']['viewContext']\n preparePageEventRequestOptions?: PreparePageEventRequestOptions\n onRequest?: OnRequestCallback\n baseUrl: string // base URL of the application, protocol and hostname e.g. \"https://myapp.com\"\n}\n"],"mappings":"AAgCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAmBA;AACA;AACA;AACA;;AAkGA,WAAYA,YAAY,0BAAZA,YAAY;EAAZA,YAAY;EAAZA,YAAY;EAAZA,YAAY;EAAA,OAAZA,YAAY;AAAA;AAMxB,WAAYC,UAAU,0BAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA","ignoreList":[]}
1
+ {"version":3,"file":"types.js","names":["UploadStatus","FileStatus"],"sources":["../../../../src/server/plugins/engine/types.ts"],"sourcesContent":["import {\n type ComponentDef,\n type Event,\n type FormDefinition,\n type FormMetadata,\n type Item,\n type List,\n type Page\n} from '@defra/forms-model'\nimport { type PluginProperties, type Request } from '@hapi/hapi'\nimport { type JoiExpression, type ValidationErrorItem } from 'joi'\n\nimport { FormComponent } from '~/src/server/plugins/engine/components/FormComponent.js'\nimport { type Component } from '~/src/server/plugins/engine/components/helpers.js'\nimport {\n type BackLink,\n type ComponentText,\n type ComponentViewModel\n} from '~/src/server/plugins/engine/components/types.js'\nimport { type FormModel } from '~/src/server/plugins/engine/models/index.js'\nimport { type PageController } from '~/src/server/plugins/engine/pageControllers/PageController.js'\nimport { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers.js'\nimport { type ViewContext } from '~/src/server/plugins/nunjucks/types.js'\nimport {\n type FormAction,\n type FormParams,\n type FormRequest,\n type FormRequestPayload\n} from '~/src/server/routes/types.js'\nimport { type RequestOptions } from '~/src/server/services/httpService.js'\nimport { type Services } from '~/src/server/types.js'\n\ntype RequestType = Request | FormRequest | FormRequestPayload\n\n/**\n * Form submission state stores the following in Redis:\n * Props containing user's submitted values as `{ [inputId]: value }` or as `{ [sectionName]: { [inputName]: value } }`\n * a) . e.g:\n * ```ts\n * {\n * _C9PRHmsgt: 'Ben',\n * WfLk9McjzX: 'Music',\n * IK7jkUFCBL: 'Royal Academy of Music'\n * }\n * ```\n *\n * b)\n * ```ts\n * {\n * checkBeforeYouStart: { ukPassport: true },\n * applicantDetails: {\n * numberOfApplicants: 1,\n * phoneNumber: '77777777',\n * emailAddress: 'aaa@aaa.com'\n * },\n * applicantOneDetails: {\n * firstName: 'a',\n * middleName: 'a',\n * lastName: 'a',\n * address: { addressLine1: 'a', addressLine2: 'a', town: 'a', postcode: 'a' }\n * }\n * }\n * ```\n */\n\n/**\n * Form submission state\n */\nexport type FormSubmissionState = {\n upload?: Record<string, TempFileState>\n} & FormState\n\nexport interface FormSubmissionError\n extends Pick<ValidationErrorItem, 'context' | 'path'> {\n href: string // e.g: '#dateField__day'\n name: string // e.g: 'dateField__day'\n text: string // e.g: 'Date field must be a real date'\n}\n\nexport interface FormPayloadParams {\n action?: FormAction\n confirm?: true\n crumb?: string\n itemId?: string\n}\n\n/**\n * Form POST for question pages\n * (after Joi has converted value types)\n */\nexport type FormPayload = FormPayloadParams & Partial<Record<string, FormValue>>\n\nexport type FormValue =\n | Item['value']\n | Item['value'][]\n | UploadState\n | RepeatListState\n | undefined\n\nexport type FormState = Partial<Record<string, FormStateValue>>\nexport type FormStateValue = Exclude<FormValue, undefined> | null\n\nexport interface FormValidationResult<\n ValueType extends FormPayload | FormSubmissionState\n> {\n value: ValueType\n errors: FormSubmissionError[] | undefined\n}\n\nexport interface FormContext {\n /**\n * Evaluation form state only (filtered by visited paths),\n * with values formatted for condition evaluation using\n * {@link FormComponent.getContextValueFromState}\n */\n evaluationState: FormState\n\n /**\n * Relevant form state only (filtered by visited paths)\n */\n relevantState: FormState\n\n /**\n * Relevant pages only (filtered by visited paths)\n */\n relevantPages: PageControllerClass[]\n\n /**\n * Form submission payload (single page)\n */\n payload: FormPayload\n\n /**\n * Form submission state (entire form)\n */\n state: FormSubmissionState\n\n /**\n * Validation errors (entire form)\n */\n errors?: FormSubmissionError[]\n\n /**\n * Visited paths evaluated from form state\n */\n paths: string[]\n\n /**\n * Preview URL direct access is allowed\n */\n isForceAccess: boolean\n\n /**\n * Miscellaneous extra data from event responses\n */\n data: object\n\n pageDefMap: Map<string, Page>\n listDefMap: Map<string, List>\n componentDefMap: Map<string, ComponentDef>\n pageMap: Map<string, PageControllerClass>\n componentMap: Map<string, Component>\n referenceNumber: string\n}\n\nexport type FormContextRequest = (\n | {\n method: 'get'\n payload?: undefined\n }\n | {\n method: 'post'\n payload: FormPayload\n }\n | {\n method: FormRequest['method']\n payload?: object | undefined\n }\n) &\n Pick<FormRequest, 'app' | 'method' | 'params' | 'path' | 'query' | 'url'>\n\nexport interface UploadInitiateResponse {\n uploadId: string\n uploadUrl: string\n statusUrl: string\n}\n\nexport enum UploadStatus {\n initiated = 'initiated',\n pending = 'pending',\n ready = 'ready'\n}\n\nexport enum FileStatus {\n complete = 'complete',\n rejected = 'rejected',\n pending = 'pending'\n}\n\nexport type UploadState = FileState[]\n\nexport type FileUpload = {\n fileId: string\n filename: string\n contentLength: number\n} & (\n | {\n fileStatus: FileStatus.complete | FileStatus.rejected | FileStatus.pending\n errorMessage?: string\n }\n | {\n fileStatus: FileStatus.complete\n errorMessage?: undefined\n }\n)\n\nexport interface FileUploadMetadata {\n retrievalKey: string\n}\n\nexport type UploadStatusResponse =\n | {\n uploadStatus: UploadStatus.initiated\n metadata: FileUploadMetadata\n form: { file?: undefined }\n }\n | {\n uploadStatus: UploadStatus.pending | UploadStatus.ready\n metadata: FileUploadMetadata\n form: { file: FileUpload }\n numberOfRejectedFiles?: number\n }\n | {\n uploadStatus: UploadStatus.ready\n metadata: FileUploadMetadata\n form: { file: FileUpload }\n numberOfRejectedFiles: 0\n }\n\nexport type UploadStatusFileResponse = Exclude<\n UploadStatusResponse,\n { uploadStatus: UploadStatus.initiated }\n>\n\nexport interface FileState {\n uploadId: string\n status: UploadStatusFileResponse\n}\n\nexport interface TempFileState {\n upload?: UploadInitiateResponse\n files: UploadState\n}\n\nexport interface RepeatItemState extends FormPayload {\n itemId: string\n}\n\nexport type RepeatListState = RepeatItemState[]\n\nexport interface CheckAnswers {\n title?: ComponentText\n summaryList: SummaryList\n}\n\nexport interface SummaryList {\n classes?: string\n rows: SummaryListRow[]\n}\n\nexport interface SummaryListRow {\n key: ComponentText\n value: ComponentText\n actions?: { items: SummaryListAction[] }\n}\n\nexport type SummaryListAction = ComponentText & {\n href: string\n visuallyHiddenText: string\n}\n\nexport interface PageViewModelBase extends Partial<ViewContext> {\n page: PageController\n name?: string\n pageTitle: string\n sectionTitle?: string\n showTitle: boolean\n isStartPage: boolean\n backLink?: BackLink\n feedbackLink?: string\n serviceUrl: string\n phaseTag?: string\n}\n\nexport interface ItemDeletePageViewModel extends PageViewModelBase {\n context: FormContext\n itemTitle: string\n confirmation?: ComponentText\n buttonConfirm: ComponentText\n buttonCancel: ComponentText\n}\n\nexport interface FormPageViewModel extends PageViewModelBase {\n components: ComponentViewModel[]\n context: FormContext\n errors?: FormSubmissionError[]\n hasMissingNotificationEmail?: boolean\n allowSaveAndReturn?: boolean\n}\n\nexport interface RepeaterSummaryPageViewModel extends PageViewModelBase {\n context: FormContext\n errors?: FormSubmissionError[]\n checkAnswers: CheckAnswers[]\n repeatTitle: string\n}\n\nexport interface FeaturedFormPageViewModel extends FormPageViewModel {\n formAction?: string\n formComponent: ComponentViewModel\n componentsBefore: ComponentViewModel[]\n uploadId: string | undefined\n proxyUrl: string | null\n}\n\nexport type PageViewModel =\n | PageViewModelBase\n | ItemDeletePageViewModel\n | FormPageViewModel\n | RepeaterSummaryPageViewModel\n | FeaturedFormPageViewModel\n\nexport type GlobalFunction = (value: unknown) => unknown\nexport type FilterFunction = (value: unknown) => unknown\nexport interface ErrorMessageTemplate {\n type: string\n template: JoiExpression\n}\n\nexport interface ErrorMessageTemplateList {\n baseErrors: ErrorMessageTemplate[]\n advancedSettingsErrors: ErrorMessageTemplate[]\n}\n\nexport type PreparePageEventRequestOptions = (\n options: RequestOptions,\n event: Event,\n page: PageControllerClass,\n context: FormContext\n) => void\n\nexport type OnRequestCallback = (\n request: FormRequest | FormRequestPayload,\n params: FormParams,\n definition: FormDefinition,\n metadata: FormMetadata\n) => void\n\nexport interface PluginOptions {\n model?: FormModel\n services?: Services\n controllers?: Record<string, typeof PageController>\n cacheName?: string\n globals?: Record<string, GlobalFunction>\n filters?: Record<string, FilterFunction>\n keyGenerator?: (request: RequestType) => string\n sessionHydrator?: (request: RequestType) => Promise<FormSubmissionState>\n sessionPersister?: (\n key: string,\n state: FormSubmissionState,\n request: RequestType\n ) => Promise<void>\n pluginPath?: string\n nunjucks: {\n baseLayoutPath: string\n paths: string[]\n }\n viewContext: PluginProperties['forms-engine-plugin']['viewContext']\n preparePageEventRequestOptions?: PreparePageEventRequestOptions\n onRequest?: OnRequestCallback\n baseUrl: string // base URL of the application, protocol and hostname e.g. \"https://myapp.com\"\n}\n"],"mappings":"AAkCA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;;AAEA;AACA;AACA;;AAmBA;AACA;AACA;AACA;;AAkGA,WAAYA,YAAY,0BAAZA,YAAY;EAAZA,YAAY;EAAZA,YAAY;EAAZA,YAAY;EAAA,OAAZA,YAAY;AAAA;AAMxB,WAAYC,UAAU,0BAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA","ignoreList":[]}
@@ -0,0 +1,31 @@
1
+ {% extends baseLayoutPath %}
2
+
3
+ {% from "govuk/components/panel/macro.njk" import govukPanel %}
4
+ {% from "govuk/components/button/macro.njk" import govukButton %}
5
+
6
+ {% set mainClasses = "govuk-main-wrapper--l" %}
7
+
8
+ {% block content %}
9
+ <div class="govuk-grid-row">
10
+ <div class="govuk-grid-column-two-thirds">
11
+ {{ govukPanel({
12
+ titleText: pageTitle or "Your progress has been saved"
13
+ }) }}
14
+
15
+ <h2 class="govuk-heading-m">What happens next</h2>
16
+ <div class="app-prose-scope">
17
+ <p class="govuk-body">Your form progress has been saved. You can return to complete your application at any time using the link provided.</p>
18
+
19
+ {% if returnUrl %}
20
+ <p class="govuk-body">
21
+ {{ govukButton({
22
+ text: "Return to application",
23
+ href: returnUrl,
24
+ classes: "govuk-button--secondary"
25
+ }) }}
26
+ </p>
27
+ {% endif %}
28
+ </div>
29
+ </div>
30
+ </div>
31
+ {% endblock %}
@@ -3,13 +3,24 @@
3
3
 
4
4
  <form method="post" novalidate>
5
5
  <input type="hidden" name="crumb" value="{{ crumb }}">
6
- <input type="hidden" name="action" value="validate">
7
6
 
8
7
  {{ componentList(components) }}
9
8
 
10
- {{ govukButton({
11
- text: "Start now" if isStartPage else "Continue",
12
- isStartButton: isStartPage,
13
- preventDoubleClick: true
14
- }) }}
9
+ <div class="govuk-button-group">
10
+ {{ govukButton({
11
+ text: "Start now" if isStartPage else "Continue",
12
+ isStartButton: isStartPage,
13
+ preventDoubleClick: true
14
+ }) }}
15
+
16
+ {% if allowSaveAndReturn %}
17
+ {{ govukButton({
18
+ text: "Save and return",
19
+ classes: "govuk-button--secondary",
20
+ name: "action",
21
+ value: "save-and-return",
22
+ preventDoubleClick: true
23
+ }) }}
24
+ {% endif %}
25
+ </div>
15
26
  </form>
@@ -30,7 +30,8 @@ export declare enum FormAction {
30
30
  Validate = "validate",
31
31
  Delete = "delete",
32
32
  AddAnother = "add-another",
33
- Send = "send"
33
+ Send = "send",
34
+ SaveAndReturn = "save-and-return"
34
35
  }
35
36
  export declare enum FormStatus {
36
37
  Draft = "draft",
@@ -4,6 +4,7 @@ export let FormAction = /*#__PURE__*/function (FormAction) {
4
4
  FormAction["Delete"] = "delete";
5
5
  FormAction["AddAnother"] = "add-another";
6
6
  FormAction["Send"] = "send";
7
+ FormAction["SaveAndReturn"] = "save-and-return";
7
8
  return FormAction;
8
9
  }({});
9
10
  export let FormStatus = /*#__PURE__*/function (FormStatus) {
@@ -1 +1 @@
1
- {"version":3,"file":"types.js","names":["FormAction","FormStatus"],"sources":["../../../src/server/routes/types.ts"],"sourcesContent":["import { type ReqRefDefaults, type Request } from '@hapi/hapi'\n\nimport { type FormPayload } from '~/src/server/plugins/engine/types.js'\n\nexport interface FormQuery extends Partial<Record<string, string>> {\n /**\n * Allow preview URL direct access without relevant page checks\n */\n force?: string\n\n /**\n * Redirect location after 'continue' form action\n */\n returnUrl?: string\n}\n\nexport interface FormParams extends Partial<Record<string, string>> {\n path: string\n slug: string\n state?: FormStatus\n}\n\nexport interface FormRequestRefs\n extends Omit<ReqRefDefaults, 'Params' | 'Payload' | 'Query'> {\n Params: FormParams\n Payload: object | undefined\n Query: FormQuery\n}\n\nexport interface FormRequestPayloadRefs extends FormRequestRefs {\n Payload: FormPayload\n}\n\nexport type FormRequest = Request<FormRequestRefs>\nexport type FormRequestPayload = Request<FormRequestPayloadRefs>\n\nexport enum FormAction {\n Continue = 'continue',\n Validate = 'validate',\n Delete = 'delete',\n AddAnother = 'add-another',\n Send = 'send'\n}\n\nexport enum FormStatus {\n Draft = 'draft',\n Live = 'live'\n}\n"],"mappings":"AAoCA,WAAYA,UAAU,0BAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA;AAQtB,WAAYC,UAAU,0BAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA","ignoreList":[]}
1
+ {"version":3,"file":"types.js","names":["FormAction","FormStatus"],"sources":["../../../src/server/routes/types.ts"],"sourcesContent":["import { type ReqRefDefaults, type Request } from '@hapi/hapi'\n\nimport { type FormPayload } from '~/src/server/plugins/engine/types.js'\n\nexport interface FormQuery extends Partial<Record<string, string>> {\n /**\n * Allow preview URL direct access without relevant page checks\n */\n force?: string\n\n /**\n * Redirect location after 'continue' form action\n */\n returnUrl?: string\n}\n\nexport interface FormParams extends Partial<Record<string, string>> {\n path: string\n slug: string\n state?: FormStatus\n}\n\nexport interface FormRequestRefs\n extends Omit<ReqRefDefaults, 'Params' | 'Payload' | 'Query'> {\n Params: FormParams\n Payload: object | undefined\n Query: FormQuery\n}\n\nexport interface FormRequestPayloadRefs extends FormRequestRefs {\n Payload: FormPayload\n}\n\nexport type FormRequest = Request<FormRequestRefs>\nexport type FormRequestPayload = Request<FormRequestPayloadRefs>\n\nexport enum FormAction {\n Continue = 'continue',\n Validate = 'validate',\n Delete = 'delete',\n AddAnother = 'add-another',\n Send = 'send',\n SaveAndReturn = 'save-and-return'\n}\n\nexport enum FormStatus {\n Draft = 'draft',\n Live = 'live'\n}\n"],"mappings":"AAoCA,WAAYA,UAAU,0BAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA;AAStB,WAAYC,UAAU,0BAAVA,UAAU;EAAVA,UAAU;EAAVA,UAAU;EAAA,OAAVA,UAAU;AAAA","ignoreList":[]}
@@ -1,7 +1,7 @@
1
1
  import Joi from 'joi';
2
2
  import { FormAction, FormStatus } from "../routes/types.js";
3
3
  export const stateSchema = Joi.string().valid(FormStatus.Draft, FormStatus.Live).required();
4
- export const actionSchema = Joi.string().valid(FormAction.Continue, FormAction.Validate, FormAction.Delete, FormAction.AddAnother, FormAction.Send).default(FormAction.Validate).optional();
4
+ export const actionSchema = Joi.string().valid(FormAction.Continue, FormAction.Validate, FormAction.Delete, FormAction.AddAnother, FormAction.Send, FormAction.SaveAndReturn).default(FormAction.Validate).optional();
5
5
  export const pathSchema = Joi.string().required();
6
6
  export const itemIdSchema = Joi.string().uuid().required();
7
7
  export const crumbSchema = Joi.string().optional().allow('');
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","names":["Joi","FormAction","FormStatus","stateSchema","string","valid","Draft","Live","required","actionSchema","Continue","Validate","Delete","AddAnother","Send","default","optional","pathSchema","itemIdSchema","uuid","crumbSchema","allow","confirmSchema","boolean","empty","paramsSchema","object","keys","action","confirm","crumb","itemId"],"sources":["../../../src/server/schemas/index.ts"],"sourcesContent":["import Joi from 'joi'\n\nimport { type FormPayloadParams } from '~/src/server/plugins/engine/types.js'\nimport { FormAction, FormStatus } from '~/src/server/routes/types.js'\n\nexport const stateSchema = Joi.string<FormStatus>()\n .valid(FormStatus.Draft, FormStatus.Live)\n .required()\n\nexport const actionSchema = Joi.string<FormAction>()\n .valid(\n FormAction.Continue,\n FormAction.Validate,\n FormAction.Delete,\n FormAction.AddAnother,\n FormAction.Send\n )\n .default(FormAction.Validate)\n .optional()\n\nexport const pathSchema = Joi.string().required()\nexport const itemIdSchema = Joi.string().uuid().required()\nexport const crumbSchema = Joi.string().optional().allow('')\nexport const confirmSchema = Joi.boolean().empty(false)\n\nexport const paramsSchema = Joi.object<FormPayloadParams>()\n .keys({\n action: actionSchema,\n confirm: confirmSchema,\n crumb: crumbSchema,\n itemId: itemIdSchema.optional()\n })\n .default({})\n .optional()\n"],"mappings":"AAAA,OAAOA,GAAG,MAAM,KAAK;AAGrB,SAASC,UAAU,EAAEC,UAAU;AAE/B,OAAO,MAAMC,WAAW,GAAGH,GAAG,CAACI,MAAM,CAAa,CAAC,CAChDC,KAAK,CAACH,UAAU,CAACI,KAAK,EAAEJ,UAAU,CAACK,IAAI,CAAC,CACxCC,QAAQ,CAAC,CAAC;AAEb,OAAO,MAAMC,YAAY,GAAGT,GAAG,CAACI,MAAM,CAAa,CAAC,CACjDC,KAAK,CACJJ,UAAU,CAACS,QAAQ,EACnBT,UAAU,CAACU,QAAQ,EACnBV,UAAU,CAACW,MAAM,EACjBX,UAAU,CAACY,UAAU,EACrBZ,UAAU,CAACa,IACb,CAAC,CACAC,OAAO,CAACd,UAAU,CAACU,QAAQ,CAAC,CAC5BK,QAAQ,CAAC,CAAC;AAEb,OAAO,MAAMC,UAAU,GAAGjB,GAAG,CAACI,MAAM,CAAC,CAAC,CAACI,QAAQ,CAAC,CAAC;AACjD,OAAO,MAAMU,YAAY,GAAGlB,GAAG,CAACI,MAAM,CAAC,CAAC,CAACe,IAAI,CAAC,CAAC,CAACX,QAAQ,CAAC,CAAC;AAC1D,OAAO,MAAMY,WAAW,GAAGpB,GAAG,CAACI,MAAM,CAAC,CAAC,CAACY,QAAQ,CAAC,CAAC,CAACK,KAAK,CAAC,EAAE,CAAC;AAC5D,OAAO,MAAMC,aAAa,GAAGtB,GAAG,CAACuB,OAAO,CAAC,CAAC,CAACC,KAAK,CAAC,KAAK,CAAC;AAEvD,OAAO,MAAMC,YAAY,GAAGzB,GAAG,CAAC0B,MAAM,CAAoB,CAAC,CACxDC,IAAI,CAAC;EACJC,MAAM,EAAEnB,YAAY;EACpBoB,OAAO,EAAEP,aAAa;EACtBQ,KAAK,EAAEV,WAAW;EAClBW,MAAM,EAAEb,YAAY,CAACF,QAAQ,CAAC;AAChC,CAAC,CAAC,CACDD,OAAO,CAAC,CAAC,CAAC,CAAC,CACXC,QAAQ,CAAC,CAAC","ignoreList":[]}
1
+ {"version":3,"file":"index.js","names":["Joi","FormAction","FormStatus","stateSchema","string","valid","Draft","Live","required","actionSchema","Continue","Validate","Delete","AddAnother","Send","SaveAndReturn","default","optional","pathSchema","itemIdSchema","uuid","crumbSchema","allow","confirmSchema","boolean","empty","paramsSchema","object","keys","action","confirm","crumb","itemId"],"sources":["../../../src/server/schemas/index.ts"],"sourcesContent":["import Joi from 'joi'\n\nimport { type FormPayloadParams } from '~/src/server/plugins/engine/types.js'\nimport { FormAction, FormStatus } from '~/src/server/routes/types.js'\n\nexport const stateSchema = Joi.string<FormStatus>()\n .valid(FormStatus.Draft, FormStatus.Live)\n .required()\n\nexport const actionSchema = Joi.string<FormAction>()\n .valid(\n FormAction.Continue,\n FormAction.Validate,\n FormAction.Delete,\n FormAction.AddAnother,\n FormAction.Send,\n FormAction.SaveAndReturn\n )\n .default(FormAction.Validate)\n .optional()\n\nexport const pathSchema = Joi.string().required()\nexport const itemIdSchema = Joi.string().uuid().required()\nexport const crumbSchema = Joi.string().optional().allow('')\nexport const confirmSchema = Joi.boolean().empty(false)\n\nexport const paramsSchema = Joi.object<FormPayloadParams>()\n .keys({\n action: actionSchema,\n confirm: confirmSchema,\n crumb: crumbSchema,\n itemId: itemIdSchema.optional()\n })\n .default({})\n .optional()\n"],"mappings":"AAAA,OAAOA,GAAG,MAAM,KAAK;AAGrB,SAASC,UAAU,EAAEC,UAAU;AAE/B,OAAO,MAAMC,WAAW,GAAGH,GAAG,CAACI,MAAM,CAAa,CAAC,CAChDC,KAAK,CAACH,UAAU,CAACI,KAAK,EAAEJ,UAAU,CAACK,IAAI,CAAC,CACxCC,QAAQ,CAAC,CAAC;AAEb,OAAO,MAAMC,YAAY,GAAGT,GAAG,CAACI,MAAM,CAAa,CAAC,CACjDC,KAAK,CACJJ,UAAU,CAACS,QAAQ,EACnBT,UAAU,CAACU,QAAQ,EACnBV,UAAU,CAACW,MAAM,EACjBX,UAAU,CAACY,UAAU,EACrBZ,UAAU,CAACa,IAAI,EACfb,UAAU,CAACc,aACb,CAAC,CACAC,OAAO,CAACf,UAAU,CAACU,QAAQ,CAAC,CAC5BM,QAAQ,CAAC,CAAC;AAEb,OAAO,MAAMC,UAAU,GAAGlB,GAAG,CAACI,MAAM,CAAC,CAAC,CAACI,QAAQ,CAAC,CAAC;AACjD,OAAO,MAAMW,YAAY,GAAGnB,GAAG,CAACI,MAAM,CAAC,CAAC,CAACgB,IAAI,CAAC,CAAC,CAACZ,QAAQ,CAAC,CAAC;AAC1D,OAAO,MAAMa,WAAW,GAAGrB,GAAG,CAACI,MAAM,CAAC,CAAC,CAACa,QAAQ,CAAC,CAAC,CAACK,KAAK,CAAC,EAAE,CAAC;AAC5D,OAAO,MAAMC,aAAa,GAAGvB,GAAG,CAACwB,OAAO,CAAC,CAAC,CAACC,KAAK,CAAC,KAAK,CAAC;AAEvD,OAAO,MAAMC,YAAY,GAAG1B,GAAG,CAAC2B,MAAM,CAAoB,CAAC,CACxDC,IAAI,CAAC;EACJC,MAAM,EAAEpB,YAAY;EACpBqB,OAAO,EAAEP,aAAa;EACtBQ,KAAK,EAAEV,WAAW;EAClBW,MAAM,EAAEb,YAAY,CAACF,QAAQ,CAAC;AAChC,CAAC,CAAC,CACDD,OAAO,CAAC,CAAC,CAAC,CAAC,CACXC,QAAQ,CAAC,CAAC","ignoreList":[]}
@@ -14,6 +14,7 @@ export declare class CacheService {
14
14
  }>;
15
15
  generateKey?: (request: Request | FormRequest | FormRequestPayload) => string;
16
16
  customFetcher?: (request: Request | FormRequest | FormRequestPayload) => Promise<FormSubmissionState | null>;
17
+ customPersister?: (key: string, state: FormSubmissionState, request: Request | FormRequest | FormRequestPayload) => Promise<void>;
17
18
  logger: Server['logger'];
18
19
  constructor({ server, cacheName, options }: {
19
20
  server: Server;
@@ -21,6 +22,7 @@ export declare class CacheService {
21
22
  options?: {
22
23
  keyGenerator?: (request: Request | FormRequest | FormRequestPayload) => string;
23
24
  sessionHydrator?: (request: Request | FormRequest | FormRequestPayload) => Promise<FormSubmissionState | null>;
25
+ sessionPersister?: (key: string, state: FormSubmissionState, request: Request | FormRequest | FormRequestPayload) => Promise<void>;
24
26
  };
25
27
  });
26
28
  getState(request: Request | FormRequest | FormRequestPayload): Promise<FormSubmissionState>;
@@ -12,6 +12,7 @@ export class CacheService {
12
12
  cache;
13
13
  generateKey;
14
14
  customFetcher;
15
+ customPersister;
15
16
  logger;
16
17
  constructor({
17
18
  server,
@@ -20,13 +21,15 @@ export class CacheService {
20
21
  }) {
21
22
  const {
22
23
  keyGenerator,
23
- sessionHydrator
24
+ sessionHydrator,
25
+ sessionPersister
24
26
  } = options ?? {};
25
27
  if (!cacheName) {
26
28
  server.log('warn', 'You are using the default hapi cache. Please provide a cache name in plugin registration options.');
27
29
  }
28
30
  this.generateKey = keyGenerator ?? this.defaultKeyGenerator.bind(this);
29
31
  this.customFetcher = sessionHydrator ?? undefined;
32
+ this.customPersister = sessionPersister ?? undefined;
30
33
  this.cache = server.cache({
31
34
  cache: cacheName,
32
35
  segment: 'formSubmission'
@@ -34,13 +37,14 @@ export class CacheService {
34
37
  this.logger = server.logger;
35
38
  }
36
39
  async getState(request) {
37
- let cached = await this.cache.get(this.Key(request));
40
+ const key = this.Key(request);
41
+ let cached = await this.cache.get(key);
38
42
 
39
43
  // If nothing in Redis, attempt to rehydrate from backend DB
40
44
  if (!cached && this.customFetcher) {
41
45
  const rehydrated = await this.customFetcher(request);
42
46
  if (rehydrated != null) {
43
- await this.cache.set(this.Key(request), rehydrated, config.get('sessionTimeout'));
47
+ await this.cache.set(key, rehydrated, config.get('sessionTimeout'));
44
48
  cached = await this.getState(request);
45
49
  }
46
50
  }
@@ -82,8 +86,8 @@ export class CacheService {
82
86
  if (!request.yar.id) {
83
87
  throw new Error('No session ID found');
84
88
  }
85
- const state = request.params.state ?? '';
86
- const slug = request.params.slug ?? '';
89
+ const state = request.params.state || '';
90
+ const slug = request.params.slug || '';
87
91
  return `${request.yar.id}:${state}:${slug}:`;
88
92
  }
89
93
 
@@ -1 +1 @@
1
- {"version":3,"file":"cacheService.js","names":["Hoek","config","partition","ADDITIONAL_IDENTIFIER","CacheService","cache","generateKey","customFetcher","logger","constructor","server","cacheName","options","keyGenerator","sessionHydrator","log","defaultKeyGenerator","bind","undefined","segment","getState","request","cached","get","Key","rehydrated","set","setState","state","key","ttl","getConfirmationState","Confirmation","value","setConfirmationState","confirmationState","clearState","yar","id","drop","getFlash","messages","flash","Array","isArray","length","at","setFlash","message","Error","params","slug","additionalIdentifier","baseKey","merge","update","mergeArrays"],"sources":["../../../src/server/services/cacheService.ts"],"sourcesContent":["import { type Request, type Server } from '@hapi/hapi'\nimport * as Hoek from '@hapi/hoek'\n\nimport { config } from '~/src/config/index.js'\nimport { type createServer } from '~/src/server/index.js'\nimport {\n type FormPayload,\n type FormState,\n type FormSubmissionError,\n type FormSubmissionState\n} from '~/src/server/plugins/engine/types.js'\nimport {\n type FormRequest,\n type FormRequestPayload\n} from '~/src/server/routes/types.js'\n\nconst partition = 'cache'\n\nexport enum ADDITIONAL_IDENTIFIER {\n Confirmation = ':confirmation'\n}\n\nexport class CacheService {\n /**\n * This service is responsible for getting, storing or deleting a user's session data in the cache. This service has been registered by {@link createServer}\n */\n cache\n generateKey?: (request: Request | FormRequest | FormRequestPayload) => string\n customFetcher?: (\n request: Request | FormRequest | FormRequestPayload\n ) => Promise<FormSubmissionState | null>\n\n logger: Server['logger']\n\n constructor({\n server,\n cacheName,\n options\n }: {\n server: Server\n cacheName?: string\n options?: {\n keyGenerator?: (\n request: Request | FormRequest | FormRequestPayload\n ) => string\n sessionHydrator?: (\n request: Request | FormRequest | FormRequestPayload\n ) => Promise<FormSubmissionState | null>\n }\n }) {\n const { keyGenerator, sessionHydrator } = options ?? {}\n if (!cacheName) {\n server.log(\n 'warn',\n 'You are using the default hapi cache. Please provide a cache name in plugin registration options.'\n )\n }\n this.generateKey = keyGenerator ?? this.defaultKeyGenerator.bind(this)\n this.customFetcher = sessionHydrator ?? undefined\n this.cache = server.cache({ cache: cacheName, segment: 'formSubmission' })\n this.logger = server.logger\n }\n\n async getState(\n request: Request | FormRequest | FormRequestPayload\n ): Promise<FormSubmissionState> {\n let cached = await this.cache.get(this.Key(request))\n\n // If nothing in Redis, attempt to rehydrate from backend DB\n if (!cached && this.customFetcher) {\n const rehydrated = await this.customFetcher(request)\n\n if (rehydrated != null) {\n await this.cache.set(\n this.Key(request),\n rehydrated,\n config.get('sessionTimeout')\n )\n cached = await this.getState(request)\n }\n }\n\n return cached ?? {}\n }\n\n async setState(\n request: FormRequest | FormRequestPayload,\n state: FormSubmissionState\n ) {\n const key = this.Key(request)\n const ttl = config.get('sessionTimeout')\n\n await this.cache.set(key, state, ttl)\n return this.getState(request)\n }\n\n async getConfirmationState(\n request: FormRequest | FormRequestPayload\n ): Promise<{ confirmed?: true }> {\n const key = this.Key(request, ADDITIONAL_IDENTIFIER.Confirmation)\n const value = await this.cache.get(key)\n\n return value ?? {}\n }\n\n async setConfirmationState(\n request: FormRequest | FormRequestPayload,\n confirmationState: { confirmed?: true }\n ) {\n const key = this.Key(request, ADDITIONAL_IDENTIFIER.Confirmation)\n const ttl = config.get('confirmationSessionTimeout')\n\n return this.cache.set(key, confirmationState, ttl)\n }\n\n async clearState(request: FormRequest | FormRequestPayload) {\n if (request.yar.id) {\n await this.cache.drop(this.Key(request))\n }\n }\n\n getFlash(\n request: FormRequest | FormRequestPayload\n ): { errors: FormSubmissionError[] } | undefined {\n const key = this.Key(request)\n const messages = request.yar.flash(key.id)\n\n if (Array.isArray(messages) && messages.length) {\n return messages.at(0) as { errors: FormSubmissionError[] }\n }\n }\n\n setFlash(\n request: FormRequest | FormRequestPayload,\n message: { errors: FormSubmissionError[] }\n ) {\n const key = this.Key(request)\n\n request.yar.flash(key.id, message)\n }\n\n private defaultKeyGenerator(\n request: Request | FormRequest | FormRequestPayload\n ): string {\n if (!request.yar.id) {\n throw new Error('No session ID found')\n }\n\n const state = request.params.state ?? ''\n const slug = request.params.slug ?? ''\n return `${request.yar.id}:${state}:${slug}:`\n }\n\n /**\n * The key used to store user session data against.\n * If there are multiple forms on the same runner instance, for example `form-a` and `form-a-feedback` this will prevent CacheService from clearing data from `form-a` if a user gave feedback before they finished `form-a`\n * @param request - hapi request object\n * @param additionalIdentifier - appended to the id\n */\n Key(\n request: Request | FormRequest | FormRequestPayload,\n additionalIdentifier?: ADDITIONAL_IDENTIFIER\n ) {\n const baseKey = this.generateKey\n ? this.generateKey(request)\n : this.defaultKeyGenerator(request)\n\n return {\n segment: partition,\n id: `${baseKey}${additionalIdentifier ?? ''}`\n }\n }\n}\n\n/**\n * State merge helper\n * 1. Merges objects (form fields)\n * 2. Overwrites arrays\n */\nexport function merge<StateType extends FormState | FormPayload>(\n state: StateType,\n update: object\n): StateType {\n return Hoek.merge(state, update, {\n mergeArrays: false\n })\n}\n"],"mappings":"AACA,OAAO,KAAKA,IAAI,MAAM,YAAY;AAElC,SAASC,MAAM;AAaf,MAAMC,SAAS,GAAG,OAAO;AAEzB,WAAYC,qBAAqB,0BAArBA,qBAAqB;EAArBA,qBAAqB;EAAA,OAArBA,qBAAqB;AAAA;AAIjC,OAAO,MAAMC,YAAY,CAAC;EACxB;AACF;AACA;EACEC,KAAK;EACLC,WAAW;EACXC,aAAa;EAIbC,MAAM;EAENC,WAAWA,CAAC;IACVC,MAAM;IACNC,SAAS;IACTC;EAYF,CAAC,EAAE;IACD,MAAM;MAAEC,YAAY;MAAEC;IAAgB,CAAC,GAAGF,OAAO,IAAI,CAAC,CAAC;IACvD,IAAI,CAACD,SAAS,EAAE;MACdD,MAAM,CAACK,GAAG,CACR,MAAM,EACN,mGACF,CAAC;IACH;IACA,IAAI,CAACT,WAAW,GAAGO,YAAY,IAAI,IAAI,CAACG,mBAAmB,CAACC,IAAI,CAAC,IAAI,CAAC;IACtE,IAAI,CAACV,aAAa,GAAGO,eAAe,IAAII,SAAS;IACjD,IAAI,CAACb,KAAK,GAAGK,MAAM,CAACL,KAAK,CAAC;MAAEA,KAAK,EAAEM,SAAS;MAAEQ,OAAO,EAAE;IAAiB,CAAC,CAAC;IAC1E,IAAI,CAACX,MAAM,GAAGE,MAAM,CAACF,MAAM;EAC7B;EAEA,MAAMY,QAAQA,CACZC,OAAmD,EACrB;IAC9B,IAAIC,MAAM,GAAG,MAAM,IAAI,CAACjB,KAAK,CAACkB,GAAG,CAAC,IAAI,CAACC,GAAG,CAACH,OAAO,CAAC,CAAC;;IAEpD;IACA,IAAI,CAACC,MAAM,IAAI,IAAI,CAACf,aAAa,EAAE;MACjC,MAAMkB,UAAU,GAAG,MAAM,IAAI,CAAClB,aAAa,CAACc,OAAO,CAAC;MAEpD,IAAII,UAAU,IAAI,IAAI,EAAE;QACtB,MAAM,IAAI,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CAACL,mBAAmB,CAACK,OAAO,CAAC;IAErC,OAAO;MACLF,OAAO,EAAEjB,SAAS;MAClBoC,EAAE,EAAE,GAAGe,OAAO,GAAGD,oBAAoB,IAAI,EAAE;IAC7C,CAAC;EACH;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASE,KAAKA,CACnB1B,KAAgB,EAChB2B,MAAc,EACH;EACX,OAAOvD,IAAI,CAACsD,KAAK,CAAC1B,KAAK,EAAE2B,MAAM,EAAE;IAC/BC,WAAW,EAAE;EACf,CAAC,CAAC;AACJ","ignoreList":[]}
1
+ {"version":3,"file":"cacheService.js","names":["Hoek","config","partition","ADDITIONAL_IDENTIFIER","CacheService","cache","generateKey","customFetcher","customPersister","logger","constructor","server","cacheName","options","keyGenerator","sessionHydrator","sessionPersister","log","defaultKeyGenerator","bind","undefined","segment","getState","request","key","Key","cached","get","rehydrated","set","setState","state","ttl","getConfirmationState","Confirmation","value","setConfirmationState","confirmationState","clearState","yar","id","drop","getFlash","messages","flash","Array","isArray","length","at","setFlash","message","Error","params","slug","additionalIdentifier","baseKey","merge","update","mergeArrays"],"sources":["../../../src/server/services/cacheService.ts"],"sourcesContent":["import { type Request, type Server } from '@hapi/hapi'\nimport * as Hoek from '@hapi/hoek'\n\nimport { config } from '~/src/config/index.js'\nimport { type createServer } from '~/src/server/index.js'\nimport {\n type FormPayload,\n type FormState,\n type FormSubmissionError,\n type FormSubmissionState\n} from '~/src/server/plugins/engine/types.js'\nimport {\n type FormRequest,\n type FormRequestPayload\n} from '~/src/server/routes/types.js'\n\nconst partition = 'cache'\n\nexport enum ADDITIONAL_IDENTIFIER {\n Confirmation = ':confirmation'\n}\n\nexport class CacheService {\n /**\n * This service is responsible for getting, storing or deleting a user's session data in the cache. This service has been registered by {@link createServer}\n */\n cache\n generateKey?: (request: Request | FormRequest | FormRequestPayload) => string\n customFetcher?: (\n request: Request | FormRequest | FormRequestPayload\n ) => Promise<FormSubmissionState | null>\n\n customPersister?: (\n key: string,\n state: FormSubmissionState,\n request: Request | FormRequest | FormRequestPayload\n ) => Promise<void>\n\n logger: Server['logger']\n\n constructor({\n server,\n cacheName,\n options\n }: {\n server: Server\n cacheName?: string\n options?: {\n keyGenerator?: (\n request: Request | FormRequest | FormRequestPayload\n ) => string\n sessionHydrator?: (\n request: Request | FormRequest | FormRequestPayload\n ) => Promise<FormSubmissionState | null>\n sessionPersister?: (\n key: string,\n state: FormSubmissionState,\n request: Request | FormRequest | FormRequestPayload\n ) => Promise<void>\n }\n }) {\n const { keyGenerator, sessionHydrator, sessionPersister } = options ?? {}\n if (!cacheName) {\n server.log(\n 'warn',\n 'You are using the default hapi cache. Please provide a cache name in plugin registration options.'\n )\n }\n this.generateKey = keyGenerator ?? this.defaultKeyGenerator.bind(this)\n this.customFetcher = sessionHydrator ?? undefined\n this.customPersister = sessionPersister ?? undefined\n this.cache = server.cache({ cache: cacheName, segment: 'formSubmission' })\n this.logger = server.logger\n }\n\n async getState(\n request: Request | FormRequest | FormRequestPayload\n ): Promise<FormSubmissionState> {\n const key = this.Key(request)\n\n let cached = await this.cache.get(key)\n\n // If nothing in Redis, attempt to rehydrate from backend DB\n if (!cached && this.customFetcher) {\n const rehydrated = await this.customFetcher(request)\n\n if (rehydrated != null) {\n await this.cache.set(key, rehydrated, config.get('sessionTimeout'))\n cached = await this.getState(request)\n }\n }\n\n return cached ?? {}\n }\n\n async setState(\n request: FormRequest | FormRequestPayload,\n state: FormSubmissionState\n ) {\n const key = this.Key(request)\n const ttl = config.get('sessionTimeout')\n\n await this.cache.set(key, state, ttl)\n\n return this.getState(request)\n }\n\n async getConfirmationState(\n request: FormRequest | FormRequestPayload\n ): Promise<{ confirmed?: true }> {\n const key = this.Key(request, ADDITIONAL_IDENTIFIER.Confirmation)\n const value = await this.cache.get(key)\n\n return value ?? {}\n }\n\n async setConfirmationState(\n request: FormRequest | FormRequestPayload,\n confirmationState: { confirmed?: true }\n ) {\n const key = this.Key(request, ADDITIONAL_IDENTIFIER.Confirmation)\n const ttl = config.get('confirmationSessionTimeout')\n\n return this.cache.set(key, confirmationState, ttl)\n }\n\n async clearState(request: FormRequest | FormRequestPayload) {\n if (request.yar.id) {\n await this.cache.drop(this.Key(request))\n }\n }\n\n getFlash(\n request: FormRequest | FormRequestPayload\n ): { errors: FormSubmissionError[] } | undefined {\n const key = this.Key(request)\n const messages = request.yar.flash(key.id)\n\n if (Array.isArray(messages) && messages.length) {\n return messages.at(0) as { errors: FormSubmissionError[] }\n }\n }\n\n setFlash(\n request: FormRequest | FormRequestPayload,\n message: { errors: FormSubmissionError[] }\n ) {\n const key = this.Key(request)\n\n request.yar.flash(key.id, message)\n }\n\n private defaultKeyGenerator(\n request: Request | FormRequest | FormRequestPayload\n ): string {\n if (!request.yar.id) {\n throw new Error('No session ID found')\n }\n\n const state = (request.params.state as string) || ''\n const slug = (request.params.slug as string) || ''\n return `${request.yar.id}:${state}:${slug}:`\n }\n\n /**\n * The key used to store user session data against.\n * If there are multiple forms on the same runner instance, for example `form-a` and `form-a-feedback` this will prevent CacheService from clearing data from `form-a` if a user gave feedback before they finished `form-a`\n * @param request - hapi request object\n * @param additionalIdentifier - appended to the id\n */\n Key(\n request: Request | FormRequest | FormRequestPayload,\n additionalIdentifier?: ADDITIONAL_IDENTIFIER\n ) {\n const baseKey = this.generateKey\n ? this.generateKey(request)\n : this.defaultKeyGenerator(request)\n\n return {\n segment: partition,\n id: `${baseKey}${additionalIdentifier ?? ''}`\n }\n }\n}\n\n/**\n * State merge helper\n * 1. Merges objects (form fields)\n * 2. Overwrites arrays\n */\nexport function merge<StateType extends FormState | FormPayload>(\n state: StateType,\n update: object\n): StateType {\n return Hoek.merge(state, update, {\n mergeArrays: false\n })\n}\n"],"mappings":"AACA,OAAO,KAAKA,IAAI,MAAM,YAAY;AAElC,SAASC,MAAM;AAaf,MAAMC,SAAS,GAAG,OAAO;AAEzB,WAAYC,qBAAqB,0BAArBA,qBAAqB;EAArBA,qBAAqB;EAAA,OAArBA,qBAAq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;IAC7C,CAAC;EACH;AACF;;AAEA;AACA;AACA;AACA;AACA;AACA,OAAO,SAASE,KAAKA,CACnBzB,KAAgB,EAChB0B,MAAc,EACH;EACX,OAAOzD,IAAI,CAACwD,KAAK,CAACzB,KAAK,EAAE0B,MAAM,EAAE;IAC/BC,WAAW,EAAE;EACf,CAAC,CAAC;AACJ","ignoreList":[]}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@defra/forms-engine-plugin",
3
- "version": "1.3.1",
3
+ "version": "1.4.0",
4
4
  "description": "Defra forms engine",
5
5
  "type": "module",
6
6
  "files": [
@@ -639,3 +639,42 @@ describe('prepareEnvironment', () => {
639
639
  )
640
640
  })
641
641
  })
642
+
643
+ describe('Exit route handlers', () => {
644
+ let server: Server
645
+
646
+ beforeAll(async () => {
647
+ server = await createServer({
648
+ services: defaultServices
649
+ })
650
+ await server.initialize()
651
+ })
652
+
653
+ afterAll(async () => {
654
+ await server.stop()
655
+ })
656
+
657
+ beforeEach(() => {
658
+ jest.mocked(getFormMetadata).mockResolvedValue(fixtures.form.metadata)
659
+ server.app.models.clear()
660
+ })
661
+
662
+ test('GET /exit returns 200 with exit page content', async () => {
663
+ jest.mocked(getFormMetadata).mockResolvedValueOnce({
664
+ ...fixtures.form.metadata,
665
+ live: fixtures.form.state
666
+ })
667
+
668
+ jest.mocked(getFormDefinition).mockResolvedValue(fixtures.form.definition)
669
+
670
+ const options = {
671
+ method: 'GET',
672
+ url: `${FORM_PREFIX}/slug/exit`
673
+ }
674
+
675
+ const res = await server.inject(options)
676
+
677
+ expect(res.statusCode).toBe(StatusCodes.OK)
678
+ expect(res.result).toContain('Your progress has been saved')
679
+ })
680
+ })
@@ -1,5 +1,6 @@
1
1
  import { type ComponentDef } from '@defra/forms-model'
2
2
 
3
+ import { ComponentBase } from '~/src/server/plugins/engine/components/ComponentBase.js'
3
4
  import { createComponent } from '~/src/server/plugins/engine/components/helpers.js'
4
5
  import { FormModel } from '~/src/server/plugins/engine/models/FormModel.js'
5
6
  import definition from '~/test/form/definitions/basic.js'
@@ -22,3 +23,33 @@ describe('helpers tests', () => {
22
23
  ).toThrow('Component type invalid-type does not exist')
23
24
  })
24
25
  })
26
+
27
+ describe('ComponentBase tests', () => {
28
+ test('should handle save and return functionality', () => {
29
+ const mockComponentDef = {
30
+ type: 'TextField',
31
+ name: 'testField',
32
+ title: 'Test Field'
33
+ } as ComponentDef
34
+
35
+ const component = new ComponentBase(mockComponentDef, { model: formModel })
36
+
37
+ expect(component.name).toBe('testField')
38
+ expect(component.title).toBe('Test Field')
39
+ expect(component.type).toBe('TextField')
40
+ })
41
+
42
+ test('should handle context correctly', () => {
43
+ const mockComponentDef = {
44
+ type: 'TextField',
45
+ name: 'contextField',
46
+ title: 'Context Field'
47
+ } as ComponentDef
48
+
49
+ const component = new ComponentBase(mockComponentDef, { model: formModel })
50
+
51
+ expect(component.model).toBe(formModel)
52
+ expect(component.name).toBe('contextField')
53
+ expect(component.title).toBe('Context Field')
54
+ })
55
+ })
@@ -33,9 +33,7 @@ export const prepareNunjucksEnvironment = function (
33
33
  env.addFilter(name, nunjucksFilter)
34
34
  }
35
35
 
36
- env.addFilter('markdown', (text: string) =>
37
- markdownToHtml(text, pluginOptions.baseUrl)
38
- )
36
+ env.addFilter('markdown', (text: string) => markdownToHtml(text))
39
37
 
40
38
  for (const [name, nunjucksGlobal] of Object.entries(globals)) {
41
39
  env.addGlobal(name, nunjucksGlobal)
@@ -3,6 +3,7 @@ import {
3
3
  FormModel,
4
4
  SummaryViewModel
5
5
  } from '~/src/server/plugins/engine/models/index.js'
6
+ import { SummaryPageController } from '~/src/server/plugins/engine/pageControllers/SummaryPageController.js'
6
7
  import {
7
8
  createPage,
8
9
  type PageControllerClass
@@ -249,3 +250,61 @@ describe('SummaryViewModel', () => {
249
250
  })
250
251
  })
251
252
  })
253
+
254
+ describe('SummaryPageController', () => {
255
+ let model: FormModel
256
+ let controller: SummaryPageController
257
+ let request: FormContextRequest
258
+
259
+ beforeEach(() => {
260
+ model = new FormModel(definition, {
261
+ basePath: `${FORM_PREFIX}/test`
262
+ })
263
+
264
+ controller = new SummaryPageController(model, definition.pages[2])
265
+
266
+ request = {
267
+ method: 'get',
268
+ url: new URL('http://example.com/repeat/pizza-order/summary'),
269
+ path: '/repeat/pizza-order/summary',
270
+ params: {
271
+ path: 'pizza-order',
272
+ slug: 'repeat'
273
+ },
274
+ query: {},
275
+ app: { model }
276
+ }
277
+ })
278
+
279
+ describe('Save and Return functionality', () => {
280
+ it('should show save and return button on summary page', () => {
281
+ expect(controller.shouldShowSaveAndReturn()).toBe(true)
282
+ })
283
+
284
+ it('should handle save and return from summary page', () => {
285
+ const state: FormState = {
286
+ $$__referenceNumber: 'foobar',
287
+ orderType: 'collection',
288
+ pizza: []
289
+ }
290
+
291
+ const context = model.getFormContext(request, state)
292
+ const viewModel = controller.getViewModel(request, context)
293
+
294
+ expect(viewModel).toHaveProperty('allowSaveAndReturn', true)
295
+ })
296
+
297
+ it('should display correct page title', () => {
298
+ const state: FormState = {
299
+ $$__referenceNumber: 'foobar',
300
+ orderType: 'collection',
301
+ pizza: []
302
+ }
303
+
304
+ const context = model.getFormContext(request, state)
305
+ const viewModel = controller.getViewModel(request, context)
306
+
307
+ expect(viewModel.pageTitle).toBe('Check your answers')
308
+ })
309
+ })
310
+ })