From 418f6c3e4288522d03264c025f619648da535bb4 Mon Sep 17 00:00:00 2001 From: Trevor Bosaw Date: Mon, 18 Nov 2024 15:45:28 -0800 Subject: [PATCH] [VI-771] Adding preferred name to user serializer, sourced from MPI (#19488) --- app/models/user.rb | 8 ++++++++ app/services/users/profile.rb | 1 + app/swagger/swagger/schemas/user_internal_services.rb | 1 + lib/mpi/models/mvi_profile_identity.rb | 1 + lib/mpi/responses/profile_parser.rb | 10 ++++++---- spec/factories/mvi_profiles.rb | 2 ++ spec/factories/users.rb | 3 +++ spec/lib/mpi/responses/profile_parser_spec.rb | 1 + spec/models/user_spec.rb | 4 ++++ spec/services/users/profile_spec.rb | 4 ++++ spec/support/rakelib/users_serialized.json | 9 ++++++++- spec/support/schemas/user_loa1.json | 7 +++++++ spec/support/schemas/user_loa3.json | 7 +++++++ spec/support/schemas_camelized/user_loa1.json | 7 +++++++ spec/support/schemas_camelized/user_loa3.json | 7 +++++++ 15 files changed, 67 insertions(+), 5 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index 5011e1f6760..e0c73ce0510 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -131,6 +131,10 @@ def full_name_normalized } end + def preferred_name + preferred_name_mpi + end + def gender identity.gender.presence || gender_mpi end @@ -236,6 +240,10 @@ def first_name_mpi given_names&.first end + def preferred_name_mpi + mpi_profile&.preferred_names&.first + end + def middle_name_mpi mpi&.profile&.given_names.to_a[1..]&.join(' ').presence end diff --git a/app/services/users/profile.rb b/app/services/users/profile.rb index 8b98ecf2630..fd11d0e19ac 100644 --- a/app/services/users/profile.rb +++ b/app/services/users/profile.rb @@ -71,6 +71,7 @@ def profile first_name: user.first_name, middle_name: user.middle_name, last_name: user.last_name, + preferred_name: user.preferred_name, birth_date: user.birth_date, gender: user.gender, zip: user.postal_code, diff --git a/app/swagger/swagger/schemas/user_internal_services.rb b/app/swagger/swagger/schemas/user_internal_services.rb index 02bffb76490..36b9768bd0c 100644 --- a/app/swagger/swagger/schemas/user_internal_services.rb +++ b/app/swagger/swagger/schemas/user_internal_services.rb @@ -36,6 +36,7 @@ class UserInternalServices property :first_name, type: :string, example: 'Abigail' property :middle_name, type: :string, example: 'Jane' property :last_name, type: :string, example: 'Brown' + property :preferred_name, type: %i[string null], example: 'Abby' property :birth_date, type: :string, example: '1900-01-01' property :gender, type: :string, example: 'F' property :zip, diff --git a/lib/mpi/models/mvi_profile_identity.rb b/lib/mpi/models/mvi_profile_identity.rb index d031b38c967..93e7761f49c 100644 --- a/lib/mpi/models/mvi_profile_identity.rb +++ b/lib/mpi/models/mvi_profile_identity.rb @@ -10,6 +10,7 @@ module MviProfileIdentity attribute :given_names, Array[String] attribute :family_name, String + attribute :preferred_names, Array[String] attribute :suffix, String attribute :gender, String attribute :birth_date, Common::DateTimeString diff --git a/lib/mpi/responses/profile_parser.rb b/lib/mpi/responses/profile_parser.rb index 287f1cc44c4..f9db92ee83d 100644 --- a/lib/mpi/responses/profile_parser.rb +++ b/lib/mpi/responses/profile_parser.rb @@ -144,10 +144,12 @@ def create_mpi_profile_identity(person) person_component = locate_element(person, PATIENT_PERSON_PREFIX) person_types = parse_person_type(person) name = parse_name(locate_elements(person_component, NAME_XPATH)) + preferred_names = parse_name(locate_elements(person_component, NAME_XPATH), indicator: NAME_PREFERRED_INDICATOR) { given_names: name[:given], family_name: name[:family], suffix: name[:suffix], + preferred_names: preferred_names[:given], gender: locate_element(person_component, GENDER_XPATH), birth_date: locate_element(person_component, DOB_XPATH), deceased_date: locate_element(person_component, DECEASED_XPATH), @@ -248,15 +250,15 @@ def parse_relationship_name(name) { given:, family: } end - def parse_name(name) - name_element = parse_name_node(name, indicator: NAME_LEGAL_INDICATOR) + def parse_name(name, indicator: NAME_LEGAL_INDICATOR) + name_element = parse_name_node(name, indicator:) given = [*name_element.locate('given')].map { |el| el.nodes.first.capitalize } - family = name_element.locate('family').first.nodes.first.capitalize + family = name_element.locate('family')&.first&.nodes&.first&.capitalize suffix = name_element.locate('suffix')&.first&.nodes&.first&.capitalize { given:, family:, suffix: } rescue - Rails.logger.warn 'MPI::Response.parse_name failed' + Rails.logger.warn 'MPI::Response.parse_name failed' if indicator == NAME_LEGAL_INDICATOR { given: nil, family: nil } end diff --git a/spec/factories/mvi_profiles.rb b/spec/factories/mvi_profiles.rb index a4ba5b7a5de..e8bbbe64d06 100644 --- a/spec/factories/mvi_profiles.rb +++ b/spec/factories/mvi_profiles.rb @@ -38,6 +38,7 @@ FactoryBot.define do factory :mpi_profile, class: 'MPI::Models::MviProfile' do given_names { Array.new(1) { Faker::Name.first_name } } + preferred_names { Array.new(1) { Faker::Name.first_name } } family_name { Faker::Name.last_name } suffix { Faker::Name.suffix } gender { %w[M F].sample } @@ -82,6 +83,7 @@ factory :mpi_profile_response do given_names { %w[John William] } + preferred_names { [] } family_name { 'Smith' } suffix { 'Sr' } gender { 'M' } diff --git a/spec/factories/users.rb b/spec/factories/users.rb index d2a9cba68f3..f9bfb6538e9 100644 --- a/spec/factories/users.rb +++ b/spec/factories/users.rb @@ -15,6 +15,7 @@ middle_name { nil } last_name { 'lincoln' } gender { 'M' } + preferred_name { 'abe' } birth_date { '1809-02-12' } ssn { '796111863' } idme_uuid { 'b2fab2b5-6af0-45e1-a9e2-394347af91ef' } @@ -94,6 +95,7 @@ mpi_profile do given_names = [first_name] given_names << middle_name if middle_name.present? + preferred_names = [preferred_name] mpi_attributes = { active_mhv_ids:, address:, @@ -105,6 +107,7 @@ family_name: last_name, gender:, given_names:, + preferred_names:, home_phone:, icn:, mhv_ids:, diff --git a/spec/lib/mpi/responses/profile_parser_spec.rb b/spec/lib/mpi/responses/profile_parser_spec.rb index ee72fbe0d13..861d3e8bac6 100644 --- a/spec/lib/mpi/responses/profile_parser_spec.rb +++ b/spec/lib/mpi/responses/profile_parser_spec.rb @@ -126,6 +126,7 @@ :address_austin, family_name: 'Smith', given_names: %w[John William], + preferred_names: %w[General], suffix: 'Sr', birls_id: nil, birls_ids: [], diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index e1296b7face..b59644f2aab 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -368,6 +368,10 @@ country: 'USA' } end + it 'fetches preferred name from MPI' do + expect(user.preferred_name).to eq(user.preferred_name_mpi) + end + context 'user has an address' do it 'returns mpi_profile\'s address as hash' do expect(user.address).to eq(expected_address) diff --git a/spec/services/users/profile_spec.rb b/spec/services/users/profile_spec.rb index bea9a2b7f9c..1efa5dd4f41 100644 --- a/spec/services/users/profile_spec.rb +++ b/spec/services/users/profile_spec.rb @@ -190,6 +190,10 @@ expect(profile[:last_name]).to eq(user.last_name) end + it 'includes preferred_name' do + expect(profile[:preferred_name]).to eq(user.preferred_name) + end + it 'includes birth_date' do expect(profile[:birth_date]).to eq(user.birth_date) end diff --git a/spec/support/rakelib/users_serialized.json b/spec/support/rakelib/users_serialized.json index 62ceef2f661..1ddfaec6636 100644 --- a/spec/support/rakelib/users_serialized.json +++ b/spec/support/rakelib/users_serialized.json @@ -14,6 +14,7 @@ ":first_name": "Abraham", ":middle_name": null, ":last_name": "Lincoln", + ":preferred_name": "abe", ":gender": "M", ":birth_date": "1809-02-12", ":zip": null, @@ -35,11 +36,14 @@ ":response": { "^o": "MPI::Responses::FindProfileResponse", "status": "OK", - "profile": { + "profile": { "^o": "MPI::Models::MviProfile", "given_names": [ "Abraham" ], + "preferred_names": [ + "abe" + ], "family_name": "Lincoln", "suffix": "Jr", "gender": "M", @@ -118,6 +122,9 @@ "Jebediah" ], "family_name": "Simpson", + "preferred_names": [ + "abe" + ], "suffix": null, "gender": "M", "birth_date": "19690406", diff --git a/spec/support/schemas/user_loa1.json b/spec/support/schemas/user_loa1.json index 4b881c22bc9..de91e8bb9c3 100644 --- a/spec/support/schemas/user_loa1.json +++ b/spec/support/schemas/user_loa1.json @@ -97,6 +97,7 @@ "first_name", "middle_name", "last_name", + "preferred_name", "birth_date", "gender", "zip", @@ -137,6 +138,12 @@ null ] }, + "preferred_name": { + "type": [ + "string", + null + ] + }, "birth_date": { "type": [ "string", diff --git a/spec/support/schemas/user_loa3.json b/spec/support/schemas/user_loa3.json index da7a84a0b8b..cbe98620014 100644 --- a/spec/support/schemas/user_loa3.json +++ b/spec/support/schemas/user_loa3.json @@ -101,6 +101,7 @@ "first_name", "middle_name", "last_name", + "preferred_name", "birth_date", "gender", "zip", @@ -135,6 +136,12 @@ "last_name": { "type": "string" }, + "preferred_name": { + "type": [ + "string", + null + ] + }, "birth_date": { "type": "string" }, diff --git a/spec/support/schemas_camelized/user_loa1.json b/spec/support/schemas_camelized/user_loa1.json index 51f8a853cd6..e6c15aadf01 100644 --- a/spec/support/schemas_camelized/user_loa1.json +++ b/spec/support/schemas_camelized/user_loa1.json @@ -97,6 +97,7 @@ "firstName", "middleName", "lastName", + "preferredName", "birthDate", "gender", "zip", @@ -137,6 +138,12 @@ null ] }, + "preferredName": { + "type": [ + "string", + null + ] + }, "birthDate": { "type": [ "string", diff --git a/spec/support/schemas_camelized/user_loa3.json b/spec/support/schemas_camelized/user_loa3.json index 865ea65bf12..270f1634c4b 100644 --- a/spec/support/schemas_camelized/user_loa3.json +++ b/spec/support/schemas_camelized/user_loa3.json @@ -101,6 +101,7 @@ "firstName", "middleName", "lastName", + "preferredName", "birthDate", "gender", "zip", @@ -135,6 +136,12 @@ "lastName": { "type": "string" }, + "preferredName": { + "type": [ + "string", + null + ] + }, "birthDate": { "type": "string" },