diff --git a/src/server/plugins/engine/pageControllers/helpers/submission.test.ts b/src/server/plugins/engine/pageControllers/helpers/submission.test.ts index d72ef6258..c17a7aacc 100644 --- a/src/server/plugins/engine/pageControllers/helpers/submission.test.ts +++ b/src/server/plugins/engine/pageControllers/helpers/submission.test.ts @@ -284,6 +284,36 @@ describe('Submission helpers', () => { } ]) }) + + it('should send empty string for optional GeospatialField', () => { + const mockGeospatialField = Object.create( + GeospatialField.prototype + ) as GeospatialField + mockGeospatialField.name = 'geospatial' + mockGeospatialField.options = { required: false } + + const items = [ + { + name: 'geospatial', + label: 'Site features', + field: mockGeospatialField, + state: { + geospatial: undefined + } as FormSubmissionState + } + ] as unknown as DetailItemField[] + + const result = buildMainRecords(items) + + expect(result).toHaveLength(1) + expect(result).toEqual([ + { + name: 'geospatial', + title: 'Site features', + value: '' + } + ]) + }) }) describe('buildRepeaterRecords', () => { @@ -383,5 +413,51 @@ describe('Submission helpers', () => { } ]) }) + + it('should send empty string for optional GeospatialField', () => { + const mockGeospatialField = Object.create( + GeospatialField.prototype + ) as GeospatialField + mockGeospatialField.name = 'geospatial' + mockGeospatialField.options = { required: false } + + const items = [ + { + name: 'features', + label: 'Site features repeater', + subItems: [ + [ + { + name: 'geospatial', + label: 'Site features', + field: mockGeospatialField, + state: { + geospatial: undefined + } as FormSubmissionState + } as unknown as DetailItemField[] + ] + ] + } + ] as unknown as DetailItemField[] + + const result = buildRepeaterRecords(items) + + expect(result).toHaveLength(1) + expect(result).toEqual([ + { + name: 'features', + title: 'Site features repeater', + value: [ + [ + { + name: 'geospatial', + title: 'Site features', + value: '' + } + ] + ] + } + ]) + }) }) }) diff --git a/src/server/plugins/engine/pageControllers/helpers/submission.ts b/src/server/plugins/engine/pageControllers/helpers/submission.ts index 34810bdd8..f2974d2ec 100644 --- a/src/server/plugins/engine/pageControllers/helpers/submission.ts +++ b/src/server/plugins/engine/pageControllers/helpers/submission.ts @@ -36,10 +36,11 @@ export function buildMainRecords(items: DetailItem[]): SubmitRecord[] { } else if (item.field instanceof GeospatialField) { // Stringify of GeoJSON is done here rather than inside `getContextValueFromState` // so we don't incur the overhead of JSON.stringify on every request when building context + const value = item.field.getFormValueFromState(item.state) records.push({ name: item.name, title: item.label, - value: JSON.stringify(item.field.getFormValueFromState(item.state)) + value: value === undefined ? '' : JSON.stringify(value) }) } else { records.push({ @@ -109,18 +110,24 @@ export function buildRepeaterRecords( name: item.name, title: item.label, value: item.subItems.map((detailItems) => - detailItems.map((subItem) => ({ - name: subItem.name, - title: subItem.label, - value: + detailItems.map((subItem) => { + let value + + if (subItem.field instanceof GeospatialField) { // Stringify of GeoJSON is done here rather than inside `getContextValueFromState` // so we don't incur the overhead of JSON.stringify on every request when building context - subItem.field instanceof GeospatialField - ? JSON.stringify( - subItem.field.getFormValueFromState(subItem.state) - ) - : getAnswer(subItem.field, subItem.state, { format: 'data' }) - })) + const formValue = subItem.field.getFormValueFromState(subItem.state) + value = formValue === undefined ? '' : JSON.stringify(formValue) + } else { + value = getAnswer(subItem.field, subItem.state, { format: 'data' }) + } + + return { + name: subItem.name, + title: subItem.label, + value + } + }) ) })) }