From 325eb847141d61d8633e13aa25ce973cc97ba8b1 Mon Sep 17 00:00:00 2001 From: Daniel Liburd Date: Tue, 14 Nov 2023 16:40:43 +0000 Subject: [PATCH] Strip whitespace from dtr date for report If this date contains whitespace it will form a bad request and prevent submission of the application. Here, we remove any whitespace before submitting. --- .../reportDataFromApplication.test.ts | 16 ++++++++++++++-- .../applications/reportDataFromApplication.ts | 3 ++- server/utils/utils.test.ts | 7 +++++++ server/utils/utils.ts | 4 ++++ 4 files changed, 27 insertions(+), 3 deletions(-) diff --git a/server/utils/applications/reportDataFromApplication.test.ts b/server/utils/applications/reportDataFromApplication.test.ts index f57a02aa4..4b3a5117d 100644 --- a/server/utils/applications/reportDataFromApplication.test.ts +++ b/server/utils/applications/reportDataFromApplication.test.ts @@ -37,11 +37,23 @@ describe('reportDataFromApplication', () => { data: { 'accommodation-referral-details': { 'dtr-submitted': { dtrSubmitted: 'yes' }, - 'dtr-details': { date: 'Duty of care submission date' }, + 'dtr-details': { date: '2023-05-24' }, }, }, }) - expect(dutyToReferSubmissionDateFromApplication(application)).toEqual('Duty of care submission date') + expect(dutyToReferSubmissionDateFromApplication(application)).toEqual('2023-05-24') + }) + + it('strips the date if it contains whitespace', () => { + const application = applicationFactory.build({ + data: { + 'accommodation-referral-details': { + 'dtr-submitted': { dtrSubmitted: 'yes' }, + 'dtr-details': { date: ' 2023 - 05 - 24 ' }, + }, + }, + }) + expect(dutyToReferSubmissionDateFromApplication(application)).toEqual('2023-05-24') }) }) diff --git a/server/utils/applications/reportDataFromApplication.ts b/server/utils/applications/reportDataFromApplication.ts index 65ee26e2f..20da2093f 100644 --- a/server/utils/applications/reportDataFromApplication.ts +++ b/server/utils/applications/reportDataFromApplication.ts @@ -1,6 +1,7 @@ import { eligibilityReasons } from '../../form-pages/apply/accommodation-need/eligibility/eligibilityReason' import { TemporaryAccommodationApplication as Application } from '../../@types/shared' import { SessionDataError } from '../errors' +import { stripWhitespace } from '../utils' const isDutyToReferSubmittedFromApplication = (application: Application): boolean => { const isDutyToReferSubmitted: string = (application.data as Record)?.[ @@ -29,7 +30,7 @@ const dutyToReferSubmissionDateFromApplication = (application: Application): str throw new SessionDataError('No duty to refer submitted date') } - return dutyToReferSubmissionDate + return stripWhitespace(dutyToReferSubmissionDate) } const needsAccessiblePropertyFromApplication = (application: Application): boolean => { diff --git a/server/utils/utils.test.ts b/server/utils/utils.test.ts index 3cef83402..faa312e33 100644 --- a/server/utils/utils.test.ts +++ b/server/utils/utils.test.ts @@ -12,6 +12,7 @@ import { pascalCase, removeBlankSummaryListItems, sentenceCase, + stripWhitespace, unique, } from './utils' @@ -231,3 +232,9 @@ describe('unique', () => { ) }) }) + +describe('stripWhitespace', () => { + it('removes whitespace from a string', () => { + expect(stripWhitespace(' 2034 - 05 - 25 ')).toEqual('2034-05-25') + }) +}) diff --git a/server/utils/utils.ts b/server/utils/utils.ts index 2dec133a2..c0a8cdc19 100644 --- a/server/utils/utils.ts +++ b/server/utils/utils.ts @@ -118,3 +118,7 @@ export const appendQueryString = ( } return `${path}${queryString ? `?${queryString}` : ''}` } + +export const stripWhitespace = (text: string): string => { + return text.replace(/\s+/g, '') +}