Skip to content

Commit

Permalink
[VI-771] Adding preferred name to user serializer, sourced from MPI (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
bosawt authored Nov 18, 2024
1 parent d4bd565 commit 418f6c3
Show file tree
Hide file tree
Showing 15 changed files with 67 additions and 5 deletions.
8 changes: 8 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ def full_name_normalized
}
end

def preferred_name
preferred_name_mpi
end

def gender
identity.gender.presence || gender_mpi
end
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions app/services/users/profile.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions app/swagger/swagger/schemas/user_internal_services.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions lib/mpi/models/mvi_profile_identity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 6 additions & 4 deletions lib/mpi/responses/profile_parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions spec/factories/mvi_profiles.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down Expand Up @@ -82,6 +83,7 @@

factory :mpi_profile_response do
given_names { %w[John William] }
preferred_names { [] }
family_name { 'Smith' }
suffix { 'Sr' }
gender { 'M' }
Expand Down
3 changes: 3 additions & 0 deletions spec/factories/users.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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' }
Expand Down Expand Up @@ -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:,
Expand All @@ -105,6 +107,7 @@
family_name: last_name,
gender:,
given_names:,
preferred_names:,
home_phone:,
icn:,
mhv_ids:,
Expand Down
1 change: 1 addition & 0 deletions spec/lib/mpi/responses/profile_parser_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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: [],
Expand Down
4 changes: 4 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 4 additions & 0 deletions spec/services/users/profile_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion spec/support/rakelib/users_serialized.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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",
Expand Down Expand Up @@ -118,6 +122,9 @@
"Jebediah"
],
"family_name": "Simpson",
"preferred_names": [
"abe"
],
"suffix": null,
"gender": "M",
"birth_date": "19690406",
Expand Down
7 changes: 7 additions & 0 deletions spec/support/schemas/user_loa1.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"first_name",
"middle_name",
"last_name",
"preferred_name",
"birth_date",
"gender",
"zip",
Expand Down Expand Up @@ -137,6 +138,12 @@
null
]
},
"preferred_name": {
"type": [
"string",
null
]
},
"birth_date": {
"type": [
"string",
Expand Down
7 changes: 7 additions & 0 deletions spec/support/schemas/user_loa3.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"first_name",
"middle_name",
"last_name",
"preferred_name",
"birth_date",
"gender",
"zip",
Expand Down Expand Up @@ -135,6 +136,12 @@
"last_name": {
"type": "string"
},
"preferred_name": {
"type": [
"string",
null
]
},
"birth_date": {
"type": "string"
},
Expand Down
7 changes: 7 additions & 0 deletions spec/support/schemas_camelized/user_loa1.json
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@
"firstName",
"middleName",
"lastName",
"preferredName",
"birthDate",
"gender",
"zip",
Expand Down Expand Up @@ -137,6 +138,12 @@
null
]
},
"preferredName": {
"type": [
"string",
null
]
},
"birthDate": {
"type": [
"string",
Expand Down
7 changes: 7 additions & 0 deletions spec/support/schemas_camelized/user_loa3.json
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
"firstName",
"middleName",
"lastName",
"preferredName",
"birthDate",
"gender",
"zip",
Expand Down Expand Up @@ -135,6 +136,12 @@
"lastName": {
"type": "string"
},
"preferredName": {
"type": [
"string",
null
]
},
"birthDate": {
"type": "string"
},
Expand Down

0 comments on commit 418f6c3

Please sign in to comment.