department-of-veterans-affairs/vets-website

View on GitHub
src/applications/representative-appoint/config/prefillTransformer.js

Summary

Maintainability
C
1 day
Test Coverage
import { preparerIsVeteran } from '../utilities/helpers';

export default function prefillTransformer(formData) {
  const newFormData = {
    ...formData,
  };

  if (preparerIsVeteran({ formData })) {
    newFormData.veteranFullName = formData?.personalInformation?.fullName;
    newFormData.veteranDateOfBirth = formData?.personalInformation?.dateOfBirth;
    newFormData.veteranSocialSecurityNumber =
      formData?.personalInformation?.ssn;
    newFormData.veteranHomeAddress = {
      city: formData?.contactInformation?.address?.city,
      country: formData?.contactInformation?.address?.country,
      postalCode: formData?.contactInformation?.address?.postalCode,
      state: formData?.contactInformation?.address?.state,
      street: formData?.contactInformation?.address?.street,
    };
    newFormData.veteranEmail = formData?.contactInformation?.email;
    newFormData.primaryPhone = formData?.contactInformation?.primaryPhone;
    newFormData['Branch of Service'] =
      formData?.militaryInformation?.serviceBranch;
    // reset the applicant information in case of claimant type change
    newFormData.applicantName = undefined;
    newFormData.applicantDOB = undefined;
    newFormData.applicantEmail = undefined;
    newFormData.applicantPhone = undefined;
    newFormData.homeAddress = {
      city: undefined,
      country: undefined,
      postalCode: undefined,
      state: undefined,
      street: undefined,
    };
  } else {
    newFormData.applicantName = formData?.personalInformation?.fullName;
    newFormData.applicantDOB = formData?.personalInformation?.dateOfBirth;
    newFormData.applicantEmail = formData?.contactInformation?.email;
    newFormData.applicantPhone = formData?.contactInformation?.primaryPhone;
    newFormData.homeAddress = {
      city: formData?.contactInformation?.address?.city,
      country: formData?.contactInformation?.address?.country,
      postalCode: formData?.contactInformation?.address?.postalCode,
      state: formData?.contactInformation?.address?.state,
      street: formData?.contactInformation?.address?.street,
    };

    // reset the applicant information in case of claimant type change
    newFormData.veteranFullName = undefined;
    newFormData.veteranDateOfBirth = undefined;
    newFormData.veteranSocialSecurityNumber = undefined;
    newFormData.veteranHomeAddress = {
      city: undefined,
      country: undefined,
      postalCode: undefined,
      state: undefined,
      street: undefined,
    };
    newFormData.veteranEmail = undefined;
    newFormData.primaryPhone = undefined;
    newFormData['Branch of Service'] = undefined;
  }

  return newFormData;
}