66 getErrorMessage ,
77 hasComponentsEvenIfNoNext ,
88 hasPaymentQuestionInForm ,
9- hasSpecificQuestionTypeInForm
9+ hasPostcodeLookupInForm ,
10+ hasSpecificQuestionTypeInForm ,
11+ isSummaryPage
1012} from '@defra/forms-model'
1113import { StatusCodes } from 'http-status-codes'
1214
@@ -151,8 +153,8 @@ export function calcSummaryMetrics(metadata, definition, definitionType) {
151153 status : definitionType ,
152154 pages : definition . pages . length ,
153155 questionTypes : getUniqueComponentTypes ( definition ) . size ,
154- conditions : definition . conditions . length ,
155- sections : definition . sections . length ,
156+ conditions : getUniqueAssignedConditions ( definition ) . size ,
157+ sections : getUniqueAssignedSections ( definition ) . size ,
156158 features : getFeatureList ( definition )
157159 } )
158160}
@@ -166,7 +168,21 @@ export function calcFeatureMetrics(definition) {
166168 if ( hasComponentsEvenIfNoNext ( page ) ) {
167169 allComponents . push ( ...page . components )
168170 }
171+ // Special case - if declaration in CYA page, remove the Markdown component,
172+ // and add 'Declaration in CYA' component to totals
173+ if ( isSummaryPage ( page ) && hasDeclarationInCYA ( definition ) ) {
174+ const markdownPos = allComponents . findIndex (
175+ ( comp ) => comp . type === ComponentType . Markdown
176+ )
177+ if ( markdownPos > - 1 ) {
178+ allComponents . splice ( markdownPos , 1 )
179+ // @ts -expect-error - 'DeclarationInCYA' is not strictly in the enum of ComponentType
180+ // but we want a separate value for metrics display purposes
181+ allComponents . push ( { type : 'DeclarationInCYA' } )
182+ }
183+ }
169184 }
185+
170186 const questionTypes = getQuestionTypeCounts ( allComponents )
171187 return {
172188 questionTypes : Object . fromEntries ( questionTypes ) ,
@@ -192,10 +208,10 @@ export function getQuestionTypeCounts(components) {
192208 */
193209export function getComponentUsageFeatureMetrics ( definition ) {
194210 const features = getFeatureList ( definition )
195- if ( definition . pages . some ( ( p ) => p . section ) ) {
211+ if ( getUniqueAssignedSections ( definition ) . size ) {
196212 features . push ( 'Sections' )
197213 }
198- if ( definition . pages . some ( ( p ) => p . condition ) ) {
214+ if ( getUniqueAssignedConditions ( definition ) . size ) {
199215 features . push ( 'Conditional logic' )
200216 }
201217 const featureResult = /** @type {Record<string, number> } */ ( { } )
@@ -218,12 +234,25 @@ export function getFormStructureCounts(definition, questionTypes) {
218234 return {
219235 pages : definition . pages . length ,
220236 questions : numOfQuestions ,
221- sections : definition . pages . filter ( ( p ) => p . section ) . length ,
222- conditions : definition . pages . filter ( ( p ) => p . condition ) . length ,
237+ sections : getUniqueAssignedSections ( definition ) . size ,
238+ conditions : getUniqueAssignedConditions ( definition ) . size ,
223239 questionTypes : questionTypes . size
224240 }
225241}
226242
243+ /**
244+ * @param {FormDefinition } definition
245+ */
246+ export function hasDeclarationInCYA ( definition ) {
247+ const summaryPage = definition . pages . find ( ( pg ) => isSummaryPage ( pg ) )
248+ const markdown = hasComponentsEvenIfNoNext ( summaryPage )
249+ ? summaryPage . components . find (
250+ ( comp , idx ) => comp . type === ComponentType . Markdown && idx === 0
251+ )
252+ : undefined
253+ return markdown !== undefined
254+ }
255+
227256/**
228257 * @param {FormDefinition } definition
229258 */
@@ -247,13 +276,21 @@ export function getFeatureList(definition) {
247276 if (
248277 hasSpecificQuestionTypeInForm ( definition , ComponentType . DeclarationField )
249278 ) {
250- features . push ( 'Declarations' )
279+ features . push ( 'Declaration field' )
280+ }
281+ if ( hasDeclarationInCYA ( definition ) ) {
282+ features . push ( 'Declaration in CYA' )
283+ }
284+ if ( hasPostcodeLookupInForm ( definition ) ) {
285+ features . push ( 'Postcode lookup' )
286+ }
287+ if ( definition . options ?. showReferenceNumber ) {
288+ features . push ( 'Reference number' )
251289 }
252290 return features
253291}
254292
255293/**
256- *
257294 * @param {FormDefinition } definition
258295 */
259296export function getUniqueComponentTypes ( definition ) {
@@ -266,6 +303,24 @@ export function getUniqueComponentTypes(definition) {
266303 return componentTypes
267304}
268305
306+ /**
307+ * @param {FormDefinition } definition
308+ */
309+ export function getUniqueAssignedConditions ( definition ) {
310+ return new Set (
311+ definition . pages . filter ( ( p ) => p . condition ) . map ( ( p2 ) => p2 . condition )
312+ )
313+ }
314+
315+ /**
316+ * @param {FormDefinition } definition
317+ */
318+ export function getUniqueAssignedSections ( definition ) {
319+ return new Set (
320+ definition . pages . filter ( ( p ) => p . section ) . map ( ( p2 ) => p2 . section )
321+ )
322+ }
323+
269324/**
270325 * @import { ClientSession } from 'mongodb'
271326 * @import { ComponentDef, FormDefinition, FormMetadata } from '@defra/forms-model'
0 commit comments