Skip to content

Commit 3782284

Browse files
authored
DMT | Fix divorce date (#43887)
* Move reformatDate to formHelpers * Add onFormLoaded date processing
1 parent 7020a26 commit 3782284

7 files changed

Lines changed: 90 additions & 40 deletions

File tree

src/applications/dependents/686c-674/components/picklist/PicklistRemoveDependentFollowup.jsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ import {
1616
import { routing, getPicklistRoutes } from './routes';
1717
import { showExitLink } from './utils';
1818

19-
import { reformatDate } from '../../config/utilities/submission';
19+
import { reformatDate } from '../../config/utilities';
2020

2121
import { getFullName } from '../../../shared/utils';
2222
import ExitForm from '../../../shared/components/ExitFormLink';

src/applications/dependents/686c-674/config/form.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
showOptionsSelection,
5757
isAddingDependents,
5858
isRemovingDependents,
59+
onFormLoaded,
5960
} from './utilities';
6061
import migrations from './migrations';
6162
import reviewDependents from './chapters/picklist/reviewDependents';
@@ -102,6 +103,7 @@ export const formConfig = {
102103
prefillEnabled: true,
103104
prefillTransformer,
104105
verifyRequiredPrefill: true,
106+
onFormLoaded,
105107
footerContent: FormFooter,
106108
getHelp: GetFormHelp,
107109
downtime: {

src/applications/dependents/686c-674/config/utilities/formHelpers.js

Lines changed: 41 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
1-
import { parse, parseISO, isValid } from 'date-fns';
1+
import { parse, parseISO, isValid, format } from 'date-fns';
22

33
import omit from 'platform/utilities/data/omit';
44

55
import { stringifyFormReplacer } from 'platform/forms-system/src/js/helpers';
66

7-
import { MARRIAGE_TYPES, PICKLIST_DATA } from '../constants';
7+
import {
8+
MARRIAGE_TYPES,
9+
PICKLIST_DATA,
10+
FORMAT_YMD_DATE_FNS,
11+
} from '../constants';
812

913
export const PHONE_KEYS = ['phoneNumber', 'internationalPhone'];
1014
const WHITESPACE_REGEX = /\s+/g;
@@ -229,3 +233,38 @@ export const isVisiblePicklistPage = (formData, relationship) => {
229233
*/
230234
export const hasSelectedPicklistItems = formData =>
231235
(formData?.[PICKLIST_DATA] || []).some(item => item.selected);
236+
237+
/**
238+
* Process date string for submission by adding leading zeros to month and day
239+
* parts using date-fns
240+
* @param {String} date - date string
241+
* @returns {String} Processed date string
242+
*/
243+
export function reformatDate(date) {
244+
const dateObj = parseDateToDateObj(date, FORMAT_YMD_DATE_FNS);
245+
if (!dateObj) return date; // Return original value if parsing fails
246+
return format(dateObj, FORMAT_YMD_DATE_FNS);
247+
}
248+
249+
/**
250+
* Clean up dates in yyyy-MM-dd format that don't include leading zeros in the
251+
* month and day fields
252+
* @param {object} props - The props object
253+
* @param {string} props.returnUrl - The URL to return to after the form is loaded
254+
* @param {object} props.router - The router object used to navigate to different pages
255+
* @param {object} props.formData - The form data that has been loaded
256+
* @returns {void}
257+
*/
258+
export const onFormLoaded = props => {
259+
const { returnUrl, router, formData } = props;
260+
261+
// Editing data in place
262+
formData?.[PICKLIST_DATA]?.forEach(item => {
263+
if (item?.endDate && item.endDate.length !== 10) {
264+
// eslint-disable-next-line no-param-reassign
265+
item.endDate = reformatDate(item.endDate);
266+
}
267+
});
268+
269+
router.push(returnUrl);
270+
};

src/applications/dependents/686c-674/config/utilities/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import {
2424
hasAwardedDependents,
2525
isVisiblePicklistPage,
2626
hasSelectedPicklistItems,
27+
reformatDate,
28+
onFormLoaded,
2729
} from './formHelpers';
2830
import { buildSubmissionData, customTransformForSubmit } from './submission';
2931

@@ -55,4 +57,6 @@ export {
5557
hasAwardedDependents,
5658
isVisiblePicklistPage,
5759
hasSelectedPicklistItems,
60+
reformatDate,
61+
onFormLoaded,
5862
};

src/applications/dependents/686c-674/config/utilities/submission.js

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import cloneDeep from 'lodash/cloneDeep';
2-
import { format } from 'date-fns';
32

43
import {
54
filterInactivePageData,
@@ -19,14 +18,14 @@ import { isVetInReceiptOfPension } from './api';
1918
import {
2019
customFormReplacer,
2120
showV3Picklist,
22-
parseDateToDateObj,
21+
reformatDate,
2322
} from './formHelpers';
2423
import {
2524
transformPicklistToV2,
2625
enrichDivorceWithSSN,
2726
cleanupAddressData,
2827
} from './picklistTransform';
29-
import { PICKLIST_REMOVAL_FLAG, FORMAT_YMD_DATE_FNS } from '../constants';
28+
import { PICKLIST_REMOVAL_FLAG } from '../constants';
3029

3130
/**
3231
* Extract data fields with values from source data
@@ -530,18 +529,6 @@ function cleanDataWithAddresses(data = {}) {
530529
};
531530
}
532531

533-
/**
534-
* Process date string for submission by adding leading zeros to month and day
535-
* parts using date-fns
536-
* @param {String} date - date string
537-
* @returns {String} Processed date string
538-
*/
539-
export function reformatDate(date) {
540-
const dateObj = parseDateToDateObj(date, FORMAT_YMD_DATE_FNS);
541-
if (!dateObj) return date; // Return original value if parsing fails
542-
return format(dateObj, FORMAT_YMD_DATE_FNS);
543-
}
544-
545532
/**
546533
* Add leading zeros to month and day parts in YYYY-MM-DD format
547534
* @param {Object} data - form data

src/applications/dependents/686c-674/tests/config/utilities.formHelpers.unit.spec.jsx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { expect } from 'chai';
2+
import sinon from 'sinon';
23

34
import {
45
customFormReplacer,
@@ -13,6 +14,8 @@ import {
1314
isRemovingDependents,
1415
isVisiblePicklistPage,
1516
hasSelectedPicklistItems,
17+
reformatDate,
18+
onFormLoaded,
1619
} from '../../config/utilities/formHelpers';
1720

1821
import { PICKLIST_DATA } from '../../config/constants';
@@ -373,3 +376,40 @@ describe('hasSelectedPicklistItems', () => {
373376
expect(hasSelectedPicklistItems(getData(true, true))).to.be.true;
374377
});
375378
});
379+
380+
describe('reformatDate', () => {
381+
it('should return empty string for invalid dates', () => {
382+
expect(reformatDate()).to.be.undefined;
383+
expect(reformatDate('')).to.equal('');
384+
expect(reformatDate('invalid-date')).to.equal('invalid-date');
385+
});
386+
387+
it('should handle double-digit months and days', () => {
388+
expect(reformatDate('2026-04-05')).to.equal('2026-04-05');
389+
expect(reformatDate('2026-11-10')).to.equal('2026-11-10');
390+
expect(reformatDate('2026-12-11')).to.equal('2026-12-11');
391+
expect(reformatDate('2026-10-31')).to.equal('2026-10-31');
392+
});
393+
394+
it('should convert date and include leading zeros in yyyy-MM-dd format', () => {
395+
expect(reformatDate('2026-1-3')).to.equal('2026-01-03');
396+
expect(reformatDate('2026-11-4')).to.equal('2026-11-04');
397+
expect(reformatDate('2026-4-11')).to.equal('2026-04-11');
398+
});
399+
});
400+
401+
describe('onFormLoaded', () => {
402+
it('should reformat endDate and push to returnUrl', () => {
403+
const router = { push: sinon.spy() };
404+
const formData = {
405+
[PICKLIST_DATA]: [{ endDate: '2024-1-1' }, { endDate: '2024-2-2' }],
406+
};
407+
const returnUrl = '/test-return-url';
408+
409+
onFormLoaded({ returnUrl, router, formData });
410+
411+
expect(formData[PICKLIST_DATA][0].endDate).to.equal('2024-01-01');
412+
expect(formData[PICKLIST_DATA][1].endDate).to.equal('2024-02-02');
413+
expect(router.push.calledWith(returnUrl)).to.be.true;
414+
});
415+
});

src/applications/dependents/686c-674/tests/config/utilities.submission.unit.spec.jsx

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import set from 'platform/utilities/data/set';
55
import {
66
buildSubmissionData,
77
customTransformForSubmit,
8-
reformatDate,
98
cleanUpDates,
109
} from '../../config/utilities/submission';
1110

@@ -1249,27 +1248,6 @@ describe('customTransformForSubmit integration', () => {
12491248
});
12501249

12511250
describe('clean up utilities', () => {
1252-
context('reformatDate', () => {
1253-
it('should return empty string for invalid dates', () => {
1254-
expect(reformatDate()).to.be.undefined;
1255-
expect(reformatDate('')).to.equal('');
1256-
expect(reformatDate('invalid-date')).to.equal('invalid-date');
1257-
});
1258-
1259-
it('should handle double-digit months and days', () => {
1260-
expect(reformatDate('2026-04-05')).to.equal('2026-04-05');
1261-
expect(reformatDate('2026-11-10')).to.equal('2026-11-10');
1262-
expect(reformatDate('2026-12-11')).to.equal('2026-12-11');
1263-
expect(reformatDate('2026-10-31')).to.equal('2026-10-31');
1264-
});
1265-
1266-
it('should convert date and include leading zeros in yyyy-MM-dd format', () => {
1267-
expect(reformatDate('2026-1-3')).to.equal('2026-01-03');
1268-
expect(reformatDate('2026-11-4')).to.equal('2026-11-04');
1269-
expect(reformatDate('2026-4-11')).to.equal('2026-04-11');
1270-
});
1271-
});
1272-
12731251
context('cleanUpDates', () => {
12741252
const getData = date => ({
12751253
reportDivorce: { divorceDate: date },

0 commit comments

Comments
 (0)