@defra/forms-engine-plugin 4.0.28 → 4.0.29

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.
@@ -4,19 +4,17 @@ import {
4
4
  type ResponseToolkit,
5
5
  type Server
6
6
  } from '@hapi/hapi'
7
- import { isEqual } from 'date-fns'
8
7
 
9
8
  import {
10
9
  EXTERNAL_STATE_APPENDAGE,
11
- EXTERNAL_STATE_PAYLOAD,
12
- PREVIEW_PATH_PREFIX
10
+ EXTERNAL_STATE_PAYLOAD
13
11
  } from '~/src/server/constants.js'
12
+ import { resolveFormModel } from '~/src/server/plugins/engine/beta/form-context.js'
14
13
  import {
15
14
  FormComponent,
16
15
  isFormState
17
16
  } from '~/src/server/plugins/engine/components/FormComponent.js'
18
17
  import {
19
- checkEmailAddressForLiveFormSubmission,
20
18
  checkFormStatus,
21
19
  findPage,
22
20
  getCacheService,
@@ -24,7 +22,6 @@ import {
24
22
  getStartPath,
25
23
  proceed
26
24
  } from '~/src/server/plugins/engine/helpers.js'
27
- import { FormModel } from '~/src/server/plugins/engine/models/index.js'
28
25
  import { type PageControllerClass } from '~/src/server/plugins/engine/pageControllers/helpers/pages.js'
29
26
  import { generateUniqueReference } from '~/src/server/plugins/engine/referenceNumbers.js'
30
27
  import * as defaultServices from '~/src/server/plugins/engine/services/index.js'
@@ -179,8 +176,6 @@ export function makeLoadFormPreHandler(server: Server, options: PluginOptions) {
179
176
  ordnanceSurveyApiKey
180
177
  } = options
181
178
 
182
- const { formsService } = services
183
-
184
179
  async function handler(request: AnyFormRequest, h: ResponseToolkit) {
185
180
  if (server.app.model) {
186
181
  request.app.model = server.app.model
@@ -192,71 +187,15 @@ export function makeLoadFormPreHandler(server: Server, options: PluginOptions) {
192
187
  const { slug } = params
193
188
  const { isPreview, state: formState } = checkFormStatus(params)
194
189
 
195
- // Get the form metadata using the `slug` param
196
- const metadata = await formsService.getFormMetadata(slug)
197
-
198
- const { id, [formState]: state } = metadata
199
-
200
- // Check the metadata supports the requested state
201
- if (!state) {
202
- throw Boom.notFound(`No '${formState}' state for form metadata ${id}`)
203
- }
204
-
205
- // Cache the models based on id, state and whether
206
- // it's a preview or not. There could be up to 3 models
207
- // cached for a single form:
208
- // "{id}_live_false" (live/live)
209
- // "{id}_live_true" (live/preview)
210
- // "{id}_draft_true" (draft/preview)
211
- const key = `${id}_${formState}_${isPreview}`
212
- let item = server.app.models.get(key)
213
-
214
- if (!item || !isEqual(item.updatedAt, state.updatedAt)) {
215
- server.logger.info(`Getting form definition ${id} (${slug}) ${formState}`)
216
-
217
- // Get the form definition using the `id` from the metadata
218
- const definition = await formsService.getFormDefinition(id, formState)
219
-
220
- if (!definition) {
221
- throw Boom.notFound(
222
- `No definition found for form metadata ${id} (${slug}) ${formState}`
223
- )
224
- }
225
-
226
- const emailAddress = metadata.notificationEmail ?? definition.outputEmail
227
-
228
- checkEmailAddressForLiveFormSubmission(emailAddress, isPreview)
229
-
230
- // Build the form model
231
- server.logger.info(
232
- `Building model for form definition ${id} (${slug}) ${formState}`
233
- )
234
-
235
- // Set up the basePath for the model
236
- const basePath = (
237
- isPreview
238
- ? `${prefix}${PREVIEW_PATH_PREFIX}/${formState}/${slug}`
239
- : `${prefix}/${slug}`
240
- ).substring(1)
241
-
242
- const versionNumber = metadata.versions?.[0]?.versionNumber
243
-
244
- // Construct the form model
245
- const model = new FormModel(
246
- definition,
247
- { basePath, versionNumber, ordnanceSurveyApiKey, formId: id },
248
- services,
249
- controllers
250
- )
251
-
252
- // Create new item and add it to the item cache
253
- item = { model, updatedAt: state.updatedAt }
254
- server.app.models.set(key, item)
255
- }
190
+ const model = await resolveFormModel(server, slug, formState, {
191
+ services,
192
+ controllers,
193
+ ordnanceSurveyApiKey,
194
+ routePrefix: prefix,
195
+ isPreview
196
+ })
256
197
 
257
- // Assign the model to the request data
258
- // for use in the downstream handler
259
- request.app.model = item.model
198
+ request.app.model = model
260
199
 
261
200
  return h.continue
262
201
  }