From 31b591d4f61cac4ac844b1f33a998a4ca39686cb Mon Sep 17 00:00:00 2001 From: Michael Clement <18408628+michaelclement@users.noreply.github.com> Date: Fri, 27 Dec 2024 16:12:36 -0600 Subject: [PATCH] updated address transform to use full country name (#33745) --- .../10-7959f-1/config/submitTransformer.js | 24 +++++++ .../config/submitTransformer.unit.spec.js | 66 ++++++++++++------- 2 files changed, 66 insertions(+), 24 deletions(-) diff --git a/src/applications/ivc-champva/10-7959f-1/config/submitTransformer.js b/src/applications/ivc-champva/10-7959f-1/config/submitTransformer.js index d2ff32cc6be5..d7197497a723 100644 --- a/src/applications/ivc-champva/10-7959f-1/config/submitTransformer.js +++ b/src/applications/ivc-champva/10-7959f-1/config/submitTransformer.js @@ -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'; @@ -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), @@ -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, diff --git a/src/applications/ivc-champva/10-7959f-1/tests/unit/config/submitTransformer.unit.spec.js b/src/applications/ivc-champva/10-7959f-1/tests/unit/config/submitTransformer.unit.spec.js index 5b51e9670aca..7009a01ab8d6 100644 --- a/src/applications/ivc-champva/10-7959f-1/tests/unit/config/submitTransformer.unit.spec.js +++ b/src/applications/ivc-champva/10-7959f-1/tests/unit/config/submitTransformer.unit.spec.js @@ -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: 'john@gmail.com0', + 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: 'john@gmail.com0', + }, + }; + 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'); + }); });