Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[VI-468] Add mhv_user_account to User #18552

Merged
merged 1 commit into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 17 additions & 1 deletion app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,18 @@ def mhv_correlation_id
identity.mhv_correlation_id || mpi_mhv_correlation_id
end

def mhv_user_account
@mhv_user_account ||= if va_patient?
MHV::UserAccount::Creator.new(user_verification:).perform
else
log_mhv_user_account_error('User has no va_treatment_facility_ids')
nil
end
rescue MHV::UserAccount::Errors::UserAccountError => e
log_mhv_user_account_error(e.message)
nil
end

def middle_name
identity.middle_name.presence || middle_name_mpi
end
Expand Down Expand Up @@ -315,7 +327,7 @@ def can_access_user_profile?

# True if the user has 1 or more treatment facilities, false otherwise
def va_patient?
va_treatment_facility_ids.length.positive?
va_treatment_facility_ids.any?
end

# User's profile contains a list of VHA facility-specific identifiers.
Expand Down Expand Up @@ -481,4 +493,8 @@ def bgs_relationships
def pciu
@pciu ||= EVSS::PCIU::Service.new self if loa3? && edipi.present?
end

def log_mhv_user_account_error(error_message)
Rails.logger.info('[User] mhv_user_account error', error_message:, icn:)
end
end
99 changes: 99 additions & 0 deletions spec/models/user_spec.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require 'rails_helper'
require 'mhv/account_creation/service'

RSpec.describe User, type: :model do
subject { described_class.new(build(:user)) }
Expand Down Expand Up @@ -1239,4 +1240,102 @@
end
end
end

describe '#mhv_user_account' do
let(:user) { build(:user, :loa3, vha_facility_ids:) }
let(:vha_facility_ids) { %w[450MH] }
let(:icn) { user.icn }

let!(:user_verification) do
create(:idme_user_verification, idme_uuid: user.idme_uuid, user_credential_email:, user_account:)
end
let(:user_credential_email) { create(:user_credential_email) }
let(:user_account) { create(:user_account, icn:) }
let!(:terms_of_use_agreement) { create(:terms_of_use_agreement, user_account:, response: terms_of_use_response) }
let(:terms_of_use_response) { 'accepted' }

let(:mhv_client) { MHV::AccountCreation::Service.new }
let(:mhv_response) do
{
user_profile_id: '12345678',
premium: true,
champ_va: true,
patient: true,
sm_account_created: true,
message: 'some-message'
}
end

before do
allow(Rails.logger).to receive(:info)
end

context 'when the user is a va_patient' do
before do
allow(MHV::AccountCreation::Service).to receive(:new).and_return(mhv_client)
allow(mhv_client).to receive(:create_account).and_return(mhv_response)
end

context 'when the user has all required attributes' do
it 'returns a MHVUserAccount with the expected attributes' do
mhv_user_account = user.mhv_user_account

expect(mhv_user_account).to be_a(MHVUserAccount)
expect(mhv_user_account.attributes).to eq(mhv_response.with_indifferent_access)
end
end

context 'when there is an error creating the account' do
shared_examples 'mhv_user_account error' do
let(:expected_log_message) { '[User] mhv_user_account error' }
let(:expected_log_payload) { { error_message: /#{expected_error_message}/, icn: user.icn } }

it 'logs an error and returns nil' do
expect(user.mhv_user_account).to be_nil
expect(Rails.logger).to have_received(:info).with(expected_log_message, expected_log_payload)
end
end

context 'when the user does not have a terms_of_use_agreement' do
let(:terms_of_use_agreement) { nil }
let(:expected_error_message) { 'Current terms of use agreement must be present' }

it_behaves_like 'mhv_user_account error'
end

context 'when the user has not accepted the terms of use' do
let(:terms_of_use_response) { 'declined' }
let(:expected_error_message) { "Current terms of use agreement must be 'accepted'" }

it_behaves_like 'mhv_user_account error'
end

context 'when the user does not have a user_credential_email' do
let(:user_credential_email) { nil }
let(:expected_error_message) { 'Email must be present' }

it_behaves_like 'mhv_user_account error'
end

context 'when the user does not have an icn' do
let(:icn) { nil }
let(:expected_error_message) { 'ICN must be present' }

it_behaves_like 'mhv_user_account error'
end
end
end

context 'when the user is not a va_patient' do
let(:vha_facility_ids) { [] }
let(:expected_log_message) { '[User] mhv_user_account error' }
let(:expected_log_payload) { { error_message: expected_error_message, icn: user.icn } }
let(:expected_error_message) { 'User has no va_treatment_facility_ids' }

it 'logs an error and returns nil' do
expect(user.mhv_user_account).to be_nil
expect(Rails.logger).to have_received(:info).with(expected_log_message, expected_log_payload)
end
end
end
end
Loading