Skip to content

Commit

Permalink
updated address transform to use full country name (#33745)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelclement authored Dec 27, 2024
1 parent 9730f6c commit 31b591d
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* eslint-disable camelcase */
import CONSTANTS from 'vets-json-schema/dist/constants.json'; // For countries
import { formatDateShort } from 'platform/utilities/date';
import { transformForSubmit as formsSystemTransformForSubmit } from 'platform/forms-system/src/js/helpers';
import { concatStreets } from '../../shared/utilities';
Expand All @@ -12,6 +13,21 @@ function stringifyAddress(addr) {
: '';
}

/**
* Converts country codes to full country names and returns updated address.
* e.g., {country: 'USA'} => {country: 'United States'}
* @param {object} addr Standard address object provided by the addressUI component.
* @returns Updated address object with country value replaced or left alone depending on presence of matching country label in the CONSTANTS file.
*/
function getCountryLabel(addr) {
const tmpAdr = addr;
// Find country label that matches country code in `addr`
tmpAdr.country =
CONSTANTS.countries.filter(c => c.value === tmpAdr.country)[0]?.label ??
tmpAdr.country; // leave untouched if no match found
return tmpAdr;
}

export default function transformForSubmit(formConfig, form) {
const transformedData = JSON.parse(
formsSystemTransformForSubmit(formConfig, form),
Expand Down Expand Up @@ -62,6 +78,14 @@ export default function transformForSubmit(formConfig, form) {
dataPostTransform.veteran.mailing_address,
);

// Replace country code with full name:
dataPostTransform.veteran.physical_address = getCountryLabel(
dataPostTransform.veteran.physical_address,
);
dataPostTransform.veteran.mailing_address = getCountryLabel(
dataPostTransform.veteran.mailing_address,
);

return JSON.stringify({
...dataPostTransform,
form_number: formConfig.formId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,35 +4,53 @@ import formConfig from '../../../config/form';
import transformForSubmit from '../../../config/submitTransformer';

describe('submit transformer', () => {
it('should return expected data', () => {
const formData = {
data: {
veteranDateOfBirth: '2004-02-19',
fullName: 'John Smith',
physical_address: {
street: '1 Main st',
city: 'Canton',
state: 'NY',
postalCode: '13625',
country: 'US',
},
mailing_address: {
street: '21 Jump St',
city: 'Prattville',
state: 'WA',
postalCode: '12569',
country: 'US',
},
ssn: '963879632',
va_claim_number: '5236978',
phone_number: '2056321459',
email_address: '[email protected]',
const formData = {
data: {
veteranDateOfBirth: '2004-02-19',
fullName: 'John Smith',
veteranAddress: {
street: '1 Main st',
city: 'Canton',
state: 'NY',
postalCode: '13625',
country: 'AFG',
},
physicalAddress: {
street: '21 Jump St',
city: 'Prattville',
state: 'WA',
postalCode: '12569',
country: 'USA',
},
};
ssn: '963879632',
va_claim_number: '5236978',
phone_number: '2056321459',
email_address: '[email protected]',
},
};
it('should return expected data', () => {
const newTransformData = JSON.parse(
transformForSubmit(formConfig, formData),
);
// eslint-disable-next-line no-console
expect(newTransformData.veteran.date_of_birth).to.equal('02/19/2004');
});
it('should replace country code with full country name', () => {
const data = JSON.parse(
transformForSubmit(formConfig, {
data: {
veteranDateOfBirth: '2004-02-19',
sameMailingAddress: true,
veteranAddress: {
street: '1 Main st',
city: 'Canton',
state: 'NY',
postalCode: '13625',
country: 'USA',
},
},
}),
);
expect(data.veteran.physical_address.country).to.equal('United States');
});
});

0 comments on commit 31b591d

Please sign in to comment.